2014-11-19 00:25:21 +08:00
|
|
|
|
#ifndef ACL_DBPOOL_INCLUDE_H
|
|
|
|
|
#define ACL_DBPOOL_INCLUDE_H
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "stdlib/acl_define.h"
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include "stdlib/acl_stdlib.h"
|
|
|
|
|
|
|
|
|
|
#define ACL_DB_DEBUG_MEM (1<<0)
|
|
|
|
|
|
|
|
|
|
typedef struct ACL_DB_HANDLE ACL_DB_HANDLE;
|
|
|
|
|
typedef struct ACL_SQL_RES ACL_SQL_RES;
|
|
|
|
|
typedef struct ACL_DB_POOL ACL_DB_POOL;
|
|
|
|
|
|
|
|
|
|
typedef struct ACL_DB_INFO {
|
|
|
|
|
int db_max; /* <20><><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
char db_addr[256]; /* <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ */
|
|
|
|
|
char db_name[256]; /* <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
char db_user[256]; /* <20><><EFBFBD>ݿ<EFBFBD><DDBF>ʺ<EFBFBD> */
|
|
|
|
|
char db_pass[256]; /* <20>ʺ<EFBFBD><CABA><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
unsigned long db_flags; /* (mysql) <20><><EFBFBD>ӱ<EFBFBD>־λ */
|
|
|
|
|
int ping_inter; /* ̽<><CCBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ӵ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
int timeout_inter; /* <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ӵĿ<D3B5><C4BF>г<EFBFBD>ʱʱ<CAB1><CAB1> */
|
|
|
|
|
int auto_commit; /* (mysql) <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>ύ<EFBFBD><E1BDBB><EFBFBD><EFBFBD> */
|
|
|
|
|
int conn_timeout; /* (mysql/null) <20><><EFBFBD>ӳ<EFBFBD>ʱʱ<CAB1><CAB1> */
|
|
|
|
|
int rw_timeout; /* (mysql/null) IO<49><4F>д<EFBFBD><D0B4>ʱʱ<CAB1><CAB1> */
|
|
|
|
|
int buf_size; /* (null) IO<49><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С */
|
|
|
|
|
int debug_flag; /* <20><><EFBFBD>Ա<EFBFBD>־λ */
|
|
|
|
|
|
|
|
|
|
/* <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>֮ǰ/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>õĻص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ NULL,
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> db_before_connect/db_after_connect <EFBFBD><EFBFBD><EFBFBD><EFBFBD> < 0 <EFBFBD><EFBFBD><EFBFBD>ᵼ<EFBFBD><EFBFBD>
|
|
|
|
|
* acl_dbpool_peek <EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL
|
|
|
|
|
*/
|
|
|
|
|
int (*db_before_connect)(ACL_DB_HANDLE* db_handle, void *ctx);
|
|
|
|
|
int (*db_after_connect)(ACL_DB_HANDLE* db_handle, void *ctx);
|
|
|
|
|
|
|
|
|
|
void *ctx; /* db_before_connect/db_after_connect <20><><EFBFBD><EFBFBD>֮һ */
|
|
|
|
|
} ACL_DB_INFO;
|
|
|
|
|
|
|
|
|
|
struct ACL_DB_HANDLE {
|
|
|
|
|
#define ACL_DBH_STATUS_NULL 0
|
|
|
|
|
#define ACL_DBH_STATUS_READY 1
|
|
|
|
|
#define ACL_DBH_STATUS_INUSE 2
|
|
|
|
|
int status;
|
|
|
|
|
time_t timeout;
|
|
|
|
|
time_t ping;
|
|
|
|
|
|
|
|
|
|
ACL_DB_POOL *parent;
|
|
|
|
|
|
|
|
|
|
int (*sql_results)(ACL_DB_HANDLE *handle, const char *sql, int *error,
|
|
|
|
|
int (*walk_fn)(const void** result_row, void *arg), void *arg);
|
|
|
|
|
int (*sql_result)(ACL_DB_HANDLE *handle, const char *sql, int *error,
|
|
|
|
|
int (*callback)(const void** result_row, void *arg), void *arg);
|
|
|
|
|
int (*sql_update)(ACL_DB_HANDLE *handle, const char *sql, int *error);
|
|
|
|
|
|
|
|
|
|
ACL_SQL_RES *(*sql_select)(ACL_DB_HANDLE *handle, const char *sql, int *error);
|
|
|
|
|
void (*free_result)(ACL_SQL_RES *res);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ACL_SQL_RES {
|
|
|
|
|
void *res;
|
|
|
|
|
int num;
|
|
|
|
|
|
|
|
|
|
/* for acl_iterator */
|
|
|
|
|
|
|
|
|
|
/* ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD> */
|
|
|
|
|
const void *(*iter_head)(ACL_ITER*, struct ACL_SQL_RES*);
|
|
|
|
|
/* ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
const void *(*iter_next)(ACL_ITER*, struct ACL_SQL_RES*);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ACL_DB_POOL {
|
|
|
|
|
ACL_DB_INFO db_info;
|
|
|
|
|
|
|
|
|
|
ACL_DB_HANDLE *(*dbh_peek)(ACL_DB_POOL *);
|
|
|
|
|
void (*dbh_check)(ACL_DB_POOL *);
|
|
|
|
|
void (*dbh_release)(ACL_DB_HANDLE *);
|
|
|
|
|
void *(*dbh_export)(ACL_DB_HANDLE *);
|
|
|
|
|
void (*dbh_close)(ACL_DB_HANDLE *);
|
|
|
|
|
int (*dbh_ping)(ACL_DB_HANDLE *);
|
|
|
|
|
|
|
|
|
|
void (*destroy)(ACL_DB_POOL *);
|
|
|
|
|
|
|
|
|
|
int db_max;
|
|
|
|
|
int db_ready;
|
|
|
|
|
int db_inuse;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
#define ACL_DB_AUTO_COMMIT(_db_pool_) (_db_pool_ ? _db_pool_->db_info.auto_commit : 0)
|
|
|
|
|
|
|
|
|
|
/* in acl_dbpool.c */
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
|
|
|
|
* @param db_type {const char*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ŀǰ<EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD> mysql
|
|
|
|
|
* @param db_info {const ACL_DB_INFO*} <EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
* @return {ACL_DB_POOL*} һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
ACL_API ACL_DB_POOL *acl_dbpool_create(const char *db_type, const ACL_DB_INFO *db_info);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
|
|
|
|
* @param db_pool <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳؾ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
ACL_API void acl_dbpool_destroy(ACL_DB_POOL *db_pool);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD>л<EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param db_pool {ACL_DB_POOL*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳؾ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {ACL_DB_HANDLE*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EF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D>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
|
|
|
|
* @param db_handle {ACL_DB_HANDLE*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
ACL_API void acl_dbpool_release(ACL_DB_HANDLE *db_handle);
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊʵ<EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param db_handle {ACL_DB_HANDLE*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return void * ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
ACL_API void *acl_dbpool_export(ACL_DB_HANDLE *db_handle);
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>˽ӿ<EFBFBD>ǿ<EFBFBD>йرո<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param db_handle {ACL_DB_HANDLE*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
ACL_API void acl_dbpool_close(ACL_DB_HANDLE *db_handle);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳصĶ<EFBFBD>ʱPING<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱʡ<EFBFBD><EFBFBD>ʽ
|
|
|
|
|
* @param db_pool {ACL_DB_POOL*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳؾ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param ping_fn {int (*)(ACL_DB_HANDLE*)} ̽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
ACL_API void acl_dbpool_set_ping(ACL_DB_POOL *db_pool, int (*ping_fn)(ACL_DB_HANDLE*));
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|