Browse code

modules_k/*: moved k modules in directory modules/

Daniel-Constantin Mierla authored on 20/01/2013 11:57:52
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,580 +0,0 @@
1
-/*
2
- * $Id$
3
- *
4
- * SQlite module core functions
5
- *
6
- * Copyright (C) 2010 Timo Teräs
7
- *
8
- * This file is part of Kamailio, a free SIP server.
9
- *
10
- * Kamailio is free software; you can redistribute it and/or modify
11
- * it under the terms of the GNU General Public License as published by
12
- * the Free Software Foundation; either version 2 of the License, or
13
- * (at your option) any later version
14
- *
15
- * Kamailio is distributed in the hope that it will be useful,
16
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
- * GNU General Public License for more details.
19
- *
20
- * You should have received a copy of the GNU General Public License
21
- * along with this program; if not, write to the Free Software
22
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
- */
24
-
25
-#include "../../mem/mem.h"
26
-#include "../../dprint.h"
27
-#include "../../lib/srdb1/db_pool.h"
28
-#include "../../lib/srdb1/db_ut.h"
29
-#include "../../lib/srdb1/db_res.h"
30
-#include "../../lib/srdb1/db_query.h"
31
-#include "dbase.h"
32
-
33
-static time_t sqlite_to_timet(double rT)
34
-{
35
-	return 86400.0*(rT - 2440587.5) + 0.5;
36
-}
37
-
38
-static double timet_to_sqlite(time_t t)
39
-{
40
-	return ((((double) t) - 0.5) / 86400.0) + 2440587.5;
41
-}
42
-
43
-/*
44
- * Initialize database module
45
- * No function should be called before this
46
- */
47
-
48
-static struct sqlite_connection * db_sqlite_new_connection(const struct db_id* id)
49
-{
50
-	struct sqlite_connection *con;
51
-	int rc;
52
-
53
-	con = pkg_malloc(sizeof(*con));
54
-	if (!con) {
55
-		LM_ERR("failed to allocate driver connection\n");
56
-		return NULL;
57
-	}
58
-
59
-	memset(con, 0, sizeof(*con));
60
-	con->hdr.ref = 1;
61
-	con->hdr.id = (struct db_id*) id; /* set here - freed on error */
62
-
63
-	rc = sqlite3_open_v2(id->database, &con->conn,
64
-		SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
65
-	if (rc != SQLITE_OK) {
66
-		pkg_free(con);
67
-		LM_ERR("failed to open sqlite database '%s'\n", id->database);
68
-		return NULL;
69
-	}
70
-
71
-	return con;
72
-}
73
-
74
-db1_con_t* db_sqlite_init(const str* _url)
75
-{
76
-	return db_do_init(_url, (void *) db_sqlite_new_connection);
77
-}
78
-
79
-
80
-/*
81
- * Shut down database module
82
- * No function should be called after this
83
- */
84
-
85
-static void db_sqlite_free_connection(struct sqlite_connection* con)
86
-{
87
-	if (!con) return;
88
-
89
-	sqlite3_close(con->conn);
90
-	free_db_id(con->hdr.id);
91
-	pkg_free(con);
92
-}
93
-
94
-void db_sqlite_close(db1_con_t* _h)
95
-{
96
-	db_do_close(_h, db_sqlite_free_connection);
97
-}
98
-
99
-/*
100
- * Release a result set from memory
101
- */
102
-int db_sqlite_free_result(db1_con_t* _h, db1_res_t* _r)
103
-{
104
-	if (!_h || !_r) {
105
-		LM_ERR("invalid parameter value\n");
106
-		return -1;
107
-	}
108
-
109
-	if (db_free_result(_r) < 0)
110
-	{
111
-		LM_ERR("failed to free result structure\n");
112
-		return -1;
113
-	}
114
-	return 0;
115
-}
116
-
117
-/*
118
- * Store name of table that will be used by
119
- * subsequent database functions
120
- */
121
-int db_sqlite_use_table(db1_con_t* _h, const str* _t)
122
-{
123
-	return db_use_table(_h, _t);
124
-}
125
-
126
-/*
127
- * Reset query context
128
- */
129
-static void db_sqlite_cleanup_query(const db1_con_t* _c)
130
-{
131
-	struct sqlite_connection *conn = CON_SQLITE(_c);
132
-	int rc;
133
-
134
-	if (conn->stmt != NULL) {
135
-		rc = sqlite3_finalize(conn->stmt);
136
-		if (rc != SQLITE_OK)
137
-			LM_ERR("finalize failed: %s\n",
138
-				sqlite3_errmsg(conn->conn));
139
-	}
140
-
141
-	conn->stmt = NULL;
142
-	conn->bindpos = 0;
143
-}
144
-
145
-/*
146
- * Convert value to sql-string as db bind index
147
- */
148
-static int db_sqlite_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
149
-{
150
-	struct sqlite_connection *conn;
151
-	int ret;
152
-
153
-	if (!_c || !_v || !_s || !_len || *_len <= 0) {
154
-		LM_ERR("invalid parameter value\n");
155
-		return -1;
156
-	}
157
-
158
-	conn = CON_SQLITE(_c);
159
-	if (conn->bindpos >= DB_SQLITE_MAX_BINDS) {
160
-		LM_ERR("too many bindings, recompile with larger DB_SQLITE_MAX_BINDS\n");
161
-		return -2;
162
-	}
163
-
164
-	conn->bindarg[conn->bindpos] = _v;
165
-	ret = snprintf(_s, *_len, "?%u", ++conn->bindpos);
166
-	if ((unsigned)ret >= (unsigned) *_len)
167
-		return -11;
168
-
169
-	*_len = ret;
170
-	return 0;
171
-}
172
-
173
-/*
174
- * Send an SQL query to the server
175
- */
176
-static int db_sqlite_submit_query(const db1_con_t* _h, const str* _s)
177
-{
178
-	struct sqlite_connection *conn = CON_SQLITE(_h);
179
-	sqlite3_stmt *stmt;
180
-	const db_val_t *val;
181
-	int rc, i;
182
-
183
-	LM_DBG("submit_query: %.*s\n", _s->len, _s->s);
184
-
185
-	rc = sqlite3_prepare_v2(conn->conn, _s->s, _s->len, &stmt, NULL);
186
-	if (rc != SQLITE_OK) {
187
-		LM_ERR("failed to prepare statement: %s\n",
188
-			sqlite3_errmsg(conn->conn));
189
-		return -1;
190
-	}
191
-	conn->stmt = stmt;
192
-
193
-	for (i = 1; i <= conn->bindpos; i++) {
194
-		val = conn->bindarg[i-1];
195
-		if (VAL_NULL(val)) {
196
-			rc = sqlite3_bind_null(stmt, i);
197
-		} else switch (VAL_TYPE(val)) {
198
-		case DB1_INT:
199
-			rc = sqlite3_bind_int(stmt, i, VAL_INT(val));
200
-			break;
201
-		case DB1_BIGINT:
202
-			rc = sqlite3_bind_int64(stmt, i, VAL_BIGINT(val));
203
-			break;
204
-		case DB1_DOUBLE:
205
-			rc = sqlite3_bind_double(stmt, i, VAL_DOUBLE(val));
206
-			break;
207
-		case DB1_STRING:
208
-			rc = sqlite3_bind_text(stmt, i,
209
-				VAL_STRING(val), -1, NULL);
210
-			break;
211
-		case DB1_STR:
212
-			rc = sqlite3_bind_text(stmt, i,
213
-				VAL_STR(val).s, VAL_STR(val).len, NULL);
214
-			break;
215
-		case DB1_DATETIME:
216
-			rc = sqlite3_bind_double(stmt, i, timet_to_sqlite(VAL_TIME(val)));
217
-			break;
218
-		case DB1_BLOB:
219
-			rc = sqlite3_bind_blob(stmt, i,
220
-				VAL_BLOB(val).s, VAL_BLOB(val).len,
221
-				NULL);
222
-			break;
223
-		case DB1_BITMAP:
224
-			rc = sqlite3_bind_int(stmt, i, VAL_BITMAP(val));
225
-			break;
226
-		default:
227
-			LM_ERR("unknown bind value type %d\n", VAL_TYPE(val));
228
-			return -1;
229
-		}
230
-		if (rc != SQLITE_OK) {
231
-			LM_ERR("Parameter bind failed: %s\n",
232
-				sqlite3_errmsg(conn->conn));
233
-			return -1;
234
-		}
235
-	}
236
-
237
-	return 0;
238
-}
239
-
240
-#define H3(a,b,c)	((a<<16) + (b<<8) + c)
241
-#define H4(a,b,c,d)	((a<<24) + (b<<16) + (c<<8) + d)
242
-
243
-static int decltype_to_dbtype(const char *decltype)
244
-{
245
-	/* SQlite3 has dynamic typing. It does not store the actual
246
-	 * exact type, instead it uses 'affinity' depending on the
247
-	 * value. We have to go through the declaration type to see
248
-	 * what to return.
249
-	 * The loose string matching (4 letter substring match) is what
250
-	 * SQlite does internally, but our return values differ as we want
251
-	 * the more exact srdb type instead of the affinity. */
252
-
253
-	uint32_t h = 0;
254
-
255
-	for (; *decltype; decltype++) {
256
-		h <<= 8;
257
-		h += toupper(*decltype);
258
-
259
-		switch (h & 0x00ffffff) {
260
-		case H3('I','N','T'):
261
-			return DB1_INT;
262
-		}
263
-
264
-		switch (h) {
265
-		case H4('S','E','R','I'): /* SERIAL */
266
-			return DB1_INT;
267
-		case H4('B','I','G','I'): /* BIGINT */
268
-			return DB1_BIGINT;
269
-		case H4('C','H','A','R'):
270
-		case H4('C','L','O','B'):
271
-			return DB1_STRING;
272
-		case H4('T','E','X','T'):
273
-			return DB1_STR;
274
-		case H4('R','E','A','L'):
275
-		case H4('F','L','O','A'): /* FLOAT */
276
-		case H4('D','O','U','B'): /* DOUBLE */
277
-			return DB1_DOUBLE;
278
-		case H4('B','L','O','B'):
279
-			return DB1_BLOB;
280
-		case H4('T','I','M','E'):
281
-		case H4('D','A','T','E'):
282
-			return DB1_DATETIME;
283
-		}
284
-	}
285
-
286
-	LM_ERR("sqlite decltype '%s' not recognized, defaulting to int",
287
-		decltype);
288
-	return DB1_INT;
289
-}
290
-
291
-static int type_to_dbtype(int type)
292
-{
293
-	switch (type) {
294
-	case SQLITE_INTEGER:
295
-		return DB1_INT;
296
-	case SQLITE_FLOAT:
297
-		return DB1_DOUBLE;
298
-	case SQLITE_TEXT:
299
-		return DB1_STR;
300
-	case SQLITE_BLOB:
301
-		return DB1_BLOB;
302
-	default:
303
-		/* Unknown, or NULL column value. Assume this is a
304
-		 * string. */
305
-		return DB1_STR;
306
-	}
307
-}
308
-
309
-static str* str_dup(const char *_s)
310
-{
311
-	str *s;
312
-	int len = strlen(_s);
313
-
314
-	s = (str*) pkg_malloc(sizeof(str)+len+1);
315
-	if (!s)
316
-		return NULL;
317
-
318
-	s->len = len;
319
-	s->s = ((char*)s) + sizeof(str);
320
-	memcpy(s->s, _s, len);
321
-	s->s[len] = '\0';
322
-
323
-	return s;
324
-}
325
-
326
-static void str_assign(str* s, const char *_s, int len)
327
-{
328
-	s->s = (char *) pkg_malloc(len + 1);
329
-	if (s->s) {
330
-		s->len = len;
331
-		memcpy(s->s, _s, len);
332
-		s->s[len] = 0;
333
-	}
334
-}
335
-
336
-/*
337
- * Read database answer and fill the structure
338
- */
339
-int db_sqlite_store_result(const db1_con_t* _h, db1_res_t** _r)
340
-{
341
-	struct sqlite_connection *conn = CON_SQLITE(_h);
342
-	db1_res_t *res;
343
-	int i, rc, num_rows = 0, num_alloc = 0;
344
-	db_row_t *rows = NULL, *row;
345
-	db_val_t *val;
346
-
347
-	res = db_new_result();
348
-	if (res == NULL)
349
-		goto no_mem;
350
-
351
-	while (1) {
352
-		rc = sqlite3_step(conn->stmt);
353
-		if (rc == SQLITE_DONE) {
354
-			*_r = res;
355
-			return 0;
356
-		}
357
-		if (rc != SQLITE_ROW) {
358
-			LM_INFO("sqlite3_step failed: %s\n", sqlite3_errmsg(conn->conn));
359
-			goto err;
360
-		}
361
-		if (num_rows == 0) {
362
-			/* get column types */
363
-			rc = sqlite3_column_count(conn->stmt);
364
-			if (db_allocate_columns(res, rc) != 0)
365
-				goto err;
366
-			RES_COL_N(res) = rc;
367
-
368
-			for (i = 0; i < RES_COL_N(res); i++) {
369
-				const char *decltype;
370
-				int dbtype;
371
-
372
-				RES_NAMES(res)[i] = str_dup(sqlite3_column_name(conn->stmt, i));
373
-				if (RES_NAMES(res)[i] == NULL)
374
-					goto no_mem;
375
-				decltype = sqlite3_column_decltype(conn->stmt, i);
376
-				if (decltype != NULL)
377
-					dbtype = decltype_to_dbtype(decltype);
378
-				else
379
-					dbtype = type_to_dbtype(sqlite3_column_type(conn->stmt, i));
380
-				RES_TYPES(res)[i] = dbtype;
381
-			}
382
-		}
383
-		if (num_rows >= num_alloc) {
384
-			if (num_alloc)
385
-				num_alloc *= 2;
386
-			else
387
-				num_alloc = 8;
388
-			rows = pkg_realloc(rows, sizeof(db_row_t) * num_alloc);
389
-			if (rows == NULL)
390
-				goto no_mem;
391
-			RES_ROWS(res) = rows;
392
-		}
393
-
394
-		row = &RES_ROWS(res)[num_rows];
395
-		num_rows++;
396
-		RES_ROW_N(res) = num_rows;		/* rows in this result set */
397
-		RES_NUM_ROWS(res) = num_rows;		/* rows in total */
398
-
399
-		if (db_allocate_row(res, row) != 0)
400
-			goto no_mem;
401
-
402
-		for (i = 0, val = ROW_VALUES(row); i < RES_COL_N(res); i++, val++) {
403
-			VAL_TYPE(val) = RES_TYPES(res)[i];
404
-			VAL_NULL(val) = 0;
405
-			VAL_FREE(val) = 0;
406
-			if (sqlite3_column_type(conn->stmt, i) == SQLITE_NULL) {
407
-				VAL_NULL(val) = 1;
408
-			} else switch (VAL_TYPE(val)) {
409
-			case DB1_INT:
410
-				VAL_INT(val) = sqlite3_column_int(conn->stmt, i);
411
-				break;
412
-			case DB1_BIGINT:
413
-				VAL_BIGINT(val) = sqlite3_column_int64(conn->stmt, i);
414
-				break;
415
-			case DB1_STRING:
416
-				/* first field of struct str* is the char* so we can just
417
-				 * do whatever DB1_STR case does */
418
-			case DB1_STR:
419
-				str_assign(&VAL_STR(val),
420
-					(const char*) sqlite3_column_text(conn->stmt, i),
421
-					sqlite3_column_bytes(conn->stmt, i));
422
-				if (!VAL_STR(val).s)
423
-					goto no_mem;
424
-				VAL_FREE(val) = 1;
425
-				break;
426
-			case DB1_DOUBLE:
427
-				VAL_DOUBLE(val) = sqlite3_column_double(conn->stmt, i);
428
-				break;
429
-			case DB1_DATETIME:
430
-				VAL_TIME(val) = sqlite_to_timet(sqlite3_column_double(conn->stmt, i));
431
-				break;
432
-			case DB1_BLOB:
433
-				str_assign(&VAL_BLOB(val),
434
-					(const char*) sqlite3_column_blob(conn->stmt, i),
435
-					sqlite3_column_bytes(conn->stmt, i));
436
-				if (!VAL_STR(val).s)
437
-					goto no_mem;
438
-				VAL_FREE(val) = 1;
439
-				break;
440
-			default:
441
-				LM_ERR("unhandled db-type\n");
442
-				goto err;
443
-			}
444
-		}
445
-	}
446
-
447
-no_mem:
448
-	LM_ERR("no private memory left\n");
449
-err:
450
-	if (res)
451
-		db_free_result(res);
452
-	return -1;
453
-}
454
-
455
-/*
456
- * Query table for specified rows
457
- * _h: structure representing database connection
458
- * _k: key names
459
- * _op: operators
460
- * _v: values of the keys that must match
461
- * _c: column names to return
462
- * _n: number of key=values pairs to compare
463
- * _nc: number of columns to return
464
- * _o: order by the specified column
465
- */
466
-int db_sqlite_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
467
-		const db_val_t* _v, const db_key_t* _c, int _n, int _nc,
468
-		const db_key_t _o, db1_res_t** _r)
469
-{
470
-	int rc;
471
-
472
-	rc = db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
473
-			 db_sqlite_val2str,
474
-			 db_sqlite_submit_query,
475
-			 db_sqlite_store_result);
476
-	db_sqlite_cleanup_query(_h);
477
-
478
-	return rc;
479
-}
480
-
481
-static int db_sqlite_commit(const db1_con_t* _h)
482
-{
483
-	struct sqlite_connection *conn = CON_SQLITE(_h);
484
-	int rc;
485
-
486
-	rc = sqlite3_step(conn->stmt);
487
-	if (rc != SQLITE_DONE && rc != SQLITE_OK) {
488
-		LM_ERR("sqlite commit failed: %s\n",
489
-		       sqlite3_errmsg(conn->conn));
490
-		return -1;
491
-	}
492
-
493
-	return 0;
494
-}
495
-
496
-/*
497
- * Insert a row into specified table
498
- * _h: structure representing database connection
499
- * _k: key names
500
- * _v: values of the keys
501
- * _n: number of key=value pairs
502
- */
503
-int db_sqlite_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
504
-		int _n)
505
-{
506
-	int rc = -1;
507
-
508
-	rc = db_do_insert(_h, _k, _v, _n,
509
-			  db_sqlite_val2str,
510
-			  db_sqlite_submit_query);
511
-	if (rc == 0)
512
-		rc = db_sqlite_commit(_h);
513
-	db_sqlite_cleanup_query(_h);
514
-
515
-	return rc;
516
-}
517
-
518
-
519
-/*
520
- * Delete a row from the specified table
521
- * _h: structure representing database connection
522
- * _k: key names
523
- * _o: operators
524
- * _v: values of the keys that must match
525
- * _n: number of key=value pairs
526
- */
527
-int db_sqlite_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
528
-		const db_val_t* _v, int _n)
529
-{
530
-	int rc;
531
-
532
-	rc = db_do_delete(_h, _k, _o, _v, _n,
533
-			  db_sqlite_val2str,
534
-			  db_sqlite_submit_query);
535
-	if (rc == 0)
536
-		rc = db_sqlite_commit(_h);
537
-	db_sqlite_cleanup_query(_h);
538
-
539
-	return rc;
540
-}
541
-
542
-
543
-/*
544
- * Update some rows in the specified table
545
- * _h: structure representing database connection
546
- * _k: key names
547
- * _o: operators
548
- * _v: values of the keys that must match
549
- * _uk: updated columns
550
- * _uv: updated values of the columns
551
- * _n: number of key=value pairs
552
- * _un: number of columns to update
553
- */
554
-int db_sqlite_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
555
-		const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv,
556
-		int _n, int _un)
557
-{
558
-	int rc;
559
-
560
-	rc = db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un,
561
-			  db_sqlite_val2str,
562
-			  db_sqlite_submit_query);
563
-	if (rc == 0)
564
-		rc = db_sqlite_commit(_h);
565
-	db_sqlite_cleanup_query(_h);
566
-
567
-	return rc;
568
-}
569
-
570
-int db_sqlite_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
571
-{
572
-	int rc;
573
-
574
-	rc = db_do_raw_query(_h, _s, _r,
575
-			       db_sqlite_submit_query,
576
-			       db_sqlite_store_result);
577
-	db_sqlite_cleanup_query(_h);
578
-
579
-	return rc;
580
-}
Browse code

