Browse code

db_mysql: fixed typo in comments (timout -> timeout)

Daniel-Constantin Mierla authored on 01/10/2019 06:59:44
Showing 1 changed files
... ...
@@ -89,7 +89,7 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
89 89
 			}
90 90
 		}
91 91
 		/*
92
-		 * We're doing later a query anyway that will reset the timout of the server,
92
+		 * We're doing later a query anyway that will reset the timeout of the server,
93 93
 		 * so it makes sense to set the timestamp value to the actual time in order
94 94
 		 * to prevent unnecessary pings.
95 95
 		 */
Browse code

db_mysql: remove long commented out DBG statement that don't work for all data

Henning Westerholt authored on 21/01/2019 22:27:43
Showing 1 changed files
... ...
@@ -96,10 +96,6 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
96 96
 		CON_TIMESTAMP(_h) = t;
97 97
 	}
98 98
 
99
-	/* screws up the terminal when the query contains a BLOB :-( (by bogdan)
100
-	 * LM_DBG("submit_query(): %.*s\n", _s->len, _s->s);
101
-	 */
102
-
103 99
 	/* When a server connection is lost and a query is attempted, most of
104 100
 	 * the time the query will return a CR_SERVER_LOST, then at the second
105 101
 	 * attempt to execute it, the mysql lib will reconnect and succeed.
Browse code

db_mysql: use generic PKG_MEM_ERROR, SHM_ERROR and SYS_MEM_ERROR helper defines

Henning Westerholt authored on 21/01/2019 22:17:21
Showing 1 changed files
... ...
@@ -170,7 +170,7 @@ int db_mysql_submit_query_async(const db1_con_t* _h, const str* _s)
170 170
 	asize = sizeof(async_task_t) + 2*sizeof(str) + di->url.len + _s->len + 2;
171 171
 	atask = shm_malloc(asize);
172 172
 	if(atask==NULL) {
173
-		LM_ERR("no more shared memory to allocate %d\n", asize);
173
+		SHM_MEM_ERROR_FMT("size %d\n", asize);
174 174
 		return -1;
175 175
 	}
176 176
 
... ...
@@ -370,7 +370,7 @@ int db_mysql_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows)
370 370
 		/* Allocate a new result structure */
371 371
 		*_r = db_mysql_new_result();
372 372
 		if (*_r == 0) {
373
-			LM_ERR("no memory left\n");
373
+			LM_ERR("could not allocate new result\n");
374 374
 			return -2;
375 375
 		}
376 376
 
... ...
@@ -430,7 +430,7 @@ int db_mysql_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows)
430 430
 
431 431
 	RES_ROWS(*_r) = (struct db_row*)pkg_malloc(sizeof(db_row_t) * rows);
432 432
 	if (!RES_ROWS(*_r)) {
433
-		LM_ERR("no memory left\n");
433
+		PKG_MEM_ERROR;
434 434
 		return -5;
435 435
 	}
436 436
 
... ...
@@ -625,7 +625,7 @@ int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
625 625
 	case DB_LOCKING_WRITE:
626 626
 		if ((lock_str.s = pkg_malloc((lock_start_str.len + CON_TABLE(_h)->len + lock_end_str.len) * sizeof(char))) == NULL)
627 627
 		{
628
-			LM_ERR("allocating pkg memory\n");
628
+			PKG_MEM_ERROR;
629 629
 			goto error;
630 630
 		}
631 631
 
... ...
@@ -896,8 +896,10 @@ int db_mysql_alloc_buffer(void)
896 896
 	}
897 897
 
898 898
 	mysql_sql_buf = (char*)malloc(sql_buffer_size);
899
-	if (mysql_sql_buf == NULL)
899
+	if (mysql_sql_buf == NULL) {
900
+		SYS_MEM_ERROR;
900 901
 		return -1;
901
-	else
902
+	} else {
902 903
 		return 0;
904
+	}
903 905
 }
Browse code

