浏览代码

lib/srdb1: Cleaner implementation of non-pooled connections

- Uses a new db_do_init2() function that takes a parameter to indicate
  pooling.
- This leaves the parameters for db_do_init() unchanged.
Peter Dunkley 13 年之前
父节点
当前提交
6eb22c8da9
共有 4 个文件被更改,包括 44 次插入10 次删除
  1. 13 3
      lib/srdb1/db.c
  2. 24 4
      lib/srdb1/db.h
  3. 4 2
      lib/srdb1/db_id.c
  4. 3 1
      lib/srdb1/db_id.h

+ 13 - 3
lib/srdb1/db.c

@@ -221,7 +221,7 @@ int db_bind_mod(const str* mod, db_func_t* mydbf)
 		dbf.use_table = (db_use_table_f)find_mod_export(tmp,
 			"db_use_table", 2, 0);
 		dbf.init = (db_init_f)find_mod_export(tmp, "db_init", 1, 0);
-		dbf.init_nopool = (db_init_nopool_f)find_mod_export(tmp, "db_init_nopool", 1, 0);
+		dbf.init2 = (db_init2_f)find_mod_export(tmp, "db_init2", 1, 0);
 		dbf.close = (db_close_f)find_mod_export(tmp, "db_close", 2, 0);
 		dbf.query = (db_query_f)find_mod_export(tmp, "db_query", 2, 0);
 		dbf.fetch_result = (db_fetch_result_f)find_mod_export(tmp,
@@ -260,7 +260,17 @@ error:
  * Initialize database module
  * \note No function should be called before this
  */
-db1_con_t* db_do_init(const str* url, void* (*new_connection)(), int nopool)
+db1_con_t* db_do_init(const str* url, void* (*new_connection)())
+{
+	return db_do_init2(url, *new_connection, DB_POOLING_PERMITTED);
+}
+
+
+/*! \brief
+ * Initialize database module
+ * \note No function should be called before this
+ */
+db1_con_t* db_do_init2(const str* url, void* (*new_connection)(), db_pooling_t pooling)
 {
 	struct db_id* id;
 	void* con;
@@ -288,7 +298,7 @@ db1_con_t* db_do_init(const str* url, void* (*new_connection)(), int nopool)
 	}
 	memset(res, 0, con_size);
 
-	id = new_db_id(url, nopool);
+	id = new_db_id(url, pooling);
 	if (!id) {
 		LM_ERR("cannot parse URL '%.*s'\n", url->len, url->s);
 		goto err;

+ 24 - 4
lib/srdb1/db.h

@@ -52,6 +52,11 @@
 #include "db_row.h"
 
 
+typedef enum {
+	DB_POOLING_PERMITTED,
+	DB_POOLING_NONE	
+} db_pooling_t;
+
 /**
  * \brief Specify table name that will be used for subsequent operations.
  * 
@@ -89,7 +94,7 @@ typedef int (*db_use_table_f)(db1_con_t* _h, const str * _t);
 typedef db1_con_t* (*db_init_f) (const str* _sqlurl);
 
 /**
- * \brief Initialize database connection and obtain a non-pooled connection handle.
+ * \brief Initialize database connection and obtain the connection handle.
  *
  * This function initialize the database API and open a new database
  * connection. This function must be called after bind_dbmod but before any
@@ -107,10 +112,11 @@ typedef db1_con_t* (*db_init_f) (const str* _sqlurl);
  * name of the database (optional).
  * \see bind_dbmod
  * \param _sqlurl database connection URL
+ * \param _pooling whether or not to use a pooled connection
  * \return returns a pointer to the db1_con_t representing the connection if it was
  * successful, otherwise 0 is returned
  */
-typedef db1_con_t* (*db_init_nopool_f) (const str* _sqlurl);
+typedef db1_con_t* (*db_init2_f) (const str* _sqlurl, db_pooling_t _pooling);
 
 /**
  * \brief Close a database connection and free all memory used.
@@ -347,7 +353,7 @@ typedef struct db_func {
 	unsigned int      cap;           /* Capability vector of the database transport */
 	db_use_table_f    use_table;     /* Specify table name */
 	db_init_f         init;          /* Initialize database connection */
-	db_init_nopool_f  init_nopool;   /* Initialize database connection - no pooling */
+	db_init2_f        init2;         /* Initialize database connection */
 	db_close_f        close;         /* Close database connection */
 	db_query_f        query;         /* query a table */
 	db_fetch_result_f fetch_result;  /* fetch result */
@@ -396,7 +402,21 @@ int db_bind_mod(const str* mod, db_func_t* dbf);
  * \return returns a pointer to the db1_con_t representing the connection if it was
    successful, otherwise 0 is returned.
  */
-db1_con_t* db_do_init(const str* url, void* (*new_connection)(), int nopool);
+db1_con_t* db_do_init(const str* url, void* (*new_connection)());
+
+
+/**
+ * \brief Helper for db_init2 function.
+ *
+ * This helper method do the actual work for the database specific db_init
+ * functions.
+ * \param url database connection URL
+ * \param (*new_connection)() Pointer to the db specific connection creation method
+ * \param pooling whether or not to use a pooled connection
+ * \return returns a pointer to the db1_con_t representing the connection if it was
+   successful, otherwise 0 is returned.
+ */
+db1_con_t* db_do_init2(const str* url, void* (*new_connection)(), db_pooling_t pooling);
 
 
 /**

+ 4 - 2
lib/srdb1/db_id.c

@@ -27,6 +27,7 @@
  * \brief Functions for parsing a database URL and work with db identifier.
  */
 
+#include "db.h"
 #include "db_id.h"
 #include "../../dprint.h"
 #include "../../mem/mem.h"
@@ -225,9 +226,10 @@ static int parse_db_url(struct db_id* id, const str* url)
 /**
  * Create a new connection identifier
  * \param url database URL
+ * \param pooling whether or not a pooled connection may be used
  * \return connection identifier, or zero on error
  */
-struct db_id* new_db_id(const str* url, int nopool)
+struct db_id* new_db_id(const str* url, db_pooling_t pooling)
 {
 	static int poolid=0;
 	struct db_id* ptr;
@@ -249,7 +251,7 @@ struct db_id* new_db_id(const str* url, int nopool)
 		goto err;
 	}
 
-	if (nopool) ptr->poolid = ++poolid;
+	if (pooling == DB_POOLING_NONE) ptr->poolid = ++poolid;
 	else ptr->poolid = 0;
 	ptr->pid = my_pid();
 

+ 3 - 1
lib/srdb1/db_id.h

@@ -31,6 +31,7 @@
 #define _DB1_ID_H
 
 #include "../../str.h"
+#include "db.h"
 
 /** Structure representing a database ID */
 struct db_id {
@@ -48,9 +49,10 @@ struct db_id {
 /**
  * Create a new connection identifier
  * \param url database URL
+ * \param pooling whether or not a pooled connection may be used
  * \return new allocated db_id structure, NULL on failure
  */
-struct db_id* new_db_id(const str* url, int nopool);
+struct db_id* new_db_id(const str* url, db_pooling_t pooling);
 
 
 /**