Module: sip-router
Branch: master
Commit: 2ba94ce0218a7e7ac0d849c0a9089dc57a59334b
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=2ba94ce…
Author: Jan Janak <jan(a)iptel.org>
Committer: Jan Janak <jan(a)iptel.org>
Date: Tue Feb 17 14:46:42 2009 +0100
Support for db driver names with "db_" prefix in libsrdb2.
This patch adds support for database driver names with "db_" prefix. We
adopted this kamailio convention for sip-router and all database driver
modules will have names starting with "db_", for example, the mysql
database driver will be named "db_mysql".
When searching for a database driver for database "foo", libsrdb2 will
first try to search for a module named "db_foo". If no such module can
be found then the library repeats the search for a module named just
"foo".
Signed-off-by: Jan Janak <jan(a)iptel.org>
---
lib/srdb2/db_drv.c | 42 ++++++++++++++++++++++++++++--------------
1 files changed, 28 insertions(+), 14 deletions(-)
diff --git a/lib/srdb2/db_drv.c b/lib/srdb2/db_drv.c
index 52ae539..0cfcec8 100644
--- a/lib/srdb2/db_drv.c
+++ b/lib/srdb2/db_drv.c
@@ -62,28 +62,42 @@ void db_drv_free(db_drv_t* ptr)
*/
int db_drv_func(db_drv_func_t* func, str* module, char* func_name)
{
- char* buf;
-
- buf = pkg_malloc(module->len + 1);
- if (buf == NULL) {
+ static str prefix = STR_STATIC_INIT("db_");
+ char* buf = NULL, *name;
+
+ if ((buf = pkg_malloc(prefix.len + module->len + 1)) == NULL) {
LOG(L_ERR, "db_drv_func: No memory left\n");
- return -1;
+ goto error;
}
- memcpy(buf, module->s, module->len);
- buf[module->len] = '\0';
-
- if (find_module_by_name(buf) == 0) {
- ERR("db_drv_func: database driver for '%s' not found\n", buf);
- pkg_free(buf);
- return -1;
+
+ memcpy(buf, prefix.s, prefix.len);
+ memcpy(buf + prefix.len, module->s, module->len);
+ buf[prefix.len + module->len] = '\0';
+
+ /* First try to find the module with prefix "db_" */
+ name = buf;
+ if (find_module_by_name(name) == 0) {
+ /* Not found, so try without the prefix */
+ name = buf + prefix.len;
+ if (find_module_by_name(name) == 0) {
+ ERR("db_drv_func: database driver for '%.*s' not found\n",
STR_FMT(module));
+ goto error;
+ }
}
- *func = (db_drv_func_t)find_mod_export(buf, func_name, 0, 0);
- pkg_free(buf);
+
+ *func = (db_drv_func_t)find_mod_export(name, func_name, 0, 0);
+
+ if (buf) pkg_free(buf);
if (*func) return 0;
else return 1;
+
+error:
+ if (buf) pkg_free(buf);
+ return -1;
}
+
/*
* Call function with name <func_name> in DB driver <module>, give
* it pointer <db_struct> as the pointer to the corresponding DB structure