modules_k/db_sqlite: fix crash with computed fields in custom queries

Computed fields do not have decltype available, so guess the proper
field type based on the result type of the first row. This does not
work if the first row has null type as result, but is the best we can
do easily and fixes gives right result in most cases.

Reported-by: Pedro Antonio Vico Solano <pvsolano@amper.es>

Timo Teräs authored on 15/11/2012 14:11:41
Showing 1 changed files
... ...
@@ -288,6 +288,24 @@ static int decltype_to_dbtype(const char *decltype)
288 288
 	return DB1_INT;
289 289
 }
290 290
 
291
+static int type_to_dbtype(int type)
292
+{
293
+	switch (type) {
294
+	case SQLITE_INTEGER:
295
+		return DB1_INT;
296
+	case SQLITE_FLOAT:
297
+		return DB1_DOUBLE;
298
+	case SQLITE_TEXT:
299
+		return DB1_STR;
300
+	case SQLITE_BLOB:
301
+		return DB1_BLOB;
302
+	default:
303
+		/* Unknown, or NULL column value. Assume this is a
304
+		 * string. */
305
+		return DB1_STR;
306
+	}
307
+}
308
+
291 309
 static str* str_dup(const char *_s)
292 310
 {
293 311
 	str *s;
... ...
@@ -348,10 +366,18 @@ int db_sqlite_store_result(const db1_con_t* _h, db1_res_t** _r)
348 366
 			RES_COL_N(res) = rc;
349 367
 
350 368
 			for (i = 0; i < RES_COL_N(res); i++) {
369
+				const char *decltype;
370
+				int dbtype;
371
+
351 372
 				RES_NAMES(res)[i] = str_dup(sqlite3_column_name(conn->stmt, i));
352 373
 				if (RES_NAMES(res)[i] == NULL)
353 374
 					goto no_mem;
354
-				RES_TYPES(res)[i] = decltype_to_dbtype(sqlite3_column_decltype(conn->stmt, i));
375
+				decltype = sqlite3_column_decltype(conn->stmt, i);
376
+				if (decltype != NULL)
377
+					dbtype = decltype_to_dbtype(decltype);
378
+				else
379
+					dbtype = type_to_dbtype(sqlite3_column_type(conn->stmt, i));
380
+				RES_TYPES(res)[i] = dbtype;
355 381
 			}
356 382
 		}
