瀏覽代碼

- global db api data

Jan Janak 18 年之前
父節點
當前提交
2ff46d3ec3
共有 2 個文件被更改,包括 24 次插入323 次删除
  1. 4 165
      db/db.c
  2. 20 158
      db/db.h

+ 4 - 165
db/db.c

@@ -2,6 +2,7 @@
  * $Id$
  *
  * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2006-2007 iptelorg GmbH
  *
  * This file is part of ser, a free SIP server.
  *
@@ -36,170 +37,8 @@
 #include "../mem/mem.h"
 #include "../str.h"
 #include "../ut.h"
-#include "db_cap.h"
+#include "../list.h"
+#include "db_drv.h"
 #include "db.h"
 
-
-
-/* fills mydbf with the corresponding db module callbacks
- * returns 0 on success, -1 on error
- * on error mydbf will contain only 0s */
-int bind_dbmod(char* mod, db_func_t* mydbf)
-{
-	char* tmp, *p;
-	int len;
-	db_func_t dbf;
-
-	if (!mod) {
-		LOG(L_CRIT, "BUG: bind_dbmod(): null database module name\n");
-		return -1;
-	}
-	if (mydbf==0) {
-		LOG(L_CRIT, "BUG: bind_dbmod(): null dbf parameter\n");
-		return -1;
-	}
-	/* for safety we initialize mydbf with 0 (this will cause
-	 *  a segfault immediately if someone tries to call a function
-	 *  from it without checking the return code from bind_dbmod -- andrei */
-	memset((void*)mydbf, 0, sizeof(db_func_t));
-
-	p = strchr(mod, ':');
-	if (p) {
-		len = p - mod;
-		tmp = (char*)pkg_malloc(len + 1);
-		if (!tmp) {
-			LOG(L_ERR, "ERROR: bind_dbmod(): No memory left\n");
-			return -1;
-		}
-		memcpy(tmp, mod, len);
-		tmp[len] = '\0';
-	} else {
-		tmp = mod;
-	}
-
-	dbf.cap = 0;
-
-	if (find_module_by_name(tmp) == 0) {
-		ERR("Database driver '%s' not found\n", tmp);
-		goto err;
-	}
-
-	     /* All modules must export db_use_table */
-	dbf.use_table = (db_use_table_f)find_mod_export(tmp, "db_use_table", 2, 0);
-	if (dbf.use_table == 0) {
-		LOG(L_ERR, "bind_dbmod: Module %s does not export db_use_table function\n", tmp);
-		goto err;
-	}
-
-	     /* All modules must export db_init */
-	dbf.init = (db_init_f)find_mod_export(tmp, "db_init", 1, 0);
-	if (dbf.init == 0) {
-		LOG(L_ERR, "bind_dbmod: Module %s does not export db_init function\n", tmp);
-		goto err;
-	}
-
-	     /* All modules must export db_close */
-	dbf.close = (db_close_f)find_mod_export(tmp, "db_close", 2, 0);
-	if (dbf.close == 0) {
-		LOG(L_ERR, "bind_dbmod: Module %s does not export db_close function\n", tmp);
-		goto err;
-	}
-
-	dbf.query = (db_query_f)find_mod_export(tmp, "db_query", 2, 0);
-	if (dbf.query) {
-		dbf.cap |= DB_CAP_QUERY;
-	}
-
-	dbf.raw_query = (db_raw_query_f)find_mod_export(tmp, "db_raw_query", 2, 0);
-	if (dbf.raw_query) {
-		dbf.cap |= DB_CAP_RAW_QUERY;
-	}
-
-	     /* Free result must be exported if DB_CAP_QUERY 
-	      * or DB_CAP_RAW_QUERY is set
-	      */
-	dbf.free_result = (db_free_result_f)find_mod_export(tmp, "db_free_result", 2, 0);
-	if ((dbf.cap & (DB_CAP_QUERY | DB_CAP_RAW_QUERY))
-	    && (dbf.free_result == 0)) {
-		LOG(L_ERR, "bind_dbmod: Module %s supports quries but does not export free_result function\n", tmp);
-		goto err;
-	}
-
-	dbf.insert = (db_insert_f)find_mod_export(tmp, "db_insert", 2, 0);
-	if (dbf.insert) {
-		dbf.cap |= DB_CAP_INSERT;
-	}
-
-	dbf.delete = (db_delete_f)find_mod_export(tmp, "db_delete", 2, 0);
-	if (dbf.delete) {
-		dbf.cap |= DB_CAP_DELETE;
-	}
-
-	dbf.update = (db_update_f)find_mod_export(tmp, "db_update", 2, 0);
-	if (dbf.update) {
-		dbf.cap |= DB_CAP_UPDATE;
-	}
-
-	dbf.replace = (db_replace_f)find_mod_export(tmp, "db_replace", 2, 0);
-	if (dbf.replace) {
-		dbf.cap |= DB_CAP_REPLACE;
-	}
-
-	*mydbf=dbf; /* copy */
-	return 0;
-
- err:
-	if (tmp != mod) pkg_free(tmp);
-	return -1;
-}
-
-
-/*
- * Get version of a table
- * If there is no row for the given table, return version 0
- */
-int table_version(db_func_t* dbf, db_con_t* connection, const str* table)
-{
-	db_key_t key[1], col[1];
-	db_val_t val[1];
-	db_res_t* res;
-	int ret;
-
-	if (!dbf||!connection || !table) {
-		LOG(L_CRIT, "BUG: table_version(): Invalid parameter value\n");
-		return -1;
-	}
-
-	if (dbf->use_table(connection, VERSION_TABLE) < 0) {
-		LOG(L_ERR, "table_version(): Error while changing table\n");
-		return -1;
-	}
-
-	key[0] = TABLENAME_COLUMN;
-
-	VAL_TYPE(val) = DB_STR;
-	VAL_NULL(val) = 0;
-	VAL_STR(val) = *table;
-	
-	col[0] = VERSION_COLUMN;
-	
-	if (dbf->query(connection, key, 0, val, col, 1, 1, 0, &res) < 0) {
-		LOG(L_ERR, "table_version(): Error in db_query\n");
-		return -1;
-	}
-
-	if (RES_ROW_N(res) == 0) {
-		DBG("table_version(): No row for table %.*s found\n", table->len, ZSW(table->s));
-		return 0;
-	}
-
-	if (RES_ROW_N(res) != 1) {
-		LOG(L_ERR, "table_version(): Invalid number of rows received: %d, %.*s\n", RES_ROW_N(res), table->len, ZSW(table->s));
-		dbf->free_result(connection, res);
-		return -1;
-	}
-
-	ret = VAL_INT(ROW_VALUES(RES_ROWS(res)));
-	dbf->free_result(connection, res);
-	return ret;
-}
+struct db_root db = DBLIST_INITIALIZER(db);

