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,844 +0,0 @@
1
-/**
2
- * $Id$
3
- *
4
- * Copyright (C) 2008 Elena-Ramona Modroiu (asipto.com)
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
- */
22
-
23
-/*! \file
24
- * \ingroup sqlops
25
- * \brief SIP-router SQL-operations :: API
26
- *
27
- * - Module: \ref sqlops
28
- */
29
-
30
-
31
-#include "../../mem/mem.h"
32
-#include "../../dprint.h"
33
-#include "../../hashes.h"
34
-#include "../../ut.h"
35
-#include "../../lib/srdb1/db_ut.h"
36
-#ifdef WITH_XAVP
37
-#include "../../xavp.h"
38
-#endif
39
-
40
-#include "sql_api.h"
41
-
42
-sql_con_t *_sql_con_root = NULL;
43
-sql_result_t *_sql_result_root = NULL;
44
-
45
-static str _sql_empty_str = {"", 0};
46
-
47
-sql_con_t* sql_get_connection(str *name)
48
-{
49
-	sql_con_t *sc;
50
-	unsigned int conid;
51
-
52
-	conid = core_case_hash(name, 0, 0);
53
-
54
-	sc = _sql_con_root;
55
-	while(sc)
56
-	{
57
-		if(conid==sc->conid && sc->name.len==name->len
58
-				&& strncmp(sc->name.s, name->s, name->len)==0)
59
-			return sc;
60
-		sc = sc->next;
61
-	}
62
-	return NULL;
63
-}
64
-
65
-int sql_init_con(str *name, str *url)
66
-{
67
-	sql_con_t *sc;
68
-	unsigned int conid;
69
-
70
-	conid = core_case_hash(name, 0, 0);
71
-
72
-	sc = _sql_con_root;
73
-	while(sc)
74
-	{
75
-		if(conid==sc->conid && sc->name.len==name->len
76
-				&& strncmp(sc->name.s, name->s, name->len)==0)
77
-		{
78
-			LM_ERR("duplicate connection name\n");
79
-			return -1;
80
-		}
81
-		sc = sc->next;
82
-	}
83
-	sc = (sql_con_t*)pkg_malloc(sizeof(sql_con_t));
84
-	if(sc==NULL)
85
-	{
86
-		LM_ERR("no pkg memory\n");
87
-		return -1;
88
-	}
89
-	memset(sc, 0, sizeof(sql_con_t));
90
-	sc->conid = conid;
91
-	sc->name = *name;
92
-	sc->db_url = *url;
93
-	sc->next = _sql_con_root;
94
-	_sql_con_root = sc;
95
-
96
-	return 0;
97
-}
98
-
99
-int pv_parse_con_name(pv_spec_p sp, str *in)
100
-{
101
-	sql_con_t *con;
102
-
103
-	if(sp==NULL || in==NULL || in->len<=0)
104
-		return -1;
105
-
106
-	con = sql_get_connection(in);
107
-	if (con==NULL) {
108
-		LM_ERR("invalid connection [%.*s]\n", in->len, in->s);
109
-		return -1;
110
-	}
111
-
112
-	sp->pvp.pvn.type = PV_NAME_INTSTR;
113
-	sp->pvp.pvn.u.isname.type = AVP_VAL_STR;
114
-	sp->pvp.pvn.u.isname.name.s = *in;
115
-	return 0;
116
-}
117
-
118
-int pv_get_sqlrows(struct sip_msg *msg,  pv_param_t *param,
119
-		pv_value_t *res)
120
-{
121
-	sql_con_t *con;
122
-	str* sc;
123
-
124
-	sc = &param->pvn.u.isname.name.s;
125
-	con = sql_get_connection(sc);
126
-	if(con==NULL)
127
-	{
128
-		LM_ERR("invalid connection [%.*s]\n", sc->len, sc->s);
129
-		return -1;
130
-	}
131
-
132
-	if (!DB_CAPABILITY(con->dbf, DB_CAP_AFFECTED_ROWS))
133
-	{
134
-		LM_ERR("con: %p database module does not have DB_CAP_AFFECTED_ROWS [%.*s]\n",
135
-		       con, sc->len, sc->s);
136
-		return -1;
137
-	}
138
-
139
-	return pv_get_sintval(msg, param, res, con->dbf.affected_rows(con->dbh));
140
-}
141
-
142
-int sql_connect(void)
143
-{
144
-	sql_con_t *sc;
145
-	sc = _sql_con_root;
146
-	while(sc)
147
-	{
148
-		if (db_bind_mod(&sc->db_url, &sc->dbf))
149
-		{
150
-			LM_DBG("database module not found for [%.*s]\n",
151
-					sc->name.len, sc->name.s);
152
-			return -1;
153
-		}
154
-		if (!DB_CAPABILITY(sc->dbf, DB_CAP_RAW_QUERY))
155
-		{
156
-			LM_ERR("database module does not have DB_CAP_ALL [%.*s]\n",
157
-					sc->name.len, sc->name.s);
158
-			return -1;
159
-		}
160
-		sc->dbh = sc->dbf.init(&sc->db_url);
161
-		if (sc->dbh==NULL)
162
-		{
163
-			LM_ERR("failed to connect to the database [%.*s]\n",
164
-					sc->name.len, sc->name.s);
165
-			return -1;
166
-		}
167
-		sc = sc->next;
168
-	}
169
-	return 0;
170
-}
171
-
172
-void sql_disconnect(void)
173
-{
174
-	sql_con_t *sc;
175
-	sc = _sql_con_root;
176
-	while(sc)
177
-	{
178
-		if (sc->dbh!=NULL)
179
-			sc->dbf.close(sc->dbh);
180
-		sc->dbh= NULL;
181
-		sc = sc->next;
182
-	}
183
-}
184
-
185
-sql_result_t* sql_get_result(str *name)
186
-{
187
-	sql_result_t *sr;
188
-	unsigned int resid;
189
-
190
-	resid = core_case_hash(name, 0, 0);
191
-
192
-	sr = _sql_result_root;
193
-	while(sr)
194
-	{
195
-		if(sr->resid==resid && sr->name.len==name->len
196
-				&& strncmp(sr->name.s, name->s, name->len)==0)
197
-			return sr;
198
-		sr = sr->next;
199
-	}
200
-	sr = (sql_result_t*)pkg_malloc(sizeof(sql_result_t));
201
-	if(sr==NULL)
202
-	{
203
-		LM_ERR("no pkg memory\n");
204
-		return NULL;
205
-	}
206
-	memset(sr, 0, sizeof(sql_result_t));
207
-	sr->name = *name;
208
-	sr->resid = resid;
209
-	sr->next = _sql_result_root;
210
-	_sql_result_root = sr;
211
-	return sr;
212
-}
213
-
214
-void sql_reset_result(sql_result_t *res)
215
-{
216
-	int i, j;
217
-	if(res->cols)
218
-	{
219
-		for(i=0; i<res->ncols; i++)
220
-			if(res->cols[i].name.s!=NULL)
221
-				pkg_free(res->cols[i].name.s);
222
-		pkg_free(res->cols);
223
-		res->cols = NULL;
224
-	}
225
-	if(res->vals)
226
-	{
227
-		for(i=0; i<res->nrows; i++)
228
-		{
229
-			for(j=0; j<res->ncols; j++)
230
-			{
231
-				if(res->vals[i][j].flags&PV_VAL_STR
232
-						&& res->vals[i][j].value.s.len>0)
233
-					pkg_free(res->vals[i][j].value.s.s);
234
-			}
235
-			pkg_free(res->vals[i]);
236
-		}
237
-		pkg_free(res->vals);
238
-		res->vals = NULL;
239
-	}
240
-	res->nrows = 0;
241
-	res->ncols = 0;
242
-}
243
-
244
-int sql_do_query(sql_con_t *con, str *query, sql_result_t *res)
245
-{
246
-	db1_res_t* db_res = NULL;
247
-	int i, j;
248
-	str sv;
249
-
250
-	if(query==NULL)
251
-	{
252
-		LM_ERR("bad parameters\n");
253
-		return -1;
254
-	}
255
-	if(con->dbf.raw_query(con->dbh, query, &db_res)!=0)
256
-	{
257
-		LM_ERR("cannot do the query\n");
258
-		return -1;
259
-	}
260
-
261
-	if(db_res==NULL || RES_ROW_N(db_res)<=0 || RES_COL_N(db_res)<=0)
262
-	{
263
-		LM_DBG("no result after query\n");
264
-		con->dbf.free_result(con->dbh, db_res);
265
-		return 2;
266
-	}
267
-	if(!res)
268
-	{
269
-		LM_DBG("no sqlresult parameter, ignoring result from query\n");
270
-		con->dbf.free_result(con->dbh, db_res);
271
-		return 3;
272
-	}
273
-
274
-	sql_reset_result(res);
275
-	res->ncols = RES_COL_N(db_res);
276
-	res->nrows = RES_ROW_N(db_res);
277
-	LM_DBG("rows [%d] cols [%d]\n", res->nrows, res->ncols);
278
-
279
-	res->cols = (sql_col_t*)pkg_malloc(res->ncols*sizeof(sql_col_t));
280
-	if(res->cols==NULL)
281
-	{
282
-		res->ncols = 0;
283
-		res->nrows = 0;
284
-		LM_ERR("no more memory\n");
285
-		return -1;
286
-	}
287
-	memset(res->cols, 0, res->ncols*sizeof(sql_col_t));
288
-	for(i=0; i<res->ncols; i++)
289
-	{
290
-		res->cols[i].name.len = (RES_NAMES(db_res)[i])->len;
291
-		res->cols[i].name.s = (char*)pkg_malloc((res->cols[i].name.len+1)
292
-				*sizeof(char));
293
-		if(res->cols[i].name.s==NULL)
294
-		{
295
-			LM_ERR("no more memory\n");
296
-			goto error;
297
-		}
298
-		memcpy(res->cols[i].name.s, RES_NAMES(db_res)[i]->s,
299
-				res->cols[i].name.len);
300
-		res->cols[i].name.s[res->cols[i].name.len]='\0';
301
-		res->cols[i].colid = core_case_hash(&res->cols[i].name, 0, 0);
302
-	}
303
-
304
-	res->vals = (sql_val_t**)pkg_malloc(res->nrows*sizeof(sql_val_t*));
305
-	if(res->vals==NULL)
306
-	{
307
-		LM_ERR("no more memory\n");
308
-		goto error;
309
-	}
310
-	memset(res->vals, 0, res->nrows*sizeof(sql_val_t*));
311
-	for(i=0; i<res->nrows; i++)
312
-	{
313
-		res->vals[i] = (sql_val_t*)pkg_malloc(res->ncols*sizeof(sql_val_t));
314
-		if(res->vals[i]==NULL)
315
-		{
316
-			LM_ERR("no more memory\n");
317
-			goto error;
318
-		}
319
-		memset(res->vals[i], 0, res->ncols*sizeof(sql_val_t));
320
-		for(j=0; j<res->ncols; j++)
321
-		{
322
-			if(RES_ROWS(db_res)[i].values[j].nul)
323
-			{
324
-				res->vals[i][j].flags = PV_VAL_NULL;
325
-				continue;
326
-			}
327
-			sv.s = NULL;
328
-			sv.len = 0;
329
-			switch(RES_ROWS(db_res)[i].values[j].type)
330
-			{
331
-				case DB1_STRING:
332
-					res->vals[i][j].flags = PV_VAL_STR;
333
-					sv.s=
334
-						(char*)RES_ROWS(db_res)[i].values[j].val.string_val;
335
-					sv.len=strlen(sv.s);
336
-				break;
337
-				case DB1_STR:
338
-					res->vals[i][j].flags = PV_VAL_STR;
339
-					sv.len=
340
-						RES_ROWS(db_res)[i].values[j].val.str_val.len;
341
-					sv.s=
342
-						(char*)RES_ROWS(db_res)[i].values[j].val.str_val.s;
343
-				break;
344
-				case DB1_BLOB:
345
-					res->vals[i][j].flags = PV_VAL_STR;
346
-					sv.len=
347
-						RES_ROWS(db_res)[i].values[j].val.blob_val.len;
348
-					sv.s=
349
-						(char*)RES_ROWS(db_res)[i].values[j].val.blob_val.s;
350
-				break;
351
-				case DB1_INT:
352
-					res->vals[i][j].flags = PV_VAL_INT;
353
-					res->vals[i][j].value.n
354
-						= (int)RES_ROWS(db_res)[i].values[j].val.int_val;
355
-				break;
356
-				case DB1_DATETIME:
357
-					res->vals[i][j].flags = PV_VAL_INT;
358
-					res->vals[i][j].value.n
359
-						= (int)RES_ROWS(db_res)[i].values[j].val.time_val;
360
-				break;
361
-				case DB1_BITMAP:
362
-					res->vals[i][j].flags = PV_VAL_INT;
363
-					res->vals[i][j].value.n
364
-						= (int)RES_ROWS(db_res)[i].values[j].val.bitmap_val;
365
-				break;
366
-				case DB1_BIGINT:
367
-					res->vals[i][j].flags = PV_VAL_STR;
368
-					res->vals[i][j].value.s.len = 21*sizeof(char);
369
-					res->vals[i][j].value.s.s
370
-						= (char*)pkg_malloc(res->vals[i][j].value.s.len);
371
-					if(res->vals[i][j].value.s.s==NULL)
372
-					{
373
-						LM_ERR("no more memory\n");
374
-						goto error;
375
-					}
376
-					db_longlong2str(RES_ROWS(db_res)[i].values[j].val.ll_val, res->vals[i][j].value.s.s, &res->vals[i][j].value.s.len);
377
-				break;
378
-				default:
379
-					res->vals[i][j].flags = PV_VAL_NULL;
380
-			}
381
-			if(res->vals[i][j].flags == PV_VAL_STR && sv.s)
382
-			{
383
-				if(sv.len==0)
384
-				{
385
-					res->vals[i][j].value.s = _sql_empty_str;
386
-					continue;
387
-				}
388
-				res->vals[i][j].value.s.s 
389
-					= (char*)pkg_malloc(sv.len*sizeof(char));
390
-				if(res->vals[i][j].value.s.s==NULL)
391
-				{
392
-					LM_ERR("no more memory\n");
393
-					goto error;
394
-				}
395
-				memcpy(res->vals[i][j].value.s.s, sv.s, sv.len);
396
-				res->vals[i][j].value.s.len = sv.len;
397
-			}
398
-		}
399
-	}
400
-
401
-	con->dbf.free_result(con->dbh, db_res);
402
-	return 1;
403
-
404
-error:
405
-	con->dbf.free_result(con->dbh, db_res);
406
-	sql_reset_result(res);
407
-	return -1;
408
-}
409
-
410
-#ifdef WITH_XAVP
411
-int sql_exec_xquery(struct sip_msg *msg, sql_con_t *con, str *query,
412
-		str *xavp)
413
-{
414
-	db1_res_t* db_res = NULL;
415
-	sr_xavp_t *row = NULL;
416
-	sr_xval_t val;
417
-	int i, j;
418
-
419
-	if(msg==NULL || query==NULL || xavp==NULL)
420
-	{
421
-		LM_ERR("bad parameters\n");
422
-		return -1;
423
-	}
424
-
425
-	if(con->dbf.raw_query(con->dbh, query, &db_res)!=0)
426
-	{
427
-		LM_ERR("cannot do the query\n");
428
-		return -1;
429
-	}
430
-
431
-	if(db_res==NULL || RES_ROW_N(db_res)<=0 || RES_COL_N(db_res)<=0)
432
-	{
433
-		LM_DBG("no result after query\n");
434
-		con->dbf.free_result(con->dbh, db_res);
435
-		return 2;
436
-	}
437
-
438
-	for(i=RES_ROW_N(db_res)-1; i>=0; i--)
439
-	{
440
-		row = NULL;
441
-		for(j=RES_COL_N(db_res)-1; j>=0; j--)
442
-		{
443
-			if(RES_ROWS(db_res)[i].values[j].nul)
444
-			{
445
-				val.type = SR_XTYPE_NULL;
446
-			} else
447
-			{
448
-				switch(RES_ROWS(db_res)[i].values[j].type)
449
-				{
450
-					case DB1_STRING:
451
-						val.type = SR_XTYPE_STR;
452
-						val.v.s.s=
453
-							(char*)RES_ROWS(db_res)[i].values[j].val.string_val;
454
-						val.v.s.len=strlen(val.v.s.s);
455
-					break;
456
-					case DB1_STR:
457
-						val.type = SR_XTYPE_STR;
458
-						val.v.s.len=
459
-							RES_ROWS(db_res)[i].values[j].val.str_val.len;
460
-						val.v.s.s=
461
-							(char*)RES_ROWS(db_res)[i].values[j].val.str_val.s;
462
-					break;
463
-					case DB1_BLOB:
464
-						val.type = SR_XTYPE_STR;
465
-						val.v.s.len=
466
-							RES_ROWS(db_res)[i].values[j].val.blob_val.len;
467
-						val.v.s.s=
468
-							(char*)RES_ROWS(db_res)[i].values[j].val.blob_val.s;
469
-					break;
470
-					case DB1_INT:
471
-						val.type = SR_XTYPE_INT;
472
-						val.v.i
473
-							= (int)RES_ROWS(db_res)[i].values[j].val.int_val;
474
-					break;
475
-					case DB1_DATETIME:
476
-						val.type = SR_XTYPE_INT;
477
-						val.v.i
478
-							= (int)RES_ROWS(db_res)[i].values[j].val.time_val;
479
-					break;
480
-					case DB1_BITMAP:
481
-						val.type = SR_XTYPE_INT;
482
-						val.v.i
483
-							= (int)RES_ROWS(db_res)[i].values[j].val.bitmap_val;
484
-					break;
485
-					case DB1_BIGINT:
486
-						val.type = SR_XTYPE_LLONG;
487
-						val.v.ll
488
-							= RES_ROWS(db_res)[i].values[j].val.ll_val;
489
-					break;
490
-					default:
491
-						val.type = SR_XTYPE_NULL;
492
-				}
493
-			}
494
-			/* Add column to current row, under the column's name */
495
-			LM_DBG("Adding column: %.*s\n", RES_NAMES(db_res)[j]->len, RES_NAMES(db_res)[j]->s);
496
-			xavp_add_value(RES_NAMES(db_res)[j], &val, &row);
497
-		}
498
-		/* Add row to result xavp */
499
-		val.type = SR_XTYPE_XAVP;
500
-		val.v.xavp = row;
501
-		LM_DBG("Adding row\n");
502
-		xavp_add_value(xavp, &val, NULL);
503
-	}
504
-
505
-	con->dbf.free_result(con->dbh, db_res);
506
-	return 1;
507
-}
508
-
509
-int sql_do_xquery(struct sip_msg *msg, sql_con_t *con, pv_elem_t *query,
510
-		pv_elem_t *res)
511
-{
512
-	str sv, xavp;
513
-	if(msg==NULL || query==NULL || res==NULL)
514
-	{
515
-		LM_ERR("bad parameters\n");
516
-		return -1;
517
-	}
518
-	if(pv_printf_s(msg, query, &sv)!=0)
519
-	{
520
-		LM_ERR("cannot print the sql query\n");
521
-		return -1;
522
-	}
523
-
524
-	if(pv_printf_s(msg, res, &xavp)!=0)
525
-	{
526
-		LM_ERR("cannot print the result parameter\n");
527
-		return -1;
528
-	}
529
-	return sql_exec_xquery(msg, con, &sv, &xavp);
530
-}
531
-
532
-#endif
533
-
534
-
535
-int sql_do_pvquery(struct sip_msg *msg, sql_con_t *con, pv_elem_t *query,
536
-		pvname_list_t *res)
537
-{
538
-	db1_res_t* db_res = NULL;
539
-	pvname_list_t* pv;
540
-	str sv;
541
-	int i, j;
542
-
543
-	if(msg==NULL || query==NULL || res==NULL)
544
-	{
545
-		LM_ERR("bad parameters\n");
546
-		return -1;
547
-	}
548
-	if(pv_printf_s(msg, query, &sv)!=0)
549
-	{
550
-		LM_ERR("cannot print the sql query\n");
551
-		return -1;
552
-	}
553
-
554
-	if(con->dbf.raw_query(con->dbh, &sv, &db_res)!=0)
555
-	{
556
-		LM_ERR("cannot do the query\n");
557
-		return -1;
558
-	}
559
-
560
-	if(db_res==NULL || RES_ROW_N(db_res)<=0 || RES_COL_N(db_res)<=0)
561
-	{
562
-		LM_DBG("no result after query\n");
563
-		con->dbf.free_result(con->dbh, db_res);
564
-		return 2;
565
-	}
566
-
567
-	for(i=RES_ROW_N(db_res)-1; i>=0; i--)
568
-	{
569
-		pv = res;
570
-		for(j=0; j<RES_COL_N(db_res); j++)
571
-		{
572
-			if (db_val2pv_spec(msg, &RES_ROWS(db_res)[0].values[j], &pv->sname) != 0) {
573
-				LM_ERR("Failed to convert value for column %.*s\n",
574
-				       RES_NAMES(db_res)[j]->len, RES_NAMES(db_res)[j]->s);
575
-				goto error;
576
-			}
577
-			pv = pv->next;
578
-		}
579
-	}
580
-
581
-	con->dbf.free_result(con->dbh, db_res);
582
-	return 1;
583
-
584
-error:
585
-	con->dbf.free_result(con->dbh, db_res);
586
-	return -1;
587
-}
588
-
589
-
590
-int sql_parse_param(char *val)
591
-{
592
-	str name;
593
-	str tok;
594
-	str in;
595
-	char *p;
596
-
597
-	/* parse: name=>db_url*/
598
-	in.s = val;
599
-	in.len = strlen(in.s);
600
-	p = in.s;
601
-
602
-	while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
603
-		p++;
604
-	if(p>in.s+in.len || *p=='\0')
605
-		goto error;
606
-	name.s = p;
607
-	while(p < in.s + in.len)
608
-	{
609
-		if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
610
-			break;
611
-		p++;
612
-	}
613
-	if(p>in.s+in.len || *p=='\0')
614
-		goto error;
615
-	name.len = p - name.s;
616
-	if(*p!='=')
617
-	{
618
-		while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
619
-			p++;
620
-		if(p>in.s+in.len || *p=='\0' || *p!='=')
621
-			goto error;
622
-	}
623
-	p++;
624
-	if(*p!='>')
625
-		goto error;
626
-	p++;
627
-	while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
628
-		p++;
629
-	tok.s = p;
630
-	tok.len = in.len + (int)(in.s - p);
631
-
632
-	LM_DBG("cname: [%.*s] url: [%.*s]\n", name.len, name.s, tok.len, tok.s);
633
-
634
-	return sql_init_con(&name, &tok);
635
-error:
636
-	LM_ERR("invalid sqlops parameter [%.*s] at [%d]\n", in.len, in.s,
637
-			(int)(p-in.s));
638
-	return -1;
639
-}
640
-
641
-void sql_destroy(void)
642
-{
643
-	sql_result_t *r;
644
-	sql_result_t *r0;
645
-	
646
-	sql_disconnect();
647
-
648
-	r=_sql_result_root;
649
-	while(r)
650
-	{
651
-		r0 = r->next;
652
-		sql_reset_result(r);
653
-		pkg_free(r);
654
-		r = r0;
655
-	}
656
-}
657
-
658
-/**
659
- *
660
- */
661
-int sqlops_do_query(str *scon, str *squery, str *sres)
662
-{
663
-	sql_con_t *con = NULL;
664
-	sql_result_t *res = NULL;
665
-
666
-	con = sql_get_connection(scon);
667
-	if(con==NULL)
668
-	{
669
-		LM_ERR("invalid connection [%.*s]\n", scon->len, scon->s);
670
-		goto error;
671
-	}
672
-	res = sql_get_result(sres);
673
-	if(res==NULL)
674
-	{
675
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
676
-		goto error;
677
-	}
678
-	if(sql_do_query(con, squery, res)<0)
679
-		goto error;
680
-
681
-	return 0;
682
-error:
683
-	return -1;
684
-}
685
-
686
-/**
687
- *
688
- */
689
-int sqlops_get_value(str *sres, int i, int j, sql_val_t **val)
690
-{
691
-	sql_result_t *res = NULL;
692
-
693
-	res = sql_get_result(sres);
694
-	if(res==NULL)
695
-	{
696
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
697
-		goto error;
698
-	}
699
-	if(i>=res->nrows)
700
-	{
701
-		LM_ERR("row index out of bounds [%d/%d]\n", i, res->nrows);
702
-		goto error;
703
-	}
704
-	if(j>=res->ncols)
705
-	{
706
-		LM_ERR("column index out of bounds [%d/%d]\n", j, res->ncols);
707
-		goto error;
708
-	}
709
-	*val = &res->vals[i][j];
710
-
711
-	return 0;
712
-error:
713
-	return -1;
714
-}
715
-
716
-/**
717
- *
718
- */
719
-int sqlops_is_null(str *sres, int i, int j)
720
-{
721
-	sql_result_t *res = NULL;
722
-
723
-	res = sql_get_result(sres);
724
-	if(res==NULL)
725
-	{
726
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
727
-		goto error;
728
-	}
729
-	if(i>=res->nrows)
730
-	{
731
-		LM_ERR("row index out of bounds [%d/%d]\n", i, res->nrows);
732
-		goto error;
733
-	}
734
-	if(i>=res->ncols)
735
-	{
736
-		LM_ERR("column index out of bounds [%d/%d]\n", j, res->ncols);
737
-		goto error;
738
-	}
739
-	if(res->vals[i][j].flags&PV_VAL_NULL)
740
-		return 1;
741
-	return 0;
742
-error:
743
-	return -1;
744
-}
745
-
746
-/**
747
- *
748
- */
749
-int sqlops_get_column(str *sres, int i, str *col)
750
-{
751
-	sql_result_t *res = NULL;
752
-
753
-	res = sql_get_result(sres);
754
-	if(res==NULL)
755
-	{
756
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
757
-		goto error;
758
-	}
759
-	if(i>=res->ncols)
760
-	{
761
-		LM_ERR("column index out of bounds [%d/%d]\n", i, res->ncols);
762
-		goto error;
763
-	}
764
-	*col = res->cols[i].name;
765
-	return 0;
766
-error:
767
-	return -1;
768
-}
769
-
770
-/**
771
- *
772
- */
773
-int sqlops_num_columns(str *sres)
774
-{
775
-	sql_result_t *res = NULL;
776
-
777
-	res = sql_get_result(sres);
778
-	if(res==NULL)
779
-	{
780
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
781
-		goto error;
782
-	}
783
-	return res->ncols;
784
-error:
785
-	return -1;
786
-}
787
-
788
-/**
789
- *
790
- */
791
-int sqlops_num_rows(str *sres)
792
-{
793
-	sql_result_t *res = NULL;
794
-
795
-	res = sql_get_result(sres);
796
-	if(res==NULL)
797
-	{
798
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
799
-		goto error;
800
-	}
801
-	return res->nrows;
802
-error:
803
-	return -1;
804
-}
805
-
806
-/**
807
- *
808
- */
809
-void sqlops_reset_result(str *sres)
810
-{
811
-	sql_result_t *res = NULL;
812
-
813
-	res = sql_get_result(sres);
814
-	if(res==NULL)
815
-	{
816
-		LM_ERR("invalid result [%.*s]\n", sres->len, sres->s);
817
-		return;
818
-	}
819
-	sql_reset_result(res);
820
-
821
-	return;
822
-}
823
-
824
-/**
825
- *
826
- */
827
-int sqlops_do_xquery(sip_msg_t *msg, str *scon, str *squery, str *xavp)
828
-{
829
-	sql_con_t *con = NULL;
830
-
831
-	con = sql_get_connection(scon);
832
-	if(con==NULL)
833
-	{
834
-		LM_ERR("invalid connection [%.*s]\n", scon->len, scon->s);
835
-		goto error;
836
-	}
837
-	if(sql_exec_xquery(msg, con, squery, xavp)<0)
838
-		goto error;
839
-
840
-	return 0;
841
-error:
842
-	return -1;
843
-}
844
-
Browse code