357 383
 		if (num_rows >= num_alloc) {
Browse code

modules_k/db_sqlite: fix memory leak in sqlops query

Seems that most other database drivers release the database
resource only at free_result time, which I some how missed.

Since we are doing a deep copy in store_result(), we can
just release the sqlite resources immediately raw_query().

Reported-by: Pedro Antonio Vico Solano <pvsolano@amper.es>

Timo Teräs authored on 17/10/2012 06:00:14
Showing 1 changed files
... ...
@@ -543,7 +543,12 @@ int db_sqlite_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
543 543
 
544 544
 int db_sqlite_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
545 545
 {
546
-	return db_do_raw_query(_h, _s, _r,
546
+	int rc;
547
+
548
+	rc = db_do_raw_query(_h, _s, _r,
547 549
 			       db_sqlite_submit_query,
548 550
 			       db_sqlite_store_result);
551
+	db_sqlite_cleanup_query(_h);
552
+
553
+	return rc;
549 554
 }
Browse code

modules_k/db_sqlite: make decltype matching case-insensitive

Search for keywords case insensitively instead of using strstr().
SQlite stores decltype as-is, and does the affinity type testing
case insensitively too.

Timo Teräs authored on 22/05/2012 10:27:53
Showing 1 changed files
... ...
@@ -237,31 +237,54 @@ static int db_sqlite_submit_query(const db1_con_t* _h, const str* _s)
237 237
 	return 0;
238 238
 }
239 239
 
240
+#define H3(a,b,c)	((a<<16) + (b<<8) + c)
241
+#define H4(a,b,c,d)	((a<<24) + (b<<16) + (c<<8) + d)
242
+
240 243
 static int decltype_to_dbtype(const char *decltype)
241 244
 {
242 245
 	/* SQlite3 has dynamic typing. It does not store the actual
243 246
 	 * exact type, instead it uses 'affinity' depending on the
244
-	 * value. We have to go through the declaration types to see
245
-	 * what to return. */
246
-	if (strstr(decltype, "INT") != NULL ||
247
-	    strncasecmp(decltype, "SERIAL", 6) == 0)
248
-		return DB1_INT;
249
-	if (strstr(decltype, "CHAR") != NULL)
250
-		return DB1_STRING;
251
-	if (strstr(decltype, "TEXT") != NULL)
252
-		return DB1_STR;
253
-	if (strstr(decltype, "REAL") != NULL ||
254
-	    strstr(decltype, "FLOA") != NULL ||
255
-	    strstr(decltype, "DOUB") != NULL)
256
-		return DB1_DOUBLE;
257
-	if (strstr(decltype, "BLOB") != NULL)
258
-		return DB1_BLOB;
259
-	if (strncasecmp(decltype, "TIME", 4) == 0 ||
260
-	    strncasecmp(decltype, "DATE", 4) == 0)
261
-		return DB1_DATETIME;
247
+	 * value. We have to go through the declaration type to see
248
+	 * what to return.
249
+	 * The loose string matching (4 letter substring match) is what
250
+	 * SQlite does internally, but our return values differ as we want
251
+	 * the more exact srdb type instead of the affinity. */
252
+
253
+	uint32_t h = 0;
254
+
255
+	for (; *decltype; decltype++) {
256
+		h <<= 8;
257
+		h += toupper(*decltype);
258
+
259
+		switch (h & 0x00ffffff) {
260
+		case H3('I','N','T'):
261
+			return DB1_INT;
262
+		}
263
+
264
+		switch (h) {
265
+		case H4('S','E','R','I'): /* SERIAL */
266
+			return DB1_INT;
267
+		case H4('B','I','G','I'): /* BIGINT */
268
+			return DB1_BIGINT;
269
+		case H4('C','H','A','R'):
270
+		case H4('C','L','O','B'):
271
+			return DB1_STRING;
272
+		case H4('T','E','X','T'):
273
+			return DB1_STR;
274
+		case H4('R','E','A','L'):
275
+		case H4('F','L','O','A'): /* FLOAT */
276
+		case H4('D','O','U','B'): /* DOUBLE */
277
+			return DB1_DOUBLE;
278
+		case H4('B','L','O','B'):
279
+			return DB1_BLOB;
280
+		case H4('T','I','M','E'):
281
+		case H4('D','A','T','E'):
282
+			return DB1_DATETIME;
283
+		}
284
+	}
262 285
 
263 286
 	LM_ERR("sqlite decltype '%s' not recognized, defaulting to int",
264
-	       decltype);
287
+		decltype);
265 288
 	return DB1_INT;
266 289
 }
