Browse code

core, srdb1, modules/db_*, modules_k/db_*: Added support for configuring SQL buffer size and mediumblob/mediumtext in MySQL

- Currently the SQL buffer size is hard-coded to 64k. This isn't
enough in some cases. For example, it is easily possible for
resource lists (which can be Network Address Books) to be larger
than 64k.
- The default buffer size is still 64k, but this can be overriden
with the new "sql_buffer_size" configuration option.
- Support added for mediumblob/mediumtext in MySQL as the blob/text
types that are currently supported are limited to 64k.
- Feature added by Andrew Miller at Crocodile RCS

pd authored on 27/07/2011 10:50:20
Showing 21 changed files
... ...
@@ -383,6 +383,7 @@ DST_BLST_SCTP_IMASK	dst_blacklist_sctp_imask
383 383
 PORT	port
384 384
 STAT	statistics
385 385
 MAXBUFFER maxbuffer
386
+SQL_BUFFER_SIZE sql_buffer_size
386 387
 CHILDREN children
387 388
 CHECK_VIA	check_via
388 389
 PHONE2TEL	phone2tel
... ...
@@ -765,6 +766,7 @@ IMPORTFILE      "import_file"
765 765
 <INITIAL>{PORT}	{ count(); yylval.strval=yytext; return PORT; }
766 766
 <INITIAL>{STAT}	{ count(); yylval.strval=yytext; return STAT; }
767 767
 <INITIAL>{MAXBUFFER}	{ count(); yylval.strval=yytext; return MAXBUFFER; }
768
+<INITIAL>{SQL_BUFFER_SIZE}	{ count(); yylval.strval=yytext; return SQL_BUFFER_SIZE; }
768 769
 <INITIAL>{CHILDREN}	{ count(); yylval.strval=yytext; return CHILDREN; }
769 770
 <INITIAL>{CHECK_VIA}	{ count(); yylval.strval=yytext; return CHECK_VIA; }
770 771
 <INITIAL>{PHONE2TEL}	{ count(); yylval.strval=yytext; return PHONE2TEL; }
... ...
@@ -447,6 +447,7 @@ extern char *finame;
447 447
 %token LOADPATH
448 448
 %token MODPARAM
449 449
 %token MAXBUFFER
450
+%token SQL_BUFFER_SIZE
450 451
 %token USER
451 452
 %token GROUP
452 453
 %token CHROOT
... ...
@@ -914,6 +915,8 @@ assign_stm:
914 914
 	}
915 915
 	| MAXBUFFER EQUAL NUMBER { maxbuffer=$3; }
916 916
 	| MAXBUFFER EQUAL error { yyerror("number expected"); }
917
+    | SQL_BUFFER_SIZE EQUAL NUMBER { sql_buffer_size=$3; }
918
+	| SQL_BUFFER_SIZE EQUAL error { yyerror("number expected"); }
917 919
 	| PORT EQUAL error    { yyerror("number expected"); }
918 920
 	| CHILDREN EQUAL NUMBER { children_no=$3; }
919 921
 	| CHILDREN EQUAL error { yyerror("number expected"); }
... ...
@@ -520,4 +520,16 @@
520 520
     </para>
521 521
 </section>
522 522
 
523
+<section id="coresql_buffer_size">
524
+    <title>core.sql_buffer_size</title>
525
+    <para>
526
+        Size for the SQL buffer.
527
+        This will limit the size of objects that can be stored in an SQL database.
528
+    </para>
529
+    <para>Default value: 65535.</para>
530
+    <para>Type: integer.</para>
531
+    <para>
532
+    </para>
533
+</section>
534
+
523 535
 </chapter>
... ...
@@ -166,7 +166,9 @@
166 166
 	    </xsl:when>
167 167
 	    <xsl:when test="$type='string' or
168 168
 						$type='text' or
169
-						$type='binary'">
169
+						$type='binary' or 
170
+                        $type='largetext' or
171
+						$type='largebinary'">
170 172
 		<xsl:text>str</xsl:text>
