Browse code

db_mysql: implemented async raw query and async insert

- both use the core async framework

Daniel-Constantin Mierla authored on 14/04/2014 20:17:51
Showing 3 changed files
... ...
@@ -119,6 +119,8 @@ int db_mysql_bind_api(db_func_t *dbb)
119 119
 	dbb->start_transaction= db_mysql_start_transaction;
120 120
 	dbb->end_transaction  = db_mysql_end_transaction;
121 121
 	dbb->abort_transaction= db_mysql_abort_transaction;
122
+	dbb->raw_query_async  = db_mysql_raw_query_async;
123
+	dbb->insert_async     = db_mysql_insert_async;
122 124
 
123 125
 	return 0;
124 126
 }
... ...
@@ -40,6 +40,7 @@
40 40
 #include <mysql/mysql_version.h>
41 41
 #include "../../mem/mem.h"
42 42
 #include "../../dprint.h"
43
+#include "../../async_task.h"
43 44
 #include "../../lib/srdb1/db_query.h"
44 45
 #include "../../lib/srdb1/db_ut.h"
45 46
 #include "mysql_mod.h"
... ...
@@ -124,6 +125,66 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
124 125
 }
125 126
 
126 127
 
128
+/**
129
+ *
130
+ */
131
+void db_mysql_async_exec_task(void *param)
132
+{
133
+	str *p;
134
+	db1_con_t* dbc;
135
+	
136
+	p = (str*)param;
137
+	
138
+	dbc = db_mysql_init(&p[0]);
139
+
140
+	if(dbc==NULL) {
141
+		LM_ERR("failed to open connection for [%.*s]\n", p[0].len, p[0].s);
142
+		return;
143
+	}
144
+	if(db_mysql_submit_query(dbc, &p[1])<0) {
145
+		LM_ERR("failed to execute query on async worker\n");
146
+	}
147
+	db_mysql_close(dbc);
148
+}
149
+
150
+/**
151
+ * Execute a raw SQL query via core async framework.
152
+ * \param _h handle for the database
153
+ * \param _s raw query string
154
+ * \return zero on success, negative value on failure
155
+ */
156
+int db_mysql_submit_query_async(const db1_con_t* _h, const str* _s)
157
+{
158
+	struct db_id* di;
159
+	async_task_t *atask;
160
+	int asize;
161
+	str *p;
162
+
163
+	di = ((struct pool_con*)_h->tail)->id;
164
+
165
+	asize = sizeof(async_task_t) + 2*sizeof(str) + di->url.len + _s->len + 2;
166
+	atask = shm_malloc(asize);
167
+	if(atask==NULL) {
168
+		LM_ERR("no more shared memory to allocate %d\n", asize);
169
+		return -1;
170
+	}
171
+
172
+	atask->exec = db_mysql_async_exec_task;
173
+	atask->param = (char*)atask + sizeof(async_task_t);
174
+
175
+	p = (str*)((char*)atask + sizeof(async_task_t));
176
+	p[0].s = (char*)p + 2*sizeof(str);
177
+	p[0].len = di->url.len;
178
+	strncpy(p[0].s, di->url.s, di->url.len);
179
+	p[1].s = p[0].s + p[0].len + 1;
180
+	p[1].len = _s->len;
181
+	strncpy(p[1].s, _s->s, _s->len);
182
+
183
+	async_task_push(atask);
184
+
185
+	return 0;
186
+}
187
+
127 188
 
128 189
 /**
129 190
  * Initialize the database module.
... ...
@@ -397,6 +458,16 @@ int db_mysql_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
397 458
 	db_mysql_store_result);
398 459
 }
399 460
 
461
+/**
462
+ * Execute a raw SQL query via core async framework.
463
+ * \param _h handle for the database
464
+ * \param _s raw query string
465
+ * \return zero on success, negative value on failure
466
+ */
467
+int db_mysql_raw_query_async(const db1_con_t* _h, const str* _s)
468
+{
469
+	return db_mysql_submit_query_async(_h, _s);
470
+}
400 471
 
401 472
 /**
402 473
  * Insert a row into a specified table.
... ...
@@ -767,6 +838,21 @@ int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_va
767 838
 	db_mysql_submit_query);
768 839
 }
769 840
 
841
+/**
842
+ * Insert a row into a specified table via core async framework.
843
+ * \param _h structure representing database connection
844
+ * \param _k key names
845
+ * \param _v values of the keys
846
+ * \param _n number of key=value pairs
847
+ * \return zero on success, negative value on failure
848
+ */
849
+int db_mysql_insert_async(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
850
+{
851
+	return db_do_insert_delayed(_h, _k, _v, _n, db_mysql_val2str,
852
+	db_mysql_submit_query_async);
853
+}
854
+
855
+
770 856
 
771 857
 /**
772 858
  * Store the name of table that will be used by subsequent database functions
... ...
@@ -81,6 +81,12 @@ int db_mysql_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows);
81 81
 int db_mysql_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r);
82 82
 
83 83
 
84
+/*! \brief
85
+ * Raw SQL query via async framework
86
+ */
87
+int db_mysql_raw_query_async(const db1_con_t* _h, const str* _s);
88
+
89
+
84 90
 /*! \brief
85 91
  * Insert a row into table
86 92
  */
... ...
@@ -149,6 +155,13 @@ int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k,
149 155
 		const db_val_t* _v, const int _n);
150 156
 
151 157
 
158
+/*! \brief
159
+ * Insert a row into table via async framework
160
+ */
161
+int db_mysql_insert_async(const db1_con_t* _h, const db_key_t* _k,
162
+		const db_val_t* _v, const int _n);
163
+
164
+
152 165
 /*! \brief
153 166
  * Store name of table that will be used by
154 167
  * subsequent database functions