Browse code

sqlops: new modparam results_maxsize

- allow setting the maximum size of the result containers list

Daniel-Constantin Mierla authored on 07/10/2020 15:25:41
Showing 1 changed files
... ...
@@ -79,6 +79,8 @@ static int sql_res_param(modparam_t type, void* val);
79 79
 
80 80
 extern int sqlops_tr_buf_size;
81 81
 
82
+int sqlops_results_maxsize = 32;
83
+
82 84
 static int sqlops_connect_mode = 0;
83 85
 
84 86
 static pv_export_t mod_pvs[] = {
... ...
@@ -111,6 +113,7 @@ static param_export_t params[]={
111 113
 	{"sqlres",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_res_param},
112 114
 	{"tr_buf_size",     PARAM_INT,   &sqlops_tr_buf_size},
113 115
 	{"connect_mode",    PARAM_INT,   &sqlops_connect_mode},
116
+	{"results_maxsize", PARAM_INT,   &sqlops_results_maxsize},
114 117
 	{0,0,0}
115 118
 };
116 119
 
Browse code

sqlops: export sql_pvquery to KEMI

Victor Seva authored on 09/06/2020 10:02:43
Showing 1 changed files
... ...
@@ -492,6 +492,60 @@ static int ki_sqlops_query(sip_msg_t *msg, str *scon, str *squery, str *sres)
492 492
 	return sqlops_do_query(scon, squery, sres);
493 493
 }
494 494
 
495
+static int ki_sqlops_pvquery(sip_msg_t *msg, str *scon, str *squery, str *sres)
496
+{
497
+	pv_elem_t *query = NULL;
498
+	pvname_list_t *pv_res = NULL;
499
+	pvname_list_t *pvl = NULL;
500
+	sql_con_t *con = NULL;
501
+	int i, res;
502
+
503
+	if (scon == NULL || scon->s == NULL || scon->len<=0) {
504
+		LM_ERR("invalid connection name\n");
505
+		return -1;
506
+	}
507
+
508
+	con = sql_get_connection(scon);
509
+	if(con==NULL) {
510
+		LM_ERR("invalid connection [%.*s]\n", scon->len, scon->s);
511
+		return -1;
512
+	}
513
+
514
+	if(pv_parse_format(squery, &query)<0)
515
+	{
516
+		LM_ERR("invalid query string [%s]\n", squery->s);
517
+		return -1;
518
+	}
519
+
520
+	/* parse result variables into list of pv_spec_t's */
521
+	pv_res = parse_pvname_list(sres, 0);
522
+	if(pv_res==NULL)
523
+	{
524
+		LM_ERR("invalid result parameter [%s]\n", sres->s);
525
+		pv_elem_free_all(query);
526
+		return -1;
527
+	}
528
+	/* check if all result variables are writable */
529
+	pvl = pv_res;
530
+	i = 1;
531
+	while (pvl) {
532
+		if (pvl->sname.setf == NULL)
533
+		{
534
+			LM_ERR("result variable [%d] is read-only\n", i);
535
+			pv_elem_free_all(query);
536
+			free_pvname_list(pv_res);
537
+			return -1;
538
+		}
539
+		i++;
540
+		pvl = pvl->next;
541
+	}
542
+	res = sql_do_pvquery(msg, con, query, pv_res);
543
+
544
+	pv_elem_free_all(query);
545
+	free_pvname_list(pv_res);
546
+	return res;
547
+}
548
+
495 549
 static int ki_sqlops_query_async(sip_msg_t *msg, str *scon, str *squery)
496 550
 {
497 551
 	sql_con_t *con = NULL;
... ...
@@ -642,6 +696,11 @@ static sr_kemi_t sr_kemi_sqlops_exports[] = {
642 696
 		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
643 697
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
644 698
 	},
699
+	{ str_init("sqlops"), str_init("sql_pvquery"),
700
+		SR_KEMIP_INT, ki_sqlops_pvquery,
701
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
702
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
703
+	},
645 704
 	{ str_init("sqlops"), str_init("sql_xquery"),
646 705
 		SR_KEMIP_INT, sqlops_do_xquery,
647 706
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
Browse code

sqlops: functions to get null result value as empty or zero

Daniel-Constantin Mierla authored on 03/05/2020 07:44:01
Showing 1 changed files
... ...
@@ -540,7 +540,8 @@ static sr_kemi_xval_t _ksr_kemi_sqlops_xval = {0};
540 540
 /**
541 541
  *
542 542
  */
543
-static sr_kemi_xval_t* ki_sqlops_result_get(sip_msg_t *msg, str *resid, int row, int col)
543
+static sr_kemi_xval_t* ki_sqlops_result_get_mode(sip_msg_t *msg, str *resid,
544
+		int row, int col, int rmode)
544 545
 {
545 546
 	sql_result_t *res = NULL;
546 547
 
... ...
@@ -548,31 +549,31 @@ static sr_kemi_xval_t* ki_sqlops_result_get(sip_msg_t *msg, str *resid, int row,
548 549
 
549 550
 	if (resid == NULL || resid->s == NULL || resid->len == 0) {
550 551
 		LM_ERR("invalid result name\n");
551
-		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
552
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, rmode);
552 553
 		return &_ksr_kemi_sqlops_xval;
553 554
 	}
554 555
 
555 556
 	res = sql_get_result(resid);
556 557
 	if(res==NULL) {
557 558
 		LM_ERR("invalid result container [%.*s]\n", resid->len, resid->s);
558
-		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
559
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, rmode);
559 560
 		return &_ksr_kemi_sqlops_xval;
560 561
 	}
561 562
 
562 563
 	if(row >= res->nrows) {
563
-		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
564
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, rmode);
564 565
 		return &_ksr_kemi_sqlops_xval;
565 566
 	}
566 567
 	if(col >= res->ncols) {
567
-		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
568
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, rmode);
568 569
 		return &_ksr_kemi_sqlops_xval;
569 570
 	}