171 173
 	    </xsl:when>
172 174
 	    <xsl:otherwise>
... ...
@@ -85,12 +85,14 @@
85 85
 		<xsl:call-template name="column.size"/>
86 86
 		<xsl:call-template name="column.trailing"/>
87 87
 	    </xsl:when>
88
-	    <xsl:when test="$type='binary'">
88
+	    <xsl:when test="$type='binary' or
89
+						$type='largebinary'">
89 90
 		<xsl:text>BYTEA</xsl:text>
90 91
 		<xsl:call-template name="column.size"/>
91 92
 		<xsl:call-template name="column.trailing"/>
92 93
 	    </xsl:when>
93
-	    <xsl:when test="$type='text'">
94
+	    <xsl:when test="$type='text'or
95
+						$type='largetext'">
94 96
 		<xsl:text>TEXT</xsl:text>
95 97
 		<xsl:call-template name="column.size"/>
96 98
 		<xsl:call-template name="column.trailing"/>
... ...
@@ -90,7 +90,9 @@
90 90
 	    </xsl:when>
91 91
 	    <xsl:when test="$type='string' or
92 92
 						$type='text' or
93
-						$type='binary'">
93
+						$type='binary' or 
94
+                        $type='largetext' or
95
+						$type='largebinary'">
94 96
 		<xsl:text>string</xsl:text>
95 97
 	    </xsl:when>
96 98
 	    <xsl:otherwise>
... ...
@@ -103,11 +103,23 @@
103 103
 		<xsl:call-template name="column.size"/>
104 104
 		<xsl:call-template name="column.trailing"/>
105 105
 	    </xsl:when>
106
+        <xsl:when test="$type='largebinary'">
107
+        <!-- In MySQL  MEDIUMBLOB <= 16MB -->
108
+		<xsl:text>MEDIUMBLOB</xsl:text>
109
+		<xsl:call-template name="column.size"/>
110
+		<xsl:call-template name="column.trailing"/>
111
+	    </xsl:when>
106 112
 	    <xsl:when test="$type='text'">
107 113
 		<xsl:text>TEXT</xsl:text>
108 114
 		<xsl:call-template name="column.size"/>
109 115
 		<xsl:call-template name="column.trailing"/>
110 116
 	    </xsl:when>
117
+	    <xsl:when test="$type='largetext'">
118
+        <!-- In MySQL  MEDIUMTEXT <= 16MB -->
119
+		<xsl:text>MEDIUMTEXT</xsl:text>
120
+		<xsl:call-template name="column.size"/>
121
+		<xsl:call-template name="column.trailing"/>
122
+	    </xsl:when>
111 123
 	    <xsl:otherwise>
112 124
 		<xsl:call-template name="type-error"/>
113 125
 	    </xsl:otherwise>
... ...
@@ -114,12 +114,14 @@
114 114
 		<xsl:call-template name="column.size"/>
115 115
 		<xsl:call-template name="column.trailing"/>
116 116
 	    </xsl:when>
117
-	    <xsl:when test="$type='binary'">
117
+	    <xsl:when test="$type='binary' or 
118
+						$type='largebinary'">
118 119
 		<xsl:text>BLOB</xsl:text>
119 120
 		<xsl:call-template name="column.size"/>
120 121
 		<xsl:call-template name="column.trailing"/>
121 122
 	    </xsl:when>
122
-	    <xsl:when test="$type='text'">
123
+	    <xsl:when test="$type='text' or 
124
+                        $type='largetext'">
123 125
 		<xsl:text>CLOB</xsl:text>
124 126
 		<xsl:call-template name="column.size"/>
125 127
 		<xsl:call-template name="column.trailing"/>
... ...
@@ -87,12 +87,14 @@
87 87
 		<xsl:call-template name="column.size"/>
88 88
 		<xsl:call-template name="column.trailing"/>
89 89
 	    </xsl:when>
