Browse code

db_mysql: initialize DB API in mod_register() function

- internal buffer sql_buf initialized in this function
- renamed sql_buf to mysql_sql_buf to avoid potential conflict with
global variable in srdb1 library

Daniel-Constantin Mierla authored on 01/11/2011 19:18:36
Showing 5 changed files
... ...
@@ -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 db_mysql_alloc_buffer();
93
+	return 0;
94 94
 }
95 95
 
96 96
 int db_mysql_bind_api(db_func_t *dbb)
... ...
@@ -47,4 +47,11 @@ int db_mysql_bind_api(db_func_t *dbb);
47 47
 
48 48
 int kam_mysql_mod_init(void);
49 49
 
50
+/**
51
+ * Allocate a buffer for database module
52
+ * No function should be called before this
53
+ * \return zero on success, negative value on failure
54
+ */
55
+int db_mysql_alloc_buffer(void);
56
+
50 57
 #endif /* KM_DB_MOD_H */
... ...
@@ -50,7 +50,7 @@
50 50
 #include "km_db_mysql.h"
51 51
 #include "km_dbase.h"
52 52
 
53
-static char *sql_buf;
53
+static char *mysql_sql_buf;
54 54
 
55 55
 
56 56
 /**
... ...
@@ -513,32 +513,32 @@ int db_mysql_affected_rows(const db1_con_t* _h)
513 513
 		return -1;
514 514
 	}
515 515
  
516
-	ret = snprintf(sql_buf, sql_buffer_size, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
516
+	ret = snprintf(mysql_sql_buf, sql_buffer_size, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
517 517
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
518 518
 	off = ret;
519 519
 
520
-	ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n);
520
+	ret = db_print_columns(mysql_sql_buf + off, sql_buffer_size - off, _k, _n);
521 521
 	if (ret < 0) return -1;
522 522
 	off += ret;
523 523
 
524
-	ret = snprintf(sql_buf + off, sql_buffer_size - off, ") values (");
524
+	ret = snprintf(mysql_sql_buf + off, sql_buffer_size - off, ") values (");
525 525
 	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
526 526
 	off += ret;
527
-	ret = db_print_values(_h, sql_buf + off, sql_buffer_size - off, _v, _n, db_mysql_val2str);
527
+	ret = db_print_values(_h, mysql_sql_buf + off, sql_buffer_size - off, _v, _n, db_mysql_val2str);
528 528
 	if (ret < 0) return -1;
529 529
 	off += ret;
530 530
 
531
-	*(sql_buf + off++) = ')';
531
+	*(mysql_sql_buf + off++) = ')';
532 532
 	
533
-	ret = snprintf(sql_buf + off, sql_buffer_size - off, " on duplicate key update ");
533
+	ret = snprintf(mysql_sql_buf + off, sql_buffer_size - off, " on duplicate key update ");
534 534
 	if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
535 535
 	off += ret;
536 536
 	
537
-	ret = db_print_set(_h, sql_buf + off, sql_buffer_size - off, _k, _v, _n, db_mysql_val2str);
537
+	ret = db_print_set(_h, mysql_sql_buf + off, sql_buffer_size - off, _k, _v, _n, db_mysql_val2str);
538 538
 	if (ret < 0) return -1;
539 539
 	off += ret;
540 540
 	
541
-	sql_str.s = sql_buf;
541
+	sql_str.s = mysql_sql_buf;
542 542
 	sql_str.len = off;
543 543
  
544 544
 	if (db_mysql_submit_query(_h, &sql_str) < 0) {
... ...
@@ -587,14 +587,14 @@ int db_mysql_use_table(db1_con_t* _h, const str* _t)
587 587
  */
588 588
 int db_mysql_alloc_buffer(void)
589 589
 {
590
-    if (db_query_init())
590
+    if (db_api_init())
591 591
     {
592
-        LM_ERR("Failed to initialise db_query\n");
592
+        LM_ERR("Failed to initialise db api\n");
593 593
 		return -1;
594 594
     }
595 595
 
596
-    sql_buf = (char*)malloc(sql_buffer_size);
597
-    if (sql_buf == NULL)
596
+    mysql_sql_buf = (char*)malloc(sql_buffer_size);
597
+    if (mysql_sql_buf == NULL)
598 598
         return -1;
599 599
     else
600 600
         return 0;
... ...
@@ -139,11 +139,4 @@ int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k,
139 139
 int db_mysql_use_table(db1_con_t* _h, const str* _t);
140 140
 
141 141
 
142
-/**
143
- * Allocate a buffer for database module
144
- * No function should be called before this
145
- * \return zero on success, negative value on failure
146
- */
147
-int db_mysql_alloc_buffer(void);
148
-
149 142
 #endif /* KM_DBASE_H */
... ...
@@ -124,6 +124,13 @@ struct module_exports exports = {
124 124
 };
125 125
 
126 126
 
127
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
128
+{
129
+	if(db_mysql_alloc_buffer()<0)
130
+		return -1;
131
+	return 0;
132
+}
133
+
127 134
 static int mysql_mod_init(void)
128 135
 {
129 136
 #if MYSQL_VERSION_ID >= 40101