acl  3.5.3.0
acl_dbpool.h
浏览该文件的文档.
1 #ifndef ACL_DBPOOL_INCLUDE_H
2 #define ACL_DBPOOL_INCLUDE_H
3 
4 #ifndef ACL_CLIENT_ONLY
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #include "../stdlib/acl_define.h"
11 #include <time.h>
12 #include "../stdlib/acl_stdlib.h"
13 
14 #define ACL_DB_DEBUG_MEM (1<<0)
15 
17 typedef struct ACL_SQL_RES ACL_SQL_RES;
18 typedef struct ACL_DB_POOL ACL_DB_POOL;
19 
20 typedef struct ACL_DB_INFO {
21  int db_max; /* 连接池最大连接数 */
22  char db_addr[256]; /* 数据库服务地址 */
23  char db_name[256]; /* 数据库名称 */
24  char db_user[256]; /* 数据库帐号 */
25  char db_pass[256]; /* 帐号密码 */
26  unsigned long db_flags; /* (mysql) 连接标志位 */
27  int ping_inter; /* 探测数据库连接的时间间隔 */
28  int timeout_inter; /* 数据库连接的空闲超时时间 */
29  int auto_commit; /* (mysql) 是否启用自动提交过程 */
30  int conn_timeout; /* (mysql/null) 连接超时时间 */
31  int rw_timeout; /* (mysql/null) IO读写超时时间 */
32  int buf_size; /* (null) IO缓冲区大小 */
33  int debug_flag; /* 调试标志位 */
34 
35  /* 在真实连接数据库之前/后调用用户设置的回调函数, 此项可以设为 NULL,
36  * 如果 db_before_connect/db_after_connect 返回 < 0 则会导致
37  * acl_dbpool_peek 返回 NULL
38  */
39  int (*db_before_connect)(ACL_DB_HANDLE* db_handle, void *ctx);
40  int (*db_after_connect)(ACL_DB_HANDLE* db_handle, void *ctx);
41 
42  void *ctx; /* db_before_connect/db_after_connect 参数之一 */
43 } ACL_DB_INFO;
44 
45 struct ACL_DB_HANDLE {
46 #define ACL_DBH_STATUS_NULL 0
47 #define ACL_DBH_STATUS_READY 1
48 #define ACL_DBH_STATUS_INUSE 2
49  int status;
50  time_t timeout;
51  time_t ping;
52 
54 
55  int (*sql_results)(ACL_DB_HANDLE *handle, const char *sql, int *error,
56  int (*walk_fn)(const void** result_row, void *arg), void *arg);
57  int (*sql_result)(ACL_DB_HANDLE *handle, const char *sql, int *error,
58  int (*callback)(const void** result_row, void *arg), void *arg);
59  int (*sql_update)(ACL_DB_HANDLE *handle, const char *sql, int *error);
60 
61  ACL_SQL_RES *(*sql_select)(ACL_DB_HANDLE *handle, const char *sql, int *error);
62  void (*free_result)(ACL_SQL_RES *res);
63 };
64 
65 struct ACL_SQL_RES {
66  void *res;
67  int num;
68 
69  /* for acl_iterator */
70 
71  /* 取迭代器头函数 */
72  const void *(*iter_head)(ACL_ITER*, struct ACL_SQL_RES*);
73  /* 取迭代器下一个函数 */
74  const void *(*iter_next)(ACL_ITER*, struct ACL_SQL_RES*);
75 };
76 
77 struct ACL_DB_POOL {
79 
80  ACL_DB_HANDLE *(*dbh_peek)(ACL_DB_POOL *);
81  void (*dbh_check)(ACL_DB_POOL *);
83  void *(*dbh_export)(ACL_DB_HANDLE *);
86 
87  void (*destroy)(ACL_DB_POOL *);
88 
89  int db_max;
90  int db_ready;
91  int db_inuse;
92 };
93 
94 /*----------------------------------------------------------------------------*/
95 #define ACL_DB_AUTO_COMMIT(_db_pool_) (_db_pool_ ? _db_pool_->db_info.auto_commit : 0)
96 
97 /* in acl_dbpool.c */
98 /**
99  * 创建一个数据库连接池
100  * @param db_type {const char*} 数据库类型名, 目前仅支持 mysql
101  * @param db_info {const ACL_DB_INFO*} 记录着有关连接数据所需要的信息
102  * @return {ACL_DB_POOL*} 一个数据库连接池
103  */
104 ACL_API ACL_DB_POOL *acl_dbpool_create(const char *db_type, const ACL_DB_INFO *db_info);
105 
106 /**
107  * 销毁一个数据库连接池
108  * @param db_pool 数据库连接池句柄
109  */
110 ACL_API void acl_dbpool_destroy(ACL_DB_POOL *db_pool);
111 
112 /**
113  * 从连接池中获取一个连接句柄
114  * @param db_pool {ACL_DB_POOL*} 数据库连接池句柄
115  * @return {ACL_DB_HANDLE*} 数据库连接句柄,如果为空则表示出错或连接池已满
116  */
117 ACL_API ACL_DB_HANDLE *acl_dbpool_peek(ACL_DB_POOL *db_pool);
118 
119 /**
120  * 手工检查连接池的每个连接?一般连接池内部会定期检查每个连接,
121  * 也可以通过此函数手工进行强制检查
122  * @param db_pool {ACL_DB_POOL*} 数据库连接池句柄
123  */
124 ACL_API void acl_dbpool_check(ACL_DB_POOL *db_pool);
125 
126 /**
127  * 将数据库连接句柄释放给数据库连接池
128  * @param db_handle {ACL_DB_HANDLE*} 数据库连接句柄
129  */
130 ACL_API void acl_dbpool_release(ACL_DB_HANDLE *db_handle);
131 /**
132  * 将数据库连接转换为实际的数据库连接句柄
133  * @param db_handle {ACL_DB_HANDLE*} 数据库连接句柄
134  * @return void * 使用者需要将其强制转换为自己所用的数据库连接引擎
135  */
136 ACL_API void *acl_dbpool_export(ACL_DB_HANDLE *db_handle);
137 /**
138  * 当使用者自己检测到该数据库连接出错时,可以通过此接口强行关闭该连接
139  * @param db_handle {ACL_DB_HANDLE*} 数据库连接句柄
140  */
141 ACL_API void acl_dbpool_close(ACL_DB_HANDLE *db_handle);
142 
143 /**
144  * 设置连接池的定时PING处理函数,如果不设置此值则内部采用缺省方式
145  * @param db_pool {ACL_DB_POOL*} 数据库连接池句柄
146  * @param ping_fn {int (*)(ACL_DB_HANDLE*)} 探测连接状态的函数指针
147  */
148 ACL_API void acl_dbpool_set_ping(ACL_DB_POOL *db_pool, int (*ping_fn)(ACL_DB_HANDLE*));
149 /*----------------------------------------------------------------------------*/
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif /* ACL_CLIENT_ONLY */
156 #endif
int(* sql_results)(ACL_DB_HANDLE *handle, const char *sql, int *error, int(*walk_fn)(const void **result_row, void *arg), void *arg)
Definition: acl_dbpool.h:55
int(* db_after_connect)(ACL_DB_HANDLE *db_handle, void *ctx)
Definition: acl_dbpool.h:40
void(* destroy)(ACL_DB_POOL *)
Definition: acl_dbpool.h:87
ACL_API void acl_dbpool_destroy(ACL_DB_POOL *db_pool)
int(* sql_update)(ACL_DB_HANDLE *handle, const char *sql, int *error)
Definition: acl_dbpool.h:59
ACL_API void acl_dbpool_set_ping(ACL_DB_POOL *db_pool, int(*ping_fn)(ACL_DB_HANDLE *))
ACL_API void acl_dbpool_close(ACL_DB_HANDLE *db_handle)
ACL_API ACL_DB_HANDLE * acl_dbpool_peek(ACL_DB_POOL *db_pool)
int(* dbh_ping)(ACL_DB_HANDLE *)
Definition: acl_dbpool.h:85
int buf_size
Definition: acl_dbpool.h:32
void(* dbh_check)(ACL_DB_POOL *)
Definition: acl_dbpool.h:81
int debug_flag
Definition: acl_dbpool.h:33
void(* dbh_close)(ACL_DB_HANDLE *)
Definition: acl_dbpool.h:84
struct ACL_DB_INFO ACL_DB_INFO
ACL_API void acl_dbpool_check(ACL_DB_POOL *db_pool)
int db_inuse
Definition: acl_dbpool.h:91
char db_name[256]
Definition: acl_dbpool.h:23
char db_pass[256]
Definition: acl_dbpool.h:25
ACL_API void * acl_dbpool_export(ACL_DB_HANDLE *db_handle)
unsigned long db_flags
Definition: acl_dbpool.h:26
int ping_inter
Definition: acl_dbpool.h:27
ACL_API void acl_dbpool_release(ACL_DB_HANDLE *db_handle)
time_t timeout
Definition: acl_dbpool.h:50
char db_addr[256]
Definition: acl_dbpool.h:22
int db_ready
Definition: acl_dbpool.h:90
time_t ping
Definition: acl_dbpool.h:51
void * ctx
Definition: acl_dbpool.h:42
void(* dbh_release)(ACL_DB_HANDLE *)
Definition: acl_dbpool.h:82
ACL_DB_INFO db_info
Definition: acl_dbpool.h:78
char db_user[256]
Definition: acl_dbpool.h:24
int(* sql_result)(ACL_DB_HANDLE *handle, const char *sql, int *error, int(*callback)(const void **result_row, void *arg), void *arg)
Definition: acl_dbpool.h:57
int timeout_inter
Definition: acl_dbpool.h:28
int rw_timeout
Definition: acl_dbpool.h:31
void * res
Definition: acl_dbpool.h:66
int auto_commit
Definition: acl_dbpool.h:29
int conn_timeout
Definition: acl_dbpool.h:30
int(* db_before_connect)(ACL_DB_HANDLE *db_handle, void *ctx)
Definition: acl_dbpool.h:39
ACL_DB_POOL * parent
Definition: acl_dbpool.h:53
ACL_API ACL_DB_POOL * acl_dbpool_create(const char *db_type, const ACL_DB_INFO *db_info)
void(* free_result)(ACL_SQL_RES *res)
Definition: acl_dbpool.h:62