90
-	    <xsl:when test="$type='binary'">
90
+	    <xsl:when test="$type='binary' or 
91
+						$type='largebinary'">
91 92
 		<xsl:text>BYTEA</xsl:text>
92 93
 		<xsl:call-template name="column.size"/>
93 94
 		<xsl:call-template name="column.trailing"/>
94 95
 	    </xsl:when>
95
-	    <xsl:when test="$type='text'">
96
+	    <xsl:when test="$type='text' or 
97
+                        $type='largetext'">
96 98
 		<xsl:text>TEXT</xsl:text>
97 99
 		<xsl:call-template name="column.size"/>
98 100
 		<xsl:call-template name="column.trailing"/>
... ...
@@ -86,6 +86,7 @@ extern struct socket_info* sendipv6_sctp; /* same as above for ipv6 */
86 86
 #endif
87 87
 
88 88
 extern unsigned int maxbuffer;
89
+extern unsigned int sql_buffer_size;
89 90
 extern int children_no;
90 91
 #ifdef USE_TCP
91 92
 extern int tcp_main_pid;
... ...
@@ -36,9 +36,10 @@
36 36
 #include "../../dprint.h"
37 37
 #include "db_ut.h"
38 38
 #include "db_query.h"
39
+#include "../../globals.h"
39 40
 
40 41
 static str  sql_str;
41
-static char sql_buf[SQL_BUF_LEN];
42
+static char *sql_buf = NULL;
42 43
 
43 44
 int db_do_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
44 45
 	const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
... ...
@@ -54,35 +55,35 @@ int db_do_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
54 54
 	}
55 55
 
56 56
 	if (!_c) {
57
-		ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
58
-		if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
57
+		ret = snprintf(sql_buf, sql_buffer_size, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
58
+		if (ret < 0 || ret >= sql_buffer_size) goto error;
59 59
 		off = ret;
60 60
 	} else {
61
-		ret = snprintf(sql_buf, SQL_BUF_LEN, "select ");
62
-		if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
61
+		ret = snprintf(sql_buf, sql_buffer_size, "select ");
62
+		if (ret < 0 || ret >= sql_buffer_size) goto error;
63 63
 		off = ret;
64 64
 
65
-		ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc);
65
+		ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _c, _nc);
66 66
 		if (ret < 0) return -1;
67 67
 		off += ret;
68 68
 
69
-		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
70
-		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
69
+		ret = snprintf(sql_buf + off, sql_buffer_size - off, "from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
70
+		if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
71 71
 		off += ret;
72 72
 	}
73 73
 	if (_n) {
74
-		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "where ");
75
-		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
74
+		ret = snprintf(sql_buf + off, sql_buffer_size - off, "where ");
75
+		if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
76 76
 		off += ret;
77 77
 
78 78
 		ret = db_print_where(_h, sql_buf + off,
79
-				SQL_BUF_LEN - off, _k, _op, _v, _n, val2str);
79
+				sql_buffer_size - off, _k, _op, _v, _n, val2str);
80 80
 		if (ret < 0) return -1;;
81 81
 		off += ret;
82 82
 	}
83 83
 	if (_o) {
84
-		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " order by %.*s", _o->len, _o->s);
85
-		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
84
+		ret = snprintf(sql_buf + off, sql_buffer_size - off, " order by %.*s", _o->len, _o->s);
85
+		if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
86 86
 		off += ret;
87 87
 	}
88 88
 	/*
... ...
@@ -90,9 +91,9 @@ int db_do_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
90 90
 	 * don't support a length parameter, so they need this for the correct
91 91
 	 * function of strlen. This zero is not included in the 'str' length.
92 92
 	 * We need to check the length here, otherwise we could overwrite the buffer
93
-	 * boundaries if off is equal to SQL_BUF_LEN.
93
+	 * boundaries if off is equal to sql_buffer_size.
94 94
 	 */