267 290
 
Browse code

modules/db_mysql, modules_k/db_oracle, modules_k/db_sqlite, modules_k/db_unixodbc: Reverted changes for non-pooled connections as no longer needed

- The original db_do_init() function is available so these modules
don't need to be changed.

Peter Dunkley authored on 23/04/2012 11:38:50
Showing 1 changed files
... ...
@@ -73,7 +73,7 @@ static struct sqlite_connection * db_sqlite_new_connection(const struct db_id* i
73 73
 
74 74
 db1_con_t* db_sqlite_init(const str* _url)
75 75
 {
76
-	return db_do_init(_url, (void *) db_sqlite_new_connection, 0);
76
+	return db_do_init(_url, (void *) db_sqlite_new_connection);
77 77
 }
78 78
 
79 79
 
Browse code

modules/db_mysql, modules_k/db_oracle, modules_k/db_sqlite, modules_k/db_unixodbc: Updated to work with latest lib/srdb1

- db_do_init() in lib/srdb1 has an extra parameter to allow a unique
(non-pooled) connection to be created.
- These DB modules do not yet support non-pooled connections so the
init functions in the modules just call db_do_init() with the new
parameter set to 0.

Peter Dunkley authored on 20/04/2012 16:20:17
Showing 1 changed files
... ...
@@ -73,7 +73,7 @@ static struct sqlite_connection * db_sqlite_new_connection(const struct db_id* i
73 73
 
74 74
 db1_con_t* db_sqlite_init(const str* _url)
75 75
 {
76
-	return db_do_init(_url, (void *) db_sqlite_new_connection);
76
+	return db_do_init(_url, (void *) db_sqlite_new_connection, 0);
77 77
 }
78 78
 
79 79
 
Browse code

modules_k/db_sqlite: new sql backend

Implements the kamailio database API for SQLite.

Timo Teräs authored on 31/03/2011 14:49:05
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,526 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * SQlite module core functions
5
+ *
6
+ * Copyright (C) 2010 Timo Teräs
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program; if not, write to the Free Software
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+#include "../../mem/mem.h"
26
+#include "../../dprint.h"
27
+#include "../../lib/srdb1/db_pool.h"
28
+#include "../../lib/srdb1/db_ut.h"
29
+#include "../../lib/srdb1/db_res.h"
30
+#include "../../lib/srdb1/db_query.h"
31
+#include "dbase.h"
32
+
33
+static time_t sqlite_to_timet(double rT)
34
+{
35
+	return 86400.0*(rT - 2440587.5) + 0.5;
36
+}
37
+
38
+static double timet_to_sqlite(time_t t)
39
+{
40
+	return ((((double) t) - 0.5) / 86400.0) + 2440587.5;
41
+}
42
+
43
+/*
44
+ * Initialize database module
45
+ * No function should be called before this
46
+ */
47
+
48
+static struct sqlite_connection * db_sqlite_new_connection(const struct db_id* id)
49
+{
50
+	struct sqlite_connection *con;
51
+	int rc;
52
+
53
+	con = pkg_malloc(sizeof(*con));
54
+	if (!con) {
55
+		LM_ERR("failed to allocate driver connection\n");
56
+		return NULL;
57
+	}
58
+
59
+	memset(con, 0, sizeof(*con));
60
+	con->hdr.ref = 1;
61
+	con->hdr.id = (struct db_id*) id; /* set here - freed on error */
62
+
63
+	rc = sqlite3_open_v2(id->database, &con->conn,
64
+		SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
65
+	if (rc != SQLITE_OK) {
66
+		pkg_free(con);
67
+		LM_ERR("failed to open sqlite database '%s'\n", id->database);
68
+		return NULL;
69
+	}
70
+
71
+	return con;
72
+}
73
+
74
+db1_con_t* db_sqlite_init(const str* _url)
75
+{
76
+	return db_do_init(_url, (void *) db_sqlite_new_connection);
77
+}
78
+
79
+
80
+/*
81
+ * Shut down database module
82
+ * No function should be called after this
83
+ */
84
+
85
+static void db_sqlite_free_connection(struct sqlite_connection* con)
86
+{
87
+	if (!con) return;
88
+
89
+	sqlite3_close(con->conn);
90
+	free_db_id(con->hdr.id);
91
+	pkg_free(con);
92
+}
93
+
94
+void db_sqlite_close(db1_con_t* _h)
95
+{
96
+	db_do_close(_h, db_sqlite_free_connection);
97
+}
98
+
99
+/*
100
+ * Release a result set from memory
101
+ */
102
+int db_sqlite_free_result(db1_con_t* _h, db1_res_t* _r)
103
+{
104
+	if (!_h || !_r) {
105
+		LM_ERR("invalid parameter value\n");
106
+		return -1;
107
+	}
108
+
109
+	if (db_free_result(_r) < 0)
110
+	{
111
+		LM_ERR("failed to free result structure\n");
112
+		return -1;
113
+	}
114
+	return 0;
115
+}
116
+
117
+/*
118
+ * Store name of table that will be used by
119
+ * subsequent database functions
120
+ */
121
+int db_sqlite_use_table(db1_con_t* _h, const str* _t)
122
+{
123
+	return db_use_table(_h, _t);
124
+}
125
+
126
+/*
127
+ * Reset query context
128
+ */
129
+static void db_sqlite_cleanup_query(const db1_con_t* _c)
130
+{
131
+	struct sqlite_connection *conn = CON_SQLITE(_c);
132
+	int rc;
133
+
134
+	if (conn->stmt != NULL) {
135
+		rc = sqlite3_finalize(conn->stmt);
136
+		if (rc != SQLITE_OK)
137
+			LM_ERR("finalize failed: %s\n",
138
+				sqlite3_errmsg(conn->conn));
139
+	}
140
+
141
+	conn->stmt = NULL;
142
+	conn->bindpos = 0;
143
+}
144
+
145
+/*
146
+ * Convert value to sql-string as db bind index
147
+ */
148
+static int db_sqlite_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
149
+{
150
+	struct sqlite_connection *conn;
151
+	int ret;
152
+
153
+	if (!_c || !_v || !_s || !_len || *_len <= 0) {
154
+		LM_ERR("invalid parameter value\n");
155
+		return -1;
156
+	}
157
+
158
+	conn = CON_SQLITE(_c);
159
+	if (conn->bindpos >= DB_SQLITE_MAX_BINDS) {
160
+		LM_ERR("too many bindings, recompile with larger DB_SQLITE_MAX_BINDS\n");
161
+		return -2;
162
+	}
163
+
164
+	conn->bindarg[conn->bindpos] = _v;
165
+	ret = snprintf(_s, *_len, "?%u", ++conn->bindpos);
166
+	if ((unsigned)ret >= (unsigned) *_len)
167