2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Proformatique
6 * Written by Richard Braun <rbraun@proformatique.com>
8 * Based on res_sqlite3 by Anthony Minessale II,
9 * and res_config_mysql by Matthew Boehm
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
23 * \mainpage res_config_sqlite
25 * \section intro_sec Presentation
27 * res_config_sqlite is a module for the Asterisk Open Source PBX to
28 * support SQLite 2 databases. It can be used to fetch configuration
29 * from a database (static configuration files and/or using the Asterisk
30 * RealTime Architecture - ARA).
31 * It can also be used to log CDR entries. Finally, it can be used for simple
32 * queries in the Dialplan. Note that Asterisk already comes with a module
33 * named cdr_sqlite. There are two reasons for including it in res_sqlite:
34 * the first is that rewriting it was a training to learn how to write a
35 * simple module for Asterisk, the other is to have the same database open for
36 * all kinds of operations, which improves reliability and performance.
38 * There is already a module for SQLite 3 (named res_sqlite3) in the Asterisk
39 * addons. res_sqlite was developed because we, at Proformatique, are using
40 * PHP 4 in our embedded systems, and PHP 4 has no stable support for SQLite 3
41 * at this time. We also needed RealTime support.
43 * \section conf_sec Configuration
45 * The main configuration file is res_config_sqlite.conf. It must be readable or
46 * res_sqlite will fail to start. It is suggested to use the sample file
47 * in this package as a starting point. The file has only one section
48 * named <code>general</code>. Here are the supported parameters :
51 * <dt><code>dbfile</code></dt>
52 * <dd>The absolute path to the SQLite database (the file can be non existent,
53 * res_sqlite will create it if is has the appropriate rights)</dd>
54 * <dt><code>config_table</code></dt>
55 * <dd>The table used for static configuration</dd>
56 * <dt><code>cdr_table</code></dt>
57 * <dd>The table used to store CDR entries (if ommitted, CDR support is
61 * To use res_sqlite for static and/or RealTime configuration, refer to the
62 * Asterisk documentation. The file tables.sql can be used to create the
65 * The SQLITE() application is very similar to the MYSQL() application. You
66 * can find more details at
67 * <a href="http://voip-info.org/wiki/view/Asterisk+cmd+MYSQL">http://voip-info.org/wiki/view/Asterisk+cmd+MYSQL</a>.
68 * The main difference is that you cannot choose your database - it's the
69 * file set in the <code>dbfile</code> parameter. As a result, there is no
70 * Connect or Disconnect command, and there is no connid variable.
72 * \section status_sec Driver status
74 * The CLI command <code>show sqlite status</code> returns status information
75 * about the running driver. One information is more important than others:
76 * the number of registered virtual machines. A SQLite virtual machine is
77 * created each time a SQLITE() query command is used. If the number of
78 * registered virtual machines isn't 0 (or near 0, since one or more SQLITE()
79 * commands can be running when requesting the module status) and increases
80 * over time, this probably means that you're badly using the application
81 * and you're creating resource leaks. You should check your Dialplan and
82 * reload res_sqlite (by unloading and then loading again - reloading isn't
85 * \section credits_sec Credits
87 * res_config_sqlite was developed by Richard Braun at the Proformatique company.
92 * \brief res_sqlite module.
96 <depend>sqlite</depend>
107 #include "asterisk/pbx.h"
108 #include "asterisk/cdr.h"
109 #include "asterisk/cli.h"
110 #include "asterisk/lock.h"
111 #include "asterisk/config.h"
112 #include "asterisk/logger.h"
113 #include "asterisk/module.h"
114 #include "asterisk/options.h"
115 #include "asterisk/linkedlists.h"
117 #define RES_SQLITE_NAME "res_sqlite"
118 #define RES_SQLITE_DRIVER "sqlite"
119 #define RES_SQLITE_APP_DRIVER "SQLITE"
120 #define RES_SQLITE_DESCRIPTION "Resource Module for SQLite 2"
121 #define RES_SQLITE_CONF_FILE "res_config_sqlite.conf"
122 #define RES_SQLITE_APP_SYNOPSIS "Dialplan access to SQLite 2"
123 #define RES_SQLITE_APP_DESCRIPTION \
124 "SQLITE(): " RES_SQLITE_APP_SYNOPSIS "\n"
125 #define RES_SQLITE_STATUS_SUMMARY \
126 "Show status information about the SQLite 2 driver"
127 #define RES_SQLITE_STATUS_USAGE \
128 "Usage: show sqlite status\n" \
129 " " RES_SQLITE_STATUS_SUMMARY "\n"
132 RES_SQLITE_CONFIG_ID,
133 RES_SQLITE_CONFIG_COMMENTED,
134 RES_SQLITE_CONFIG_FILENAME,
135 RES_SQLITE_CONFIG_CATEGORY,
136 RES_SQLITE_CONFIG_VAR_NAME,
137 RES_SQLITE_CONFIG_VAR_VAL,
138 RES_SQLITE_CONFIG_COLUMNS,
142 * Limit the number of maximum simultaneous registered SQLite VMs to avoid
143 * a denial of service attack.
145 #define RES_SQLITE_VM_MAX 1024
147 #define SET_VAR(config, to, from) \
151 __error = set_var(&to, #to, from->value); \
154 ast_config_destroy(config); \
162 * Maximum number of loops before giving up executing a query. Calls to
163 * sqlite_xxx() functions which can return SQLITE_BUSY or SQLITE_LOCKED
164 * are enclosed by RES_SQLITE_BEGIN and RES_SQLITE_END, e.g.
170 * error = sqlite_exec(db, query, NULL, NULL, &errormsg);
171 * RES_SQLITE_END(error)
177 #define RES_SQLITE_MAX_LOOPS 10
180 * Macro used before executing a query.
182 * \see RES_SQLITE_MAX_LOOPS.
184 #define RES_SQLITE_BEGIN \
187 for (__i = 0; __i < RES_SQLITE_MAX_LOOPS; __i++) \
191 * Macro used after executing a query.
193 * \see RES_SQLITE_MAX_LOOPS.
195 #define RES_SQLITE_END(error) \
196 if (error != SQLITE_BUSY && error != SQLITE_LOCKED) \
203 * Structure sent to the SQLite callback function for static configuration.
205 * \see add_cfg_entry()
207 struct cfg_entry_args {
208 struct ast_config *cfg;
209 struct ast_category *cat;
214 * Structure sent to the SQLite callback function for RealTime configuration.
216 * \see add_rt_cfg_entry()
218 struct rt_cfg_entry_args {
219 struct ast_variable *var;
220 struct ast_variable *last;
224 * Structure sent to the SQLite callback function for RealTime configuration
225 * (realtime_multi_handler()).
227 * \see add_rt_multi_cfg_entry()
229 struct rt_multi_cfg_entry_args {
230 struct ast_config *cfg;
235 * Allocate a variable.
237 * \param var the address of the variable to set (it will be allocated)
238 * \param name the name of the variable (for error handling)
239 * \param value the value to store in var
240 * \return 1 if an allocation error occurred, 0 otherwise
242 static int set_var(char **var, char *name, char *value);
245 * Load the configuration file.
247 * This function sets dbfile, config_table, and cdr_table. It calls
248 * check_vars() before returning, and unload_config() if an error occurred.
250 * \return 1 if an error occurred, 0 otherwise
251 * \see unload_config()
253 static int load_config(void);
256 * Free resources related to configuration.
260 static void unload_config(void);
263 * Asterisk callback function for CDR support.
265 * Asterisk will call this function each time a CDR entry must be logged if
266 * CDR support is enabled.
268 * \param cdr the CDR entry Asterisk sends us
269 * \return 1 if an error occurred, 0 otherwise
271 static int cdr_handler(struct ast_cdr *cdr);
274 * SQLite callback function for static configuration.
276 * This function is passed to the SQLite engine as a callback function to
277 * parse a row and store it in a struct ast_config object. It relies on
278 * resulting rows being sorted by category.
280 * \param arg a pointer to a struct cfg_entry_args object
281 * \param argc number of columns
282 * \param argv values in the row
283 * \param columnNames names and types of the columns
284 * \return 1 if an error occurred, 0 otherwise
285 * \see cfg_entry_args
286 * \see sql_get_config_table
287 * \see config_handler()
289 static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames);
292 * Asterisk callback function for static configuration.
294 * Asterisk will call this function when it loads its static configuration,
295 * which usually happens at startup and reload.
297 * \param database the database to use (ignored)
298 * \param table the table to use
299 * \param file the file to load from the database
300 * \param cfg the struct ast_config object to use when storing variables
301 * \param withcomments Integer. Flag
302 * \return NULL if an error occurred, cfg otherwise
303 * \see add_cfg_entry()
305 static struct ast_config * config_handler(const char *database,
306 const char *table, const char *file,
307 struct ast_config *cfg, int withcomments);
310 * Helper function to parse a va_list object into 2 dynamic arrays of
311 * strings, parameters and values.
313 * ap must have the following format : param1 val1 param2 val2 param3 val3 ...
314 * arguments will be extracted to create 2 arrays:
317 * <li>params : param1 param2 param3 ...</li>
318 * <li>vals : val1 val2 val3 ...</li>
321 * The address of these arrays are stored in params_ptr and vals_ptr. It
322 * is the responsibility of the caller to release the memory of these arrays.
323 * It is considered an error that va_list has a null or odd number of strings.
325 * \param ap the va_list object to parse
326 * \param params_ptr where the address of the params array is stored
327 * \param vals_ptr where the address of the vals array is stored
328 * \return 0 if an error occurred, the number of elements in the arrays (which
329 * have the same size) otherwise
331 static size_t get_params(va_list ap, const char ***params_ptr,
332 const char ***vals_ptr);
335 * SQLite callback function for RealTime configuration.
337 * This function is passed to the SQLite engine as a callback function to
338 * parse a row and store it in a linked list of struct ast_variable objects.
340 * \param arg a pointer to a struct rt_cfg_entry_args object
341 * \param argc number of columns
342 * \param argv values in the row
343 * \param columnNames names and types of the columns
344 * \return 1 if an error occurred, 0 otherwise
345 * \see rt_cfg_entry_args
346 * \see realtime_handler()
348 static int add_rt_cfg_entry(void *arg, int argc, char **argv,
352 * Asterisk callback function for RealTime configuration.
354 * Asterisk will call this function each time it requires a variable
355 * through the RealTime architecture. ap is a list of parameters and
356 * values used to find a specific row, e.g one parameter "name" and
357 * one value "123" so that the SQL query becomes <code>SELECT * FROM
358 * table WHERE name = '123';</code>.
360 * \param database the database to use (ignored)
361 * \param table the table to use
362 * \param ap list of parameters and values to match
363 * \return NULL if an error occurred, a linked list of struct ast_variable
365 * \see add_rt_cfg_entry()
367 static struct ast_variable * realtime_handler(const char *database,
368 const char *table, va_list ap);
371 * SQLite callback function for RealTime configuration.
373 * This function performs the same actions as add_rt_cfg_entry() except
374 * that the rt_multi_cfg_entry_args structure is designed to store
375 * categories in addition of variables.
377 * \param arg a pointer to a struct rt_multi_cfg_entry_args object
378 * \param argc number of columns
379 * \param argv values in the row
380 * \param columnNames names and types of the columns
381 * \return 1 if an error occurred, 0 otherwise
382 * \see rt_multi_cfg_entry_args
383 * \see realtime_multi_handler()
385 static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv,
389 * Asterisk callback function for RealTime configuration.
391 * This function performs the same actions as realtime_handler() except
392 * that it can store variables per category, and can return several
395 * \param database the database to use (ignored)
396 * \param table the table to use
397 * \param ap list of parameters and values to match
398 * \return NULL if an error occurred, a struct ast_config object storing
399 * categories and variables
400 * \see add_rt_multi_cfg_entry()
402 static struct ast_config * realtime_multi_handler(const char *database,
407 * Asterisk callback function for RealTime configuration (variable
410 * Asterisk will call this function each time a variable has been modified
411 * internally and must be updated in the backend engine. keyfield and entity
412 * are used to find the row to update, e.g. <code>UPDATE table SET ... WHERE
413 * keyfield = 'entity';</code>. ap is a list of parameters and values with the
414 * same format as the other realtime functions.
416 * \param database the database to use (ignored)
417 * \param table the table to use
418 * \param keyfield the column of the matching cell
419 * \param entity the value of the matching cell
420 * \param ap list of parameters and new values to update in the database
421 * \return -1 if an error occurred, the number of affected rows otherwise
423 static int realtime_update_handler(const char *database, const char *table,
424 const char *keyfield, const char *entity,
428 * Asterisk callback function for the CLI status command.
430 * \param fd file descriptor provided by Asterisk to use with ast_cli()
431 * \param argc number of arguments
432 * \param argv arguments list
433 * \return RESULT_SUCCESS
435 static int cli_status(int fd, int argc, char *argv[]);
438 * The SQLite database object.
443 * Set to 1 if CDR support is enabled.
448 * Set to 1 if the CDR callback function was registered.
450 static int cdr_registered;
453 * Set to 1 if the CLI status command callback function was registered.
455 static int cli_status_registered;
458 * The path of the database file.
463 * The name of the static configuration table.
465 static char *config_table;
468 * The name of the table used to store CDR entries.
470 static char *cdr_table;
473 * The number of registered virtual machines.
478 * The structure specifying all callback functions used by Asterisk for static
479 * and RealTime configuration.
481 static struct ast_config_engine sqlite_engine =
483 .name = RES_SQLITE_DRIVER,
484 .load_func = config_handler,
485 .realtime_func = realtime_handler,
486 .realtime_multi_func = realtime_multi_handler,
487 .update_func = realtime_update_handler
491 * The mutex used to prevent simultaneous access to the SQLite database.
492 * SQLite isn't always compiled with thread safety.
494 AST_MUTEX_DEFINE_STATIC(mutex);
497 * Structure containing details and callback functions for the CLI status
500 static struct ast_cli_entry cli_status_cmd =
502 .cmda = {"show", "sqlite", "status", NULL},
503 .handler = cli_status,
504 .summary = RES_SQLITE_STATUS_SUMMARY,
505 .usage = RES_SQLITE_STATUS_USAGE
509 * Taken from Asterisk 1.2 cdr_sqlite.so.
513 * SQL query format to create the CDR table if non existent.
515 static char *sql_create_cdr_table =
516 "CREATE TABLE '%q' ("
517 " id INTEGER PRIMARY KEY,"
518 " clid VARCHAR(80) NOT NULL DEFAULT '',"
519 " src VARCHAR(80) NOT NULL DEFAULT '',"
520 " dst VARCHAR(80) NOT NULL DEFAULT '',"
521 " dcontext VARCHAR(80) NOT NULL DEFAULT '',"
522 " channel VARCHAR(80) NOT NULL DEFAULT '',"
523 " dstchannel VARCHAR(80) NOT NULL DEFAULT '',"
524 " lastapp VARCHAR(80) NOT NULL DEFAULT '',"
525 " lastdata VARCHAR(80) NOT NULL DEFAULT '',"
526 " start CHAR(19) NOT NULL DEFAULT '0000-00-00 00:00:00',"
527 " answer CHAR(19) NOT NULL DEFAULT '0000-00-00 00:00:00',"
528 " end CHAR(19) NOT NULL DEFAULT '0000-00-00 00:00:00',"
529 " duration INT(11) NOT NULL DEFAULT '0',"
530 " billsec INT(11) NOT NULL DEFAULT '0',"
531 " disposition INT(11) NOT NULL DEFAULT '0',"
532 " amaflags INT(11) NOT NULL DEFAULT '0',"
533 " accountcode VARCHAR(20) NOT NULL DEFAULT '',"
534 " uniqueid VARCHAR(32) NOT NULL DEFAULT '',"
535 " userfield VARCHAR(255) NOT NULL DEFAULT ''"
539 * SQL query format to insert a CDR entry.
541 static char *sql_add_cdr_entry =
570 " datetime(%d,'unixepoch'),"
571 " datetime(%d,'unixepoch'),"
572 " datetime(%d,'unixepoch'),"
583 * SQL query format to fetch the static configuration of a file.
584 * Rows must be sorted by category.
586 * @see add_cfg_entry()
588 static char *sql_get_config_table =
591 " WHERE filename = '%q' AND commented = 0"
592 " ORDER BY category;";
594 static int set_var(char **var, char *name, char *value)
599 *var = ast_strdup(value);
602 ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
609 static int check_vars(void)
612 ast_log(LOG_ERROR, "Undefined parameter %s\n", dbfile);
616 use_cdr = (cdr_table != NULL);
621 static int load_config(void)
623 struct ast_config *config;
624 struct ast_variable *var;
627 config = ast_config_load(RES_SQLITE_CONF_FILE);
630 ast_log(LOG_ERROR, "Unable to load " RES_SQLITE_CONF_FILE "\n");
634 for (var = ast_variable_browse(config, "general"); var; var = var->next) {
635 if (!strcasecmp(var->name, "dbfile"))
636 SET_VAR(config, dbfile, var);
637 else if (!strcasecmp(var->name, "config_table"))
638 SET_VAR(config, config_table, var);
639 else if (!strcasecmp(var->name, "cdr_table"))
640 SET_VAR(config, cdr_table, var);
642 ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
645 ast_config_destroy(config);
646 error = check_vars();
656 static void unload_config(void)
660 ast_free(config_table);
666 static int cdr_handler(struct ast_cdr *cdr)
671 ast_mutex_lock(&mutex);
674 error = sqlite_exec_printf(db, sql_add_cdr_entry, NULL, NULL, &errormsg,
675 cdr_table, cdr->clid, cdr->src, cdr->dst,
676 cdr->dcontext, cdr->channel, cdr->dstchannel,
677 cdr->lastapp, cdr->lastdata, cdr->start.tv_sec,
678 cdr->answer.tv_sec, cdr->end.tv_sec,
679 cdr->duration, cdr->billsec, cdr->disposition,
680 cdr->amaflags, cdr->accountcode, cdr->uniqueid,
682 RES_SQLITE_END(error)
684 ast_mutex_unlock(&mutex);
687 ast_log(LOG_ERROR, "%s\n", errormsg);
695 static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
697 struct cfg_entry_args *args;
698 struct ast_variable *var;
700 if (argc != RES_SQLITE_CONFIG_COLUMNS) {
701 ast_log(LOG_WARNING, "Corrupt table\n");
707 if (!args->cat_name || strcmp(args->cat_name, argv[RES_SQLITE_CONFIG_CATEGORY])) {
708 args->cat = ast_category_new(argv[RES_SQLITE_CONFIG_CATEGORY]);
711 ast_log(LOG_WARNING, "Unable to allocate category\n");
715 ast_free(args->cat_name);
716 args->cat_name = ast_strdup(argv[RES_SQLITE_CONFIG_CATEGORY]);
718 if (!args->cat_name) {
719 ast_category_destroy(args->cat);
723 ast_category_append(args->cfg, args->cat);
726 var = ast_variable_new(argv[RES_SQLITE_CONFIG_VAR_NAME],
727 argv[RES_SQLITE_CONFIG_VAR_VAL]);
730 ast_log(LOG_WARNING, "Unable to allocate variable");
734 ast_variable_append(args->cat, var);
739 static struct ast_config *config_handler(const char *database,
740 const char *table, const char *file, struct ast_config *cfg, int withcomments)
742 struct cfg_entry_args args;
748 ast_log(LOG_ERROR, "Table name unspecified\n");
752 table = config_table;
756 args.cat_name = NULL;
758 ast_mutex_lock(&mutex);
761 error = sqlite_exec_printf(db, sql_get_config_table, add_cfg_entry,
762 &args, &errormsg, table, file);
763 RES_SQLITE_END(error)
765 ast_mutex_unlock(&mutex);
767 ast_free(args.cat_name);
770 ast_log(LOG_ERROR, "%s\n", errormsg);
778 static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr)
780 const char **tmp, *param, *val, **params, **vals;
787 while ((param = va_arg(ap, const char *)) && (val = va_arg(ap, const char *))) {
788 if (!(tmp = ast_realloc(params, (params_count + 1) * sizeof(char *)))) {
795 if (!(tmp = ast_realloc(vals, (params_count + 1) * sizeof(char *)))) {
802 params[params_count] = param;
803 vals[params_count] = val;
807 if (params_count > 0) {
808 *params_ptr = params;
811 ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
816 static int add_rt_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
818 struct rt_cfg_entry_args *args;
819 struct ast_variable *var;
824 for (i = 0; i < argc; i++) {
828 if (!(var = ast_variable_new(columnNames[i], argv[i])))
837 args->last->next = var;
845 static struct ast_variable *
846 realtime_handler(const char *database, const char *table, va_list ap)
848 char *query, *errormsg, *op, *tmp_str;
849 struct rt_cfg_entry_args args;
850 const char **params, **vals;
855 ast_log(LOG_WARNING, "Table name unspecified\n");
859 params_count = get_params(ap, ¶ms, &vals);
861 if (params_count == 0)
864 op = (strchr(params[0], ' ') == NULL) ? " =" : "";
866 /* \cond DOXYGEN_CAN_PARSE_THIS */
868 #define QUERY "SELECT * FROM '%q' WHERE commented = 0 AND %q%s '%q'"
871 query = sqlite_mprintf(QUERY, table, params[0], op, vals[0]);
874 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
880 if (params_count > 1) {
883 for (i = 1; i < params_count; i++) {
884 op = (strchr(params[i], ' ') == NULL) ? " =" : "";
885 tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op,
887 sqlite_freemem(query);
890 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
903 tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
904 sqlite_freemem(query);
907 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
912 ast_debug(1, "SQL query: %s\n", query);
916 ast_mutex_lock(&mutex);
919 error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
920 RES_SQLITE_END(error)
922 ast_mutex_unlock(&mutex);
924 sqlite_freemem(query);
927 ast_log(LOG_WARNING, "%s\n", errormsg);
929 ast_variables_destroy(args.var);
936 static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
938 struct rt_multi_cfg_entry_args *args;
939 struct ast_category *cat;
940 struct ast_variable *var;
944 args = (struct rt_multi_cfg_entry_args *)arg;
948 * cat_name should always be set here, since initfield is forged from
949 * params[0] in realtime_multi_handler(), which is a search parameter
952 for (i = 0; i < argc; i++) {
953 if (!strcmp(args->initfield, columnNames[i]))
958 ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
962 if (!(cat = ast_category_new(cat_name))) {
963 ast_log(LOG_WARNING, "Unable to allocate category\n");
967 ast_category_append(args->cfg, cat);
969 for (i = 0; i < argc; i++) {
970 if (!argv[i] || !strcmp(args->initfield, columnNames[i]))
973 if (!(var = ast_variable_new(columnNames[i], argv[i]))) {
974 ast_log(LOG_WARNING, "Unable to allocate variable\n");
978 ast_variable_append(cat, var);
984 static struct ast_config *realtime_multi_handler(const char *database,
985 const char *table, va_list ap)
987 char *query, *errormsg, *op, *tmp_str, *initfield;
988 struct rt_multi_cfg_entry_args args;
989 const char **params, **vals;
990 struct ast_config *cfg;
995 ast_log(LOG_WARNING, "Table name unspecified\n");
999 if (!(cfg = ast_config_new())) {
1000 ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
1004 if (!(params_count = get_params(ap, ¶ms, &vals))) {
1005 ast_config_destroy(cfg);
1009 if (!(initfield = ast_strdup(params[0]))) {
1010 ast_config_destroy(cfg);
1016 tmp_str = strchr(initfield, ' ');
1021 op = (!strchr(params[0], ' ')) ? " =" : "";
1024 * Asterisk sends us an already escaped string when searching for
1025 * "exten LIKE" (uh!). Handle it separately.
1027 tmp_str = (!strcmp(vals[0], "\\_%")) ? "_%" : (char *)vals[0];
1029 /* \cond DOXYGEN_CAN_PARSE_THIS */
1031 #define QUERY "SELECT * FROM '%q' WHERE commented = 0 AND %q%s '%q'"
1034 if (!(query = sqlite_mprintf(QUERY, table, params[0], op, tmp_str))) {
1035 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1036 ast_config_destroy(cfg);
1039 ast_free(initfield);
1043 if (params_count > 1) {
1046 for (i = 1; i < params_count; i++) {
1047 op = (!strchr(params[i], ' ')) ? " =" : "";
1048 tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op,
1050 sqlite_freemem(query);
1053 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1054 ast_config_destroy(cfg);
1057 ast_free(initfield);
1068 if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
1069 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1070 ast_config_destroy(cfg);
1071 ast_free(initfield);
1075 sqlite_freemem(query);
1077 ast_debug(1, "SQL query: %s\n", query);
1079 args.initfield = initfield;
1081 ast_mutex_lock(&mutex);
1084 error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
1085 RES_SQLITE_END(error)
1087 ast_mutex_unlock(&mutex);
1089 sqlite_freemem(query);
1090 ast_free(initfield);
1093 ast_log(LOG_WARNING, "%s\n", errormsg);
1095 ast_config_destroy(cfg);
1102 static int realtime_update_handler(const char *database, const char *table,
1103 const char *keyfield, const char *entity,
1106 char *query, *errormsg, *tmp_str;
1107 const char **params, **vals;
1108 size_t params_count;
1109 int error, rows_num;
1112 ast_log(LOG_WARNING, "Table name unspecified\n");
1116 if (!(params_count = get_params(ap, ¶ms, &vals)))
1119 /* \cond DOXYGEN_CAN_PARSE_THIS */
1121 #define QUERY "UPDATE '%q' SET %q = '%q'"
1124 if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
1125 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1131 if (params_count > 1) {
1134 for (i = 1; i < params_count; i++) {
1135 tmp_str = sqlite_mprintf("%s, %q = '%q'", query, params[i],
1137 sqlite_freemem(query);
1140 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1153 if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
1154 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1158 sqlite_freemem(query);
1160 ast_debug(1, "SQL query: %s\n", query);
1162 ast_mutex_lock(&mutex);
1165 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1166 RES_SQLITE_END(error)
1169 rows_num = sqlite_changes(db);
1173 ast_mutex_unlock(&mutex);
1175 sqlite_freemem(query);
1178 ast_log(LOG_WARNING, "%s\n", errormsg);
1185 static int cli_status(int fd, int argc, char *argv[])
1187 ast_cli(fd, "SQLite database path: %s\n", dbfile);
1188 ast_cli(fd, "config_table: ");
1191 ast_cli(fd, "unspecified, must be present in extconfig.conf\n");
1193 ast_cli(fd, "%s\n", config_table);
1195 ast_cli(fd, "cdr_table: ");
1198 ast_cli(fd, "unspecified, CDR support disabled\n");
1200 ast_cli(fd, "%s\n", cdr_table);
1202 return RESULT_SUCCESS;
1205 static int unload_module(void)
1207 if (cli_status_registered)
1208 ast_cli_unregister(&cli_status_cmd);
1211 ast_cdr_unregister(RES_SQLITE_NAME);
1213 ast_config_engine_deregister(&sqlite_engine);
1223 static int load_module(void)
1230 cli_status_registered = 0;
1232 config_table = NULL;
1235 error = load_config();
1238 return AST_MODULE_LOAD_DECLINE;
1240 if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
1241 ast_log(LOG_ERROR, "%s\n", errormsg);
1247 ast_config_engine_register(&sqlite_engine);
1251 error = sqlite_exec_printf(db, "SELECT COUNT(id) FROM %Q;", NULL, NULL,
1252 &errormsg, cdr_table);
1253 RES_SQLITE_END(error)
1259 if (error != SQLITE_ERROR) {
1260 ast_log(LOG_ERROR, "%s\n", errormsg);
1267 error = sqlite_exec_printf(db, sql_create_cdr_table, NULL, NULL,
1268 &errormsg, cdr_table);
1269 RES_SQLITE_END(error)
1272 ast_log(LOG_ERROR, "%s\n", errormsg);
1279 error = ast_cdr_register(RES_SQLITE_NAME, RES_SQLITE_DESCRIPTION,
1290 error = ast_cli_register(&cli_status_cmd);
1297 cli_status_registered = 1;
1302 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Realtime SQLite configuration",
1303 .load = load_module,
1304 .unload = unload_module,