95
-	if (off + 1 >= SQL_BUF_LEN) goto error;
95
+	if (off + 1 >= sql_buffer_size) goto error;
96 96
 	sql_buf[off + 1] = '\0';
97 97
 	sql_str.s = sql_buf;
98 98
 	sql_str.len = off;
... ...
@@ -153,23 +154,23 @@ int db_do_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
153 153
 		return -1;
154 154
 	}
155 155
 
156
-	ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
157
-	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
156
+	ret = snprintf(sql_buf, sql_buffer_size, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
157
+	if (ret < 0 || ret >= sql_buffer_size) goto error;
158 158
 	off = ret;
159 159
 
160
-	ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n);
160
+	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n);
161 161
 	if (ret < 0) return -1;
162 162
 	off += ret;
163 163
 
164
-	ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");
165
-	if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
164
+	ret = snprintf(sql_buf + off, sql_buffer_size - off, ") values (");
165
+	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
166 166
 	off += ret;
167 167
 
168
-	ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, val2str);
168
+	ret = db_print_values(_h, sql_buf + off, sql_buffer_size - off, _v, _n, val2str);
169 169
 	if (ret < 0) return -1;
170 170
 	off += ret;
171 171
 
172
-	if (off + 2 > SQL_BUF_LEN) goto error;
172
+	if (off + 2 > sql_buffer_size) goto error;
173 173
 	sql_buf[off++] = ')';
174 174
 	sql_buf[off] = '\0';
175 175
 	sql_str.s = sql_buf;
... ...
@@ -199,21 +200,21 @@ int db_do_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
199 199
 		return -1;
200 200
 	}
201 201
 
202
-	ret = snprintf(sql_buf, SQL_BUF_LEN, "delete from %.*s", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
203
-	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
202
+	ret = snprintf(sql_buf, sql_buffer_size, "delete from %.*s", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
203
+	if (ret < 0 || ret >= sql_buffer_size) goto error;
204 204
 	off = ret;
205 205
 
206 206
 	if (_n) {
207
-		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");
208
-		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
207
+		ret = snprintf(sql_buf + off, sql_buffer_size - off, " where ");
208
+		if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
209 209
 		off += ret;
210 210
 
211 211
 		ret = db_print_where(_h, sql_buf + off,
212
-				SQL_BUF_LEN - off, _k, _o, _v, _n, val2str);
212
+				sql_buffer_size - off, _k, _o, _v, _n, val2str);
213 213
 		if (ret < 0) return -1;
214 214
 		off += ret;
215 215
 	}
216
-	if (off + 1 > SQL_BUF_LEN) goto error;
216
+	if (off + 1 > sql_buffer_size) goto error;
217 217
 	sql_buf[off] = '\0';
218 218
 	sql_str.s = sql_buf;
219 219
 	sql_str.len = off;
... ...
@@ -242,24 +243,24 @@ int db_do_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
242 242
 		return -1;
243 243
 	}
244 244
 
245
-	ret = snprintf(sql_buf, SQL_BUF_LEN, "update %.*s set ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
246
-	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
245
+	ret = snprintf(sql_buf, sql_buffer_size, "update %.*s set ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
246
+	if (ret < 0 || ret >= sql_buffer_size) goto error;
247 247
 	off = ret;
248 248
 
249
-	ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un, val2str);
249
+	ret = db_print_set(_h, sql_buf + off, sql_buffer_size - off, _uk, _uv, _un, val2str);
250 250
 	if (ret < 0) return -1;
251 251
 	off += ret;
252 252
 
253 253
 	if (_n) {
254
-		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");
255
-		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
254
+		ret = snprintf(sql_buf + off, sql_buffer_size - off, " where ");
255
+		if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
256 256
 		off += ret;
257 257
 
258
-		ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n, val2str);
258
+		ret = db_print_where(_h, sql_buf + off, sql_buffer_size - off, _k, _o, _v, _n, val2str);
259 259
 		if (ret < 0) return -1;
260 260
 		off += ret;
261 261
 	}
