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