570 571
 	if(res->vals[row][col].flags&PV_VAL_NULL) {
571
-		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
572
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, rmode);
572 573
 		return &_ksr_kemi_sqlops_xval;
573 574
 	}
574 575
 	if(res->vals[row][col].flags&PV_VAL_INT) {
575
-		_ksr_kemi_sqlops_xval.vtype = SR_KEMIP_STR;
576
+		_ksr_kemi_sqlops_xval.vtype = SR_KEMIP_INT;
576 577
 		_ksr_kemi_sqlops_xval.v.n = res->vals[row][col].value.n;
577 578
 		return &_ksr_kemi_sqlops_xval;
578 579
 	}
... ...
@@ -581,6 +582,36 @@ static sr_kemi_xval_t* ki_sqlops_result_get(sip_msg_t *msg, str *resid, int row,
581 582
 	return &_ksr_kemi_sqlops_xval;
582 583
 }
583 584
 
585
+/**
586
+ *
587
+ */
588
+static sr_kemi_xval_t* ki_sqlops_result_get(sip_msg_t *msg, str *resid,
589
+		int row, int col)
590
+{
591
+	return ki_sqlops_result_get_mode(msg, resid, row, col,
592
+			SR_KEMI_XVAL_NULL_NONE);
593
+}
594
+
595
+/**
596
+ *
597
+ */
598
+static sr_kemi_xval_t* ki_sqlops_result_gete(sip_msg_t *msg, str *resid,
599
+		int row, int col)
600
+{
601
+	return ki_sqlops_result_get_mode(msg, resid, row, col,
602
+			SR_KEMI_XVAL_NULL_EMPTY);
603
+}
604
+
605
+/**
606
+ *
607
+ */
608
+static sr_kemi_xval_t* ki_sqlops_result_getz(sip_msg_t *msg, str *resid,
609
+		int row, int col)
610
+{
611
+	return ki_sqlops_result_get_mode(msg, resid, row, col,
612
+			SR_KEMI_XVAL_NULL_ZERO);
613
+}
614
+
584 615
 /**
585 616
  *
586 617
  */
... ...
@@ -626,6 +657,16 @@ static sr_kemi_t sr_kemi_sqlops_exports[] = {
626 657
 		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
627 658
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
628 659
 	},
660
+	{ str_init("sqlops"), str_init("sql_result_gete"),
661
+		SR_KEMIP_XVAL, ki_sqlops_result_gete,
662
+		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
663
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
664
+	},
665
+	{ str_init("sqlops"), str_init("sql_result_getz"),
666
+		SR_KEMIP_XVAL, ki_sqlops_result_getz,
667
+		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
668
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
669
+	},
629 670
 
630 671
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
631 672
 };
Browse code

sqlops: exported sql_result_get() to kemi

- return value of sql query result by id, row and column

Daniel-Constantin Mierla authored on 28/01/2020 09:32:49
Showing 1 changed files
... ...
@@ -531,6 +531,56 @@ static int ki_sqlops_is_null(sip_msg_t *msg, str *sres, int i, int j)
531 531
 	return sqlops_is_null(sres, i, j);
532 532
 }
533 533
 