sqlops: remove unused variable `sv'

Ovidiu Sas authored on 11/01/2013 20:20:11
Showing 1 changed files
... ...
@@ -415,7 +415,6 @@ int sql_exec_xquery(struct sip_msg *msg, sql_con_t *con, str *query,
415 415
 	sr_xavp_t *row = NULL;
416 416
 	sr_xval_t val;
417 417
 	int i, j;
418
-	str sv;
419 418
 
420 419
 	if(msg==NULL || query==NULL || xavp==NULL)
421 420
 	{
Browse code

modules_k/sqlops: eleminate string copy in sql_exec_xquery()

xavp api will make a copy in shmem, no need to make a private copy first.

Thanks to Juha heinanen and Daniel-Constantin Mierla for finding this
inefficiency.

Alex Hermann authored on 07/01/2013 14:35:46
Showing 1 changed files
... ...
@@ -450,22 +450,22 @@ int sql_exec_xquery(struct sip_msg *msg, sql_con_t *con, str *query,
450 450
 				{
451 451
 					case DB1_STRING:
452 452
 						val.type = SR_XTYPE_STR;
453
-						sv.s=
453
+						val.v.s.s=
454 454
 							(char*)RES_ROWS(db_res)[i].values[j].val.string_val;
455
-						sv.len=strlen(sv.s);
455
+						val.v.s.len=strlen(val.v.s.s);
456 456
 					break;
457 457
 					case DB1_STR:
458 458
 						val.type = SR_XTYPE_STR;
459
-						sv.len=
459
+						val.v.s.len=
460 460
 							RES_ROWS(db_res)[i].values[j].val.str_val.len;
461
-						sv.s=
461
+						val.v.s.s=
462 462
 							(char*)RES_ROWS(db_res)[i].values[j].val.str_val.s;
463 463
 					break;
464 464
 					case DB1_BLOB:
465 465
 						val.type = SR_XTYPE_STR;
466
-						sv.len=
466
+						val.v.s.len=
467 467
 							RES_ROWS(db_res)[i].values[j].val.blob_val.len;
468
-						sv.s=
468
+						val.v.s.s=
469 469
 							(char*)RES_ROWS(db_res)[i].values[j].val.blob_val.s;
470 470
 					break;
471 471
 					case DB1_INT:
... ...
@@ -491,28 +491,10 @@ int sql_exec_xquery(struct sip_msg *msg, sql_con_t *con, str *query,
491 491
 					default:
492 492
 						val.type = SR_XTYPE_NULL;
493 493
 				}
494
-				if(val.type == SR_XTYPE_STR)
495
-				{
496
-					if(sv.len==0)
497
-					{
498
-						val.v.s = _sql_empty_str;
499
-					} else {
500
-						val.v.s.s = (char*)pkg_malloc(sv.len*sizeof(char));
501
-						if(val.v.s.s == NULL)
502
-						{
503
-							LM_ERR("no more memory\n");
504
-							goto error;
505
-						}
506
-						memcpy(val.v.s.s, sv.s, sv.len);
507
-						val.v.s.len = sv.len;
508
-					}
509
-				}
510 494
 			}
511 495
 			/* Add column to current row, under the column's name */
512 496
 			LM_DBG("Adding column: %.*s\n", RES_NAMES(db_res)[j]->len, RES_NAMES(db_res)[j]->s);
513 497
 			xavp_add_value(RES_NAMES(db_res)[j], &val, &row);
514
-			if (val.type == SR_XTYPE_STR && val.v.s.len > 0)
515