The changes for trunk are less extensive, but include
[asterisk/asterisk.git] / funcs / func_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2005, 2006 Tilghman Lesher
5  *
6  * Tilghman Lesher <func_odbc__200508@the-tilghman.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  *
22  * \brief ODBC lookups
23  *
24  * \author Tilghman Lesher <func_odbc__200508@the-tilghman.com>
25  *
26  * \ingroup functions
27  */
28
29 /*** MODULEINFO
30         <depend>unixodbc</depend>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <errno.h>
43
44 #include "asterisk/module.h"
45 #include "asterisk/file.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/options.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/module.h"
51 #include "asterisk/config.h"
52 #include "asterisk/res_odbc.h"
53 #include "asterisk/app.h"
54
55 static char *config = "func_odbc.conf";
56
57 enum {
58         OPT_ESCAPECOMMAS =      (1 << 0),
59 } odbc_option_flags;
60
61 struct acf_odbc_query {
62         AST_LIST_ENTRY(acf_odbc_query) list;
63         char readhandle[5][30];
64         char writehandle[5][30];
65         char sql_read[2048];
66         char sql_write[2048];
67         unsigned int flags;
68         struct ast_custom_function *acf;
69 };
70
71 AST_LIST_HEAD_STATIC(queries, acf_odbc_query);
72
73 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
74 {
75         int res;
76         char *sql = data;
77         SQLHSTMT stmt;
78
79         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
80         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
81                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
82                 return NULL;
83         }
84
85         res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
86         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
87                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
88                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
89                 return NULL;
90         }
91
92         return stmt;
93 }
94
95 /*
96  * Master control routine
97  */
98 static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, const char *value)
99 {
100         struct odbc_obj *obj = NULL;
101         struct acf_odbc_query *query;
102         char *t, buf[2048]="", varname[15];
103         int i, dsn;
104         AST_DECLARE_APP_ARGS(values,
105                 AST_APP_ARG(field)[100];
106         );
107         AST_DECLARE_APP_ARGS(args,
108                 AST_APP_ARG(field)[100];
109         );
110         SQLHSTMT stmt;
111         SQLLEN rows=0;
112
113         AST_LIST_LOCK(&queries);
114         AST_LIST_TRAVERSE(&queries, query, list) {
115                 if (!strcmp(query->acf->name, cmd)) {
116                         break;
117                 }
118         }
119
120         if (!query) {
121                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
122                 AST_LIST_UNLOCK(&queries);
123                 return -1;
124         }
125
126         /* Parse our arguments */
127         t = value ? ast_strdupa(value) : "";
128
129         if (!s || !t) {
130                 ast_log(LOG_ERROR, "Out of memory\n");
131                 AST_LIST_UNLOCK(&queries);
132                 return -1;
133         }
134
135         AST_STANDARD_APP_ARGS(args, s);
136         for (i = 0; i < args.argc; i++) {
137                 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
138                 pbx_builtin_pushvar_helper(chan, varname, args.field[i]);
139         }
140
141         /* Parse values, just like arguments */
142         /* Can't use the pipe, because app Set removes them */
143         AST_NONSTANDARD_APP_ARGS(values, t, ',');
144         for (i = 0; i < values.argc; i++) {
145                 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
146                 pbx_builtin_pushvar_helper(chan, varname, values.field[i]);
147         }
148
149         /* Additionally set the value as a whole (but push an empty string if value is NULL) */
150         pbx_builtin_pushvar_helper(chan, "VALUE", value ? value : "");
151
152         pbx_substitute_variables_helper(chan, query->sql_write, buf, sizeof(buf) - 1);
153
154         /* Restore prior values */
155         for (i = 0; i < args.argc; i++) {
156                 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
157                 pbx_builtin_setvar_helper(chan, varname, NULL);
158         }
159
160         for (i = 0; i < values.argc; i++) {
161                 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
162                 pbx_builtin_setvar_helper(chan, varname, NULL);
163         }
164         pbx_builtin_setvar_helper(chan, "VALUE", NULL);
165
166         AST_LIST_UNLOCK(&queries);
167
168         for (dsn = 0; dsn < 5; dsn++) {
169                 if (!ast_strlen_zero(query->writehandle[dsn])) {
170                         obj = ast_odbc_request_obj(query->writehandle[dsn], 0);
171                         if (obj)
172                                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, buf);
173                 }
174                 if (stmt)
175                         break;
176         }
177
178         if (stmt) {
179                 /* Rows affected */
180                 SQLRowCount(stmt, &rows);
181         }
182
183         /* Output the affected rows, for all cases.  In the event of failure, we
184          * flag this as -1 rows.  Note that this is different from 0 affected rows
185          * which would be the case if we succeeded in our query, but the values did
186          * not change. */
187         snprintf(varname, sizeof(varname), "%d", (int)rows);
188         pbx_builtin_setvar_helper(chan, "ODBCROWS", varname);
189
190         if (stmt)
191                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
192         if (obj)
193                 ast_odbc_release_obj(obj);
194
195         return 0;
196 }
197
198 static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, char *buf, size_t len)
199 {
200         struct odbc_obj *obj = NULL;
201         struct acf_odbc_query *query;
202         char sql[2048] = "", varname[15], colnames[2048] = "";
203         int res, x, buflen = 0, escapecommas, dsn;
204         AST_DECLARE_APP_ARGS(args,
205                 AST_APP_ARG(field)[100];
206         );
207         SQLHSTMT stmt;
208         SQLSMALLINT colcount=0;
209         SQLLEN indicator;
210         SQLSMALLINT collength;
211
212         AST_LIST_LOCK(&queries);
213         AST_LIST_TRAVERSE(&queries, query, list) {
214                 if (!strcmp(query->acf->name, cmd)) {
215                         break;
216                 }
217         }
218
219         if (!query) {
220                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
221                 AST_LIST_UNLOCK(&queries);
222                 return -1;
223         }
224
225         AST_STANDARD_APP_ARGS(args, s);
226         for (x = 0; x < args.argc; x++) {
227                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
228                 pbx_builtin_pushvar_helper(chan, varname, args.field[x]);
229         }
230
231         pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1);
232
233         /* Restore prior values */
234         for (x = 0; x < args.argc; x++) {
235                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
236                 pbx_builtin_setvar_helper(chan, varname, NULL);
237         }
238
239         /* Save this flag, so we can release the lock */
240         escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS);
241
242         AST_LIST_UNLOCK(&queries);
243
244         for (dsn = 0; dsn < 5; dsn++) {
245                 if (!ast_strlen_zero(query->writehandle[dsn])) {
246                         obj = ast_odbc_request_obj(query->writehandle[dsn], 0);
247                         if (obj)
248                                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
249                 }
250                 if (stmt)
251                         break;
252         }
253
254         if (!stmt) {
255                 ast_log(LOG_ERROR, "Unable to execute query [%s]\n", sql);
256                 if (obj)
257                         ast_odbc_release_obj(obj);
258                 return -1;
259         }
260
261         res = SQLNumResultCols(stmt, &colcount);
262         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
263                 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
264                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
265                 ast_odbc_release_obj(obj);
266                 return -1;
267         }
268
269         *buf = '\0';
270
271         res = SQLFetch(stmt);
272         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
273                 int res1 = -1;
274                 if (res == SQL_NO_DATA) {
275                         if (option_verbose > 3) {
276                                 ast_verbose(VERBOSE_PREFIX_4 "Found no rows [%s]\n", sql);
277                         }
278                         res1 = 0;
279                 } else if (option_verbose > 3) {
280                         ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql);
281                 }
282                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
283                 ast_odbc_release_obj(obj);
284                 return res1;
285         }
286
287         for (x = 0; x < colcount; x++) {
288                 int i, namelen;
289                 char coldata[256], colname[256];
290
291                 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, NULL, NULL, NULL);
292                 if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) {
293                         snprintf(colname, sizeof(colname), "field%d", x);
294                 }
295
296                 if (!ast_strlen_zero(colnames))
297                         strncat(colnames, ",", sizeof(colnames) - 1);
298                 namelen = strlen(colnames);
299
300                 /* Copy data, encoding '\' and ',' for the argument parser */
301                 for (i = 0; i < sizeof(colname); i++) {
302                         if (escapecommas && (colname[i] == '\\' || colname[i] == ',')) {
303                                 colnames[namelen++] = '\\';
304                         }
305                         colnames[namelen++] = colname[i];
306
307                         if (namelen >= sizeof(colnames) - 2) {
308                                 colnames[namelen >= sizeof(colnames) ? sizeof(colnames) - 1 : namelen] = '\0';
309                                 break;
310                         }
311
312                         if (colname[i] == '\0')
313                                 break;
314                 }
315
316                 buflen = strlen(buf);
317                 res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator);
318                 if (indicator == SQL_NULL_DATA) {
319                         coldata[0] = '\0';
320                         res = SQL_SUCCESS;
321                 }
322
323                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
324                         ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
325                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
326                         ast_odbc_release_obj(obj);
327                         return -1;
328                 }
329
330                 /* Copy data, encoding '\' and ',' for the argument parser */
331                 for (i = 0; i < sizeof(coldata); i++) {
332                         if (escapecommas && (coldata[i] == '\\' || coldata[i] == ',')) {
333                                 buf[buflen++] = '\\';
334                         }
335                         buf[buflen++] = coldata[i];
336
337                         if (buflen >= len - 2)
338                                 break;
339
340                         if (coldata[i] == '\0')
341                                 break;
342                 }
343
344                 buf[buflen - 1] = ',';
345                 buf[buflen] = '\0';
346         }
347         /* Trim trailing comma */
348         buf[buflen - 1] = '\0';
349
350         pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames);
351
352         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
353         ast_odbc_release_obj(obj);
354         return 0;
355 }
356
357 static int acf_escape(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
358 {
359         char *out = buf;
360
361         for (; *data && out - buf < len; data++) {
362                 if (*data == '\'') {
363                         *out = '\'';
364                         out++;
365                 }
366                 *out++ = *data;
367         }
368         *out = '\0';
369
370         return 0;
371 }
372
373 static struct ast_custom_function escape_function = {
374         .name = "SQL_ESC",
375         .synopsis = "Escapes single ticks for use in SQL statements",
376         .syntax = "SQL_ESC(<string>)",
377         .desc =
378 "Used in SQL templates to escape data which may contain single ticks (') which\n"
379 "are otherwise used to delimit data.  For example:\n"
380 "SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n",
381         .read = acf_escape,
382         .write = NULL,
383 };
384
385 static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query)
386 {
387         const char *tmp;
388         int i;
389
390         if (!cfg || !catg) {
391                 return EINVAL;
392         }
393
394         *query = ast_calloc(1, sizeof(struct acf_odbc_query));
395         if (! (*query))
396                 return ENOMEM;
397
398         if (((tmp = ast_variable_retrieve(cfg, catg, "writehandle"))) || ((tmp = ast_variable_retrieve(cfg, catg, "dsn")))) {
399                 char *tmp2 = ast_strdupa(tmp);
400                 AST_DECLARE_APP_ARGS(write,
401                         AST_APP_ARG(dsn)[5];
402                 );
403                 AST_NONSTANDARD_APP_ARGS(write, tmp2, ',');
404                 for (i = 0; i < 5; i++) {
405                         if (!ast_strlen_zero(write.dsn[i]))
406                                 ast_copy_string((*query)->writehandle[i], write.dsn[i], sizeof((*query)->writehandle[i]));
407                 }
408         }
409
410         if ((tmp = ast_variable_retrieve(cfg, catg, "readhandle"))) {
411                 char *tmp2 = ast_strdupa(tmp);
412                 AST_DECLARE_APP_ARGS(read,
413                         AST_APP_ARG(dsn)[5];
414                 );
415                 AST_NONSTANDARD_APP_ARGS(read, tmp2, ',');
416                 for (i = 0; i < 5; i++) {
417                         if (!ast_strlen_zero(read.dsn[i]))
418                                 ast_copy_string((*query)->readhandle[i], read.dsn[i], sizeof((*query)->readhandle[i]));
419                 }
420         } else {
421                 /* If no separate readhandle, then use the writehandle for reading */
422                 for (i = 0; i < 5; i++) {
423                         if (!ast_strlen_zero((*query)->writehandle[i]))
424                                 ast_copy_string((*query)->readhandle[i], (*query)->writehandle[i], sizeof((*query)->readhandle[i]));
425                 }
426         }
427
428         if ((tmp = ast_variable_retrieve(cfg, catg, "read"))) {
429                 ast_log(LOG_WARNING, "Parameter 'read' is deprecated for category %s.  Please use 'readsql' instead.\n", catg);
430                 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
431         } else if ((tmp = ast_variable_retrieve(cfg, catg, "readsql")))
432                 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
433
434         if (!ast_strlen_zero((*query)->sql_read) && ast_strlen_zero((*query)->readhandle[0])) {
435                 free(*query);
436                 *query = NULL;
437                 ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for reading: %s\n", catg);
438                 return EINVAL;
439         }
440
441         if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) {
442                 ast_log(LOG_WARNING, "Parameter 'write' is deprecated for category %s.  Please use 'writesql' instead.\n", catg);
443                 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
444         } else if ((tmp = ast_variable_retrieve(cfg, catg, "writesql")))
445                 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
446
447         if (!ast_strlen_zero((*query)->sql_write) && ast_strlen_zero((*query)->writehandle[0])) {
448                 free(*query);
449                 *query = NULL;
450                 ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for writing: %s\n", catg);
451                 return EINVAL;
452         }
453
454         /* Allow escaping of embedded commas in fields to be turned off */
455         ast_set_flag((*query), OPT_ESCAPECOMMAS);
456         if ((tmp = ast_variable_retrieve(cfg, catg, "escapecommas"))) {
457                 if (ast_false(tmp))
458                         ast_clear_flag((*query), OPT_ESCAPECOMMAS);
459         }
460
461         (*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
462         if (! (*query)->acf) {
463                 free(*query);
464                 *query = NULL;
465                 return ENOMEM;
466         }
467
468         if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
469                 asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg);
470         } else {
471                 asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg);
472         }
473
474         if (!((*query)->acf->name)) {
475                 free((*query)->acf);
476                 free(*query);
477                 *query = NULL;
478                 return ENOMEM;
479         }
480
481         asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name);
482
483         if (!((*query)->acf->syntax)) {
484                 free((char *)(*query)->acf->name);
485                 free((*query)->acf);
486                 free(*query);
487                 *query = NULL;
488                 return ENOMEM;
489         }
490
491         (*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
492         if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
493                 asprintf((char **)&((*query)->acf->desc),
494                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
495                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
496                                         "${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n"
497                                         "either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
498                                         "\nRead:\n%s\n\nWrite:\n%s\n",
499                                         (*query)->sql_read,
500                                         (*query)->sql_write);
501         } else if (!ast_strlen_zero((*query)->sql_read)) {
502                 asprintf((char **)&((*query)->acf->desc),
503                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
504                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
505                                         "${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n",
506                                         (*query)->sql_read);
507         } else if (!ast_strlen_zero((*query)->sql_write)) {
508                 asprintf((char **)&((*query)->acf->desc),
509                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
510                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
511                                         "${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n"
512                                         "${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
513                                         "This function may only be set.\nSQL:\n%s\n",
514                                         (*query)->sql_write);
515         } else {
516                 free((char *)(*query)->acf->syntax);
517                 free((char *)(*query)->acf->name);
518                 free((*query)->acf);
519                 free(*query);
520                 ast_log(LOG_WARNING, "Section %s was found, but there was no SQL to execute.  Ignoring.\n", catg);
521                 return EINVAL;
522         }
523
524         if (! ((*query)->acf->desc)) {
525                 free((char *)(*query)->acf->syntax);
526                 free((char *)(*query)->acf->name);
527                 free((*query)->acf);
528                 free(*query);
529                 *query = NULL;
530                 return ENOMEM;
531         }
532
533         if (ast_strlen_zero((*query)->sql_read)) {
534                 (*query)->acf->read = NULL;
535         } else {
536                 (*query)->acf->read = acf_odbc_read;
537         }
538
539         if (ast_strlen_zero((*query)->sql_write)) {
540                 (*query)->acf->write = NULL;
541         } else {
542                 (*query)->acf->write = acf_odbc_write;
543         }
544
545         return 0;
546 }
547
548 static int free_acf_query(struct acf_odbc_query *query)
549 {
550         if (query) {
551                 if (query->acf) {
552                         if (query->acf->name)
553                                 free((char *)query->acf->name);
554                         if (query->acf->syntax)
555                                 free((char *)query->acf->syntax);
556                         if (query->acf->desc)
557                                 free((char *)query->acf->desc);
558                         free(query->acf);
559                 }
560                 free(query);
561         }
562         return 0;
563 }
564
565 static int load_module(void)
566 {
567         int res = 0;
568         struct ast_config *cfg;
569         char *catg;
570
571         AST_LIST_LOCK(&queries);
572
573         cfg = ast_config_load(config);
574         if (!cfg) {
575                 ast_log(LOG_NOTICE, "Unable to load config for func_odbc: %s\n", config);
576                 AST_LIST_UNLOCK(&queries);
577                 return AST_MODULE_LOAD_DECLINE;
578         }
579
580         for (catg = ast_category_browse(cfg, NULL);
581              catg;
582              catg = ast_category_browse(cfg, catg)) {
583                 struct acf_odbc_query *query = NULL;
584                 int err;
585
586                 if ((err = init_acf_query(cfg, catg, &query))) {
587                         if (err == ENOMEM)
588                                 ast_log(LOG_ERROR, "Out of memory\n");
589                         else if (err == EINVAL)
590                                 ast_log(LOG_ERROR, "Invalid parameters for category %s\n", catg);
591                         else
592                                 ast_log(LOG_ERROR, "%s (%d)\n", strerror(err), err);
593                 } else {
594                         AST_LIST_INSERT_HEAD(&queries, query, list);
595                         ast_custom_function_register(query->acf);
596                 }
597         }
598
599         ast_config_destroy(cfg);
600         res |= ast_custom_function_register(&escape_function);
601
602         AST_LIST_UNLOCK(&queries);
603         return res;
604 }
605
606 static int unload_module(void)
607 {
608         struct acf_odbc_query *query;
609         int res = 0;
610
611         AST_LIST_LOCK(&queries);
612         while (!AST_LIST_EMPTY(&queries)) {
613                 query = AST_LIST_REMOVE_HEAD(&queries, list);
614                 ast_custom_function_unregister(query->acf);
615                 free_acf_query(query);
616         }
617
618         res |= ast_custom_function_unregister(&escape_function);
619
620         /* Allow any threads waiting for this lock to pass (avoids a race) */
621         AST_LIST_UNLOCK(&queries);
622         usleep(1);
623         AST_LIST_LOCK(&queries);
624
625         AST_LIST_UNLOCK(&queries);
626         return 0;
627 }
628
629 static int reload(void)
630 {
631         int res = 0;
632         struct ast_config *cfg;
633         struct acf_odbc_query *oldquery;
634         char *catg;
635
636         AST_LIST_LOCK(&queries);
637
638         while (!AST_LIST_EMPTY(&queries)) {
639                 oldquery = AST_LIST_REMOVE_HEAD(&queries, list);
640                 ast_custom_function_unregister(oldquery->acf);
641                 free_acf_query(oldquery);
642         }
643
644         cfg = ast_config_load(config);
645         if (!cfg) {
646                 ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
647                 goto reload_out;
648         }
649
650         for (catg = ast_category_browse(cfg, NULL);
651              catg;
652              catg = ast_category_browse(cfg, catg)) {
653                 struct acf_odbc_query *query = NULL;
654
655                 if (init_acf_query(cfg, catg, &query)) {
656                         ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
657                 } else {
658                         AST_LIST_INSERT_HEAD(&queries, query, list);
659                         ast_custom_function_register(query->acf);
660                 }
661         }
662
663         ast_config_destroy(cfg);
664 reload_out:
665         AST_LIST_UNLOCK(&queries);
666         return res;
667 }
668
669 /* XXX need to revise usecount - set if query_lock is set */
670
671 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC lookups",
672                 .load = load_module,
673                 .unload = unload_module,
674                 .reload = reload,
675                );
676