534
+/**
535
+ *
536
+ */
537
+static sr_kemi_xval_t _ksr_kemi_sqlops_xval = {0};
538
+
539
+
540
+/**
541
+ *
542
+ */
543
+static sr_kemi_xval_t* ki_sqlops_result_get(sip_msg_t *msg, str *resid, int row, int col)
544
+{
545
+	sql_result_t *res = NULL;
546
+
547
+	memset(&_ksr_kemi_sqlops_xval, 0, sizeof(sr_kemi_xval_t));
548
+
549
+	if (resid == NULL || resid->s == NULL || resid->len == 0) {
550
+		LM_ERR("invalid result name\n");
551
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
552
+		return &_ksr_kemi_sqlops_xval;
553
+	}
554
+
555
+	res = sql_get_result(resid);
556
+	if(res==NULL) {
557
+		LM_ERR("invalid result container [%.*s]\n", resid->len, resid->s);
558
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
559
+		return &_ksr_kemi_sqlops_xval;
560
+	}
561
+
562
+	if(row >= res->nrows) {
563
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
564
+		return &_ksr_kemi_sqlops_xval;
565
+	}
566
+	if(col >= res->ncols) {
567
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
568
+		return &_ksr_kemi_sqlops_xval;
569
+	}
570
+	if(res->vals[row][col].flags&PV_VAL_NULL) {
571
+		sr_kemi_xval_null(&_ksr_kemi_sqlops_xval, SR_KEMI_XVAL_NULL_NONE);
572
+		return &_ksr_kemi_sqlops_xval;
573
+	}
574
+	if(res->vals[row][col].flags&PV_VAL_INT) {
575
+		_ksr_kemi_sqlops_xval.vtype = SR_KEMIP_STR;
576
+		_ksr_kemi_sqlops_xval.v.n = res->vals[row][col].value.n;
577
+		return &_ksr_kemi_sqlops_xval;
578
+	}
579
+	_ksr_kemi_sqlops_xval.vtype = SR_KEMIP_STR;
580
+	_ksr_kemi_sqlops_xval.v.s = res->vals[row][col].value.s;
581
+	return &_ksr_kemi_sqlops_xval;
582
+}
583
+
534 584
 /**
535 585
  *
536 586
  */
... ...
@@ -571,6 +621,11 @@ static sr_kemi_t sr_kemi_sqlops_exports[] = {
571 621
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
572 622
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
573 623
 	},
624
+	{ str_init("sqlops"), str_init("sql_result_get"),
625
+		SR_KEMIP_XVAL, ki_sqlops_result_get,
626
+		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
627
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
628
+	},
574 629
 
575 630
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
576 631
 };
Browse code

sqlops: add a limit of maximum 32 result containers

- prevent use of too much memory by defining too many result containers
- related to GH #2032

Daniel-Constantin Mierla authored on 14/08/2019 07:53:17
Showing 1 changed files
... ...
@@ -205,7 +205,7 @@ int sql_res_param(modparam_t type, void *val)
205 205
 	res = sql_get_result(&s);
206 206
 	if(res==NULL)
207 207
 	{
208
-		LM_ERR("invalid result [%s]\n", s.s);
208
+		LM_ERR("invalid result container [%s]\n", s.s);
209 209
 		goto error;
210 210
 	}
211 211
 	return 0;
... ...
@@ -335,7 +335,7 @@ static int fixup_sql_query(void** param, int param_no)
335 335
 		res = sql_get_result(&s);
336 336
 		if(res==NULL)
337 337
 		{
338
-			LM_ERR("invalid result [%s]\n", s.s);
338
+			LM_ERR("invalid result container [%s]\n", s.s);
339 339
 			return E_UNSPEC;
340 340
 		}
341 341
 		*param = (void*)res;
... ...
@@ -458,7 +458,7 @@ static int fixup_sql_rfree(void** param, int param_no)
458 458
 		res = sql_get_result(&s);
459 459
 		if(res==NULL)
460 460
 		{
461
-			LM_ERR("invalid result [%s]\n", s.s);
461
+			LM_ERR("invalid result container [%s]\n", s.s);
462 462
 			return E_UNSPEC;
463 463
 		}
464 464
 		*param = (void*)res;
Browse code

sqlops: removed ifdefs for WITH_XAVP

Daniel-Constantin Mierla authored on 18/04/2019 12:00:01
Showing 1 changed files
... ...
@@ -62,9 +62,7 @@ static int sql_check_connection(sql_con_t*);
62 62
 static int sql_query(struct sip_msg*, char*, char*, char*);
63 63
 static int sql_query2(struct sip_msg*, char*, char*);
64 64
 static int sql_query_async(struct sip_msg*, char*, char*);
65
-#ifdef WITH_XAVP
66 65
 static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res);
67
-#endif
68 66
 static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res);
69 67
 static int sql_rfree(struct sip_msg*, char*, char*);
70 68
 static int mod_init(void);
... ...
@@ -72,9 +70,7 @@ static int child_init(int rank);
72 70
 static void destroy(void);
73 71
 
74 72
 static int fixup_sql_query(void** param, int param_no);
75
-#ifdef WITH_XAVP
76 73
 static int fixup_sql_xquery(void** param, int param_no);
77
-#endif
78 74
 static int fixup_sql_pvquery(void** param, int param_no);
79 75
 static int fixup_sql_rfree(void** param, int param_no);
80 76
 
... ...
@@ -100,10 +96,8 @@ static cmd_export_t cmds[]={
100 96
 		ANY_ROUTE},
101 97
 	{"sql_query_async",  (cmd_function)sql_query_async, 2, fixup_sql_query, 0,
102 98
 		ANY_ROUTE},