+ 20 - 158
db/db.h

@@ -1,7 +1,8 @@
 /*
  * $Id$
  *
- * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
  *
  * This file is part of ser, a free SIP server.
  *
@@ -24,168 +25,29 @@
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-/*
- * History:
- * --------
- *  2004-06-06  removed db_* macros and global dbf (andrei)
- */
-
-
-#ifndef DB_H
-#define DB_H
+#ifndef _DB_H
+#define _DB_H  1
 
-#include "db_key.h"
-#include "db_op.h"
-#include "db_val.h"
-#include "db_con.h"
-#include "db_row.h"
+#include "db_gen.h"
+#include "db_ctx.h"
+#include "db_uri.h"
+#include "db_cmd.h"
 #include "db_res.h"
-#include "db_cap.h"
-
-/*
- * Various database flags shared by modules 
- */
-#define DB_LOAD_SER   (1 << 0)  /* The row should be loaded by SER */
-#define DB_DISABLED   (1 << 1)  /* The row is disabled */
-#define DB_CANON      (1 << 2)  /* Canonical entry (domain or uri) */
-#define DB_IS_TO      (1 << 3)  /* The URI can be used in To */
-#define DB_IS_FROM    (1 << 4)  /* The URI can be used in From */
-#define DB_FOR_SERWEB (1 << 5)  /* Credentials instance can be used by serweb */
-#define DB_PENDING    (1 << 6)
-#define DB_DELETED    (1 << 7)
-#define DB_CALLER_DELETED (1 << 8) /* Accounting table */
-#define DB_CALLEE_DELETED (1 << 9) /* Accounting table */
-#define DB_MULTIVALUE     (1 << 10) /* Attr_types table */
-#define DB_FILL_ON_REG    (1 << 11) /* Attr_types table */
-#define DB_REQUIRED       (1 << 12) /* Attr_types table */
-#define DB_DIR            (1 << 13) /* Domain_settings table */
+#include "db_rec.h"
+#include "db_fld.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
 
