Add SQL_ESC to allow single ticks to be escaped
[asterisk/asterisk.git] / funcs / func_odbc.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * func_odbc
5  * 
6  * Copyright (c) 2005 Tilghman Lesher
7  *
8  * Tilghman Lesher <func_odbc__200508@the-tilghman.com>
9  *
10  * Special thanks to Anthony Minessale II for debugging help.
11  */
12
13 #include <sys/types.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <asterisk/file.h>
19 #include <asterisk/logger.h>
20 #include <asterisk/options.h>
21 #include <asterisk/channel.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/module.h>
24 #include <asterisk/config.h>
25 #include <asterisk/res_odbc.h>
26
27 static char *tdesc = "ODBC lookups";
28
29 static char *config = "func_odbc.conf";
30
31 struct acf_odbc_query {
32         char name[30];
33         char dsn[30];
34         char sql_read[512];
35         char sql_write[512];
36         struct ast_custom_function *acf;
37         unsigned int deleteme:1;
38         struct acf_odbc_query *next;
39 };
40
41 static struct acf_odbc_query *queries = NULL;
42 AST_MUTEX_DEFINE_STATIC(query_lock);
43
44 #ifdef NEEDTRACE
45 static void acf_odbc_error(SQLHSTMT stmt, int res)
46 {
47         char state[10] = "", diagnostic[256] = "";
48         SQLINTEGER nativeerror = 0;
49         SQLSMALLINT diagbytes = 0;
50         SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
51         ast_log(LOG_WARNING, "SQL return value %d: error %s: %s (len %d)\n", res, state, diagnostic, diagbytes);
52 }
53 #endif
54
55 /*
56  * Master control routine
57  */
58 static void acf_odbc_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
59 {
60         odbc_obj *obj;
61         struct acf_odbc_query *query;
62         char *s, *t, *arg, buf[512]="", varname[15];
63         int res, argcount=0, valcount=0, i, retry=0;
64         struct ast_channel *ast;
65         SQLHSTMT stmt;
66         SQLINTEGER nativeerror=0, numfields=0, rows=0;
67         SQLSMALLINT diagbytes=0;
68         unsigned char state[10], diagnostic[256];
69 #ifdef NEEDTRACE
70         SQLINTEGER enable = 1;
71         char *tracefile = "/tmp/odbc.trace";
72 #endif
73
74         ast_mutex_lock(&query_lock);
75         for (query=queries; query; query = query->next) {
76                 if (!strcasecmp(query->name, cmd + 5)) {
77                         break;
78                 }
79         }
80
81         if (!query) {
82                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
83                 ast_mutex_unlock(&query_lock);
84                 return;
85         }
86
87         obj = fetch_odbc_obj(query->dsn, 0);
88
89         if (!obj) {
90                 ast_log(LOG_ERROR, "No such DSN registered: %s (check res_odbc.conf)\n", query->dsn);
91                 ast_mutex_unlock(&query_lock);
92                 return;
93         }
94
95         /* Parse our arguments */
96         s = ast_strdupa(data);
97         if (value) {
98                 t = ast_strdupa(value);
99         } else {
100                 t = "";
101         }
102
103         if (!s || !t) {
104                 ast_log(LOG_ERROR, "Out of memory\n");
105                 ast_mutex_unlock(&query_lock);
106                 return;
107         }
108
109         /* XXX You might be tempted to change this section into using
110          * pbx_builtin_pushvar_helper().  However, note that if you try
111          * to set a NULL (like for VALUE), then nothing gets set, and the
112          * value doesn't get masked out.  Even worse, when you subsequently
113          * try to remove the value you just set, you'll wind up unsetting
114          * the previous value (which is wholly undesireable).  Hence, this
115          * has to remain the way it is done here. XXX
116          */
117
118         /* Save old arguments as variables in a fake channel */
119         ast = ast_channel_alloc(0);
120         while ((arg = strsep(&s, "|"))) {
121                 argcount++;
122                 snprintf(varname, sizeof(varname), "ARG%d", argcount);
123                 pbx_builtin_setvar_helper(ast, varname, pbx_builtin_getvar_helper(chan, varname));
124                 pbx_builtin_setvar_helper(chan, varname, arg);
125         }
126
127         /* Parse values, just like arguments */
128         while ((arg = strsep(&t, "|"))) {
129                 valcount++;
130                 snprintf(varname, sizeof(varname), "VAL%d", valcount);
131                 pbx_builtin_setvar_helper(ast, varname, pbx_builtin_getvar_helper(chan, varname));
132                 pbx_builtin_setvar_helper(chan, varname, arg);
133         }
134
135         /* Additionally set the value as a whole */
136         /* Note that pbx_builtin_setvar_helper will quite happily take a NULL for the 3rd argument */
137         pbx_builtin_setvar_helper(ast, "VALUE", pbx_builtin_getvar_helper(chan, "VALUE"));
138         pbx_builtin_setvar_helper(chan, "VALUE", value);
139
140         pbx_substitute_variables_helper(chan, query->sql_write, buf, sizeof(buf) - 1);
141
142         /* Restore prior values */
143         for (i=1; i<=argcount; i++) {
144                 snprintf(varname, sizeof(varname), "ARG%d", argcount);
145                 pbx_builtin_setvar_helper(chan, varname, pbx_builtin_getvar_helper(ast, varname));
146         }
147
148         for (i=1; i<=valcount; i++) {
149                 snprintf(varname, sizeof(varname), "VAL%d", argcount);
150                 pbx_builtin_setvar_helper(chan, varname, pbx_builtin_getvar_helper(ast, varname));
151         }
152         pbx_builtin_setvar_helper(chan, "VALUE", pbx_builtin_getvar_helper(ast, "VALUE"));
153
154         ast_channel_free(ast);
155         ast_mutex_unlock(&query_lock);
156
157 retry_write:
158 #ifdef NEEDTRACE
159         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
160         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
161 #endif
162
163         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
164         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
165                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
166                 pbx_builtin_setvar_helper(chan, "ODBCROWS", "-1");
167                 return;
168         }
169
170         res = SQLPrepare(stmt, (unsigned char *)buf, SQL_NTS);
171         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
172                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", buf);
173                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
174                 pbx_builtin_setvar_helper(chan, "ODBCROWS", "-1");
175                 return;
176         }
177
178         res = SQLExecute(stmt);
179         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
180                 if (res == SQL_ERROR) {
181                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
182                         for (i = 0; i <= numfields; i++) {
183                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
184                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
185                                 if (i > 10) {
186                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
187                                         break;
188                                 }
189                         }
190                 }
191                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
192                 odbc_obj_disconnect(obj);
193                 /* All handles are now invalid (after a disconnect), so we gotta redo all handles */
194                 odbc_obj_connect(obj);
195                 if (!retry) {
196                         retry = 1;
197                         goto retry_write;
198                 }
199                 rows = -1;
200         } else {
201                 /* Rows affected */
202                 SQLRowCount(stmt, &rows);
203         }
204
205         /* Output the affected rows, for all cases.  In the event of failure, we
206          * flag this as -1 rows.  Note that this is different from 0 affected rows
207          * which would be the case if we succeeded in our query, but the values did
208          * not change. */
209         snprintf(varname, sizeof(varname), "%d", (int)rows);
210         pbx_builtin_setvar_helper(chan, "ODBCROWS", varname);
211
212         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
213                 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", buf);
214         }
215
216         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
217 }
218
219 static char *acf_odbc_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
220 {
221         odbc_obj *obj;
222         struct acf_odbc_query *query;
223         char *s, *arg, sql[512] = "", varname[15];
224         int count=0, res, x;
225         SQLHSTMT stmt;
226         SQLSMALLINT colcount=0;
227         SQLINTEGER indicator;
228 #ifdef NEEDTRACE
229         SQLINTEGER enable = 1;
230         char *tracefile = "/tmp/odbc.trace";
231 #endif
232
233         ast_mutex_lock(&query_lock);
234         for (query=queries; query; query = query->next) {
235                 if (!strcasecmp(query->name, cmd + 5)) {
236                         break;
237                 }
238         }
239
240         if (!query) {
241                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
242                 ast_mutex_unlock(&query_lock);
243                 return "";
244         }
245
246         obj = fetch_odbc_obj(query->dsn, 0);
247
248         if (!obj) {
249                 ast_log(LOG_ERROR, "No such DSN registered: %s (check res_odbc.conf)\n", query->dsn);
250                 ast_mutex_unlock(&query_lock);
251                 return "";
252         }
253
254 #ifdef NEEDTRACE
255         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
256         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
257 #endif
258
259         /* Parse our arguments */
260         s = ast_strdupa(data);
261         if (!s) {
262                 ast_log(LOG_ERROR, "Out of memory\n");
263                 ast_mutex_unlock(&query_lock);
264                 return "";
265         }
266
267         while ((arg = strsep(&s, "|"))) {
268                 count++;
269                 snprintf(varname, sizeof(varname), "ARG%d", count);
270                 /* arg is by definition non-NULL, so this works, here */
271                 pbx_builtin_pushvar_helper(chan, varname, arg);
272         }
273
274         pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1);
275
276         /* Restore prior values */
277         for (x = 1; x <= count; x++) {
278                 snprintf(varname, sizeof(varname), "ARG%d", x);
279                 pbx_builtin_setvar_helper(chan, varname, NULL);
280         }
281
282         ast_mutex_unlock(&query_lock);
283
284         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
285         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
286                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
287                 return "";
288         }
289
290         res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
291         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
292                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
293                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
294                 return "";
295         }
296
297         res = odbc_smart_execute(obj, stmt);
298         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
299                 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
300                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
301                 return "";
302         }
303
304         res = SQLNumResultCols(stmt, &colcount);
305         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
306                 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
307                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
308                 return "";
309         }
310
311         memset(buf, 0, len);
312
313         res = SQLFetch(stmt);
314         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
315                 if (res == SQL_NO_DATA) {
316                         if (option_verbose > 3) {
317                                 ast_verbose(VERBOSE_PREFIX_4 "Found no rows [%s]\n", sql);
318                         }
319                 } else if (option_verbose > 3) {
320                         ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql);
321                 }
322                 goto acf_out;
323         }
324
325         for (x=0; x<colcount; x++) {
326                 int buflen, coldatalen;
327                 char coldata[256];
328
329                 buflen = strlen(buf);
330                 res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator);
331                 if (indicator == SQL_NULL_DATA) {
332                         coldata[0] = '\0';
333                         res = SQL_SUCCESS;
334                 }
335
336                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
337                         ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
338                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
339                         return "";
340                 }
341
342                 strncat(buf + buflen, coldata, len - buflen);
343                 coldatalen = strlen(coldata);
344                 strncat(buf + buflen + coldatalen, ",", len - buflen - coldatalen);
345         }
346         /* Trim trailing comma */
347         buf[strlen(buf) - 1] = '\0';
348
349 acf_out:
350         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
351         return buf;
352 }
353
354 static char *acf_escape(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
355 {
356         char *in, *out = buf;
357         for (in = data; *in && out - buf < len; in++) {
358                 if (*in == '\'') {
359                         *out = '\'';
360                         out++;
361                 }
362                 *out = *in;
363                 out++;
364         }
365         *out = '\0';
366         return buf;
367 }
368
369 struct ast_custom_function escape_function = {
370         .name = "SQL_ESC",
371         .synopsis = "Escapes single ticks for use in SQL statements",
372         .syntax = "SQL_ESC(<string>)",
373         .desc =
374 "Used in SQL templates to escape data which may contain single ticks (') which\n"
375 "are otherwise used to delimit data.  For example:\n"
376 "SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n",
377         .read = acf_escape,
378         .write = NULL,
379 };
380
381
382
383 static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query)
384 {
385         char *tmp;
386
387         if (!cfg || !catg) {
388                 return -1;
389         }
390
391         *query = calloc(1, sizeof(struct acf_odbc_query));
392         if (! (*query))
393                 return -1;
394
395         ast_copy_string((*query)->name, catg, sizeof((*query)->name));
396
397         if ((tmp = ast_variable_retrieve(cfg, catg, "dsn"))) {
398                 ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn));
399         } else {
400                 return -1;
401         }
402
403         if ((tmp = ast_variable_retrieve(cfg, catg, "read"))) {
404                 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
405         }
406
407         if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) {
408                 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
409         }
410
411         (*query)->acf = calloc(1, sizeof(struct ast_custom_function));
412         if ((*query)->acf) {
413                 asprintf(&((*query)->acf->name), "ODBC_%s", catg);
414                 asprintf(&((*query)->acf->syntax), "ODBC_%s(<arg1>[...[,<argN>]])", catg);
415                 (*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
416                 if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
417                         asprintf(&((*query)->acf->desc),
418                                                 "Runs the following query, as defined in func_odbc.conf, performing\n"
419                                                 "substitution of the arguments into the query as specified by ${ARG1},\n"
420                                                 "${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n"
421                                                 "either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
422                                                 "\nRead:\n%s\n\nWrite:\n%s\n",
423                                                 (*query)->sql_read,
424                                                 (*query)->sql_write);
425                 } else if (!ast_strlen_zero((*query)->sql_read)) {
426                         asprintf(&((*query)->acf->desc),
427                                                 "Runs the following query, as defined in func_odbc.conf, performing\n"
428                                                 "substitution of the arguments into the query as specified by ${ARG1},\n"
429                                                 "${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n",
430                                                 (*query)->sql_read);
431                 } else if (!ast_strlen_zero((*query)->sql_write)) {
432                         asprintf(&((*query)->acf->desc),
433                                                 "Runs the following query, as defined in func_odbc.conf, performing\n"
434                                                 "substitution of the arguments into the query as specified by ${ARG1},\n"
435                                                 "${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n"
436                                                 "${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
437                                                 "This function may only be set.\nSQL:\n%s\n",
438                                                 (*query)->sql_write);
439                 }
440
441                 if (ast_strlen_zero((*query)->sql_read)) {
442                         (*query)->acf->read = NULL;
443                 } else {
444                         (*query)->acf->read = acf_odbc_read;
445                 }
446
447                 if (ast_strlen_zero((*query)->sql_write)) {
448                         (*query)->acf->write = NULL;
449                 } else {
450                         (*query)->acf->write = acf_odbc_write;
451                 }
452
453                 if (! (*query)->acf->name || ! (*query)->acf->syntax || ! (*query)->acf->desc) {
454                         return -1;
455                 }
456         } else {
457                 return -1;
458         }
459         return 0;
460 }
461
462 static int free_acf_query(struct acf_odbc_query *query)
463 {
464         if (query) {
465                 if (query->acf) {
466                         if (query->acf->name)
467                                 free(query->acf->name);
468                         if (query->acf->syntax)
469                                 free(query->acf->syntax);
470                         if (query->acf->desc)
471                                 free(query->acf->desc);
472                         free(query->acf);
473                 }
474                 free(query);
475         }
476         return 0;
477 }
478
479 static int odbc_load_module(void)
480 {
481         int res = 0;
482         struct ast_config *cfg;
483         char *catg;
484
485         ast_mutex_lock(&query_lock);
486
487         cfg = ast_config_load(config);
488         if (!cfg) {
489                 ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
490                 goto out;
491         }
492
493         for (catg = ast_category_browse(cfg, NULL);
494                  catg;
495                  catg = ast_category_browse(cfg, catg)) {
496                 struct acf_odbc_query *query=NULL;
497
498                 if (init_acf_query(cfg, catg, &query)) {
499                         ast_log(LOG_ERROR, "Out of memory\n");
500                         free_acf_query(query);
501                 } else {
502                         query->next = queries;
503                         queries = query;
504                         ast_custom_function_register(query->acf);
505                 }
506         }
507
508         ast_config_destroy(cfg);
509         ast_custom_function_register(&escape_function);
510 out:
511         ast_mutex_unlock(&query_lock);
512         return res;
513 }
514
515 static int odbc_unload_module(void)
516 {
517         struct acf_odbc_query *query, *lastquery = NULL;
518
519         ast_mutex_lock(&query_lock);
520         for (query = queries; query; query = query->next) {
521                 if (lastquery)
522                         free_acf_query(lastquery);
523                 if (ast_custom_function_unregister(query->acf)) {
524                         ast_log(LOG_ERROR, "Cannot unregister function '%s'?\n", query->acf->name);
525                         /* Keep state valid */
526                         queries = query;
527                         ast_mutex_unlock(&query_lock);
528                         return -1;
529                 } else {
530                         /* If anything is waiting on this lock, this will let it pass (avoids a race) */
531                         ast_mutex_unlock(&query_lock);
532                         ast_mutex_lock(&query_lock);
533                         lastquery = query;
534                 }
535         }
536         if (lastquery)
537                 free(lastquery);
538         queries = NULL;
539
540         ast_custom_function_unregister(&escape_function);
541
542         ast_mutex_unlock(&query_lock);
543         return 0;
544 }
545
546 int reload(void)
547 {
548         int res = 0;
549         struct ast_config *cfg;
550         struct acf_odbc_query *q, *prevq = NULL, *qdel = NULL;
551         char *catg;
552
553         ast_mutex_lock(&query_lock);
554
555         for (q = queries; q; q = q->next) {
556                 q->deleteme = 1;
557         }
558
559         cfg = ast_config_load(config);
560         if (!cfg) {
561                 ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
562                 goto reload_out;
563         }
564
565         for (catg = ast_category_browse(cfg, NULL);
566                  catg;
567                  catg = ast_category_browse(cfg, catg)) {
568                 struct acf_odbc_query *query = NULL;
569
570                 /* We do this piecemeal, so that we stay in a consistent state, if there's ever an error */
571                 for (q = queries, prevq=NULL; q; prevq=q, q = q->next) {
572                         if (!strcasecmp(catg, q->name)) {
573                                 break;
574                         }
575                 }
576
577                 if (init_acf_query(cfg, catg, &query)) {
578                         ast_log(LOG_ERROR, "Cannot initialize query ODBC_%s\n", catg);
579                         free_acf_query(query);
580                 } else {
581                         if (q) {
582                                 /* Replacement */
583                                 if (ast_custom_function_unregister(q->acf)) {
584                                         ast_log(LOG_ERROR, "Cannot reload query %s\n", query->acf->name);
585                                         free_acf_query(query);
586                                 } else {
587                                         ast_custom_function_register(query->acf);
588                                         /* Add it to the list */
589                                         if (prevq)
590                                                 prevq->next = query;
591                                         else
592                                                 queries = query;
593                                         query->next = q->next;
594                                         /* Get rid of the old record */
595                                         free_acf_query(q);
596                                 }
597                         } else {
598                                 /* New */
599                                 query->next = queries;
600                                 queries = query;
601                                 ast_custom_function_register(query->acf);
602                         }
603                 }
604         }
605
606         /* Any remaining sets will now be destroyed */
607         for (q = queries; q; q = q->next) {
608                 if (qdel) {
609                         free_acf_query(qdel);
610                         qdel = NULL;
611                 }
612
613                 if (q->deleteme) {
614                         if (ast_custom_function_unregister(q->acf)) {
615                                 ast_log(LOG_ERROR, "Cannot unregister function?  Refusing to make 'ODBC_%s' go away.\n", q->name);
616                         } else {
617                                 /* If anything is waiting on the lock to execute, this will dispose of it (without a race) */
618                                 ast_mutex_unlock(&query_lock);
619                                 ast_mutex_lock(&query_lock);
620
621                                 if (prevq) {
622                                         prevq->next = q->next;
623                                 } else {
624                                         queries = q->next;
625                                 }
626                                 qdel = q;
627                         }
628                 } else {
629                         prevq = q;
630                 }
631         }
632         if (qdel)
633                 free_acf_query(qdel);
634
635         ast_config_destroy(cfg);
636 reload_out:
637         ast_mutex_unlock(&query_lock);
638         return res;
639 }
640
641 int unload_module(void)
642 {
643         return odbc_unload_module();
644 }
645
646 int load_module(void)
647 {
648         return odbc_load_module();
649 }
650
651 char *description(void)
652 {
653         return tdesc;
654 }
655
656 int usecount(void)
657 {
658         if (! ast_mutex_trylock(&query_lock)) {
659                 ast_mutex_unlock(&query_lock);
660                 return 0;
661         } else {
662                 return 1;
663         }
664 }
665
666 char *key()
667 {
668         return ASTERISK_GPL_KEY;
669 }