103
-#ifdef WITH_XAVP
104 99
 	{"sql_xquery",  (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0,
105 100
 		ANY_ROUTE},
106
-#endif
107 101
 	{"sql_pvquery",  (cmd_function)sql_pvquery, 3, fixup_sql_pvquery, 0,
108 102
 		ANY_ROUTE},
109 103
 	{"sql_result_free",  (cmd_function)sql_rfree,  1, fixup_sql_rfree, 0,
... ...
@@ -276,7 +270,6 @@ static int sql_query_async(struct sip_msg *msg, char *dbl, char *query)
276 270
 }
277 271
 
278 272
 
279
-#ifdef WITH_XAVP
280 273
 /**
281 274
  *
282 275
  */
... ...
@@ -288,7 +281,6 @@ static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res)
288 281
 	}
289 282
 	return sql_do_xquery(msg, (sql_con_t*)dbl, (pv_elem_t*)query, (pv_elem_t*)res);
290 283
 }
291
-#endif
292 284
 
293 285
 /**
294 286
  *
... ...
@@ -351,7 +343,6 @@ static int fixup_sql_query(void** param, int param_no)
351 343
 	return 0;
352 344
 }
353 345
 
354
-#ifdef WITH_XAVP
355 346
 /**
356 347
  *
357 348
  */
... ...
@@ -389,7 +380,6 @@ static int fixup_sql_xquery(void** param, int param_no)
389 380
 	}
390 381
 	return 0;
391 382
 }
392
-#endif
393 383
 
394 384
 /**
395 385
  *
Browse code

sqlops: option to start even when connecting to db fails

- new parameter connect_mode: 0 (default) - do not start if connecting
to db fails; 1 - start even if connecting to db fails
- started from GH #1706

Daniel-Constantin Mierla authored on 30/11/2018 08:20:30
Showing 1 changed files
... ...
@@ -58,6 +58,7 @@ MODULE_VERSION
58 58
 static int bind_sqlops(sqlops_api_t* api);
59 59
 
60 60
 /** module functions */
61
+static int sql_check_connection(sql_con_t*);
61 62
 static int sql_query(struct sip_msg*, char*, char*, char*);
62 63
 static int sql_query2(struct sip_msg*, char*, char*);
63 64
 static int sql_query_async(struct sip_msg*, char*, char*);
... ...
@@ -82,6 +83,8 @@ static int sql_res_param(modparam_t type, void* val);
82 83
 
83 84
 extern int sqlops_tr_buf_size;
84 85
 