-/*
- * Specify table name that will be used for
- * subsequent operations
- */
-typedef int (*db_use_table_f)(db_con_t* _h, const char* _t);
+struct db_gen;
 
+DBLIST_HEAD(db_root);
 
-/*
- * Initialize database connection and
- * obtain the connection handle
- */
-typedef db_con_t* (*db_init_f) (const char* _sqlurl);
-
-
-/*
- * Close a database connection and free
- * all memory used
- */
-typedef void (*db_close_f) (db_con_t* _h); 
-
-
-/*
- * Query table for specified rows
- * _h: structure representing database connection
- * _k: key names
- * _op: conditions
- * _v: values of the keys that must match
- * _c: column names to return
- * _n: nmber of key=values pairs to compare
- * _nc: number of columns to return
- * _o: order by the specified column
- * _r: Result will be stored in this variable
- *     NULL if there is no result
- */
-typedef int (*db_query_f) (db_con_t* _h, db_key_t* _k, 
-			   db_op_t* _op, db_val_t* _v, 
-			   db_key_t* _c, int _n, int _nc,
-			   db_key_t _o, db_res_t** _r);
-
-
-/*
- * Raw SQL query, database specific !
- */
-typedef int (*db_raw_query_f) (db_con_t* _h, char* _s, db_res_t** _r);
-
-
-/*
- * Free a result allocated by db_query
- * _h: structure representing database connection
- * _r: db_res structure
- */
-typedef int (*db_free_result_f) (db_con_t* _h, db_res_t* _r);
-
-
-/*
- * Insert a row into specified table
- * _h: structure representing database connection
- * _k: key names
- * _v: values of the keys
- * _n: number of key=value pairs
- */
-typedef int (*db_insert_f) (db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n);
-
-
-/*
- * Delete a row from the specified table
- * _h: structure representing database connection
- * _k: key names
- * _o: operators
- * _v: values of the keys that must match
- * _n: number of key=value pairs
- */
-typedef int (*db_delete_f) (db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n);
-
-
-/*
- * Update some rows in the specified table
- * _h: structure representing database connection
- * _k: key names
- * _o: operators
- * _v: values of the keys that must match
- * _uk: updated columns
- * _uv: updated values of the columns
- * _n: number of key=value pairs
- * _un: number of columns to update
- */
-typedef int (*db_update_f) (db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
-			    db_key_t* _uk, db_val_t* _uv, int _n, int _un);
-
-/*
- * Insert a row and replace if one already 
- */
-typedef int (*db_replace_f) (db_con_t* handle, db_key_t* keys, db_val_t* vals, int n);
-
-
-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_close_f       close;        /* Close database connection */
-	db_query_f       query;        /* query a table */
-	db_raw_query_f   raw_query;    /* Raw query - SQL */
-	db_free_result_f free_result;  /* Free a query result */
-	db_insert_f      insert;       /* Insert into table */
-	db_delete_f      delete;       /* Delete from table */ 
-	db_update_f      update;       /* Update table */
-	db_replace_f     replace;      /* Replace row in a table */
-} db_func_t;
-
-
-
-/*
- * Bind database module functions
- * returns TRUE if everything went OK
- * FALSE otherwise
- */
-int bind_dbmod(char* mod, db_func_t* dbf);
-
-
-/*
- * Get the version of the given table. If there is
- * no row for the table then the function returns
- * version 0. -1 is returned on error.
- */
-int table_version(db_func_t* dbf, db_con_t* con, const str* table);
+extern struct db_root db;
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
-#endif /* DB_H */
+#endif /* _DB_H */