Module: sip-router
Branch: master
Commit: 6eb22c8da9d3b41668bdf74f4064992066f38022
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6eb22c8…
Author: Peter Dunkley <peter.dunkley(a)crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley(a)crocodile-rcs.com>
Date: Mon Apr 23 12:37:19 2012 +0100
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.
---
lib/srdb1/db.c | 16 +++++++++++++---
lib/srdb1/db.h | 28 ++++++++++++++++++++++++----
lib/srdb1/db_id.c | 6 ++++--
lib/srdb1/db_id.h | 4 +++-
4 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/lib/srdb1/db.c b/lib/srdb1/db.c
index ff7f837..17c018a 100644
--- a/lib/srdb1/db.c
+++ b/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;
diff --git a/lib/srdb1/db.h b/lib/srdb1/db.h
index 06ff8f3..a0927e5 100644
--- a/lib/srdb1/db.h
+++ b/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);
/**
diff --git a/lib/srdb1/db_id.c b/lib/srdb1/db_id.c
index b6227a2..29b0226 100644
--- a/lib/srdb1/db_id.c
+++ b/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();
diff --git a/lib/srdb1/db_id.h b/lib/srdb1/db_id.h
index df3f474..13ffc85 100644
--- a/lib/srdb1/db_id.h
+++ b/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);
/**