Browse Source

- cleanup in database API
- introduce a helper method for db_init, that does the actual work
- database modules needs now only a small wrapper function
- functionality checked for mysql and postgres with the available tests


git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@3481 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt 17 years ago
parent
commit
2b32ef255b
2 changed files with 77 additions and 1 deletions
  1. 64 0
      lib/srdb1/db.c
  2. 13 1
      lib/srdb1/db.h

+ 64 - 0
lib/srdb1/db.c

@@ -33,6 +33,8 @@
 #include "../str.h"
 #include "../ut.h"
 #include "db_cap.h"
+#include "db_id.h"
+#include "db_pool.h"
 #include "db.h"
 
 
@@ -223,3 +225,65 @@ int table_version(db_func_t* dbf, db_con_t* connection, const str* table)
 	dbf->free_result(connection, res);
 	return ret;
 }
+
+/*
+ * Initialize database module
+ * No function should be called before this
+ */
+db_con_t* db_do_init(const char* url, void* (*new_connection)())
+{
+	struct db_id* id;
+	void* con;
+	db_con_t* res;
+
+	int con_size = sizeof(db_con_t) + sizeof(void *);
+	id = 0;
+	res = 0;
+
+	if (!url) {
+		LM_ERR("invalid parameter value\n");
+		return 0;
+	}
+	if (strlen(url) > 255)
+	{
+		LM_ERR("SQL URL too long\n");
+		return 0;
+	}
+	
+	/* this is the root memory for this database connection. */
+	res = (db_con_t*)pkg_malloc(con_size);
+	if (!res) {
+		LM_ERR("no private memory left\n");
+		return 0;
+	}
+	memset(res, 0, con_size);
+
+	id = new_db_id(url);
+	if (!id) {
+		LM_ERR("cannot parse URL '%s'\n", url);
+		goto err;
+	}
+
+	/* Find the connection in the pool */
+	con = pool_get(id);
+	if (!con) {
+		LM_DBG("connection %p not found in pool\n", id);
+		/* Not in the pool yet */
+		con = new_connection(id);
+		if (!con) {
+			LM_ERR("could not add connection to the pool");
+			goto err;
+		}
+		pool_insert((struct pool_con*)con);
+	} else {
+		LM_DBG("connection %p found in pool\n", id);
+	}
+
+	res->tail = (unsigned long)con;
+	return res;
+
+ err:
+	if (id) free_db_id(id);
+	if (res) pkg_free(res);
+	return 0;
+}

+ 13 - 1
lib/srdb1/db.h

@@ -330,7 +330,7 @@ int bind_dbmod(char* mod, db_func_t* dbf);
 
 /**
  * \brief Get the version of a table.
- * 
+ *
  * Returns the version number of a given table from the version table.
  * \param dbf database module callbacks
  * \param con database connection handle
@@ -339,4 +339,16 @@ int bind_dbmod(char* mod, db_func_t* dbf);
  */
 int table_version(db_func_t* dbf, db_con_t* con, const str* table);
 
+/**
+ * \brief Helper for db_init 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
+ * \return returns a pointer to the db_con_t representing the connection if it was
+   successful, otherwise 0 is returned.
+ */
+db_con_t* db_do_init(const char* url, void* (*new_connection)());
+
 #endif /* DB_H */