[sr-dev] git:master: lib/srdb1: Changed the creation of non-pooled DB connections from being URL based to function (C-code) based

Peter Dunkley peter.dunkley at crocodile-rcs.com
Fri Apr 20 18:22:02 CEST 2012


Module: sip-router
Branch: master
Commit: b17945f76076114fbab5f2ee198daae5c7b96950
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b17945f76076114fbab5f2ee198daae5c7b96950

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Fri Apr 20 17:17:24 2012 +0100

lib/srdb1: Changed the creation of non-pooled DB connections from being URL based to function (C-code) based

---

 lib/srdb1/db.c    |   18 +++++-------------
 lib/srdb1/db.h    |   27 ++++++++++++++++++++++++++-
 lib/srdb1/db_id.c |   26 +++++++-------------------
 lib/srdb1/db_id.h |    2 +-
 4 files changed, 39 insertions(+), 34 deletions(-)

diff --git a/lib/srdb1/db.c b/lib/srdb1/db.c
index 5905bb0..ff7f837 100644
--- a/lib/srdb1/db.c
+++ b/lib/srdb1/db.c
@@ -182,17 +182,8 @@ int db_bind_mod(const str* mod, db_func_t* mydbf)
 		return -1;
 	}
 	memcpy(name, "db_", 3);
-
-	if (mod->s[0]=='*' )
-	{
-		memcpy(name+3, (mod->s)+1, (mod->len)-1);
-		name[mod->len-1+3] = 0;
-	}
-	else
-	{
-		memcpy(name+3, mod->s, mod->len);
-		name[mod->len+3] = 0;
-	}
+	memcpy(name+3, mod->s, mod->len);
+	name[mod->len+3] = 0;
 
 	/* for safety we initialize mydbf with 0 (this will cause
 	 *  a segfault immediately if someone tries to call a function
@@ -230,6 +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.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,
@@ -268,7 +260,7 @@ error:
  * Initialize database module
  * \note No function should be called before this
  */
-db1_con_t* db_do_init(const str* url, void* (*new_connection)())
+db1_con_t* db_do_init(const str* url, void* (*new_connection)(), int nopool)
 {
 	struct db_id* id;
 	void* con;
@@ -296,7 +288,7 @@ db1_con_t* db_do_init(const str* url, void* (*new_connection)())
 	}
 	memset(res, 0, con_size);
 
-	id = new_db_id(url);
+	id = new_db_id(url, nopool);
 	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 5258e2c..06ff8f3 100644
--- a/lib/srdb1/db.h
+++ b/lib/srdb1/db.h
@@ -89,6 +89,30 @@ 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.
+ *
+ * This function initialize the database API and open a new database
+ * connection. This function must be called after bind_dbmod but before any
+ * other database API function is called.
+ * 
+ * The function takes one parameter, the parameter must contain the database
+ * connection URL. The URL is of the form 
+ * mysql://username:password\@host:port/database where:
+ * 
+ * username: Username to use when logging into database (optional).
+ * password: password if it was set (optional)
+ * host:     Hosname or IP address of the host where database server lives (mandatory)
+ * port:     Port number of the server if the port differs from default value (optional)
+ * database: If the database server supports multiple databases, you must specify the
+ * name of the database (optional).
+ * \see bind_dbmod
+ * \param _sqlurl database connection URL
+ * \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);
+
+/**
  * \brief Close a database connection and free all memory used.
  *
  * The function closes previously open connection and frees all previously 
@@ -323,6 +347,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_close_f        close;         /* Close database connection */
 	db_query_f        query;         /* query a table */
 	db_fetch_result_f fetch_result;  /* fetch result */
@@ -371,7 +396,7 @@ 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)());
+db1_con_t* db_do_init(const str* url, void* (*new_connection)(), int nopool);
 
 
 /**
diff --git a/lib/srdb1/db_id.c b/lib/srdb1/db_id.c
index 4aa564b..b6227a2 100644
--- a/lib/srdb1/db_id.c
+++ b/lib/srdb1/db_id.c
@@ -65,13 +65,12 @@ static int dupl_string(char** dst, const char* begin, const char* end)
  * \param url parsed URL
  * \return 0 if parsing was successful and -1 otherwise
  */
-static int parse_db_url(struct db_id* id, const str* url, int *poolid )
+static int parse_db_url(struct db_id* id, const str* url)
 {
 #define SHORTEST_DB_URL "s://a/b"
 #define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
 
 	enum state {
-		ST_NONPOOL,    /* Non pooling flag */
 		ST_SCHEME,     /* Scheme part */
 		ST_SLASH1,     /* First slash */
 		ST_SLASH2,     /* Second slash */
@@ -100,25 +99,11 @@ static int parse_db_url(struct db_id* id, const str* url, int *poolid )
 	
 	/* Initialize all attributes to 0 */
 	memset(id, 0, sizeof(struct db_id));
-	st = ST_NONPOOL;
+	st = ST_SCHEME;
 	begin = url->s;
 
 	for(i = 0; i < len; i++) {
 		switch(st) {
-		case ST_NONPOOL:
-			st = ST_SCHEME;
-			switch(url->s[i]) {
-			case '*':
-				id->poolid = ++(*poolid);
-				begin++;
-				break;
-
-			default:
-				id->poolid = 0;
-				break;
-			}
-			break;
-
 		case ST_SCHEME:
 			switch(url->s[i]) {
 			case ':':
@@ -242,7 +227,7 @@ static int parse_db_url(struct db_id* id, const str* url, int *poolid )
  * \param url database URL
  * \return connection identifier, or zero on error
  */
-struct db_id* new_db_id(const str* url)
+struct db_id* new_db_id(const str* url, int nopool)
 {
 	static int poolid=0;
 	struct db_id* ptr;
@@ -259,10 +244,13 @@ struct db_id* new_db_id(const str* url)
 	}
 	memset(ptr, 0, sizeof(struct db_id));
 
-	if (parse_db_url(ptr, url, &poolid) < 0) {
+	if (parse_db_url(ptr, url) < 0) {
 		LM_ERR("error while parsing database URL: '%.*s' \n", url->len, url->s);
 		goto err;
 	}
+
+	if (nopool) ptr->poolid = ++poolid;
+	else ptr->poolid = 0;
 	ptr->pid = my_pid();
 
 	return ptr;
diff --git a/lib/srdb1/db_id.h b/lib/srdb1/db_id.h
index b7427a7..df3f474 100644
--- a/lib/srdb1/db_id.h
+++ b/lib/srdb1/db_id.h
@@ -50,7 +50,7 @@ struct db_id {
  * \param url database URL
  * \return new allocated db_id structure, NULL on failure
  */
-struct db_id* new_db_id(const str* url);
+struct db_id* new_db_id(const str* url, int nopool);
 
 
 /**




More information about the sr-dev mailing list