2
0
Эх сурвалжийг харах

db_query, db_delete and db_update now accept additional parameter - operator.
New function added - db_raw_query which accepts a raw SQL query and
executes it.

Jan Janak 23 жил өмнө
parent
commit
829a01a4c5
4 өөрчлөгдсөн 39 нэмэгдсэн , 9 устгасан
  1. 3 0
      db/db.c
  2. 16 4
      db/db.h
  3. 16 1
      db/doc/db-api.txt
  4. 4 4
      db/example/dbexample.c

+ 3 - 0
db/db.c

@@ -46,6 +46,9 @@ int bind_dbmod(void)
 	db_query = (db_query_f)find_export("~db_query", 2);
 	if (db_query == 0) return -1;
 
+	db_raw_query = (db_raw_query_f)find_export("~db_raw_query", 2);
+	if (db_raw_query == 0) return -1;
+
 	db_free_query = (db_free_query_f)find_export("~db_free_query", 2);
 	if (db_free_query == 0) return -1;
 

+ 16 - 4
db/db.h

@@ -30,6 +30,7 @@
 #define DB_H
 
 #include "db_key.h"
+#include "db_op.h"
 #include "db_val.h"
 #include "db_con.h"
 #include "db_row.h"
@@ -61,6 +62,7 @@ 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
@@ -70,11 +72,17 @@ typedef void (*db_close_f) (db_con_t* _h);
  *     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_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
@@ -97,23 +105,25 @@ 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_val_t* _v, int _n);
+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_val_t* _v,
+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);
 
 
@@ -123,6 +133,7 @@ typedef struct db_func{
 	db_init_f       init;        /* Initialize dabase 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_query_f free_query;  /* Free a query result */
 	db_insert_f     insert;      /* Insert into table */
 	db_delete_f     delete;      /* Delete from table */ 
@@ -143,6 +154,7 @@ extern db_func_t dbf;
 #define db_init       (dbf.init)
 #define db_close      (dbf.close)
 #define db_query      (dbf.query)
+#define db_raw_query  (dbf.raw_query)
 #define db_free_query (dbf.free_query)
 #define db_insert     (dbf.insert)
 #define db_delete     (dbf.delete)

+ 16 - 1
db/doc/db-api.txt

@@ -54,7 +54,7 @@ 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
+1.2.2 Definition
    
    typedef const char* db_key_t;
 
@@ -286,6 +286,21 @@ Use this macro if you need to obtain the number of rows in the result
 Example: int n = RES_ROW_N(res);
 
 
+1.7 Type db_op_t
+
+1.7.1 Description
+
+This type represents an expression operator. In fact, this type is 
+identical to const char*.
+
+1.7.2 Definition
+   
+   typedef const char* db_op_t;
+
+1.7.3 Macros
+
+There are no macros (It is not needed).
+
 
 2 Functions
 

+ 4 - 4
db/example/dbexample.c

@@ -203,7 +203,7 @@ struct module_exports* mod_register()
 	     /* If you do not specify any keys and values to be
 	      * matched, all rows will be deleted
 	      */
-	if (db_delete(h, NULL, NULL, 0) < 0) {
+	if (db_delete(h, NULL, NULL, NULL, 0) < 0) {
 		fprintf(stderr, "Error while flushing table\n");
 		return &dbex_exports;
 	}
@@ -227,7 +227,7 @@ struct module_exports* mod_register()
 	      * Let's delete middle line with
 	      * user = [email protected] and q = 1.3
 	      */
-	if (db_delete(h, keys2, vals4, 2) < 0) {
+	if (db_delete(h, keys2, NULL, vals4, 2) < 0) {
 		fprintf(stderr, "Error while deleting line\n");
 		return &dbex_exports;
 	}
@@ -235,7 +235,7 @@ struct module_exports* mod_register()
 	     /*
 	      * Modify last line
 	      */
-	if (db_update(h, keys3, vals5, keys4, vals6, 2, 2) < 0) {
+	if (db_update(h, keys3, NULL, vals5, keys4, vals6, 2, 2) < 0) {
 		fprintf(stderr, "Error while modifying table\n");
 		return &dbex_exports;
 	}
@@ -244,7 +244,7 @@ struct module_exports* mod_register()
 	      * Last but not least, dump the result of db_query
 	      */
 
-	if (db_query(h, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
+	if (db_query(h, NULL, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
 		fprintf(stderr, "Error while querying table\n");
 		return &dbex_exports;
 	}