86
+static int sqlops_connect_mode = 0;
87
+
85 88
 static pv_export_t mod_pvs[] = {
86 89
 	{ {"dbr", sizeof("dbr")-1}, PVT_OTHER, pv_get_dbr, 0,
87 90
 		pv_parse_dbr_name, 0, 0, 0 },
... ...
@@ -113,6 +116,7 @@ static param_export_t params[]={
113 116
 	{"sqlcon",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_con_param},
114 117
 	{"sqlres",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_res_param},
115 118
 	{"tr_buf_size",     PARAM_INT,   &sqlops_tr_buf_size},
119
+	{"connect_mode",    PARAM_INT,   &sqlops_connect_mode},
116 120
 	{0,0,0}
117 121
 };
118 122
 
... ...
@@ -146,9 +150,23 @@ static int mod_init(void)
146 150
 
147 151
 static int child_init(int rank)
148 152
 {
153
+	int ret;
149 154
 	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
150 155
 		return 0;
151
-	return sql_connect();
156
+
157
+	ret = sql_connect((sqlops_connect_mode == 1)?1:0);
158
+
159
+	LM_DBG("SQL result: %d \n", ret);
160
+
161
+	if (ret != 0 && sqlops_connect_mode == 1)
162
+	{
163
+		LM_INFO("SQL result: %d but start_without_db_connection enabled - proceed\n",
164
+				ret);
165
+		return 0;
166
+
167
+	} else {
168
+		return ret;
169
+	}
152 170
 }
153 171
 
154 172
 /**
... ...
@@ -201,14 +219,36 @@ error:
201 219
 	return -1;
202 220
 }
203 221
 
222
+static int sql_check_connection(sql_con_t *dbl)
223
+{
224
+	if (dbl->dbh != NULL) {
225
+		return 0;
226
+	}
227
+
228
+	if(sqlops_connect_mode != 1) {
229
+		LM_CRIT("no database handle with reconnect disabled\n");
230
+		return -1;
231
+	}
232
+
233
+    LM_DBG("try to establish SQL connection\n");
234
+	if(sql_reconnect(dbl)<0) {
235
+		LM_ERR("failed to connect to database\n");
236
+		return -1;
237
+	}
238
+	return 0;
239
+}
240
+
204 241
 /**
205 242
  *
206 243
  */
207 244
 static int sql_query(struct sip_msg *msg, char *dbl, char *query, char *res)
208 245
 {
209 246
 	str sq;
210
-	if(pv_printf_s(msg, (pv_elem_t*)query, &sq)!=0)
211
-	{
247
+	if(sql_check_connection((sql_con_t*)dbl)<0) {
248
+		LM_ERR("invalid connection to database");
249
+		return -2;
250
+	}
251
+	if(pv_printf_s(msg, (pv_elem_t*)query, &sq)!=0) {
212 252
 		LM_ERR("cannot print the sql query\n");
213 253
 		return -1;
214 254
 	}
... ...
@@ -223,6 +263,10 @@ static int sql_query2(struct sip_msg *msg, char *dbl, char *query)
223 263
 static int sql_query_async(struct sip_msg *msg, char *dbl, char *query)
224 264
 {
225 265
 	str sq;
266
+	if(sql_check_connection((sql_con_t*)dbl)<0) {
267
+		LM_ERR("invalid connection to database");
268
+		return -2;
269
+	}
226 270
 	if(pv_printf_s(msg, (pv_elem_t*)query, &sq)!=0)
227 271
 	{
228 272
 		LM_ERR("cannot print the sql query\n");
... ...
@@ -238,6 +282,10 @@ static int sql_query_async(struct sip_msg *msg, char *dbl, char *query)
238 282
  */
239 283
 static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res)
240 284
 {
285
+	if(sql_check_connection((sql_con_t*)dbl)<0) {
286
+		LM_ERR("invalid connection to database");
287
+		return -2;
288
+	}
241 289
 	return sql_do_xquery(msg, (sql_con_t*)dbl, (pv_elem_t*)query, (pv_elem_t*)res);
242 290
 }
243 291
 #endif
... ...
@@ -247,6 +295,10 @@ static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res)
247 295
  */
248 296
 static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res)
249 297
 {
298
+	if(sql_check_connection((sql_con_t*)dbl)<0) {
299
+		LM_ERR("invalid connection to database");
300
+		return -2;
301
+	}
250 302
 	return sql_do_pvquery(msg, (sql_con_t*)dbl, (pv_elem_t*)query, (pvname_list_t*)res);
251 303
 }
252 304
 
Browse code

sqlops: updated to the new mod interface

Daniel-Constantin Mierla authored on 28/09/2018 08:35:30 • Victor Seva committed on 28/09/2018 11:03:26
Showing 1 changed files
... ...
@@ -124,18 +124,16 @@ static tr_export_t mod_trans[] = {
124 124
 
125 125
 /** module exports */
126 126
 struct module_exports exports= {
127
-	"sqlops",
127
+	"sqlops",   /* module name */
128 128
 	DEFAULT_DLFLAGS, /* dlopen flags */
129
-	cmds,
130
-	params,
131
-	0,          /* exported statistics */
132
-	0  ,        /* exported MI functions */
129
+	cmds,       /* exported functions */
130
+	params,     /* exported parameters */
131
+	0,          /* exported rpc functions */
133 132
 	mod_pvs,    /* exported pseudo-variables */
134
-	0,          /* extra processes */
135
-	mod_init,   /* module initialization function */
136
-	0,
137
-	(destroy_function) destroy,
138
-	child_init  /* per-child init function */
133
+	0,          /* response handling function */
134
+	mod_init,   /* module init function */
135
+	child_init, /* per-child init function */
136
+	destroy     /* module destroy function */
139 137
 };
140 138
 
141 139
 static int mod_init(void)
Browse code

sqlops: exported sql_query_async() to kemi framework

Daniel-Constantin Mierla authored on 31/03/2018 11:07:55
Showing 1 changed files
... ...
@@ -452,6 +452,24 @@ static int ki_sqlops_query(sip_msg_t *msg, str *scon, str *squery, str *sres)
452 452
 	return sqlops_do_query(scon, squery, sres);
453 453
 }
454 454
 
455
+static int ki_sqlops_query_async(sip_msg_t *msg, str *scon, str *squery)
456
+{
457
+	sql_con_t *con = NULL;
458
+
459
+	if (scon == NULL || scon->s == NULL || scon->len<=0) {
460
+		LM_ERR("invalid connection name\n");
461
+		return -1;
462
+	}
463
+
464
+	con = sql_get_connection(scon);
465
+	if(con==NULL) {
466
+		LM_ERR("invalid connection [%.*s]\n", scon->len, scon->s);
467
+		return -1;
468
+	}
469
+
470
+	return sql_do_query_async(con, squery);
471
+}
472
+
455 473
 static int ki_sqlops_reset_result(sip_msg_t *msg, str *sres)
456 474
 {
457 475
 	sqlops_reset_result(sres);
... ...
@@ -508,6 +526,11 @@ static sr_kemi_t sr_kemi_sqlops_exports[] = {
508 526
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
509 527
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
510 528
 	},
529
+	{ str_init("sqlops"), str_init("sql_query_async"),
530
+		SR_KEMIP_INT, ki_sqlops_query_async,
531
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
532
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
533
+	},
511 534
 
512 535
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
513 536
 };
Browse code

sqlops: proper function prototypes for kemi exports to get num rows and cols

Daniel-Constantin Mierla authored on 30/03/2018 15:15:26
Showing 1 changed files
... ...
@@ -458,12 +458,12 @@ static int ki_sqlops_reset_result(sip_msg_t *msg, str *sres)
458 458
 	return 1;
459 459
 }
460 460
 
461
-static int ki_sqlops_num_rows(str *sres)
461
+static int ki_sqlops_num_rows(sip_msg_t *msg, str *sres)
462 462
 {
463 463
 	return sqlops_num_rows(sres);
464 464
 }
465 465
 
466
-static int ki_sqlops_num_columns(str *sres)
466
+static int ki_sqlops_num_columns(sip_msg_t *msg, str *sres)
467 467
 {
468 468
 	return sqlops_num_columns(sres);
469 469
 }
... ...
@@ -520,4 +520,4 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
520 520
 {
521 521
 	sr_kemi_modules_add(sr_kemi_sqlops_exports);
522 522
 	return register_trans_mod(path, mod_trans);
523
-}
524 523
\ No newline at end of file
524
+}
Browse code