262
-	if (off + 1 > SQL_BUF_LEN) goto error;
262
+	if (off + 1 > sql_buffer_size) goto error;
263 263
 	sql_buf[off] = '\0';
264 264
 	sql_str.s = sql_buf;
265 265
 	sql_str.len = off;
... ...
@@ -287,24 +288,24 @@ int db_do_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
287 287
 		return -1;
288 288
 	}
289 289
 
290
-	ret = snprintf(sql_buf, SQL_BUF_LEN, "replace %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
291
-	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
290
+	ret = snprintf(sql_buf, sql_buffer_size, "replace %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
291
+	if (ret < 0 || ret >= sql_buffer_size) goto error;
292 292
 	off = ret;
293 293
 
294
-	ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n);
294
+	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n);
295 295
 	if (ret < 0) return -1;
296 296
 	off += ret;
297 297
 
298
-	ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");
299
-	if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
298
+	ret = snprintf(sql_buf + off, sql_buffer_size - off, ") values (");
299
+	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
300 300
 	off += ret;
301 301
 
302
-	ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n,
302
+	ret = db_print_values(_h, sql_buf + off, sql_buffer_size - off, _v, _n,
303 303
 	val2str);
304 304
 	if (ret < 0) return -1;
305 305
 	off += ret;
306 306
 
307
-	if (off + 2 > SQL_BUF_LEN) goto error;
307
+	if (off + 2 > sql_buffer_size) goto error;
308 308
 	sql_buf[off++] = ')';
309 309
 	sql_buf[off] = '\0';
310 310
 	sql_str.s = sql_buf;
... ...
@@ -320,3 +321,20 @@ int db_do_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
320 320
 	LM_ERR("error while preparing replace operation\n");
321 321
 	return -1;
322 322
 }
323
+
324
+int db_query_init(void)
325
+{
326
+    if (sql_buf != NULL)
327
+    {
328
+        LM_DBG("sql_buf not NULL on init\n");
329
+        return 0;
330
+    }
331
+    LM_DBG("About to allocate sql_buf size = %d\n", sql_buffer_size);
332
+    sql_buf = (char*)malloc(sql_buffer_size);
333
+    if (sql_buf == NULL)
334
+    {
335
+        LM_ERR("failed to allocate sql_buf\n");
336
+        return -1;
337
+    }
338
+    return 0;
339
+}
... ...
@@ -184,4 +184,13 @@ int db_do_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
184 184
 	int*), int (*submit_query)(const db1_con_t* _h, const str* _c));
185 185
 
186 186
 
187
+/**
188
+ * \brief Initialisation function - should be called from db.c at start-up
189
+ *
190
+ * This initialises the db_query module, and should be called before any functions in db_query are called.
191
+ *
192
+ * \return zero on success, negative on errors
193
+ */
194
+int db_query_init(void);
195
+    
187 196
 #endif
... ...
@@ -34,11 +34,6 @@
34 34
 #define DB1_UT_H
35 35
 
36 36
 /**
37
- * maximal SQL buffer length for database drivers
38
- */
39
-#define SQL_BUF_LEN 65536
40
-
41
-/**
42 37
  * make strptime available
43 38
  * use 600 for 'Single UNIX Specification, Version 3'
44 39
  * _XOPEN_SOURCE creates conflict in swab definition in Solaris
... ...
@@ -311,6 +311,8 @@ unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
311 311
 												  not want to exceed during the
312 312
 												  auto-probing procedure; may
313 313
 												  be re-configured */
314
+unsigned int sql_buffer_size = 65535; /* Size for the SQL buffer. Defaults to 64k. 
315
+                                         This may be re-configured */
314 316
 int children_no = 0;			/* number of children processing requests */
315 317
 #ifdef USE_TCP
316 318
 int tcp_children_no = 0;
... ...
@@ -46,6 +46,7 @@
46 46
 #include "../../sr_module.h"
47 47
 #include "../../lib/srdb1/db_res.h"
