Procházet zdrojové kódy

database module interface moved.

Jan Janak před 23 roky
rodič
revize
29cbdf7705
9 změnil soubory, kde provedl 1060 přidání a 1 odebrání
  1. 1 1
      Makefile.sources
  2. 33 0
      db/db.c
  3. 251 0
      db/db.h
  4. 4 0
      db/doc/NEW-DB.howto
  5. 525 0
      db/doc/db-api.txt
  6. 12 0
      db/example/Makefile
  7. 5 0
      db/example/README
  8. 228 0
      db/example/dbexample.c
  9. 1 0
      ser.cfg

+ 1 - 1
Makefile.sources

@@ -11,7 +11,7 @@
 # defines: sources, objs, depends
 # defines: sources, objs, depends
 #
 #
 
 
-sources=$(filter-out $(auto_gen), $(wildcard *.c) $(wildcard mem/*.c) $(wildcard parser/*.c) ) $(auto_gen)
+sources=$(filter-out $(auto_gen), $(wildcard *.c) $(wildcard mem/*.c) $(wildcard parser/*.c) $(wildcard db/*.c) ) $(auto_gen)
 objs=$(sources:.c=.o)
 objs=$(sources:.c=.o)
 extra_objs=
 extra_objs=
 depends=$(sources:.c=.d)
 depends=$(sources:.c=.d)

+ 33 - 0
db/db.c

@@ -0,0 +1,33 @@
+#include "db.h"
+#include "../sr_module.h"
+
+db_func_t dbf;
+
+
+int bind_dbmod(void)
+{
+	dbf.use_table = (db_use_table_f)find_export("db_use_table", 2);
+
+	dbf.init = (db_init_f)find_export("db_init", 1);
+	if (!dbf.init) return 1;
+
+	dbf.close = (db_close_f)find_export("db_close", 2);
+	if (!dbf.close) return 1;
+
+	dbf.query = (db_query_f)find_export("db_query", 2);
+	if (!dbf.query) return 1;
+
+	dbf.free_query = (db_free_query_f)find_export("db_free_query", 2);
+	if (!dbf.free_query) return 1;
+
+	dbf.insert = (db_insert_f)find_export("db_insert", 2);
+	if (!dbf.insert) return 1;
+
+	dbf.delete = (db_delete_f)find_export("db_delete", 2);
+	if (!dbf.delete) return 1;
+
+	dbf.update = (db_update_f)find_export("db_update", 2);
+	if (!dbf.update) return 1;
+
+	return 0;
+}

+ 251 - 0
db/db.h

@@ -0,0 +1,251 @@
+#ifndef __DB_H__
+#define __DB_H__
+
+#include <time.h>
+
+/*
+ * Generic database interface
+ *
+ * $id$
+ */
+
+
+/* =================== db_key =============== */
+
+/*
+ * Type of a database key (column)
+ */
+typedef const char* db_key_t;
+
+
+/* =================== db_val =============== */
+
+/*
+ * Accepted column types
+ */
+typedef enum {
+	DB_INT,
+        DB_DOUBLE,
+	DB_STRING,
+	DB_DATETIME
+} db_type_t;
+
+
+/*
+ * Column value structure
+ */
+typedef struct {
+	db_type_t type;                  /* Type of the value */
+	int nul;                         /* Means that the column in database has no value */
+	union {
+		int          int_val;    /* integer value */
+		double       double_val; /* double value */
+		time_t       time_val;   /* unix time value */
+		const char*  string_val; /* NULL terminated string */
+	} val;                           /* union of all possible types */
+} db_val_t;
+
+
+/*
+ * Useful macros for accessing attributes of db_val structure
+ */
+
+/* Get value type */
+#define VAL_TYPE(dv)   ((dv)->type)
+
+/* Get null flag (means that value in dabase is null) */
+#define VAL_NULL(dv)   ((dv)->nul)
+
+/* Get integer value */
+#define VAL_INT(dv)    ((dv)->val.int_val)
+
+/* Get double value */
+#define VAL_DOUBLE(dv) ((dv)->val.double_val)
+
+/* Get time_t value */
+#define VAL_TIME(dv)   ((dv)->val.time_val)
+
+/* Get char* value */
+#define VAL_STRING(dv) ((dv)->val.string_val)
+
+
+/* ==================== db_con ======================= */
+
+/*
+ * This structure represents a database connection
+ * and pointer to this structure is used as a connection
+ * handle
+ */
+typedef struct {
+	char* table;     /* Default table to use */
+	void* con;       /* Mysql Connection */
+	void* res;       /* Result of previous operation */
+	void* row;       /* Actual row in the result */
+	int   connected; /* TRUE if connection is established */
+} db_con_t;
+
+
+/* ===================== db_row ====================== */
+
+/*
+ * Structure holding result of query_table function (ie. table row)
+ */
+typedef struct db_row {
+	db_val_t* values;  /* Columns in the row */
+	int n;             /* Number of columns in the row */
+} db_row_t;
+
+/* Useful macros for manipulating db_row structure attributes */
+
+/* Get row members */
+#define ROW_VALUES(rw) ((rw)->values)
+
+/* Get number of member in the row */
+#define ROW_N(rw)      ((rw)->n)
+
+
+/* ===================== db_res ====================== */
+
+typedef struct db_res {
+	struct {
+		db_key_t* names;   /* Column names */
+		db_type_t* types;  /* Column types */
+		int n;             /* Number of columns */
+	} col;
+	struct db_row* rows;       /* Rows */
+	int n;                     /* Number of rows */
+} db_res_t;
+
+/* Useful macros for manipulating db_res attributes */
+
+/* Column names */
+#define RES_NAMES(re) ((re)->col.names)
+
+/* Column types */
+#define RES_TYPES(re) ((re)->col.types)
+
+/* Number of columns */
+#define RES_COL_N(re) ((re)->col.n)
+
+/* Rows */
+#define RES_ROWS(re)  ((re)->rows)
+
+/* Number of rows */
+#define RES_ROW_N(re) ((re)->n)
+
+
+/*
+ * Specify table name that will be used for
+ * subsequent operations
+ */
+typedef int (*db_use_table_f)(db_con_t* _h, const char* _t);
+
+
+/*
+ * 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
+ * _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_val_t* _v, db_key_t* _c, 
+			   int _n, int _nc,
+			   db_key_t _o, db_res_t** _r);
+
+
+/*
+ * Free a result allocated by db_query
+ * _h: structure representing database connection
+ * _r: db_res structure
+ */
+typedef int (*db_free_query_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
+ * _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_val_t* _v, int _n);
+
+
+/*
+ * Update some rows in the specified table
+ * _h: structure representing database connection
+ * _k: key names
+ * _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_val_t* _v,
+			    db_key_t* _uk, db_val_t* _uv, int _n, int _un);
+
+
+
+typedef struct db_func{
+	db_use_table_f  use_table;   /* Specify table name */
+	db_init_f       init;        /* Initialize dabase connection */
+	db_close_f      close;       /* Close database connection */
+	db_query_f      query;       /* query a table */
+	db_free_query_f free_query;  /* 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_func_t;
+
+
+/*
+ * Bind database module functions
+ * returns TRUE if everything went OK
+ * FALSE otherwise
+ */
+int bind_dbmod(void);
+
+
+extern db_func_t dbf;
+
+
+#define db_use_table  (dbf.use_table)
+#define db_init       (dbf.init)
+#define db_close      (dbf.close)
+#define db_query      (dbf.query)
+#define db_free_query (dbf.free_query)
+#define db_insert     (dbf.insert)
+#define db_delete     (dbf.delete)
+#define db_update     (dbf.update)
+ 
+#endif

+ 4 - 0
db/doc/NEW-DB.howto

@@ -0,0 +1,4 @@
+How to write a support for a new database
+
+TBD
+

+ 525 - 0
db/doc/db-api.txt

@@ -0,0 +1,525 @@
+$Id$
+
+Generic Database Interface
+--------------------------
+
+This is a generic database interface for modules that need to utilize a 
+database. The interface should be used by all modules that access database.
+The interface will be independent of the underlying database server.
+
+Notes:
+
+If possible, use predefined macros if you need to access any structure 
+attributes.  
+
+For additional description, see comments in sources of mysql module.
+
+If you want to see more complicated examples of how the API could be used, 
+see sources of dbexample, usrloc or auth modules.
+
+
+1 Data types
+
+There are several new data types. All of them are defined in header file db.h,
+a client must include the header file to be able to use them.
+
+1.1 Type db_con_t
+
+1.1.1 Description
+
+This type represents a database connection, all database functions (described 
+below) use a variable of this type as one argument. In other words, variable 
+of db_con_t type serves as a handle for a particular database connection.
+
+1.1.2 Definition
+
+   typedef struct db_con {
+        char* table;     /* Default table to use */
+        void* con;       /* Database connection */
+        void* res;       /* Result of previous operation */
+        void* row;       /* Internal, not for public use */
+        int connected;   /* TRUE if connection is established */
+   } db_con_t;
+
+1.1.3 Macros
+
+There are no macros for db_con_t type.
+
+
+1.2 Type db_key_t
+
+1.2.1 Description
+
+This type represents a database key. Every time you need to specify a key 
+value, this type should be used. In fact, this type is identical to const 
+char*.
+
+1.2.2 Definion
+   
+   typedef const char* db_key_t;
+
+1.2.3 Macros
+
+There are no macros (It is not needed).
+
+
+1.3 Type db_type_t
+
+1.3.1 Description
+
+Each cell in a database table can be of a different type. To distinguish
+among these types, the db_type_t enumeration is used. Every value of the
+enumeration represents one datatype that is recognized by the database
+API. This enumeration is used in conjunction with db_type_t. For more
+information, see the next section.
+
+1.3.2 Definition
+
+   typedef enum {
+       DB_INT,       /* Integer number */
+       DB_DOUBLE,    /* Decimal number */
+       DB_STRING,    /* String */
+       DB_DATETIME   /* Date and time */
+   } db_type_t;
+
+1.3.3 Macros
+
+There are no macros.
+
+
+1.4 Type db_val_t
+
+1.4.1 Description
+
+This structure represents a value in the database. Several datatypes are
+recognized and converted by the database API:
+
+DB_INT      - Value in the database represents an integer number
+DB_DOUBLE   - Value in the database represents a decimal number
+DB_STRING   - Value in the database represents a string
+DB_DATETIME - Value in the database represents date and time
+
+These datatypes are automaticaly recognized, converted from internal database
+representation and stored in the variable of corresponding type.
+
+1.4.2 Definition
+
+    typedef struct db_val {
+         db_type_t type;              /* Type of the value */
+         int nul;                     /* NULL flag */
+         union {                      
+             int int_val;             /* Integer value */
+             double double_val;       /* Double value */
+             time_t time_val;         /* Unix time_t value */
+             const char* string_val;  /* Zero terminated string */
+         } val;
+    } db_val_t;
+
+1.4.3 Macros
+
+Note: All macros expect reference to db_val_t variable as the parameter.
+
+1.4.3.1 VAL_TYPE(value) Macro
+
+Use this macro if you need to set/get the type of the value
+
+Example: VAL_TYPE(val) = DB_INT;
+         if (VAL_TYPE(val) == DB_FLOAT) ...
+
+1.4.3.2 VAL_NULL(value) Macro
+
+Use this macro if you need to set/get the null flag. Non-zero flag means that 
+the corresponding cell in the database contained no data (NULL value in MySQL
+terminology).
+
+Example: if (VAL_NULL(val) == 1) {
+             printf("The cell is NULL");
+         }
+
+1.4.3.3 VAL_INT(value) Macro
+
+Use this macro if you need to access integer value in the db_val_t structure.
+
+Example: if (VAL_TYPE(val) == DB_INT) {
+             printf("%d", VAL_INT(val));
+         }
+
+1.4.3.4 VAL_DOUBLE(value) Macro 
+
+Use this macro if you need to access double value in the db_val_t structure.
+
+Example: if (VAL_TYPE(val) == DB_DOUBLE) {
+             printf("%f", VAL_DOUBLE(val));
+         }
+
+1.4.3.5 VAL_TIME(value) Macro 
+
+Use this macro if you need to access time_t value in the db_val_t structure.
+
+Example: time_t tim;
+         if (VAL_TYPE(val) == DB_DATETIME) {
+             tim = VAL_TIME(val);
+         }
+
+1.4.3.6 VAL_STRING(value) Macro 
+
+Use this macro if you need to access string value in the db_val_t structure.
+
+Example: if (VAL_TYPE(val) == DB_STRING) {
+             printf("%s", VAL_STRING(val));
+         }
+
+
+1.5 Type db_row_t
+
+1.5.1 Description
+
+This type represents one row in a database table. In other words, the row is an
+array of db_val_t variables, where each db_val_t variable represents exactly 
+one cell in the table.
+
+1.5.2 Definition
+
+   typedef struct db_row {
+       db_val_t* values;    /* Array of values in the row */
+       int n;               /* Number of values in the row */
+   } db_val_t;
+
+1.5.3 Macros
+
+1.5.3.1 ROW_VALUES(row) Macro 
+
+Use this macro to get pointer to the array of db_val_t structures.
+
+Example: db_val_t* v = ROW_VALUES(row);
+         if (VAL_TYPE(v) == DB_INT) ....
+
+1.5.3.2 ROW_N(row) Macro 
+
+Use this macro to get number of cells in the row.
+
+Example: db_val_t* val = ROW_VALUES(row);
+         for(i = 0; i < ROW_N(row); i++) {
+             switch(VAL_TYPE(val + i)) {
+             case DB_INT: ...; break;
+             case DB_DOUBLE: ...; break;
+             ...
+             }
+         }
+
+
+1.6 Type db_res_t
+
+1.6.1 Description
+
+This type represents a result returned by db_query function (see below). The 
+result can consist of zero or more rows (see db_row_t description).
+
+Note: A variable of type db_res_t returned by db_query function uses dynamicaly
+      allocated memory, don't forget to call db_free_query if you don't need 
+      the variable anymore. You will encounter memory leaks if you fail to do 
+      this !
+
+In addition to zero or more rows, each db_res_t object contains also an array 
+of db_key_t objects. The objects represent keys (names of columns).
+
+1.6.2 Definition
+
+   typedef struct db_res {
+       struct {
+           db_key_t* keys;    /* Array of column names */
+           db_type_t* types;  /* Array of column types */
+           int n;             /* Number of columns */
+       } col;
+       struct db_row* rows;   /* Array of rows */
+       int n;                 /* Number of rows */
+   } db_res_t;
+
+1.6.3 Macros
+
+1.6.3.1 RES_NAMES(res) Macro 
+
+Use this macro if you want to obtain pointer to the array of cell names.
+
+Example: db_key_t* column_names = ROW_NAMES(row);
+
+1.6.3.2 RES_COL_N(res) Macro 
+
+Use this macro if you want to get the number of columns in the result.
+
+Example: int ncol = RES_COL_N(res)
+         for(i = 0; i < ncol; i++) {
+             /* do something with the column */
+         }
+
+1.6.3.3 RES_ROWS(res) Macro 
+
+Use this macro if you need to obtain pointer to array of rows.
+
+Example: db_row_t* rows = RES_ROWS(res);
+ 
+1.6.3.4 RES_ROW_N(res) Macro 
+
+Use this macro if you need to obtain the number of rows in the result
+
+Example: int n = RES_ROW_N(res);
+
+
+
+2 Functions
+
+There are several functions that implement the database API logic. All function
+names start with db_ prefix, except bind_dbmod. bind_dbmod function is 
+implemented in db.c file, all other functions are implemented in a standalone 
+database module. You will need to compile and link db.c in your module to be 
+able to use the bind_dbmod function. Detailed function description follows.
+
+
+2.1 Function bind_dbmod
+
+2.1.1 Description
+
+This function is special, it's only purpose is to call find_export function in
+the ser core and find addresses of all other functions (starting with db_
+prefix). This function MUST be called __FIRST__ !
+
+2.1.2 Prototype
+
+   int bind_dbmod(void);
+
+2.1.3 Parameters
+
+The function takes no parameters.
+
+2.1.4 Return Value
+
+The function returns TRUE if it was able to find addresses of all other 
+functions, otherwise FALSE is returned.
+
+
+2.2 Function db_init
+
+2.2.1 Description
+
+Use this function to initialize the database API and open a new database 
+connection. This function must be called after bind_dbmod but before any other 
+function is called.
+
+2.2.2 Prototype
+
+   db_con_t* db_init(const char* _sql_url);
+
+2.2.3 Parameters
+
+The function takes one parameter, the parameter must contain database 
+connection URL. The URL is of the form 
+sql://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 
+          name of the database (optional).
+
+
+2.2.4 Return Value
+
+The function returns pointer to db_con_t* representing the connection if it was
+successful, otherwise NULL is returned.
+
+
+2.3 Function db_close
+
+2.3.1 Description
+
+The function closes previously open connection and frees all previously 
+allocated memory. The function db_close must be the very last function called.
+
+
+2.3.2 Prototype
+
+   void db_close(db_con_t* _h);
+
+2.3.3 Parameters
+
+The function takes one parameter, this parameter is a pointer to db_con_t
+structure representing database connection that should be closed.
+
+2.3.4 Return Value
+
+Function doesn't return anything.
+
+
+2.4 Function db_query
+
+2.4.1 Description
+
+This function implements SELECT SQL directive.
+
+2.4.2 Prototype
+
+   int db_query(db_con_t* _h, db_key_t* _k,
+                db_val_t* _v, db_key_t* _c, 
+	        int _n, int _nc, db_key_t _o, db_res_t** _r);
+
+2.4.3 Parameters
+
+The function takes 7 parameters:
+_h: Database connection handle
+_k: Array of column names that will be compared and their values must match
+_v: Array of values, columns specified in _k parameter must match these values
+_c: Array of column names that you are interested in
+_n: Number of key-value pairs to match in _k and _v parameters
+_nc: Number of columns in _c parameter
+_o: Order by
+_r: Address of variable where pointer to the result will be stored
+
+If _k and _v parameters are NULL and _n is zero, you will get the whole table.
+if _c is NULL and _nc is zero, you will get all table columns in the result
+
+_r will point to a dynamically allocated structure, it is neccessary to call
+db_free_query function once you are finished with the result.
+
+Strings in the result are not duplicated, they will be discarded if you call
+db_free_query, make a copy yourself if you need to keep it after db_free_query.
+
+You must call db_free_query _BEFORE_ you can call db_query again !
+
+2.4.4 Return Value
+
+The function returns TRUE if everything is OK, otherwise FALSE is returned.
+
+
+2.5 Function db_free_query
+
+2.5.1 Description
+
+This function frees all memory allocated previously in db_query, it is
+neccessary to call this function on a db_res_t structure if you don't need the
+structure anymore. You must call this function _BEFORE_ you call db_query
+again !
+
+2.5.2 Prototype
+
+   int db_free_query(db_con_t* _h, db_res_t* _r);
+
+2.5.3 Parameters
+
+The function takes 2 parameters:
+_h: Database connection handle
+_r: Pointer to db_res_t structure to destroy
+
+2.5.4 Return Value
+
+The function returns TRUE if everything is OK, otherwise the function returns
+FALSE.
+
+
+2.6 Function db_insert
+
+2.6.1 Description
+
+This function implements INSERT SQL directive, you can insert one or more
+rows in a table using this function.
+
+2.6.2 Prototype
+
+   int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n);
+
+2.6.3 Parameters
+
+The function takes 4 parameters:
+_h: Database connection handle
+_k: Array of keys (column names) 
+_v: Array of values for keys specified in _k parameter
+_n: Number of keys-value pairs int _k and _v parameters
+
+2.6.4 Return Value
+
+The function returns TRUE if everything is OK, otherwise the function returns
+FALSE.
+
+
+2.7 Function db_delete
+
+2.7.1 Description
+
+This function implements DELETE SQL directive, it is possible to delete one or
+more rows from a table.
+
+2.7.2 Prototype
+
+   int db_delete(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n);
+
+2.7.3 Parameters
+
+The function takes 4 parameters:
+_h: Database connection handle
+_k: Array of keys (column names) that will be matched
+_v: Array of values that the row must match to be deleted
+_n: Number of keys-value parameters in _k and _v parameters
+
+If _k is NULL and _v is NULL and _n is zero, all rows are deleted (table will
+be empty).
+
+2.7.4 Return Value
+
+The function returns TRUE fi everything is OK, otherwise the function returns
+FALSE.
+
+
+2.8 Function db_update
+
+2.8.1 Description
+
+The function implements UPDATE SQL directive. It is possible to modify one
+or more rows in a table using this function.
+
+2.8.2 Prototype
+
+   int db_update(db_con_t* _h, db_key_t* _k, db_val_t* _v,
+	         db_key_t* _uk, db_val_t* _uv, int _n, int _un);
+
+2.8.3 Parameters
+
+The function takes 7 parameters:
+_h: Database connection handle
+_k: Array of keys (column names) that will be matched
+_v: Array of values that the row must match to be modified
+_uk: Array of keys (column names) that will be modified
+_uv: New values for keys specified in _k parameter
+_n: Number of key-value pairs in _k and _v parameters
+_un: Number of key-value pairs in _uk and _uv parameters 
+
+2.8.4 Return Value
+
+The function returns TRUE if everything is OK, otherwise the function returns
+FALSE.
+
+
+2.9 Function db_use_table
+
+2.9.1 Description
+
+The function db_use_table takes a table name and stores it db_con_t structure.
+All subsequent operations (insert, delete, update, query) are performed on
+that table.
+
+2.9.2 Prototype
+
+   int db_use_table_f(db_con_t* _h, const char* _t);
+
+2.9.3 Parameters
+
+The function takes 2 parameters:
+_h: Database connection handle
+_t: Table name
+
+2.9.4 Return Value
+
+The function returns TRUE if everything is OK, otherwise the function returns
+FALSE.
+

+ 12 - 0
db/example/Makefile

@@ -0,0 +1,12 @@
+# $Id$
+#
+# database example module makefile
+#
+# 
+# WARNING: do not run this directly, it should be run by the master Makefile
+
+auto_gen=
+NAME=dbexample.so
+LIBS=
+
+include ../../Makefile.modules

+ 5 - 0
db/example/README

@@ -0,0 +1,5 @@
+This is very simple example module that shows, how to
+use database interface.
+
+If you want to compile this module, move it to modules
+directory.

+ 228 - 0
db/example/dbexample.c

@@ -0,0 +1,228 @@
+#include "../../sr_module.h"
+#include <stdio.h>
+#include "../../db/db.h"
+
+
+#define DB_URL   "sql://root@localhost/ser"
+#define DB_TABLE "location"
+
+#define TRUE 1
+#define FALSE 0
+
+
+/*
+ * Dabase module client example
+ */
+
+
+static struct module_exports dbex_exports= {	
+	"DBExample", 
+	(char*[]) {
+	},
+	(cmd_function[]) {
+	},
+	(int[]) {
+	},
+	(fixup_function[]) {
+	},
+	0,       /* number of functions*/
+	NULL,    /* Module parameter names */
+	NULL,    /* Module parameter types */
+	NULL,    /* Module parameter variable pointers */
+	0,       /* Number of module parameters */
+	0,       /* response function*/
+	0        /* destroy function */
+};
+
+
+static int print_res(db_res_t* _r)
+{
+	int i, j;
+
+	for(i = 0; i < RES_COL_N(_r); i++) {
+		printf("%s ", RES_NAMES(_r)[i]);
+	}
+	printf("\n");
+
+	for(i = 0; i < RES_ROW_N(_r); i++) {
+		for(j = 0; j < RES_COL_N(_r); j++) {
+			if (RES_ROWS(_r)[i].values[j].nul == TRUE) {
+				printf("NULL ");
+				continue;
+			}
+			switch(RES_ROWS(_r)[i].values[j].type) {
+			case DB_INT:
+				printf("%d ", RES_ROWS(_r)[i].values[j].val.int_val);
+				break;
+			case DB_DOUBLE:
+				printf("%f ", RES_ROWS(_r)[i].values[j].val.double_val);
+				break;
+			case DB_DATETIME:
+				printf("%s ", ctime(&(RES_ROWS(_r)[i].values[j].val.time_val)));
+				break;
+			case DB_STRING:
+				printf("%s ", RES_ROWS(_r)[i].values[j].val.string_val);
+			}
+			
+		}
+		printf("\n");
+	}
+			
+	return TRUE;
+}
+
+
+
+
+
+struct module_exports* mod_register()
+{
+	     /*
+	      * Column names of table location
+	      */
+	db_key_t keys1[] = {"user", "contact", "q", "expire" };
+	db_key_t keys2[] = {"user", "q"};
+	db_key_t keys3[] = {"user", "contact"};
+	db_key_t keys4[] = {"contact", "q"};
+
+	db_val_t vals1[] = { 
+		{ DB_STRING  , 0, { .string_val = "[email protected]" }      },
+		{ DB_STRING  , 0, { .string_val = "[email protected]" } },
+		{ DB_DOUBLE  , 0, { .double_val = 1.2 }                 },
+		{ DB_DATETIME, 0, { .time_val = 439826493 }            }
+	};
+
+	db_val_t vals2[] = { 
+		{ DB_STRING  , 0, { .string_val = "[email protected]" }      },
+		{ DB_STRING  , 0, { .string_val = "[email protected]" } },
+		{ DB_DOUBLE  , 0, { .double_val = 1.3 }                   },
+		{ DB_DATETIME, 0, { .time_val = 12345 }                  }
+	};
+
+	db_val_t vals3[] = { 
+		{ DB_STRING  , 0, { .string_val = "[email protected]" }      },
+		{ DB_STRING  , 0, { .string_val = "[email protected]" } },
+		{ DB_DOUBLE  , 0, { .double_val = 1.5 }                  },
+		{ DB_DATETIME, 0, { .time_val = 123456 }                 }
+	};
+
+	db_val_t vals4[] = {
+		{ DB_STRING, 0, { .string_val = "[email protected]" } },
+		{ DB_DOUBLE, 0, { .double_val = 1.30 }            }
+	};
+		  
+	db_val_t vals5[] = {
+		{ DB_STRING, 0, { .string_val = "[email protected]" }      },
+		{ DB_STRING, 0, { .string_val = "[email protected]" } }
+	};
+
+	db_val_t vals6[] = {
+		{ DB_STRING, 0, { .string_val = "[email protected]" } },
+		{ DB_DOUBLE, 0, { .double_val = 2.5 }                     }
+	};
+
+	db_con_t* h;
+	db_res_t* res;
+
+	fprintf(stderr, "DBExample - registering...\n");
+
+	     /* The first call must be bind_dbmod
+	      * This call will search for functions
+	      * exported by a database module
+	      */
+	if (bind_dbmod()) {
+		fprintf(stderr, "Error while binding database module, did you forget to load a database module ?\n");
+		return &dbex_exports;
+	}
+
+	     /*
+	      * Create a database connection
+	      * DB_URL is database URL of form
+	      * sql://user:password@host:port/database
+	      * The function returns handle, that
+	      * represents a database connection
+	      */
+	h = db_init(DB_URL);
+	if (!h) {
+		fprintf(stderr, "Error while initializing database connection\n");
+		return &dbex_exports;
+	}
+
+	     /* 
+	      * Specify a table name, that will
+	      * be used for manipulations
+	      */
+	if (db_use_table(h, DB_TABLE) == FALSE) {
+		fprintf(stderr, "Error while calling db_use_table\n");
+		return &dbex_exports;
+	}
+
+	     /* If you do not specify any keys and values to be
+	      * matched, all rows will be deleted
+	      */
+	if (db_delete(h, NULL, NULL, 0) == FALSE) {
+		fprintf(stderr, "Error while flushing table\n");
+		return &dbex_exports;
+	}
+
+	if (db_insert(h, keys1, vals1, 4) == FALSE) {
+		fprintf(stderr, "Error while inserting line 1\n");
+		return &dbex_exports;
+	}
+
+	if (db_insert(h, keys1, vals2, 4) == FALSE) {
+		fprintf(stderr, "Error while inserting line 2\n");
+		return &dbex_exports;
+	}
+
+	if (db_insert(h, keys1, vals3, 4) == FALSE) {
+		fprintf(stderr, "Error while inserting line 3\n");
+		return &dbex_exports;
+	}
+
+	     /*
+	      * Let's delete middle line with
+	      * user = [email protected] and q = 1.3
+	      */
+	if (db_delete(h, keys2, vals4, 2) == FALSE) {
+		fprintf(stderr, "Error while deleting line\n");
+		return &dbex_exports;
+	}
+
+	     /*
+	      * Modify last line
+	      */
+	if (db_update(h, keys3, vals5, keys4, vals6, 2, 2) == FALSE) {
+		fprintf(stderr, "Error while modifying table\n");
+		return &dbex_exports;
+	}
+
+	     /*
+	      * Last but not least, dump the result of db_query
+	      */
+
+	if (db_query(h, NULL, NULL, NULL, 0, 0, NULL, &res) == FALSE) {
+		fprintf(stderr, "Error while querying table\n");
+		return &dbex_exports;
+	}
+
+
+	print_res(res);
+
+	     /*
+	      * Free the result because we don't need it
+	      * anymore
+	      */
+	if (db_free_query(h, res) == FALSE) {
+		fprintf(stderr, "Error while freeing result of query\n");
+		return &dbex_exports;
+	}
+
+	     /*
+	      * Close existing database connection
+	      * and free previously allocated 
+	      * memory
+	      */
+	db_close(h);
+	return &dbex_exports;
+}

+ 1 - 0
ser.cfg

@@ -6,6 +6,7 @@ fork=no          # (cmd. line: -D)
 log_stderror=yes # (cmd line: -E)
 log_stderror=yes # (cmd line: -E)
 # for more info: sip_router -h
 # for more info: sip_router -h
 
 
+
 route{
 route{
 
 
 	if false and forward("mobile69") { log("forwarded ok\n"); break; }
 	if false and forward("mobile69") { log("forwarded ok\n"); break; }