sqlops: free allocated pv list in case of error inside fixup_sql_pvquery()

Daniel-Constantin Mierla authored on 21/07/2017 06:56:58
Showing 1 changed files
... ...
@@ -391,6 +391,7 @@ static int fixup_sql_pvquery(void** param, int param_no)
391 391
 			if (pvl->sname.setf == NULL)
392 392
 			{
393 393
 				LM_ERR("result variable [%d] is read-only\n", i);
394
+				free_pvname_list(res);
394 395
 				return E_UNSPEC;
395 396
 			}
396 397
 			i++;
Browse code

sqlops: exported functions to kemi framework

Daniel-Constantin Mierla authored on 02/05/2017 06:25:10
Showing 1 changed files
... ...
@@ -45,8 +45,9 @@
45 45
 
46 46
 #include "../../core/sr_module.h"
47 47
 #include "../../core/dprint.h"
48
-
49 48
 #include "../../core/pvar.h"
49
+#include "../../core/kemi.h"
50
+
50 51
 #include "sql_api.h"
51 52
 #include "sql_var.h"
52 53
 #include "sql_trans.h"
... ...
@@ -137,11 +138,6 @@ struct module_exports exports= {
137 138
 	child_init  /* per-child init function */
138 139
 };
139 140
 
140
-int mod_register(char *path, int *dlflags, void *p1, void *p2)
141
-{
142
-	return register_trans_mod(path, mod_trans);
143
-}
144
-
145 141
 static int mod_init(void)
146 142
 {
147 143
 	if(sqlops_tr_buffer_init()<0) {
... ...
@@ -449,3 +445,78 @@ static int bind_sqlops(sqlops_api_t* api)
449 445
 
450 446
 	return 0;
451 447
 }
448
+
449
+static int ki_sqlops_query(sip_msg_t *msg, str *scon, str *squery, str *sres)
450
+{
451
+	return sqlops_do_query(scon, squery, sres);
452
+}
453
+
454
+static int ki_sqlops_reset_result(sip_msg_t *msg, str *sres)
455
+{
456
+	sqlops_reset_result(sres);
457
+	return 1;
458
+}
459
+
460
+static int ki_sqlops_num_rows(str *sres)
461
+{
462
+	return sqlops_num_rows(sres);
463
+}
464
+
465
+static int ki_sqlops_num_columns(str *sres)
466
+{
467
+	return sqlops_num_columns(sres);
468
+}
469
+
470
+static int ki_sqlops_is_null(sip_msg_t *msg, str *sres, int i, int j)
471
+{
472
+	return sqlops_is_null(sres, i, j);
473
+}
474
+
475
+/**
476
+ *
477
+ */
478
+/* clang-format off */
479
+static sr_kemi_t sr_kemi_sqlops_exports[] = {
480
+	{ str_init("sqlops"), str_init("sql_query"),
481
+		SR_KEMIP_INT, ki_sqlops_query,
482
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
483
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
484
+	},
485
+	{ str_init("sqlops"), str_init("sql_result_free"),
486
+		SR_KEMIP_INT, ki_sqlops_reset_result,
487
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
488
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
489
+	},
490
+	{ str_init("sqlops"), str_init("sql_num_rows"),
491
+		SR_KEMIP_INT, ki_sqlops_num_rows,
492
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
493
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
494
+	},
495
+	{ str_init("sqlops"), str_init("sql_num_columns"),
496
+		SR_KEMIP_INT, ki_sqlops_num_columns,
497
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
498
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
499
+	},
500
+	{ str_init("sqlops"), str_init("sql_is_null"),
501
+		SR_KEMIP_INT, ki_sqlops_is_null,
502
+		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
503
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
504
+	},
505
+	{ str_init("sqlops"), str_init("sql_xquery"),
506
+		SR_KEMIP_INT, sqlops_do_xquery,
507
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
508
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
509
+	},
510
+
511
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
512
+};
513
+/* clang-format on */
514
+
515
+/**
516
+ *
517
+ */
518
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
519
+{
520
+	sr_kemi_modules_add(sr_kemi_sqlops_exports);
521
+	return register_trans_mod(path, mod_trans);
522
+}
452 523
\ No newline at end of file
Browse code

sqlops: allow to set the size of sql escape buffer for transformations

- alternative to GH #899

Daniel-Constantin Mierla authored on 08/01/2017 21:10:59
Showing 1 changed files
... ...
@@ -13,8 +13,8 @@
13 13
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
  * GNU General Public License for more details.
15 15
  *
16
- * You should have received a copy of the GNU General Public License 
17
- * along with this program; if not, write to the Free Software 
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 19
  */
20 20
 
... ...
@@ -65,6 +65,7 @@ static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res);
65 65
 #endif