48 48
 #include "../../lib/srdb1/db.h"
49
+#include "../../lib/srdb1/db_query.h"
49 50
 #include "km_db_berkeley.h"
50 51
 #include "km_bdb_lib.h"
51 52
 #include "km_bdb_res.h"
... ...
@@ -128,7 +129,7 @@ int km_mod_init(void)
128 128
 	if(km_bdblib_init(&p))
129 129
 		return -1;
130 130
 
131
-	return 0;
131
+	return db_query_init();
132 132
 }
133 133
 
134 134
 void km_destroy(void)
... ...
@@ -90,7 +90,7 @@ struct kam_module_exports kam_exports = {
90 90
 int kam_mysql_mod_init(void)
91 91
 {
92 92
 	LM_DBG("MySQL client version is %s\n", mysql_get_client_info());
93
-	return 0;
93
+	return db_mysql_alloc_buffer();
94 94
 }
95 95
 
96 96
 int db_mysql_bind_api(db_func_t *dbb)
... ...
@@ -50,6 +50,8 @@
50 50
 #include "km_db_mysql.h"
51 51
 #include "km_dbase.h"
52 52
 
53
+static char *sql_buf;
54
+
53 55
 
54 56
 /**
55 57
  * \brief Send a SQL query to the server.
... ...
@@ -490,35 +492,34 @@ int db_last_inserted_id(const db1_con_t* _h)
490 490
  {
491 491
 	int off, ret;
492 492
 	static str  sql_str;
493
-	static char sql_buf[SQL_BUF_LEN];
494 493
  
495 494
 	if ((!_h) || (!_k) || (!_v) || (!_n)) {
496 495
 		LM_ERR("invalid parameter value\n");
497 496
 		return -1;
498 497
 	}
499 498
  
500
-	ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
501
-	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
499
+	ret = snprintf(sql_buf, sql_buffer_size, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
500
+	if (ret < 0 || ret >= sql_buffer_size) goto error;
502 501
 	off = ret;
503 502
 
504
-	ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n);
503
+	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n);
505 504
 	if (ret < 0) return -1;
506 505
 	off += ret;
507 506
 
508
-	ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");
509
-	if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
507
+	ret = snprintf(sql_buf + off, sql_buffer_size - off, ") values (");
508
+	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
510 509
 	off += ret;
511
-	ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, db_mysql_val2str);
510
+	ret = db_print_values(_h, sql_buf + off, sql_buffer_size - off, _v, _n, db_mysql_val2str);
512 511
 	if (ret < 0) return -1;
513 512
 	off += ret;
514 513
 
515 514
 	*(sql_buf + off++) = ')';
516 515
 	
517
-	ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " on duplicate key update ");
518
-	if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
516
+	ret = snprintf(sql_buf + off, sql_buffer_size - off, " on duplicate key update ");
517
+	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
519 518
 	off += ret;
520 519
 	
521
-	ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _v, _n, db_mysql_val2str);
520
+	ret = db_print_set(_h, sql_buf + off, sql_buffer_size - off, _k, _v, _n, db_mysql_val2str);
522 521
 	if (ret < 0) return -1;
523 522
 	off += ret;
524 523
 	
... ...
@@ -547,3 +548,24 @@ int db_mysql_use_table(db1_con_t* _h, const str* _t)
547 547
 {
548 548
 	return db_use_table(_h, _t);
549 549
 }
550
+
551
+
552
+/**
553
+ * Allocate a buffer for database module
554
+ * No function should be called before this
555
+ * \return zero on success, negative value on failure
556
+ */
557
+int db_mysql_alloc_buffer(void)
558
+{
559
+    if (db_query_init())
560
+    {
561
+        LM_ERR("Failed to initialise db_query\n");
562
+		return -1;
563
+    }
564
+
565
+    sql_buf = (char*)malloc(sql_buffer_size);
566
+    if (sql_buf == NULL)
567
+        return -1;
568
+    else
569
+        return 0;
570
+}
550 571
\ No newline at end of file
... ...
@@ -37,6 +37,7 @@
37 37
 #include "../../sr_module.h"
38 38
 #include "../../lib/srdb1/db_con.h"
39 39
 #include "../../lib/srdb1/db.h"
40
+#include "../../lib/srdb1/db_query.h"
40 41
 #include "km_dbase.h"
41 42
 #include "km_db_postgres.h"
42 43
 
... ...
@@ -72,7 +73,7 @@ struct kam_module_exports kam_exports = {
72 72
 
73 73
 int km_postgres_mod_init(void)
74 74
 {
75
-	return 0;
75
+	return db_query_init();
76 76
 }
77 77
 
78 78
 int db_postgres_bind_api(db_func_t *dbb)
... ...
@@ -30,6 +30,7 @@
30 30
 #include <oci.h>
31 31
 #include "../../sr_module.h"
32 32
 #include "../../lib/srdb1/db.h"
33
+#include "../../lib/srdb1/db_query.h"
33 34
 #include "dbase.h"
34 35
 #include "asynch.h"
35 36
 
... ...
@@ -82,7 +83,7 @@ static int oracle_mod_init(void)
82 82
 	OCIClientVersion(&major, &minor, &update, &patch, &port);
83 83
 	LM_DBG("Oracle client version is %d.%d.%d.%d.%d\n",
84 84
 		major, minor, update, patch, port);
85
-	return 0;
85
+	return db_query_init();
86 86
 }
87 87
 
88 88
 
... ...
@@ -63,7 +63,7 @@ static int sqlite_mod_init(void)
63 63
 	LM_INFO("SQlite library version %s (compiled using %s)\n",
64 64
 		sqlite3_libversion(),
65 65
 		SQLITE_VERSION);
66
-	return 0;
66
+	return db_query_init();
67 67
 }
