Browse code

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 authored on 28/11/2002 16:53:03
Showing 4 changed files
... ...
@@ -46,6 +46,9 @@ int bind_dbmod(void)
46 46
 	db_query = (db_query_f)find_export("~db_query", 2);
47 47
 	if (db_query == 0) return -1;
48 48
 
49
+	db_raw_query = (db_raw_query_f)find_export("~db_raw_query", 2);
50
+	if (db_raw_query == 0) return -1;
51
+
49 52
 	db_free_query = (db_free_query_f)find_export("~db_free_query", 2);
50 53
 	if (db_free_query == 0) return -1;
51 54
 
... ...
@@ -30,6 +30,7 @@
30 30
 #define DB_H
31 31
 
32 32
 #include "db_key.h"
33
+#include "db_op.h"
33 34
 #include "db_val.h"
34 35
 #include "db_con.h"
35 36
 #include "db_row.h"
... ...
@@ -61,6 +62,7 @@ typedef void (*db_close_f) (db_con_t* _h);
61 62
  * Query table for specified rows
62 63
  * _h: structure representing database connection
63 64
  * _k: key names
65
+ * _op: conditions
64 66
  * _v: values of the keys that must match
65 67
  * _c: column names to return
66 68
  * _n: nmber of key=values pairs to compare
... ...
@@ -70,11 +72,17 @@ typedef void (*db_close_f) (db_con_t* _h);
70 72
  *     NULL if there is no result
71 73
  */
72 74
 typedef int (*db_query_f) (db_con_t* _h, db_key_t* _k, 
73
-			   db_val_t* _v, db_key_t* _c, 
74
-			   int _n, int _nc,
75
+			   db_op_t* _op, db_val_t* _v, 
76
+			   db_key_t* _c, int _n, int _nc,
75 77
 			   db_key_t _o, db_res_t** _r);
76 78
 
77 79
 
80
+/*
81
+ * Raw SQL query, database specific !
82
+ */
83
+typedef int (*db_raw_query_f) (db_con_t* _h, char* _s, db_res_t** _r);
84
+
85
+
78 86
 /*
79 87
  * Free a result allocated by db_query
80 88
  * _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);
97 105
  * Delete a row from the specified table
98 106
  * _h: structure representing database connection
99 107
  * _k: key names
108
+ * _o: operators
100 109
  * _v: values of the keys that must match
101 110
  * _n: number of key=value pairs
102 111
  */
103
-typedef int (*db_delete_f) (db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n);
112
+typedef int (*db_delete_f) (db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n);
104 113
 
105 114
 
106 115
 /*
107 116
  * Update some rows in the specified table
108 117
  * _h: structure representing database connection
109 118
  * _k: key names
119
+ * _o: operators
110 120
  * _v: values of the keys that must match
111 121
  * _uk: updated columns
112 122
  * _uv: updated values of the columns
113 123
  * _n: number of key=value pairs
114 124
  * _un: number of columns to update
115 125
  */
116
-typedef int (*db_update_f) (db_con_t* _h, db_key_t* _k, db_val_t* _v,
126
+typedef int (*db_update_f) (db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
117 127
 			    db_key_t* _uk, db_val_t* _uv, int _n, int _un);
118 128
 
119 129
 
... ...
@@ -123,6 +133,7 @@ typedef struct db_func{
123 133
 	db_init_f       init;        /* Initialize dabase connection */
124 134
 	db_close_f      close;       /* Close database connection */
125 135
 	db_query_f      query;       /* query a table */
136
+	db_raw_query_f  raw_query;   /* Raw query - SQL */
126 137
 	db_free_query_f free_query;  /* Free a query result */
127 138
 	db_insert_f     insert;      /* Insert into table */
128 139
 	db_delete_f     delete;      /* Delete from table */ 
... ...
@@ -143,6 +154,7 @@ extern db_func_t dbf;
143 154
 #define db_init       (dbf.init)
144 155
 #define db_close      (dbf.close)
145 156
 #define db_query      (dbf.query)
157
+#define db_raw_query  (dbf.raw_query)
146 158
 #define db_free_query (dbf.free_query)
147 159
 #define db_insert     (dbf.insert)
148 160
 #define db_delete     (dbf.delete)
... ...
@@ -54,7 +54,7 @@ This type represents a database key. Every time you need to specify a key
54 54
 value, this type should be used. In fact, this type is identical to const 
55 55
 char*.
56 56
 
57
-1.2.2 Definion
57
+1.2.2 Definition
58 58
    
59 59
    typedef const char* db_key_t;
60 60
 
... ...
@@ -286,6 +286,21 @@ Use this macro if you need to obtain the number of rows in the result
286 286
 Example: int n = RES_ROW_N(res);
287 287
 
288 288
 
289
+1.7 Type db_op_t
290
+
291
+1.7.1 Description
292
+
293
+This type represents an expression operator. In fact, this type is 
294
+identical to const char*.
295
+
296
+1.7.2 Definition
297
+   
298
+   typedef const char* db_op_t;
299
+
300
+1.7.3 Macros
301
+
302
+There are no macros (It is not needed).
303
+
289 304
 
290 305
 2 Functions
291 306
 
... ...
@@ -203,7 +203,7 @@ struct module_exports* mod_register()
203 203
 	     /* If you do not specify any keys and values to be
204 204
 	      * matched, all rows will be deleted
205 205
 	      */
206
-	if (db_delete(h, NULL, NULL, 0) < 0) {
206
+	if (db_delete(h, NULL, NULL, NULL, 0) < 0) {
207 207
 		fprintf(stderr, "Error while flushing table\n");
208 208
 		return &dbex_exports;
209 209
 	}
... ...
@@ -227,7 +227,7 @@ struct module_exports* mod_register()
227 227
 	      * Let's delete middle line with
228 228
 	      * user = foo2@bar2.com and q = 1.3
229 229
 	      */
230
-	if (db_delete(h, keys2, vals4, 2) < 0) {
230
+	if (db_delete(h, keys2, NULL, vals4, 2) < 0) {
231 231
 		fprintf(stderr, "Error while deleting line\n");
232 232
 		return &dbex_exports;
233 233
 	}
... ...
@@ -235,7 +235,7 @@ struct module_exports* mod_register()
235 235
 	     /*
236 236
 	      * Modify last line
237 237
 	      */
238
-	if (db_update(h, keys3, vals5, keys4, vals6, 2, 2) < 0) {
238
+	if (db_update(h, keys3, NULL, vals5, keys4, vals6, 2, 2) < 0) {
239 239
 		fprintf(stderr, "Error while modifying table\n");
240 240
 		return &dbex_exports;
241 241
 	}
... ...
@@ -244,7 +244,7 @@ struct module_exports* mod_register()
244 244
 	      * Last but not least, dump the result of db_query
245 245
 	      */
246 246
 
247
-	if (db_query(h, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
247
+	if (db_query(h, NULL, NULL, NULL, NULL, 0, 0, NULL, &res) < 0) {
248 248
 		fprintf(stderr, "Error while querying table\n");
249 249
 		return &dbex_exports;
250 250
 	}