66 66
 static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res);
67 67
 static int sql_rfree(struct sip_msg*, char*, char*);
68
+static int mod_init(void);
68 69
 static int child_init(int rank);
69 70
 static void destroy(void);
70 71
 
... ...
@@ -78,6 +79,8 @@ static int fixup_sql_rfree(void** param, int param_no);
78 79
 static int sql_con_param(modparam_t type, void* val);
79 80
 static int sql_res_param(modparam_t type, void* val);
80 81
 
82
+extern int sqlops_tr_buf_size;
83
+
81 84
 static pv_export_t mod_pvs[] = {
82 85
 	{ {"dbr", sizeof("dbr")-1}, PVT_OTHER, pv_get_dbr, 0,
83 86
 		pv_parse_dbr_name, 0, 0, 0 },
... ...
@@ -87,19 +90,19 @@ static pv_export_t mod_pvs[] = {
87 90
 };
88 91
 
89 92
 static cmd_export_t cmds[]={
90
-	{"sql_query",  (cmd_function)sql_query, 3, fixup_sql_query, 0, 
93
+	{"sql_query",  (cmd_function)sql_query, 3, fixup_sql_query, 0,
91 94
 		ANY_ROUTE},
92
-	{"sql_query",  (cmd_function)sql_query2, 2, fixup_sql_query, 0, 
95
+	{"sql_query",  (cmd_function)sql_query2, 2, fixup_sql_query, 0,
93 96
 		ANY_ROUTE},
94
-	{"sql_query_async",  (cmd_function)sql_query_async, 2, fixup_sql_query, 0, 
97
+	{"sql_query_async",  (cmd_function)sql_query_async, 2, fixup_sql_query, 0,
95 98
 		ANY_ROUTE},
96 99
 #ifdef WITH_XAVP
97
-	{"sql_xquery",  (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0, 
100
+	{"sql_xquery",  (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0,
98 101
 		ANY_ROUTE},
99 102
 #endif
100 103
 	{"sql_pvquery",  (cmd_function)sql_pvquery, 3, fixup_sql_pvquery, 0,
101 104
 		ANY_ROUTE},
102
-	{"sql_result_free",  (cmd_function)sql_rfree,  1, fixup_sql_rfree, 0, 
105
+	{"sql_result_free",  (cmd_function)sql_rfree,  1, fixup_sql_rfree, 0,
103 106
 		ANY_ROUTE},
104 107
 	{"bind_sqlops", (cmd_function)bind_sqlops, 0, 0, 0, 0},
105 108
 	{0,0,0,0,0,0}
... ...
@@ -108,6 +111,7 @@ static cmd_export_t cmds[]={
108 111
 static param_export_t params[]={
109 112
 	{"sqlcon",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_con_param},
110 113
 	{"sqlres",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_res_param},
114
+	{"tr_buf_size",     PARAM_INT,   &sqlops_tr_buf_size},
111 115
 	{0,0,0}
112 116
 };
113 117
 
... ...
@@ -127,7 +131,7 @@ struct module_exports exports= {
127 131
 	0  ,        /* exported MI functions */
128 132
 	mod_pvs,    /* exported pseudo-variables */
129 133
 	0,          /* extra processes */
130
-	0,          /* module initialization function */
134
+	mod_init,   /* module initialization function */
131 135
 	0,
132 136
 	(destroy_function) destroy,
133 137
 	child_init  /* per-child init function */
... ...
@@ -138,6 +142,14 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
138 142
 	return register_trans_mod(path, mod_trans);
139 143
 }
140 144
 
145
+static int mod_init(void)
146
+{
147
+	if(sqlops_tr_buffer_init()<0) {
148
+		return -1;
149
+	}
150
+	return 0;
151
+}
152
+
141 153
 static int child_init(int rank)
142 154
 {
143 155
 	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -43,10 +43,10 @@
43 43
 #include <unistd.h>
44 44
 #include <fcntl.h>
45 45
 
46
-#include "../../sr_module.h"
47
-#include "../../dprint.h"
46
+#include "../../core/sr_module.h"
47
+#include "../../core/dprint.h"
48 48
 
49
-#include "../../pvar.h"
49
+#include "../../core/pvar.h"
50 50
 #include "sql_api.h"
51 51
 #include "sql_var.h"
52 52
 #include "sql_trans.h"
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,439 @@
1
+/**
2
+ * Copyright (C) 2008 Elena-Ramona Modroiu (asipto.com)
3
+ *
4
+ * This file is part of Kamailio, a free SIP server.
5
+ *
6
+ * Kamailio is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * Kamailio is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License 
17
+ * along with this program; if not, write to the Free Software 
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
+ */
20
+
21
+/*! \file
22
+ * \ingroup sqlops
23
+ * \brief Kamailio SQL-operations :: Module interface
24
+ *
25
+ * - Module: \ref sqlops
26
+ */
27
+
28
+/*! \defgroup sqlops Kamailio :: SQL Operations
29
+ *
30
+ * The module adds support for raw SQL queries in the configuration file.
31
+ *
32
+ */
33
+
34
+
35
+
36
+
37
+
38
+#include <stdio.h>
39
+#include <string.h>
40
+#include <stdlib.h>
41
+#include <sys/types.h>
42
+#include <sys/ipc.h>
43
+#include <unistd.h>
44
+#include <fcntl.h>
45
+
46
+#include "../../sr_module.h"
47
+#include "../../dprint.h"
48
+
49
+#include "../../pvar.h"
50
+#include "sql_api.h"
51
+#include "sql_var.h"
52
+#include "sql_trans.h"
53
+
54
+
55
+MODULE_VERSION
56
+
57
+static int bind_sqlops(sqlops_api_t* api);
58
+
59
+/** module functions */
60
+static int sql_query(struct sip_msg*, char*, char*, char*);
61
+static int sql_query2(struct sip_msg*, char*, char*);
62
+static int sql_query_async(struct sip_msg*, char*, char*);
63
+#ifdef WITH_XAVP
64
+static int sql_xquery(struct sip_msg *msg, char *dbl, char *query, char *res);
65
+#endif
66
+static int sql_pvquery(struct sip_msg *msg, char *dbl, char *query, char *res);
67
+static int sql_rfree(struct sip_msg*, char*, char*);
68
+static int child_init(int rank);
69
+static void destroy(void);
70
+
71
+static int fixup_sql_query(void** param, int param_no);
72
+#ifdef WITH_XAVP
73
+static int fixup_sql_xquery(void** param, int param_no);
74
+#endif
75
+static int fixup_sql_pvquery(void** param, int param_no);
76
+static int fixup_sql_rfree(void** param, int param_no);
77
+
78
+static int sql_con_param(modparam_t type, void* val);
79
+static int sql_res_param(modparam_t type, void* val);
80
+
81
+static pv_export_t mod_pvs[] = {
82
+	{ {"dbr", sizeof("dbr")-1}, PVT_OTHER, pv_get_dbr, 0,
83
+		pv_parse_dbr_name, 0, 0, 0 },
84
+	{ {"sqlrows", sizeof("sqlrows")-1}, PVT_OTHER, pv_get_sqlrows, 0,
85
+		pv_parse_con_name, 0, 0, 0 },
86
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
87
+};
88
+
89
+static cmd_export_t cmds[]={
90
+	{"sql_query",  (cmd_function)sql_query, 3, fixup_sql_query, 0, 
91
+		ANY_ROUTE},
92
+	{"sql_query",  (cmd_function)sql_query2, 2, fixup_sql_query, 0, 
93
+		ANY_ROUTE},
94
+	{"sql_query_async",  (cmd_function)sql_query_async, 2, fixup_sql_query, 0, 
95
+		ANY_ROUTE},
96
+#ifdef WITH_XAVP
97
+	{"sql_xquery",  (cmd_function)sql_xquery, 3, fixup_sql_xquery, 0, 
98
+		ANY_ROUTE},
99
+#endif
100
+	{"sql_pvquery",  (cmd_function)sql_pvquery, 3, fixup_sql_pvquery, 0,
101
+		ANY_ROUTE},
102
+	{"sql_result_free",  (cmd_function)sql_rfree,  1, fixup_sql_rfree, 0, 
103
+		ANY_ROUTE},
104
+	{"bind_sqlops", (cmd_function)bind_sqlops, 0, 0, 0, 0},
105
+	{0,0,0,0,0,0}
106
+};
107
+
108
+static param_export_t params[]={
109
+	{"sqlcon",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_con_param},
110
+	{"sqlres",  PARAM_STRING|USE_FUNC_PARAM, (void*)sql_res_param},
111
+	{0,0,0}
112
+};
113
+
114
+static tr_export_t mod_trans[] = {
115
+	{ {"sql", sizeof("sql")-1}, tr_parse_sql },
116
+	{ { 0, 0 }, 0 }
117
+};
118
+
119
+
120
+/** module exports */
121
+struct module_exports exports= {
122
+	"sqlops",
123
+	DEFAULT_DLFLAGS, /* dlopen flags */
124
+	cmds,
125
+	params,
126
+	0,          /* exported statistics */
127
+	0  ,        /* exported MI functions */
128
+	mod_pvs,    /* exported pseudo-variables */
129
+	0,          /* extra processes */
130
+	0,          /* module initialization function */
131
+	0,
132
+	(destroy_function) destroy,
133
+	child_init  /* per-child init function */