1 /*
2 * DBText module core functions
3 *
4 * Copyright (C) 2001-2003 FhG Fokus
5 *
6 * This file is part of Kamailio, a free SIP server.
7 *
8 * Kamailio is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version
12 *
13 * Kamailio is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _GNU_SOURCE
24 #include <string.h>
25
26 #include "../../core/str.h"
27 #include "../../core/mem/mem.h"
28 #include "../../core/mem/shm_mem.h"
29
30 #include "db_text.h"
31 #include "dbt_res.h"
32 #include "dbt_api.h"
33 #include "dbt_raw_util.h"
34
35
dbt_raw_query_select(db1_con_t * _h,str * _s,db1_res_t ** _r)36 int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
37 {
38 int res = -1;
39 int i, len;
40 char* table_ptr = NULL;
41 char* fields_end_ptr = NULL;
42 char* fields_ptr = NULL;
43 char* where_ptr = NULL;
44 char* order_start_ptr = NULL;
45 char** tokens = NULL;
46 str table;
47 dbt_table_p _tbc = NULL;
48 int cols;
49 int n = 0;
50 int ncols = 0;
51 int nc = 0;
52 db_key_t *result_cols = NULL;
53 db_key_t* _k = NULL;
54 db_op_t* _op = NULL;
55 db_val_t* _v = NULL;
56 str order;
57 db_key_t k_order = NULL;
58
59 LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
60
61 fields_end_ptr = strcasestr(_s->s, " from ");
62 if(fields_end_ptr == NULL)
63 return res;
64
65 len = fields_end_ptr - (_s->s + 6) + 1;
66 fields_ptr = pkg_malloc(len+1);
67 memset(fields_ptr, 0, len+1);
68 strncpy(fields_ptr, _s->s + 6, len);
69 dbt_trim(fields_ptr);
70
71 order_start_ptr = strcasestr(_s->s, " order by ");
72 if(order_start_ptr != NULL) {
73 *order_start_ptr = '\0';
74 order_start_ptr += 10;
75 }
76
77
78 where_ptr = strcasestr(_s->s, " where ");
79 if(where_ptr == NULL) {
80 len = strlen(fields_end_ptr + 6);
81 } else {
82 len = where_ptr - (fields_end_ptr + 6);
83 nc = dbt_build_where(where_ptr + 7, &_k, &_op, &_v);
84 }
85
86 table_ptr = pkg_malloc(len+1);
87 memset(table_ptr, 0, len+1);
88 strncpy(table_ptr, fields_end_ptr + 6, len);
89 dbt_trim(table_ptr);
90
91 table.s = table_ptr;
92 table.len = strlen(table_ptr);
93 LM_DBG("using table '%.*s'\n", table.len, table.s);
94
95 if(dbt_use_table(_h, &table) != 0) {
96 LM_ERR("use table is invalid %.*s\n", table.len, table.s);
97 goto error;
98 }
99
100 _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
101 if(!_tbc)
102 {
103 LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
104 goto error;
105 }
106
107 tokens = dbt_str_split(fields_ptr, ',', &ncols);
108 pkg_free(fields_ptr);
109 fields_ptr = NULL;
110 if (!tokens) {
111 LM_ERR("error extracting tokens\n");
112 goto error;
113 }
114
115 if(ncols == 1 && strncmp(*tokens, "*", 1) == 0) {
116 cols = _tbc->nrcols;
117 result_cols = pkg_malloc(sizeof(db_key_t) * cols);
118 if(result_cols == NULL) {
119 LM_ERR("no more memory allocating");
120 goto error;
121 }
122 memset(result_cols, 0, sizeof(db_key_t) * cols);
123 for(n=0; n < cols; n++) {
124 result_cols[n] = pkg_malloc(sizeof(str));
125 if(result_cols[n] == NULL) {
126 LM_ERR("no more memory allocating");
127 goto error;
128 }
129 result_cols[n]->len = _tbc->colv[n]->name.len;
130 result_cols[n]->s = pkg_malloc((_tbc->colv[n]->name.len + 1) * sizeof(char));
131 if(result_cols[n]->s == NULL) {
132 LM_ERR("no more memory allocating");
133 goto error;
134 }
135 strncpy(result_cols[n]->s, _tbc->colv[n]->name.s, _tbc->colv[n]->name.len);
136 result_cols[n]->s[_tbc->colv[n]->name.len] = '\0';
137 }
138 } else {
139 cols = ncols;
140 result_cols = pkg_malloc(sizeof(db_key_t) * cols);
141 if(result_cols == NULL) {
142 LM_ERR("no more memory allocating");
143 goto error;
144 }
145 memset(result_cols, 0, sizeof(db_key_t) * cols);
146 for(n=0; *(tokens + n); n++) {
147 result_cols[n] = pkg_malloc(sizeof(str));
148 if(result_cols[n] == NULL) {
149 LM_ERR("no more memory allocating");
150 goto error;
151 }
152 result_cols[n]->len = strlen(*(tokens + n));
153 result_cols[n]->s = pkg_malloc((strlen(*(tokens + n)) + 1) * sizeof(char));
154 if(result_cols[n]->s == NULL) {
155 LM_ERR("no more memory allocating");
156 goto error;
157 }
158 strncpy(result_cols[n]->s, *(tokens + n), strlen(*(tokens + n)));
159 result_cols[n]->s[strlen(*(tokens + n))] = '\0';
160 }
161 }
162
163
164 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
165 _tbc = NULL;
166 if(order_start_ptr != NULL) {
167 order.s = order_start_ptr;
168 order.len = strlen(order_start_ptr);
169 k_order = ℴ
170 }
171 res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, k_order, _r);
172
173 error:
174
175 if(_tbc)
176 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
177
178 if(tokens) {
179 for (i = 0; *(tokens + i); i++) {
180 pkg_free(*(tokens + i));
181 }
182 pkg_free(tokens);
183 }
184 if(fields_ptr)
185 pkg_free(fields_ptr);
186
187 if(table_ptr)
188 pkg_free(table_ptr);
189
190 dbt_clean_where(nc, _k, _op, _v);
191
192 if(result_cols) {
193 for(n=0; n < cols; n++) {
194 if(result_cols[n]->s)
195 pkg_free(result_cols[n]->s);
196 if(result_cols[n])
197 pkg_free(result_cols[n]);
198 }
199 pkg_free(result_cols);
200 }
201
202 return res;
203 }
204
dbt_raw_query_update(db1_con_t * _h,str * _s,db1_res_t ** _r)205 int dbt_raw_query_update(db1_con_t* _h, str* _s, db1_res_t** _r)
206 {
207 int res = -1;
208 int len;
209 char* table_ptr = NULL;
210 char* fields_end_ptr = NULL;
211 char* fields_start_ptr = NULL;
212 char* fields_ptr = NULL;
213 char* where_ptr = NULL;
214 char* table_start_ptr = NULL;
215 str table;
216 dbt_table_p _tbc = NULL;
217 int ncols = 0;
218 int nkeys = 0;
219 db_key_t* _k = NULL;
220 db_op_t* _op1 = NULL;
221 db_val_t* _kv = NULL;
222
223 db_key_t* _c = NULL;
224 db_op_t* _op2 = NULL;
225 db_val_t* _cv = NULL;
226
227 LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
228
229 table_start_ptr = _s->s + 6;
230 fields_start_ptr = strcasestr(_s->s, " set ");
231 if(fields_start_ptr == NULL)
232 return res;
233
234 len = fields_start_ptr - table_start_ptr;
235 table_ptr = pkg_malloc(len+1);
236 memset(table_ptr, 0, len+1);
237 strncpy(table_ptr, table_start_ptr, len);
238 dbt_trim(table_ptr);
239 table.s = table_ptr;
240 table.len = strlen(table_ptr);
241
242 where_ptr = strcasestr(_s->s, " where ");
243 if(where_ptr == NULL) {
244 LM_ERR("specify where clause to determine keys\n");
245 goto error;
246 }
247
248 fields_end_ptr = where_ptr;
249 len = fields_end_ptr - ( fields_start_ptr + 4) + 1;
250 fields_ptr = pkg_malloc(len+1);
251 memset(fields_ptr, 0, len+1);
252 strncpy(fields_ptr, fields_start_ptr + 4, len);
253 dbt_trim(fields_ptr);
254
255 ncols = dbt_build_where(fields_ptr, &_c, &_op2, &_cv);
256 if(ncols <0) {
257 LM_ERR("unexpected error buuilding fields\n");
258 goto error;
259 }
260
261 nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
262 if(nkeys < 1) {
263 LM_ERR("needsa at least one key\n");
264 goto error;
265 }
266
267
268 LM_DBG("using table '%.*s'\n", table.len, table.s);
269
270 if(dbt_use_table(_h, &table) != 0) {
271 LM_ERR("use table is invalid %.*s\n", table.len, table.s);
272 goto error;
273 }
274
275 _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
276 if(!_tbc)
277 {
278 LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
279 goto error;
280 }
281
282 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
283 _tbc = NULL;
284 res = dbt_update(_h, _k, _op1, _kv, _c, _cv, nkeys, ncols);
285
286 error:
287
288 if(_tbc)
289 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
290
291 if(fields_ptr)
292 pkg_free(fields_ptr);
293
294 if(table_ptr)
295 pkg_free(table_ptr);
296
297 dbt_clean_where(nkeys, _k, _op1, _kv);
298 dbt_clean_where(ncols, _c, _op2, _cv);
299
300 return res;
301
302 }
303
dbt_raw_query_delete(db1_con_t * _h,str * _s,db1_res_t ** _r)304 int dbt_raw_query_delete(db1_con_t* _h, str* _s, db1_res_t** _r)
305 {
306 int res = -1;
307 int len;
308 char* table_ptr = NULL;
309 char* fields_end_ptr = NULL;
310 char* fields_ptr = NULL;
311 char* where_ptr = NULL;
312 str table;
313 dbt_table_p _tbc = NULL;
314 int nkeys = 0;
315 db_key_t* _k = NULL;
316 db_op_t* _op1 = NULL;
317 db_val_t* _kv = NULL;
318
319 LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
320
321 fields_end_ptr = strcasestr(_s->s, " from ");
322 if(fields_end_ptr == NULL)
323 return res;
324
325 where_ptr = strcasestr(_s->s, " where ");
326 if(where_ptr == NULL) {
327 len = strlen(fields_end_ptr + 6);
328 } else {
329 len = where_ptr - (fields_end_ptr + 6);
330 nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
331 }
332
333 table_ptr = pkg_malloc(len+1);
334 memset(table_ptr, 0, len+1);
335 strncpy(table_ptr, fields_end_ptr + 6, len);
336 dbt_trim(table_ptr);
337
338 table.s = table_ptr;
339 table.len = strlen(table_ptr);
340 LM_DBG("using table '%.*s'\n", table.len, table.s);
341
342 if(dbt_use_table(_h, &table) != 0) {
343 LM_ERR("use table is invalid %.*s\n", table.len, table.s);
344 goto error;
345 }
346
347 _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
348 if(!_tbc)
349 {
350 LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
351 goto error;
352 }
353
354 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
355 _tbc = NULL;
356 res = dbt_delete(_h, _k, _op1, _kv, nkeys);
357
358 error:
359
360 if(_tbc)
361 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
362
363 if(fields_ptr)
364 pkg_free(fields_ptr);
365
366 if(table_ptr)
367 pkg_free(table_ptr);
368
369 dbt_clean_where(nkeys, _k, _op1, _kv);
370
371 return res;
372 }
373
dbt_raw_query_insert(db1_con_t * _h,str * _s,db1_res_t ** _r)374 int dbt_raw_query_insert(db1_con_t* _h, str* _s, db1_res_t** _r)
375 {
376 int res = -1;
377
378
379
380 return res;
381 }
382
dbt_raw_query_replace(db1_con_t * _h,str * _s,db1_res_t ** _r)383 int dbt_raw_query_replace(db1_con_t* _h, str* _s, db1_res_t** _r)
384 {
385 int res = -1;
386 int i, len;
387 char* table_ptr = NULL;
388 char* fields_end_ptr = NULL;
389 char* fields_start_ptr = NULL;
390 char* fields_ptr = NULL;
391 char* where_ptr = NULL;
392 char* table_start_ptr = NULL;
393 str table;
394 dbt_table_p _tbc = NULL;
395 int cols;
396 int n = 0;
397 int ncols = 0;
398 int nkeys = 0;
399 db_key_t* _k = NULL;
400 db_op_t* _op1 = NULL;
401 db_val_t* _kv = NULL;
402
403 db_key_t* _c = NULL;
404 db_op_t* _op2 = NULL;
405 db_val_t* _cv = NULL;
406
407 db_key_t* _f = NULL;
408 db_val_t* _v = NULL;
409
410 LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s);
411
412 table_start_ptr = _s->s + 7;
413 fields_start_ptr = strcasestr(_s->s, " set ");
414 if(fields_start_ptr == NULL)
415 return res;
416
417 len = fields_start_ptr - table_start_ptr;
418 table_ptr = pkg_malloc(len+1);
419 memset(table_ptr, 0, len+1);
420 strncpy(table_ptr, table_start_ptr, len);
421 dbt_trim(table_ptr);
422 table.s = table_ptr;
423 table.len = strlen(table_ptr);
424
425 where_ptr = strcasestr(_s->s, " where ");
426 if(where_ptr == NULL) {
427 LM_ERR("specify where clause to determine keys\n");
428 goto error;
429 }
430
431 fields_end_ptr = where_ptr;
432 len = fields_end_ptr - ( fields_start_ptr + 4) + 1;
433 fields_ptr = pkg_malloc(len+1);
434 memset(fields_ptr, 0, len+1);
435 strncpy(fields_ptr, fields_start_ptr + 4, len);
436 dbt_trim(fields_ptr);
437
438 ncols = dbt_build_where(fields_ptr, &_c, &_op2, &_cv);
439 if(ncols <0) {
440 LM_ERR("unexpected error buuilding fields\n");
441 goto error;
442 }
443
444 nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
445 if(nkeys < 1) {
446 LM_ERR("needsa at least one key\n");
447 goto error;
448 }
449
450
451 LM_DBG("using table '%.*s'\n", table.len, table.s);
452
453 if(dbt_use_table(_h, &table) != 0) {
454 LM_ERR("use table is invalid %.*s\n", table.len, table.s);
455 goto error;
456 }
457
458 _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
459 if(!_tbc)
460 {
461 LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
462 goto error;
463 }
464
465 cols = nkeys + ncols;
466 _f = pkg_malloc(sizeof(db_key_t) * cols);
467 _v = pkg_malloc(sizeof(db_val_t) * cols);
468 memset(_f, 0, sizeof(db_key_t) * cols);
469 memset(_v, 0, sizeof(db_key_t) * cols);
470 for(n=0; n < nkeys; n++) {
471 _f[n] = _k[n];
472 _v[n] = _kv[n];
473 }
474 for(i=n; i < cols; i++) {
475 _f[i] = _c[i-n];
476 _v[i] = _cv[i-n];
477 }
478
479
480 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
481 _tbc = NULL;
482 res = dbt_replace(_h, _f, _v, cols, nkeys, 0);
483
484 error:
485
486 if(_tbc)
487 dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
488
489 if(fields_ptr)
490 pkg_free(fields_ptr);
491
492 if(table_ptr)
493 pkg_free(table_ptr);
494
495 dbt_clean_where(nkeys, _k, _op1, _kv);
496 dbt_clean_where(ncols, _c, _op2, _cv);
497
498 if(_f)
499 pkg_free(_f);
500 if(_v)
501 pkg_free(_v);
502
503 return res;
504
505 }
506
507
508
509
510
511 /*
512 * Raw SQL query -- is not the case to have this method
513 */
dbt_raw_query(db1_con_t * _h,str * _s,db1_res_t ** _r)514 int dbt_raw_query(db1_con_t* _h, str* _s, db1_res_t** _r)
515 {
516 *_r = NULL;
517 int res = -1;
518
519 if(!_h) {
520 LM_ERR("invalid connection\n");
521 return res;
522 }
523
524 if(!_s) {
525 LM_ERR("sql query is null\n");
526 return res;
527 }
528
529 if(_s->s == NULL) {
530 LM_ERR("sql query is null\n");
531 return res;
532 }
533
534 ((dbt_con_p)_h->tail)->affected = 0;
535 dbt_trim(_s->s);
536 _s->len = strlen(_s->s);
537
538 if(strncasecmp(_s->s, "select", 6) == 0) {
539 return dbt_raw_query_select(_h, _s, _r);
540 } else if(strncasecmp(_s->s, "insert", 6) == 0) {
541 return dbt_raw_query_insert(_h, _s, _r);
542 } else if(strncasecmp(_s->s, "replace", 6) == 0) {
543 return dbt_raw_query_replace(_h, _s, _r);
544 } else if(strncasecmp(_s->s, "update", 6) == 0) {
545 return dbt_raw_query_update(_h, _s, _r);
546 } else if(strncasecmp(_s->s, "delete", 6) == 0) {
547 return dbt_raw_query_delete(_h, _s, _r);
548 };
549
550 return res;
551 }
552