Re-commit the verbose branch.
[asterisk/asterisk.git] / addons / app_mysql.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004, Constantine Filin and Christos Ricudis
5  *
6  * Christos Ricudis <ricudis@itc.auth.gr>
7  * Constantine Filin <cf@intermedia.net>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*!
21  * \file
22  * \brief MYSQL dialplan application
23  * \ingroup applications
24  */
25
26 /*** MODULEINFO
27         <depend>mysqlclient</depend>
28         <defaultenabled>no</defaultenabled>
29         <support_level>deprecated</support_level>
30         <replacement>func_odbc</replacement>
31  ***/
32
33 #include "asterisk.h"
34
35 #include <mysql/mysql.h>
36
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/chanvars.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/options.h"
46 #include "asterisk/app.h"
47 #include "asterisk/config.h"
48
49 #define EXTRA_LOG 0
50
51 enum { NULLSTRING, NULLVALUE, EMPTYSTRING } nullvalue = NULLSTRING;
52
53 static const char app[] = "MYSQL";
54
55 static const char synopsis[] = "Do several mySQLy things";
56
57 static const char descrip[] =
58 "MYSQL():  Do several mySQLy things\n"
59 "Syntax:\n"
60 "  MYSQL(Set timeout <num>)\n"
61 "    Set the connection timeout, in seconds.\n"
62 "  MYSQL(Connect connid dhhost dbuser dbpass dbname [dbcharset])\n"
63 "    Connects to a database.  Arguments contain standard MySQL parameters\n"
64 "    passed to function mysql_real_connect.  Optional parameter dbcharset\n"
65 "    defaults to 'latin1'.  Connection identifer returned in ${connid}\n"
66 "  MYSQL(Query resultid ${connid} query-string)\n"
67 "    Executes standard MySQL query contained in query-string using established\n"
68 "    connection identified by ${connid}. Result of query is stored in ${resultid}.\n"
69 "  MYSQL(Nextresult resultid ${connid}\n"
70 "    If last query returned more than one result set, it stores the next\n"
71 "    result set in ${resultid}. It's useful with stored procedures\n"
72 "  MYSQL(Fetch fetchid ${resultid} var1 var2 ... varN)\n"
73 "    Fetches a single row from a result set contained in ${result_identifier}.\n"
74 "    Assigns returned fields to ${var1} ... ${varn}.  ${fetchid} is set TRUE\n"
75 "    if additional rows exist in result set.\n"
76 "  MYSQL(Clear ${resultid})\n"
77 "    Frees memory and datastructures associated with result set.\n"
78 "  MYSQL(Disconnect ${connid})\n"
79 "    Disconnects from named connection to MySQL.\n"
80 "  On exit, always returns 0. Sets MYSQL_STATUS to 0 on success and -1 on error.\n";
81
82 /*
83 EXAMPLES OF USE :
84
85 exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit utf8)
86 exten => s,3,MYSQL(Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${CALLERIDNUM})
87 exten => s,4,MYSQL(Fetch fetchid ${resultid} datavar1 datavar2)
88 exten => s,5,GotoIf(${fetchid}?6:8)
89 exten => s,6,Festival("User ${datavar1} currently has credit balance of ${datavar2} dollars.")
90 exten => s,7,Goto(s,4)
91 exten => s,8,MYSQL(Clear ${resultid})
92 exten => s,9,MYSQL(Disconnect ${connid})
93 */
94
95 AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
96
97 #define MYSQL_CONFIG "app_mysql.conf"
98 #define MYSQL_CONFIG_OLD "mysql.conf"
99 #define AST_MYSQL_ID_DUMMY   0
100 #define AST_MYSQL_ID_CONNID  1
101 #define AST_MYSQL_ID_RESID   2
102 #define AST_MYSQL_ID_FETCHID 3
103
104 static int autoclear = 0;
105
106 static void mysql_ds_destroy(void *data);
107 static void mysql_ds_fixup(void *data, struct ast_channel *oldchan, struct ast_channel *newchan);
108
109 static struct ast_datastore_info mysql_ds_info = {
110         .type = "APP_ADDON_SQL_MYSQL",
111         .destroy = mysql_ds_destroy,
112         .chan_fixup = mysql_ds_fixup,
113 };
114
115 struct ast_MYSQL_id {
116         struct ast_channel *owner;
117         int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
118         int identifier;
119         void *data;
120         AST_LIST_ENTRY(ast_MYSQL_id) entries;
121 } *ast_MYSQL_id;
122
123 AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
124
125 static void mysql_ds_destroy(void *data)
126 {
127         /* Destroy any IDs owned by the channel */
128         struct ast_MYSQL_id *i;
129         if (AST_LIST_LOCK(&_mysql_ids_head)) {
130                 ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
131         } else {
132                 AST_LIST_TRAVERSE_SAFE_BEGIN(&_mysql_ids_head, i, entries) {
133                         if (i->owner == data) {
134                                 AST_LIST_REMOVE_CURRENT(entries);
135                                 if (i->identifier_type == AST_MYSQL_ID_CONNID) {
136                                         /* Drop connection */
137                                         mysql_close(i->data);
138                                 } else if (i->identifier_type == AST_MYSQL_ID_RESID) {
139                                         /* Drop result */
140                                         mysql_free_result(i->data);
141                                 }
142                                 ast_free(i);
143                         }
144                 }
145                 AST_LIST_TRAVERSE_SAFE_END
146                 AST_LIST_UNLOCK(&_mysql_ids_head);
147         }
148 }
149
150 static void mysql_ds_fixup(void *data, struct ast_channel *oldchan, struct ast_channel *newchan)
151 {
152         /* Destroy any IDs owned by the channel */
153         struct ast_MYSQL_id *i;
154         if (AST_LIST_LOCK(&_mysql_ids_head)) {
155                 ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
156         } else {
157                 AST_LIST_TRAVERSE_SAFE_BEGIN(&_mysql_ids_head, i, entries) {
158                         if (i->owner == data) {
159                                 AST_LIST_REMOVE_CURRENT(entries);
160                                 if (i->identifier_type == AST_MYSQL_ID_CONNID) {
161                                         /* Drop connection */
162                                         mysql_close(i->data);
163                                 } else if (i->identifier_type == AST_MYSQL_ID_RESID) {
164                                         /* Drop result */
165                                         mysql_free_result(i->data);
166                                 }
167                                 ast_free(i);
168                         }
169                 }
170                 AST_LIST_TRAVERSE_SAFE_END
171                 AST_LIST_UNLOCK(&_mysql_ids_head);
172         }
173 }
174
175 /* helpful procs */
176 static void *find_identifier(int identifier, int identifier_type)
177 {
178         struct MYSQLidshead *headp = &_mysql_ids_head;
179         struct ast_MYSQL_id *i;
180         void *res=NULL;
181         int found=0;
182
183         if (AST_LIST_LOCK(headp)) {
184                 ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
185         } else {
186                 AST_LIST_TRAVERSE(headp, i, entries) {
187                         if ((i->identifier == identifier) && (i->identifier_type == identifier_type)) {
188                                 found = 1;
189                                 res = i->data;
190                                 break;
191                         }
192                 }
193                 if (!found) {
194                         ast_log(LOG_WARNING, "Identifier %d, identifier_type %d not found in identifier list\n", identifier, identifier_type);
195                 }
196                 AST_LIST_UNLOCK(headp);
197         }
198
199         return res;
200 }
201
202 static int add_identifier(struct ast_channel *chan, int identifier_type, void *data)
203 {
204         struct ast_MYSQL_id *i = NULL, *j = NULL;
205         struct MYSQLidshead *headp = &_mysql_ids_head;
206         int maxidentifier = 0;
207
208         if (AST_LIST_LOCK(headp)) {
209                 ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
210                 return -1;
211         } else {
212                 i = malloc(sizeof(*i));
213                 AST_LIST_TRAVERSE(headp, j, entries) {
214                         if (j->identifier > maxidentifier) {
215                                 maxidentifier = j->identifier;
216                         }
217                 }
218                 i->identifier = maxidentifier + 1;
219                 i->identifier_type = identifier_type;
220                 i->data = data;
221                 i->owner = chan;
222                 AST_LIST_INSERT_HEAD(headp, i, entries);
223                 AST_LIST_UNLOCK(headp);
224         }
225         return i->identifier;
226 }
227
228 static int del_identifier(int identifier, int identifier_type)
229 {
230         struct ast_MYSQL_id *i;
231         struct MYSQLidshead *headp = &_mysql_ids_head;
232         int found = 0;
233
234         if (AST_LIST_LOCK(headp)) {
235                 ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
236         } else {
237                 AST_LIST_TRAVERSE(headp, i, entries) {
238                         if ((i->identifier == identifier) &&
239                             (i->identifier_type == identifier_type)) {
240                                 AST_LIST_REMOVE(headp, i, entries);
241                                 free(i);
242                                 found = 1;
243                                 break;
244                         }
245                 }
246                 AST_LIST_UNLOCK(headp);
247         }
248
249         if (found == 0) {
250                 ast_log(LOG_WARNING, "Could not find identifier %d, identifier_type %d in list to delete\n", identifier, identifier_type);
251                 return -1;
252         } else {
253                 return 0;
254         }
255 }
256
257 static int set_asterisk_int(struct ast_channel *chan, char *varname, int id)
258 {
259         if (id >= 0) {
260                 char s[12] = "";
261                 snprintf(s, sizeof(s), "%d", id);
262                 ast_debug(5, "MYSQL: setting var '%s' to value '%s'\n", varname, s);
263                 pbx_builtin_setvar_helper(chan, varname, s);
264         }
265         return id;
266 }
267
268 static int add_identifier_and_set_asterisk_int(struct ast_channel *chan, char *varname, int identifier_type, void *data)
269 {
270         return set_asterisk_int(chan, varname, add_identifier(chan, identifier_type, data));
271 }
272
273 static int safe_scan_int(char **data, char *delim, int def)
274 {
275         char *end;
276         int res = def;
277         char *s = strsep(data, delim);
278         if (s) {
279                 res = strtol(s, &end, 10);
280                 if (*end)
281                         res = def;  /* not an integer */
282         }
283         return res;
284 }
285
286 static int aMYSQL_set(struct ast_channel *chan, char *data)
287 {
288         char *var, *tmp;
289         AST_DECLARE_APP_ARGS(args,
290                 AST_APP_ARG(set);
291                 AST_APP_ARG(variable);
292                 AST_APP_ARG(value);
293         );
294
295         AST_NONSTANDARD_APP_ARGS(args, data, ' ');
296
297         if (args.argc == 3) {
298                 var = alloca(6 + strlen(args.variable) + 1);
299                 sprintf(var, "MYSQL_%s", args.variable);
300
301                 /* Make the parameter case-insensitive */
302                 for (tmp = var + 6; *tmp; tmp++)
303                         *tmp = toupper(*tmp);
304
305                 pbx_builtin_setvar_helper(chan, var, args.value);
306         }
307         return 0;
308 }
309
310 /* MYSQL operations */
311 static int aMYSQL_connect(struct ast_channel *chan, char *data)
312 {
313         AST_DECLARE_APP_ARGS(args,
314                 AST_APP_ARG(connect);
315                 AST_APP_ARG(connid);
316                 AST_APP_ARG(dbhost);
317                 AST_APP_ARG(dbuser);
318                 AST_APP_ARG(dbpass);
319                 AST_APP_ARG(dbname);
320                 AST_APP_ARG(dbcharset);
321         );
322         MYSQL *mysql;
323         int timeout;
324         const char *ctimeout;
325
326         AST_NONSTANDARD_APP_ARGS(args, data, ' ');
327
328         if (args.argc < 6) {
329                 ast_log(LOG_WARNING, "MYSQL_connect is missing some arguments\n");
330                 return -1;
331         }
332
333         if (!(mysql = mysql_init(NULL))) {
334                 ast_log(LOG_WARNING, "mysql_init returned NULL\n");
335                 return -1;
336         }
337
338         ctimeout = pbx_builtin_getvar_helper(chan, "MYSQL_TIMEOUT");
339         if (ctimeout && sscanf(ctimeout, "%30d", &timeout) == 1) {
340                 mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (void *)&timeout);
341         }
342         if(args.dbcharset && strlen(args.dbcharset) > 2){
343                 char set_names[255];
344                 char statement[512];
345                 snprintf(set_names, sizeof(set_names), "SET NAMES %s", args.dbcharset);
346                 mysql_real_escape_string(mysql, statement, set_names, sizeof(set_names));
347                 mysql_options(mysql, MYSQL_INIT_COMMAND, set_names);
348                 mysql_options(mysql, MYSQL_SET_CHARSET_NAME, args.dbcharset);
349         }
350
351         if (! mysql_real_connect(mysql, args.dbhost, args.dbuser, args.dbpass, args.dbname, 0, NULL,
352 #ifdef CLIENT_MULTI_STATEMENTS
353                         CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS
354 #elif defined(CLIENT_MULTI_QUERIES)
355                         CLIENT_MULTI_QUERIES
356 #else
357                         0
358 #endif
359                 )) {
360                 ast_log(LOG_WARNING, "mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed(%d): %s\n",
361                                 args.dbhost, args.dbuser, args.dbname, mysql_errno(mysql), mysql_error(mysql));
362                 return -1;
363         }
364
365         add_identifier_and_set_asterisk_int(chan, args.connid, AST_MYSQL_ID_CONNID, mysql);
366         return 0;
367 }
368
369 static int aMYSQL_query(struct ast_channel *chan, char *data)
370 {
371         AST_DECLARE_APP_ARGS(args,
372                 AST_APP_ARG(query);
373                 AST_APP_ARG(resultid);
374                 AST_APP_ARG(connid);
375                 AST_APP_ARG(sql);
376         );
377         MYSQL       *mysql;
378         MYSQL_RES   *mysqlres;
379         int connid;
380         int mysql_query_res;
381
382         AST_NONSTANDARD_APP_ARGS(args, data, ' ');
383
384         if (args.argc != 4 || (connid = atoi(args.connid)) == 0) {
385                 ast_log(LOG_WARNING, "missing some arguments\n");
386                 return -1;
387         }
388
389         if (!(mysql = find_identifier(connid, AST_MYSQL_ID_CONNID))) {
390                 ast_log(LOG_WARNING, "Invalid connection identifier %s passed in aMYSQL_query\n", args.connid);
391                 return -1;
392         }
393
394         if ((mysql_query_res = mysql_query(mysql, args.sql)) != 0) {
395                 ast_log(LOG_WARNING, "aMYSQL_query: mysql_query failed. Error: %s\n", mysql_error(mysql));
396                 return -1;
397         }
398
399         if ((mysqlres = mysql_store_result(mysql))) {
400                 add_identifier_and_set_asterisk_int(chan, args.resultid, AST_MYSQL_ID_RESID, mysqlres);
401                 return 0;
402         } else if (!mysql_field_count(mysql)) {
403                 return 0;
404         } else
405                 ast_log(LOG_WARNING, "mysql_store_result() failed on query %s\n", args.sql);
406
407         return -1;
408 }
409
410 static int aMYSQL_nextresult(struct ast_channel *chan, char *data)
411 {
412         MYSQL       *mysql;
413         MYSQL_RES   *mysqlres;
414         AST_DECLARE_APP_ARGS(args,
415                 AST_APP_ARG(nextresult);
416                 AST_APP_ARG(resultid);
417                 AST_APP_ARG(connid);
418         );
419         int connid = -1;
420
421         AST_NONSTANDARD_APP_ARGS(args, data, ' ');
422         sscanf(args.connid, "%30d", &connid);
423
424         if (args.argc != 3 || connid <= 0) {
425                 ast_log(LOG_WARNING, "missing some arguments\n");
426                 return -1;
427         }
428
429         if (!(mysql = find_identifier(connid, AST_MYSQL_ID_CONNID))) {
430                 ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aMYSQL_query\n", connid);
431                 return -1;
432         }
433
434         if (mysql_more_results(mysql)) {
435                 mysql_next_result(mysql);
436                 if ((mysqlres = mysql_store_result(mysql))) {
437                         add_identifier_and_set_asterisk_int(chan, args.resultid, AST_MYSQL_ID_RESID, mysqlres);
438                         return 0;
439                 } else if (!mysql_field_count(mysql)) {
440                         return 0;
441                 } else
442                         ast_log(LOG_WARNING, "mysql_store_result() failed on storing next_result\n");
443         } else
444                 ast_log(LOG_WARNING, "mysql_more_results() result set has no more results\n");
445
446         return 0;
447 }
448
449
450 static int aMYSQL_fetch(struct ast_channel *chan, char *data)
451 {
452         MYSQL_RES *mysqlres;
453         MYSQL_ROW mysqlrow;
454         AST_DECLARE_APP_ARGS(args,
455                 AST_APP_ARG(fetch);
456                 AST_APP_ARG(resultvar);
457                 AST_APP_ARG(fetchid);
458                 AST_APP_ARG(vars);
459         );
460         char *s5, *parse;
461         int resultid = -1, numFields, j;
462
463         parse = ast_strdupa(data);
464         AST_NONSTANDARD_APP_ARGS(args, parse, ' ');
465         sscanf(args.fetchid, "%30d", &resultid);
466
467         if (args.resultvar && (resultid >= 0) ) {
468                 if ((mysqlres = find_identifier(resultid, AST_MYSQL_ID_RESID)) != NULL) {
469                         /* Grab the next row */
470                         if ((mysqlrow = mysql_fetch_row(mysqlres)) != NULL) {
471                                 numFields = mysql_num_fields(mysqlres);
472                                 for (j = 0; j < numFields; j++) {
473                                         s5 = strsep(&args.vars, " ");
474                                         if (s5 == NULL) {
475                                                 ast_log(LOG_WARNING, "ast_MYSQL_fetch: More fields (%d) than variables (%d)\n", numFields, j);
476                                                 break;
477                                         }
478
479                                         pbx_builtin_setvar_helper(chan, s5, mysqlrow[j] ? mysqlrow[j] :
480                                                 nullvalue == NULLSTRING ? "NULL" :
481                                                 nullvalue == EMPTYSTRING ? "" :
482                                                 NULL);
483                                 }
484                                 ast_debug(5, "ast_MYSQL_fetch: numFields=%d\n", numFields);
485                                 set_asterisk_int(chan, args.resultvar, 1); /* try more rows */
486                         } else {
487                                 ast_debug(5, "ast_MYSQL_fetch : EOF\n");
488                                 set_asterisk_int(chan, args.resultvar, 0); /* no more rows */
489                         }
490                         return 0;
491                 } else {
492                         set_asterisk_int(chan, args.resultvar, 0);
493                         ast_log(LOG_WARNING, "aMYSQL_fetch: Invalid result identifier %d passed\n", resultid);
494                 }
495         } else {
496                 ast_log(LOG_WARNING, "aMYSQL_fetch: missing some arguments\n");
497         }
498
499         return -1;
500 }
501
502 static int aMYSQL_clear(struct ast_channel *chan, char *data)
503 {
504         MYSQL_RES *mysqlres;
505
506         int id;
507         strsep(&data, " "); /* eat the first token, we already know it :P */
508         id = safe_scan_int(&data, " \n", -1);
509         if ((mysqlres = find_identifier(id, AST_MYSQL_ID_RESID)) == NULL) {
510                 ast_log(LOG_WARNING, "Invalid result identifier %d passed in aMYSQL_clear\n", id);
511         } else {
512                 mysql_free_result(mysqlres);
513                 del_identifier(id, AST_MYSQL_ID_RESID);
514         }
515
516         return 0;
517 }
518
519 static int aMYSQL_disconnect(struct ast_channel *chan, char *data)
520 {
521         MYSQL *mysql;
522         int id;
523         strsep(&data, " "); /* eat the first token, we already know it :P */
524
525         id = safe_scan_int(&data, " \n", -1);
526         if ((mysql = find_identifier(id, AST_MYSQL_ID_CONNID)) == NULL) {
527                 ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aMYSQL_disconnect\n", id);
528         } else {
529                 mysql_close(mysql);
530                 del_identifier(id, AST_MYSQL_ID_CONNID);
531         }
532
533         return 0;
534 }
535
536 static int MYSQL_exec(struct ast_channel *chan, const char *data)
537 {
538         int result;
539         char sresult[10];
540
541         ast_debug(5, "MYSQL: data=%s\n", data);
542
543         if (!data) {
544                 ast_log(LOG_WARNING, "MYSQL requires an argument (see manual)\n");
545                 return -1;
546         }
547
548         result = 0;
549
550         if (autoclear) {
551                 struct ast_datastore *mysql_store = NULL;
552
553                 ast_channel_lock(chan);
554                 mysql_store = ast_channel_datastore_find(chan, &mysql_ds_info, NULL);
555                 if (!mysql_store) {
556                         if (!(mysql_store = ast_datastore_alloc(&mysql_ds_info, NULL))) {
557                                 ast_log(LOG_WARNING, "Unable to allocate new datastore.\n");
558                         } else {
559                                 mysql_store->data = chan;
560                                 ast_channel_datastore_add(chan, mysql_store);
561                         }
562                 }
563                 ast_channel_unlock(chan);
564         }
565         ast_mutex_lock(&_mysql_mutex);
566
567         if (strncasecmp("connect", data, strlen("connect")) == 0) {
568                 result = aMYSQL_connect(chan, ast_strdupa(data));
569         } else if (strncasecmp("query", data, strlen("query")) == 0) {
570                 result = aMYSQL_query(chan, ast_strdupa(data));
571         } else if (strncasecmp("nextresult", data, strlen("nextresult")) == 0) {
572                 result = aMYSQL_nextresult(chan, ast_strdupa(data));
573         } else if (strncasecmp("fetch", data, strlen("fetch")) == 0) {
574                 result = aMYSQL_fetch(chan, ast_strdupa(data));
575         } else if (strncasecmp("clear", data, strlen("clear")) == 0) {
576                 result = aMYSQL_clear(chan, ast_strdupa(data));
577         } else if (strncasecmp("disconnect", data, strlen("disconnect")) == 0) {
578                 result = aMYSQL_disconnect(chan, ast_strdupa(data));
579         } else if (strncasecmp("set", data, 3) == 0) {
580                 result = aMYSQL_set(chan, ast_strdupa(data));
581         } else {
582                 ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n", data);
583                 result = -1;
584         }
585
586         ast_mutex_unlock(&_mysql_mutex);
587
588         snprintf(sresult, sizeof(sresult), "%d", result);
589         pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
590         return 0;
591 }
592
593 static int unload_module(void)
594 {
595         return ast_unregister_application(app);
596 }
597
598 static int load_module(void)
599 {
600         struct MYSQLidshead *headp = &_mysql_ids_head;
601         struct ast_flags config_flags = { 0 };
602         struct ast_config *cfg = ast_config_load(MYSQL_CONFIG, config_flags);
603         const char *temp;
604
605         if (!cfg) {
606                 /* Backwards compatibility ftw */
607                 cfg = ast_config_load(MYSQL_CONFIG_OLD, config_flags);
608         }
609
610         if (cfg) {
611                 if ((temp = ast_variable_retrieve(cfg, "general", "nullvalue"))) {
612                         if (!strcasecmp(temp, "nullstring")) {
613                                 nullvalue = NULLSTRING;
614                         } else if (!strcasecmp(temp, "emptystring")) {
615                                 nullvalue = EMPTYSTRING;
616                         } else if (!strcasecmp(temp, "null")) {
617                                 nullvalue = NULLVALUE;
618                         } else {
619                                 ast_log(LOG_WARNING, "Illegal value for 'nullvalue': '%s' (must be 'nullstring', 'null', or 'emptystring')\n", temp);
620                         }
621                 }
622                 if ((temp = ast_variable_retrieve(cfg, "general", "autoclear")) && ast_true(temp)) {
623                         autoclear = 1;
624                 }
625                 ast_config_destroy(cfg);
626         }
627
628         AST_LIST_HEAD_INIT(headp);
629         return ast_register_application(app, MYSQL_exec, synopsis, descrip);
630 }
631
632 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple Mysql Interface");