Răsfoiți Sursa

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

Peter Dunkley 13 ani în urmă
părinte
comite
b17945f760
4 a modificat fișierele cu 39 adăugiri și 34 ștergeri
  1. 5 13
      lib/srdb1/db.c
  2. 26 1
      lib/srdb1/db.h
  3. 7 19
      lib/srdb1/db_id.c
  4. 1 1
      lib/srdb1/db_id.h

+ 5 - 13
lib/srdb1/db.c

@@ -182,17 +182,8 @@ int db_bind_mod(const str* mod, db_func_t* mydbf)
 		return -1;
 		return -1;
 	}
 	}
 	memcpy(name, "db_", 3);
 	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
 	/* for safety we initialize mydbf with 0 (this will cause
 	 *  a segfault immediately if someone tries to call a function
 	 *  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,
 		dbf.use_table = (db_use_table_f)find_mod_export(tmp,
 			"db_use_table", 2, 0);
 			"db_use_table", 2, 0);
 		dbf.init = (db_init_f)find_mod_export(tmp, "db_init", 1, 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.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.query = (db_query_f)find_mod_export(tmp, "db_query", 2, 0);
 		dbf.fetch_result = (db_fetch_result_f)find_mod_export(tmp,
 		dbf.fetch_result = (db_fetch_result_f)find_mod_export(tmp,
@@ -268,7 +260,7 @@ error:
  * Initialize database module
  * Initialize database module
  * \note No function should be called before this
  * \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;
 	struct db_id* id;
 	void* con;
 	void* con;
@@ -296,7 +288,7 @@ db1_con_t* db_do_init(const str* url, void* (*new_connection)())
 	}
 	}
 	memset(res, 0, con_size);
 	memset(res, 0, con_size);
 
 
-	id = new_db_id(url);
+	id = new_db_id(url, nopool);
 	if (!id) {
 	if (!id) {
 		LM_ERR("cannot parse URL '%.*s'\n", url->len, url->s);
 		LM_ERR("cannot parse URL '%.*s'\n", url->len, url->s);
 		goto err;
 		goto err;

+ 26 - 1
lib/srdb1/db.h

@@ -88,6 +88,30 @@ typedef int (*db_use_table_f)(db1_con_t* _h, const str * _t);
  */
  */
 typedef db1_con_t* (*db_init_f) (const str* _sqlurl);
 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.
  * \brief Close a database connection and free all memory used.
  *
  *
@@ -323,6 +347,7 @@ typedef struct db_func {
 	unsigned int      cap;           /* Capability vector of the database transport */
 	unsigned int      cap;           /* Capability vector of the database transport */
 	db_use_table_f    use_table;     /* Specify table name */
 	db_use_table_f    use_table;     /* Specify table name */
 	db_init_f         init;          /* Initialize database connection */
 	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_close_f        close;         /* Close database connection */
 	db_query_f        query;         /* query a table */
 	db_query_f        query;         /* query a table */
 	db_fetch_result_f fetch_result;  /* fetch result */
 	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
  * \return returns a pointer to the db1_con_t representing the connection if it was
    successful, otherwise 0 is returned.
    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);
 
 
 
 
 /**
 /**

+ 7 - 19
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
  * \param url parsed URL
  * \return 0 if parsing was successful and -1 otherwise
  * \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 "s://a/b"
 #define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
 #define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
 
 
 	enum state {
 	enum state {
-		ST_NONPOOL,    /* Non pooling flag */
 		ST_SCHEME,     /* Scheme part */
 		ST_SCHEME,     /* Scheme part */
 		ST_SLASH1,     /* First slash */
 		ST_SLASH1,     /* First slash */
 		ST_SLASH2,     /* Second 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 */
 	/* Initialize all attributes to 0 */
 	memset(id, 0, sizeof(struct db_id));
 	memset(id, 0, sizeof(struct db_id));
-	st = ST_NONPOOL;
+	st = ST_SCHEME;
 	begin = url->s;
 	begin = url->s;
 
 
 	for(i = 0; i < len; i++) {
 	for(i = 0; i < len; i++) {
 		switch(st) {
 		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:
 		case ST_SCHEME:
 			switch(url->s[i]) {
 			switch(url->s[i]) {
 			case ':':
 			case ':':
@@ -242,7 +227,7 @@ static int parse_db_url(struct db_id* id, const str* url, int *poolid )
  * \param url database URL
  * \param url database URL
  * \return connection identifier, or zero on error
  * \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;
 	static int poolid=0;
 	struct db_id* ptr;
 	struct db_id* ptr;
@@ -259,10 +244,13 @@ struct db_id* new_db_id(const str* url)
 	}
 	}
 	memset(ptr, 0, sizeof(struct db_id));
 	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);
 		LM_ERR("error while parsing database URL: '%.*s' \n", url->len, url->s);
 		goto err;
 		goto err;
 	}
 	}
+
+	if (nopool) ptr->poolid = ++poolid;
+	else ptr->poolid = 0;
 	ptr->pid = my_pid();
 	ptr->pid = my_pid();
 
 
 	return ptr;
 	return ptr;

+ 1 - 1
lib/srdb1/db_id.h

@@ -50,7 +50,7 @@ struct db_id {
  * \param url database URL
  * \param url database URL
  * \return new allocated db_id structure, NULL on failure
  * \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);
 
 
 
 
 /**
 /**