db_mysql: remove build warning, unneeded include (#1777)

db_mysql: mysql_version.h is already included at mysql.h (GH #1777)

> In file included from km_dbase.c:38:
> /usr/include/mariadb/mysql_version.h:3:2: warning: This file should not be included by clients, include only <mysql.h> [-W#warnings]
> #warning This file should not be included by clients, include only <mysql.h>

Victor Seva authored on 27/12/2018 21:36:20 • Henning Westerholt committed on 27/12/2018 21:36:20
Showing 1 changed files
... ...
@@ -35,7 +35,6 @@
35 35
 #include <string.h>
36 36
 #include <mysql.h>
37 37
 #include <errmsg.h>
38
-#include <mysql_version.h>
39 38
 #include "../../core/mem/mem.h"
40 39
 #include "../../core/dprint.h"
41 40
 #include "../../core/async_task.h"
Browse code

async,db_mysql,db_unixodbc: return -1 and free memory if sending task to the async worker fails

grumvalski authored on 27/09/2018 11:54:01
Showing 1 changed files
... ...
@@ -186,7 +186,10 @@ int db_mysql_submit_query_async(const db1_con_t* _h, const str* _s)
186 186
 	p[1].len = _s->len;
187 187
 	strncpy(p[1].s, _s->s, _s->len);
188 188
 
189
-	async_task_push(atask);
189
+	if (async_task_push(atask)<0) {
190
+		shm_free(atask);
191
+		return -1;
192
+	}
190 193
 
191 194
 	return 0;
192 195
 }
Browse code

db_mysql: doxygen fixes

Henning Westerholt authored on 14/03/2018 20:33:45
Showing 1 changed files
... ...
@@ -541,6 +541,8 @@ int db_mysql_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
541 541
  * \param _k key names
542 542
  * \param _v values of the keys that must match
543 543
  * \param _n number of key=value pairs
544
+ * \param _un unused for db_mysql
545
+ * \param _m unused for db_mysql
544 546
  * \return zero on success, negative value on failure
545 547
  */
546 548
 int db_mysql_replace(const db1_con_t* _h, const db_key_t* _k,
... ...
@@ -584,6 +586,7 @@ int db_mysql_affected_rows(const db1_con_t* _h)
584 586
 /**
585 587
  * Starts a single transaction that will consist of one or more queries (SQL BEGIN)
586 588
  * \param _h database handle
589
+ * \param _l database locking , supports no locking or full locking
587 590
  * \return 0 on success, negative on failure
588 591
  */
589 592
 int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
Browse code

db_mysql: coherent indentation and whitespacing

Daniel-Constantin Mierla authored on 09/11/2017 06:03:02
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-/* 
1
+/*
2 2
  * MySQL module core functions
3 3
  *
4 4
  * Copyright (C) 2001-2003 FhG Fokus
... ...
@@ -16,8 +16,8 @@
16 16
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 17
  * GNU General Public License for more details.
18 18
  *
19
- * You should have received a copy of the GNU General Public License 
20
- * along with this program; if not, write to the Free Software 
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program; if not, write to the Free Software
21 21
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 22
  */
23 23
 
... ...
@@ -68,7 +68,7 @@ static char *mysql_sql_buf;
68 68
  * \return zero on success, negative value on failure
69 69
  */
70 70
 static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
71
-{	
71
+{
72 72
 	time_t t;
73 73
 	int i, code;
74 74
 
... ...
@@ -137,9 +137,9 @@ void db_mysql_async_exec_task(void *param)
137 137
 {
138 138
 	str *p;
139 139
 	db1_con_t* dbc;
140
-	
140
+
141 141
 	p = (str*)param;
142
-	
142
+
143 143
 	dbc = db_mysql_init(&p[0]);
144 144
 
145 145
 	if(dbc==NULL) {
... ...
@@ -148,7 +148,7 @@ void db_mysql_async_exec_task(void *param)
148 148
 	}
149 149
 	if(db_mysql_submit_query(dbc, &p[1])<0) {
150 150
 		LM_ERR("failed to execute query [%.*s] on async worker\n",
151
-		       (p[1].len>100)?100:p[1].len, p[1].s);
151
+				(p[1].len>100)?100:p[1].len, p[1].s);
152 152
 	}
153 153
 	db_mysql_close(dbc);
154 154
 }
... ...
@@ -295,20 +295,20 @@ done:
295 295
  */
296 296
 int db_mysql_free_result(const db1_con_t* _h, db1_res_t* _r)
297 297
 {
298
-     if ((!_h) || (!_r)) {
299
-	     LM_ERR("invalid parameter value\n");
300
-	     return -1;
301
-     }
302
-
303
-     mysql_free_result(RES_RESULT(_r));
304
-     RES_RESULT(_r) = 0;
305
-     pkg_free(RES_PTR(_r));
306
-
307
-     if (db_free_result(_r) < 0) {
308
-	     LM_ERR("unable to free result structure\n");
309
-	     return -1;
310
-     }
311
-     return 0;
298
+	if ((!_h) || (!_r)) {
299
+		LM_ERR("invalid parameter value\n");
300
+		return -1;
301
+	}
302
+
303
+	mysql_free_result(RES_RESULT(_r));
304
+	RES_RESULT(_r) = 0;
305
+	pkg_free(RES_PTR(_r));
306
+
307
+	if (db_free_result(_r) < 0) {
308
+		LM_ERR("unable to free result structure\n");
309
+		return -1;
310
+	}
311
+	return 0;
312 312
 }
313 313
 
314 314
 
... ...
@@ -326,8 +326,8 @@ int db_mysql_free_result(const db1_con_t* _h, db1_res_t* _r)
326 326
  * \return zero on success, negative value on failure
327 327
  */
328 328
 int db_mysql_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
329
-	     const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
330
-	     const db_key_t _o, db1_res_t** _r)
329
+		const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
330
+		const db_key_t _o, db1_res_t** _r)
331 331
 {
332 332
 	return db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
333 333
 	db_mysql_val2str, db_mysql_submit_query, db_mysql_store_result);
... ...
@@ -526,8 +526,8 @@ int db_mysql_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
526 526
  * \param _un number of columns to update
527 527
  * \return zero on success, negative value on failure
528 528
  */
529
-int db_mysql_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o, 
530
-	const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n, 
529
+int db_mysql_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
530
+	const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
531 531
 	const int _un)
532 532
 {
533 533
 	return db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un, db_mysql_val2str,
... ...
@@ -716,8 +716,8 @@ int db_mysql_end_transaction(db1_con_t* _h)
716 716
 	}
717 717
 
718 718
 	/* Only _end_ the transaction after the raw_query.  That way, if the
719
- 	   raw_query fails, and the calling module does an abort_transaction()
720
-	   to clean-up, a ROLLBACK will be sent to the DB. */
719
+	 * raw_query fails, and the calling module does an abort_transaction()
720
+	 * to clean-up, a ROLLBACK will be sent to the DB. */
721 721
 	CON_TRANSACTION(_h) = 0;
722 722
 
723 723
 	if(db_mysql_unlock_tables(_h)<0)
... ...
@@ -749,7 +749,7 @@ int db_mysql_abort_transaction(db1_con_t* _h)
749 749
 	}