68 68
 
69 69
 
... ...
@@ -30,6 +30,7 @@
30 30
 
31 31
 #include "../../sr_module.h"
32 32
 #include "../../lib/srdb1/db.h"
33
+#include "../../lib/srdb1/db_query.h"
33 34
 #include "dbase.h"
34 35
 #include "db_unixodbc.h"
35 36
 
... ...
@@ -40,6 +41,7 @@ int use_escape_common = 0;  /* Enable common escaping */
40 40
 MODULE_VERSION
41 41
 
42 42
 int db_unixodbc_bind_api(db_func_t *dbb);
43
+int unixodbc_mod_init(void);
43 44
 
44 45
 /*
45 46
  * MySQL database module interface
... ...
@@ -65,15 +67,15 @@ struct module_exports exports = {
65 65
 	"db_unixodbc",
66 66
 	DEFAULT_DLFLAGS, /* dlopen flags */
67 67
 	cmds,
68
-	params,     /*  module parameters */
69
-	0,          /* exported statistics */
70
-	0,          /* exported MI functions */
71
-	0,          /* exported pseudo-variables */
72
-	0,          /* extra processes */
73
-	0,          /* module initialization function */
74
-	0,          /* response function*/
75
-	0,          /* destroy function */
76
-	0           /* per-child init function */
68
+	params,             /*  module parameters */
69
+	0,                  /* exported statistics */
70
+	0,                  /* exported MI functions */
71
+	0,                  /* exported pseudo-variables */
72
+	0,                  /* extra processes */
73
+	unixodbc_mod_init,  /* module initialization function */
74
+	0,                  /* response function*/
75
+	0,                  /* destroy function */
76
+	0                   /* per-child init function */
77 77
 };
78 78
 
79 79
 int db_unixodbc_bind_api(db_func_t *dbb)
... ...
@@ -98,3 +100,8 @@ int db_unixodbc_bind_api(db_func_t *dbb)
98 98
 	return 0;
99 99
 }
100 100
 
101
+int unixodbc_mod_init(void)
102
+{
103
+	return db_query_init();
104
+}
105
+