750 750
 
751 751
 	/* Whether the rollback succeeds or not we need to _end_ the
752
- 	   transaction now or all future starts will fail */
752
+	 * transaction now or all future starts will fail */
753 753
 	CON_TRANSACTION(_h) = 0;
754 754
 
755 755
 	if (db_mysql_raw_query(_h, &rollback_query_str, NULL) < 0)
... ...
@@ -775,23 +775,23 @@ done:
775 775
 
776 776
 
777 777
 /**
778
-  * Insert a row into a specified table, update on duplicate key.
779
-  * \param _h structure representing database connection
780
-  * \param _k key names
781
-  * \param _v values of the keys
782
-  * \param _n number of key=value pairs
778
+ * Insert a row into a specified table, update on duplicate key.
779
+ * \param _h structure representing database connection
780
+ * \param _k key names
781
+ * \param _v values of the keys
782
+ * \param _n number of key=value pairs
783 783
  */
784
- int db_mysql_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
784
+int db_mysql_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
785 785
 	const int _n)
786
- {
786
+{
787 787
 	int off, ret;
788 788
 	static str  sql_str;
789
- 
789
+
790 790
 	if ((!_h) || (!_k) || (!_v) || (!_n)) {
791 791
 		LM_ERR("invalid parameter value\n");
792 792
 		return -1;
793 793
 	}
794
- 
794
+
795 795
 	ret = snprintf(mysql_sql_buf, sql_buffer_size, "insert into %s%.*s%s (",
796 796
 			CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h));
797 797
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
... ...
@@ -809,18 +809,18 @@ done:
809 809
 	off += ret;
810 810
 
811 811
 	*(mysql_sql_buf + off++) = ')';
812
-	
812
+
813 813
 	ret = snprintf(mysql_sql_buf + off, sql_buffer_size - off, " on duplicate key update ");
814 814
 	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
815 815
 	off += ret;
816
-	
816
+
817 817
 	ret = db_print_set(_h, mysql_sql_buf + off, sql_buffer_size - off, _k, _v, _n, db_mysql_val2str);
818 818
 	if (ret < 0) return -1;
819 819
 	off += ret;
820
-	
820
+
821 821
 	sql_str.s = mysql_sql_buf;
822 822
 	sql_str.len = off;
823
- 
823
+
824 824
 	if (db_mysql_submit_query(_h, &sql_str) < 0) {
825 825
 		LM_ERR("error while submitting query\n");
826 826
 		return -2;
... ...
@@ -841,7 +841,8 @@ error:
841 841
  * \param _n number of key=value pairs
842 842
  * \return zero on success, negative value on failure
843 843
  */
844
-int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
844
+int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k,
845
+		const db_val_t* _v, const int _n)
845 846
 {
846 847
 	return db_do_insert_delayed(_h, _k, _v, _n, db_mysql_val2str,
847 848
 	db_mysql_submit_query);
... ...
@@ -855,7 +856,8 @@ int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_va
855 856
  * \param _n number of key=value pairs
856 857
  * \return zero on success, negative value on failure
857 858
  */
858
-int db_mysql_insert_async(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
859
+int db_mysql_insert_async(const db1_con_t* _h, const db_key_t* _k,
860
+		const db_val_t* _v, const int _n)
859 861
 {
860 862
 	return db_do_insert(_h, _k, _v, _n, db_mysql_val2str,
861 863
 	db_mysql_submit_query_async);
... ...
@@ -882,15 +884,15 @@ int db_mysql_use_table(db1_con_t* _h, const str* _t)
882 884
  */
883 885
 int db_mysql_alloc_buffer(void)
884 886
 {
885
-    if (db_api_init())
886
-    {
887
-        LM_ERR("Failed to initialise db api\n");
887
+	if (db_api_init())
888
+	{
889
+		LM_ERR("Failed to initialise db api\n");
888 890
 		return -1;
889
-    }
891
+	}
890 892
 
891
-    mysql_sql_buf = (char*)malloc(sql_buffer_size);
892
-    if (mysql_sql_buf == NULL)
893
-        return -1;
894
-    else
895
-        return 0;
893
+	mysql_sql_buf = (char*)malloc(sql_buffer_size);
894
+	if (mysql_sql_buf == NULL)
895
+		return -1;
896
+	else
897
+		return 0;
896 898
 }
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
... ...
@@ -36,9 +36,9 @@
36 36
 #include <mysql.h>
37 37
 #include <errmsg.h>
38 38
 #include <mysql_version.h>
39
-#include "../../mem/mem.h"
40
-#include "../../dprint.h"
41
-#include "../../async_task.h"
39
+#include "../../core/mem/mem.h"
40
+#include "../../core/dprint.h"
41
+#include "../../core/async_task.h"
42 42
 #include "../../lib/srdb1/db_query.h"
43 43
 #include "../../lib/srdb1/db_ut.h"
44 44
 #include "db_mysql.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,896 @@
1
+/* 
2
+ * MySQL module core functions
3
+ *
4
+ * Copyright (C) 2001-2003 FhG Fokus
5
+ * Copyright (C) 2007-2008 1&1 Internet AG
6
+ *
7
+ * This file is part of Kamailio, a free SIP server.
8
+ *
9
+ * Kamailio is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * Kamailio is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License 
20
+ * along with this program; if not, write to the Free Software 
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
+ */
23
+
24
+/*!
25
+ * \file
26
+ * \brief Implementation of core functions for the MySQL driver.
27
+ *
28
+ * This file contains the implementation of core functions for the MySQL
29
+ * database driver, for example to submit a query or fetch a result.
30
+ * \ingroup db_mysql
31
+ *  Module: \ref db_mysql
32
+ */
33
+
34
+#include <stdio.h>
35
+#include <string.h>
36
+#include <mysql.h>
37
+#include <errmsg.h>
38
+#include <mysql_version.h>
39
+#include "../../mem/mem.h"
40
+#include "../../dprint.h"
41
+#include "../../async_task.h"
42
+#include "../../lib/srdb1/db_query.h"
43
+#include "../../lib/srdb1/db_ut.h"
44
+#include "db_mysql.h"
45
+#include "km_val.h"
46
+#include "km_my_con.h"
47
+#include "km_res.h"
48
+#include "km_row.h"
49
+#include "km_db_mysql.h"
50
+#include "km_dbase.h"
51
+
52
+static char *mysql_sql_buf;
53
+
54
+
55
+/**
56
+ * \brief Send a SQL query to the server.
57
+ *
58
+ * Send a SQL query to the database server. This methods tries to reconnect
59
+ * to the server if the connection is gone and the auto_reconnect parameter is
60
+ * enabled. It also issues a mysql_ping before the query to connect again after
61
+ * a long waiting period because for some older mysql versions the auto reconnect
62
+ * don't work sufficient. If auto_reconnect is enabled and the server supports it,
63
+ * then the mysql_ping is probably not necessary, but its safer to do it in this
64
+ * cases too.
65
+ *
66
+ * \param _h handle for the db
67
+ * \param _s executed query
68
+ * \return zero on success, negative value on failure
69
+ */
70
+static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
71
+{	
72
+	time_t t;
73
+	int i, code;
74
+
75
+	if (!_h || !_s || !_s->s) {
76
+		LM_ERR("invalid parameter value\n");
77
+		return -1;
78
+	}
79
+
80
+	if (my_ping_interval) {
81
+		t = time(0);
82
+		if ((t - CON_TIMESTAMP(_h)) > my_ping_interval) {
83
+			for (i=0; i < (db_mysql_auto_reconnect ? 3 : 1); i++) {
84
+				if (mysql_ping(CON_CONNECTION(_h))) {
85
+					LM_INFO("driver error on ping: %s\n", mysql_error(CON_CONNECTION(_h)));
86
+					counter_inc(mysql_cnts_h.driver_err);
87
+				} else {
88
+					break;
89
+				}
90
+			}
91
+		}
92
+		/*
93
+		 * We're doing later a query anyway that will reset the timout of the server,
94
+		 * so it makes sense to set the timestamp value to the actual time in order
95
+		 * to prevent unnecessary pings.
96
+		 */
97
+		CON_TIMESTAMP(_h) = t;
98
+	}
99
+
100
+	/* screws up the terminal when the query contains a BLOB :-( (by bogdan)
101
+	 * LM_DBG("submit_query(): %.*s\n", _s->len, _s->s);
102
+	 */
103
+
104
+	/* When a server connection is lost and a query is attempted, most of
105
+	 * the time the query will return a CR_SERVER_LOST, then at the second
106
+	 * attempt to execute it, the mysql lib will reconnect and succeed.
107
+	 * However is a few cases, the first attempt returns CR_SERVER_GONE_ERROR
108
+	 * the second CR_SERVER_LOST and only the third succeeds.
109
+	 * Thus the 3 in the loop count. Increasing the loop count over this
110
+	 * value shouldn't be needed, but it doesn't hurt either, since the loop
111
+	 * will most of the time stop at the second or sometimes at the third
112
+	 * iteration. In the case of CR_SERVER_GONE_ERROR and CR_SERVER_LOST the
113
+	 * driver error counter is increased
114
+	 */
115
+	for (i=0; i < (db_mysql_auto_reconnect ? 3 : 1); i++) {
116
+		if (mysql_real_query(CON_CONNECTION(_h), _s->s, _s->len) == 0) {
117
+			return 0;
118
+		}
119
+		code = mysql_errno(CON_CONNECTION(_h));
120
+		if (code!=CR_SERVER_GONE_ERROR && code!=CR_SERVER_LOST
121
+				&& code!=CR_SSL_CONNECTION_ERROR && code!=CR_CONNECTION_ERROR
122
+				&& code!=CR_CONN_HOST_ERROR && code!=CR_SERVER_LOST_EXTENDED) {
123
+			break;
124
+		}
125
+		counter_inc(mysql_cnts_h.driver_err);
126
+	}
127
+	LM_ERR("driver error on query: %s (%d)\n", mysql_error(CON_CONNECTION(_h)),
128
+			mysql_errno(CON_CONNECTION(_h)));
129
+	return -2;
130
+}
131
+
132
+
133
+/**
134
+ *
135
+ */
136
+void db_mysql_async_exec_task(void *param)
137
+{
138
+	str *p;
139
+	db1_con_t* dbc;
140
+	
141
+	p = (str*)param;
142
+	
143
+	dbc = db_mysql_init(&p[0]);
144
+
145
+	if(dbc==NULL) {
146
+		LM_ERR("failed to open connection for [%.*s]\n", p[0].len, p[0].s);
147
+		return;
148
+	}
149
+	if(db_mysql_submit_query(dbc, &p[1])<0) {
150
+		LM_ERR("failed to execute query [%.*s] on async worker\n",
151
+		       (p[1].len>100)?100:p[1].len, p[1].s);
152
+	}
153
+	db_mysql_close(dbc);
154
+}
155
+
156
+/**
157
+ * Execute a raw SQL query via core async framework.
158
+ * \param _h handle for the database
159
+ * \param _s raw query string
160
+ * \return zero on success, negative value on failure
161
+ */
162
+int db_mysql_submit_query_async(const db1_con_t* _h, const str* _s)
163
+{
164
+	struct db_id* di;
165
+	async_task_t *atask;
166
+	int asize;
167
+	str *p;
168
+
169
+	di = ((struct pool_con*)_h->tail)->id;
170
+
171
+	asize = sizeof(async_task_t) + 2*sizeof(str) + di->url.len + _s->len + 2;
172
+	atask = shm_malloc(asize);
173
+	if(atask==NULL) {
174
+		LM_ERR("no more shared memory to allocate %d\n", asize);
175
+		return -1;
176
+	}
177
+
178
+	atask->exec = db_mysql_async_exec_task;
179
+	atask->param = (char*)atask + sizeof(async_task_t);
180
+
181
+	p = (str*)((char*)atask + sizeof(async_task_t));
182
+	p[0].s = (char*)p + 2*sizeof(str);
183
+	p[0].len = di->url.len;
184
+	strncpy(p[0].s, di->url.s, di->url.len);
185
+	p[1].s = p[0].s + p[0].len + 1;
186
+	p[1].len = _s->len;
187
+	strncpy(p[1].s, _s->s, _s->len);
188
+
189
+	async_task_push(atask);
190
+
191
+	return 0;
192
+}
193
+
194
+static char *db_mysql_tquote = "`";
195
+/**
196
+ * Initialize the database module.
197
+ * No function should be called before this
198
+ * \param _url URL used for initialization
199
+ * \return zero on success, negative value on failure
200
+ */
201
+db1_con_t* db_mysql_init(const str* _url)
202
+{
203
+	db1_con_t *c;
204
+	c = db_do_init(_url, (void *)db_mysql_new_connection);
205
+	if(c) CON_TQUOTE(c) = db_mysql_tquote;
206
+	return c;
207
+}
208
+
209
+
210
+/**
211
+ * Shut down the database module.
212
+ * No function should be called after this
213
+ * \param _h handle to the closed connection
214
+ * \return zero on success, negative value on failure
215
+ */
216
+void db_mysql_close(db1_con_t* _h)
217
+{
218
+	db_do_close(_h, db_mysql_free_connection);
219
+}
220
+
221
+
222
+/**
223
+ * Retrieve a result set
224
+ * \param _h handle to the database
225
+ * \param _r result set that should be retrieved
226
+ * \return zero on success, negative value on failure
227
+ */
228
+static int db_mysql_store_result(const db1_con_t* _h, db1_res_t** _r)
229
+{
230
+	int code;
231
+	if ((!_h) || (!_r)) {
232
+		LM_ERR("invalid parameter value\n");
233
+		return -1;
234
+	}
235
+
236
+	*_r = db_mysql_new_result();
237
+	if (*_r == 0) {
238
+		LM_ERR("no memory left\n");
239
+		return -2;
240
+	}
241
+
242
+	RES_RESULT(*_r) = mysql_store_result(CON_CONNECTION(_h));
243
+	if (!RES_RESULT(*_r)) {
244
+		if (mysql_field_count(CON_CONNECTION(_h)) == 0) {
245
+			(*_r)->col.n = 0;
246
+			(*_r)->n = 0;
247
+			goto done;
248
+		} else {
249
+			LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
250
+			code = mysql_errno(CON_CONNECTION(_h));
251
+			if (code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
252
+				counter_inc(mysql_cnts_h.driver_err);
253
+			}
254
+			db_mysql_free_result(_h, *_r);
255
+			*_r = 0;
256
+			return -3;
257
+		}
258
+	}
259
+
260
+	if (db_mysql_convert_result(_h, *_r) < 0) {
261
+		LM_ERR("error while converting result\n");
262
+		LM_DBG("freeing result set at %p\n", _r);
263
+		/* all mem on Kamailio API side is already freed by
264
+		 * db_mysql_convert_result in case of error, but we also need
265
+		 * to free the mem from the mysql lib side, internal pkg for it
266
+		 * and *_r */
267
+		db_mysql_free_result(_h, *_r);
268
+		*_r = 0;
269
+#if (MYSQL_VERSION_ID >= 40100)
270
+		while( mysql_more_results(CON_CONNECTION(_h)) && mysql_next_result(CON_CONNECTION(_h)) == 0 ) {
271
+			MYSQL_RES *res = mysql_store_result( CON_CONNECTION(_h) );
272
+			mysql_free_result(res);
273
+		}
274
+#endif
275
+		return -4;
276
+	}
277
+
278
+done:
279
+#if (MYSQL_VERSION_ID >= 40100)
280
+	while( mysql_more_results(CON_CONNECTION(_h)) && mysql_next_result(CON_CONNECTION(_h)) == 0 ) {
281
+		MYSQL_RES *res = mysql_store_result( CON_CONNECTION(_h) );
282
+		mysql_free_result(res);
283
+	}
284
+#endif
285
+
286
+	return 0;
287
+}
288
+
289
+
290
+/**
291
+ * Release a result set from memory.
292
+ * \param _h handle to the database
293
+ * \param _r result set that should be freed
294
+ * \return zero on success, negative value on failure
295
+ */
296
+int db_mysql_free_result(const db1_con_t* _h, db1_res_t* _r)
297
+{
298
+     if ((!_h) || (!_r)) {
299
+	     LM_ERR("invalid parameter value\n");
300
+	     return -1;
301
+     }
302
+
303
+     mysql_free_result(RES_RESULT(_r));
304
+     RES_RESULT(_r) = 0;
305
+     pkg_free(RES_PTR(_r));
306
+
307
+     if (db_free_result(_r) < 0) {
308
+	     LM_ERR("unable to free result structure\n");
309
+	     return -1;
310
+     }
311
+     return 0;
312
+}
313
+
314
+
315
+/**
316
+ * Query a table for specified rows.
317
+ * \param _h structure representing database connection
318
+ * \param _k key names
319
+ * \param _op operators
320
+ *\param  _v values of the keys that must match
321
+ * \param _c column names to return
322
+ * \param _n number of key=values pairs to compare
323
+ * \param _nc number of columns to return
324
+ * \param _o order by the specified column
325
+ * \param _r pointer to a structure representing the result
326
+ * \return zero on success, negative value on failure
327
+ */
328
+int db_mysql_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
329
+	     const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
330
+	     const db_key_t _o, db1_res_t** _r)
331
+{
332
+	return db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
333
+	db_mysql_val2str, db_mysql_submit_query, db_mysql_store_result);
334
+}
335
+
336
+/**
337
+ * \brief Gets a partial result set, fetch rows from a result
338
+ *
339
+ * Gets a partial result set, fetch a number of rows from a database result.
340
+ * This function initialize the given result structure on the first run, and
341
+ * fetches the nrows number of rows. On subsequenting runs, it uses the
342
+ * existing result and fetches more rows, until it reaches the end of the
343
+ * result set. Because of this the result needs to be null in the first
344
+ * invocation of the function. If the number of wanted rows is zero, the
345
+ * function returns anything with a result of zero.
346
+ * \param _h structure representing the database connection
347
+ * \param _r pointer to a structure representing the result
348
+ * \param nrows number of fetched rows
349
+ * \return zero on success, negative value on failure
350
+ */
351
+int db_mysql_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows)
352
+{
353
+	int rows, i, code;
354
+
355
+	if (!_h || !_r || nrows < 0) {
356
+		LM_ERR("Invalid parameter value\n");
357
+		return -1;
358
+	}
359
+
360
+	/* exit if the fetch count is zero */
361
+	if (nrows == 0) {
362
+		db_mysql_free_result(_h, *_r);
363
+		*_r = 0;
364
+		return 0;
365
+	}
366
+
367
+	if(*_r==0) {
368
+		/* Allocate a new result structure */
369
+		*_r = db_mysql_new_result();
370
+		if (*_r == 0) {
371
+			LM_ERR("no memory left\n");
372
+			return -2;
373
+		}
374
+
375
+		RES_RESULT(*_r) = mysql_store_result(CON_CONNECTION(_h));
376
+		if (!RES_RESULT(*_r)) {
377
+			if (mysql_field_count(CON_CONNECTION(_h)) == 0) {
378
+				(*_r)->col.n = 0;
379
+				(*_r)->n = 0;
380
+				return 0;
381
+			} else {
382
+				LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
383
+				code = mysql_errno(CON_CONNECTION(_h));
384
+				if (code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
385
+					counter_inc(mysql_cnts_h.driver_err);
386
+				}
387
+				db_mysql_free_result(_h, *_r);
388
+				*_r = 0;
389
+				return -3;
390
+			}
391
+		}
392
+		if (db_mysql_get_columns(_h, *_r) < 0) {
393
+			LM_ERR("error while getting column names\n");
394
+			return -4;
395
+		}
396
+
397
+		RES_NUM_ROWS(*_r) = mysql_num_rows(RES_RESULT(*_r));
398
+		if (!RES_NUM_ROWS(*_r)) {
399
+			LM_DBG("no rows returned from the query\n");
400
+			RES_ROWS(*_r) = 0;
401
+			return 0;
402
+		}
403
+
404
+	} else {
405
+		/* free old rows */
406
+		if(RES_ROWS(*_r)!=0)
407
+			db_free_rows(*_r);
408
+		RES_ROWS(*_r) = 0;
409
+		RES_ROW_N(*_r) = 0;
410
+	}
411
+
412
+	/* determine the number of rows remaining to be processed */
413
+	rows = RES_NUM_ROWS(*_r) - RES_LAST_ROW(*_r);
414
+
415
+	/* If there aren't any more rows left to process, exit */
416
+	if(rows<=0)
417
+		return 0;
418
+
419
+	/* if the fetch count is less than the remaining rows to process                 */
420
+	/* set the number of rows to process (during this call) equal to the fetch count */
421
+	if(nrows < rows)
422
+		rows = nrows;
423
+
424
+	RES_ROW_N(*_r) = rows;
425
+
426
+	LM_DBG("converting row %d of %d count %d\n", RES_LAST_ROW(*_r),
427
+			RES_NUM_ROWS(*_r), RES_ROW_N(*_r));
428
+
429
+	RES_ROWS(*_r) = (struct db_row*)pkg_malloc(sizeof(db_row_t) * rows);
430
+	if (!RES_ROWS(*_r)) {
431
+		LM_ERR("no memory left\n");
432
+		return -5;
433
+	}
434
+
435
+	for(i = 0; i < rows; i++) {
436
+		RES_ROW(*_r) = mysql_fetch_row(RES_RESULT(*_r));
437
+		if (!RES_ROW(*_r)) {
438
+			LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
439
+			RES_ROW_N(*_r) = i;
440
+			db_free_rows(*_r);
441
+			return -6;
442
+		}
443
+		if (db_mysql_convert_row(_h, *_r, &(RES_ROWS(*_r)[i])) < 0) {
444
+			LM_ERR("error while converting row #%d\n", i);
445
+			RES_ROW_N(*_r) = i;
446
+			db_free_rows(*_r);
447
+			return -7;
448
+		}
449
+	}
450
+
451
+	/* update the total number of rows processed */
452
+	RES_LAST_ROW(*_r) += rows;
453
+	return 0;
454
+}
455
+
456
+/**
457
+ * Execute a raw SQL query.
458
+ * \param _h handle for the database
459
+ * \param _s raw query string
460
+ * \param _r result set for storage
461
+ * \return zero on success, negative value on failure
462
+ */
463
+int db_mysql_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
464
+{
465
+	return db_do_raw_query(_h, _s, _r, db_mysql_submit_query,
466
+	db_mysql_store_result);
467
+}
468
+
469
+/**
470
+ * Execute a raw SQL query via core async framework.
471
+ * \param _h handle for the database
472
+ * \param _s raw query string
473
+ * \return zero on success, negative value on failure
474
+ */
475
+int db_mysql_raw_query_async(const db1_con_t* _h, const str* _s)
476
+{
477
+	return db_mysql_submit_query_async(_h, _s);
478
+}
479
+
480
+/**
481
+ * Insert a row into a specified table.
482
+ * \param _h structure representing database connection
483
+ * \param _k key names
484
+ * \param _v values of the keys
485
+ * \param _n number of key=value pairs
486
+ * \return zero on success, negative value on failure
487
+ */
488
+int db_mysql_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
489
+		const int _n)
490
+{
491
+	if(unlikely(db_mysql_insert_all_delayed==1))
492
+		return db_do_insert_delayed(_h, _k, _v, _n, db_mysql_val2str,
493
+				db_mysql_submit_query);
494
+	else
495
+		return db_do_insert(_h, _k, _v, _n, db_mysql_val2str,
496
+				db_mysql_submit_query);
497
+}
498
+
499
+
500
+/**
501
+ * Delete a row from the specified table
502
+ * \param _h structure representing database connection
503
+ * \param _k key names
504
+ * \param _o operators
505
+ * \param _v values of the keys that must match
506
+ * \param _n number of key=value pairs
507
+ * \return zero on success, negative value on failure
508
+ */
509
+int db_mysql_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
510
+	const db_val_t* _v, const int _n)
511
+{
512
+	return db_do_delete(_h, _k, _o, _v, _n, db_mysql_val2str,
513
+	db_mysql_submit_query);
514
+}
515
+
516
+
517
+/**
518
+ * Update some rows in the specified table
519
+ * \param _h structure representing database connection
520
+ * \param _k key names
521
+ * \param _o operators
522
+ * \param _v values of the keys that must match
523
+ * \param _uk updated columns
524
+ * \param _uv updated values of the columns
525
+ * \param _n number of key=value pairs
526
+ * \param _un number of columns to update
527
+ * \return zero on success, negative value on failure
528
+ */
529
+int db_mysql_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o, 
530
+	const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n, 
531
+	const int _un)
532
+{
533
+	return db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un, db_mysql_val2str,
534
+	db_mysql_submit_query);
535
+}
536
+
537
+
538
+/**
539
+ * Just like insert, but replace the row if it exists.
540
+ * \param _h database handle
541
+ * \param _k key names
542
+ * \param _v values of the keys that must match
543
+ * \param _n number of key=value pairs
544
+ * \return zero on success, negative value on failure
545
+ */
546
+int db_mysql_replace(const db1_con_t* _h, const db_key_t* _k,
547
+		const db_val_t* _v, const int _n, const int _un, const int _m)
548
+{
549
+	return db_do_replace(_h, _k, _v, _n, db_mysql_val2str,
550
+	db_mysql_submit_query);
551
+}
552
+
553
+
554
+/**
555
+ * Returns the last inserted ID.
556
+ * \param _h database handle
557
+ * \return returns the ID as integer or returns 0 if the previous statement