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 * \page 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). It can also be used to log CDR entries.
31 * Note that Asterisk already comes with a module named cdr_sqlite.
32 * There are two reasons for including it in res_config_sqlite:
33 * the first is that rewriting it was a training to learn how to write a
34 * simple module for Asterisk, the other is to have the same database open for
35 * all kinds of operations, which improves reliability and performance.
37 * \section conf_sec Configuration
39 * The main configuration file is res_config_sqlite.conf. It must be readable or
40 * res_config_sqlite will fail to start. It is suggested to use the sample file
41 * in this package as a starting point. The file has only one section
42 * named <code>general</code>. Here are the supported parameters :
45 * <dt><code>dbfile</code></dt>
46 * <dd>The absolute path to the SQLite database (the file can be non existent,
47 * res_config_sqlite will create it if it has the appropriate rights)</dd>
48 * <dt><code>config_table</code></dt>
49 * <dd>The table used for static configuration</dd>
50 * <dt><code>cdr_table</code></dt>
51 * <dd>The table used to store CDR entries (if ommitted, CDR support is
55 * To use res_config_sqlite for static and/or RealTime configuration, refer to the
56 * Asterisk documentation. The file tables.sql can be used to create the
59 * \section status_sec Driver status
61 * The CLI command <code>show sqlite status</code> returns status information
62 * about the running driver.
64 * \section credits_sec Credits
66 * res_config_sqlite was developed by Richard Braun at the Proformatique company.
71 * \brief res_config_sqlite module.
75 <depend>sqlite</depend>
76 <support_level>extended</support_level>
80 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
84 #include "asterisk/logger.h"
85 #include "asterisk/app.h"
86 #include "asterisk/pbx.h"
87 #include "asterisk/cdr.h"
88 #include "asterisk/cli.h"
89 #include "asterisk/lock.h"
90 #include "asterisk/config.h"
91 #include "asterisk/module.h"
92 #include "asterisk/linkedlists.h"
94 #define MACRO_BEGIN do {
95 #define MACRO_END } while (0)
97 #define RES_CONFIG_SQLITE_NAME "res_config_sqlite"
98 #define RES_CONFIG_SQLITE_DRIVER "sqlite"
99 #define RES_CONFIG_SQLITE_DESCRIPTION "Resource Module for SQLite 2"
100 #define RES_CONFIG_SQLITE_CONF_FILE "res_config_sqlite.conf"
103 RES_CONFIG_SQLITE_CONFIG_ID,
104 RES_CONFIG_SQLITE_CONFIG_CAT_METRIC,
105 RES_CONFIG_SQLITE_CONFIG_VAR_METRIC,
106 RES_CONFIG_SQLITE_CONFIG_COMMENTED,
107 RES_CONFIG_SQLITE_CONFIG_FILENAME,
108 RES_CONFIG_SQLITE_CONFIG_CATEGORY,
109 RES_CONFIG_SQLITE_CONFIG_VAR_NAME,
110 RES_CONFIG_SQLITE_CONFIG_VAR_VAL,
111 RES_CONFIG_SQLITE_CONFIG_COLUMNS,
114 #define SET_VAR(config, to, from) \
118 __error = set_var(&to, #to, from->value); \
121 ast_config_destroy(config); \
127 AST_THREADSTORAGE(sql_buf);
128 AST_THREADSTORAGE(where_buf);
131 * Maximum number of loops before giving up executing a query. Calls to
132 * sqlite_xxx() functions which can return SQLITE_BUSY
133 * are enclosed by RES_CONFIG_SQLITE_BEGIN and RES_CONFIG_SQLITE_END, e.g.
138 * RES_CONFIG_SQLITE_BEGIN
139 * error = sqlite_exec(db, query, NULL, NULL, &errormsg);
140 * RES_CONFIG_SQLITE_END(error)
146 #define RES_CONFIG_SQLITE_MAX_LOOPS 10
149 * Macro used before executing a query.
151 * \see RES_CONFIG_SQLITE_MAX_LOOPS.
153 #define RES_CONFIG_SQLITE_BEGIN \
157 for (__i = 0; __i < RES_CONFIG_SQLITE_MAX_LOOPS; __i++) {
160 * Macro used after executing a query.
162 * \see RES_CONFIG_SQLITE_MAX_LOOPS.
164 #define RES_CONFIG_SQLITE_END(error) \
165 if (error != SQLITE_BUSY) \
172 * Structure sent to the SQLite callback function for static configuration.
174 * \see add_cfg_entry()
176 struct cfg_entry_args {
177 struct ast_config *cfg;
178 struct ast_category *cat;
180 struct ast_flags flags;
181 const char *who_asked;
185 * Structure sent to the SQLite callback function for RealTime configuration.
187 * \see add_rt_cfg_entry()
189 struct rt_cfg_entry_args {
190 struct ast_variable *var;
191 struct ast_variable *last;
195 * Structure sent to the SQLite callback function for RealTime configuration
196 * (realtime_multi_handler()).
198 * \see add_rt_multi_cfg_entry()
200 struct rt_multi_cfg_entry_args {
201 struct ast_config *cfg;
206 * \brief Allocate a variable.
207 * \param var the address of the variable to set (it will be allocated)
208 * \param name the name of the variable (for error handling)
209 * \param value the value to store in var
210 * \retval 0 on success
211 * \retval 1 if an allocation error occurred
213 static int set_var(char **var, const char *name, const char *value);
216 * \brief Load the configuration file.
217 * \see unload_config()
219 * This function sets dbfile, config_table, and cdr_table. It calls
220 * check_vars() before returning, and unload_config() if an error occurred.
222 * \retval 0 on success
223 * \retval 1 if an error occurred
225 static int load_config(void);
228 * \brief Free resources related to configuration.
231 static void unload_config(void);
234 * \brief Asterisk callback function for CDR support.
235 * \param cdr the CDR entry Asterisk sends us.
237 * Asterisk will call this function each time a CDR entry must be logged if
238 * CDR support is enabled.
240 * \retval 0 on success
241 * \retval 1 if an error occurred
243 static int cdr_handler(struct ast_cdr *cdr);
246 * \brief SQLite callback function for static configuration.
248 * This function is passed to the SQLite engine as a callback function to
249 * parse a row and store it in a struct ast_config object. It relies on
250 * resulting rows being sorted by category.
252 * \param arg a pointer to a struct cfg_entry_args object
253 * \param argc number of columns
254 * \param argv values in the row
255 * \param columnNames names and types of the columns
256 * \retval 0 on success
257 * \retval 1 if an error occurred
258 * \see cfg_entry_args
259 * \see sql_get_config_table
260 * \see config_handler()
262 static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames);
265 * \brief Asterisk callback function for static configuration.
267 * Asterisk will call this function when it loads its static configuration,
268 * which usually happens at startup and reload.
270 * \param database the database to use (ignored)
271 * \param table the table to use
272 * \param file the file to load from the database
273 * \param cfg the struct ast_config object to use when storing variables
274 * \param flags Optional flags. Not used.
275 * \param suggested_incl suggest include.
278 * \retval NULL if an error occurred
279 * \see add_cfg_entry()
281 static struct ast_config * config_handler(const char *database, const char *table, const char *file,
282 struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl, const char *who_asked);
285 * \brief Helper function to parse a va_list object into 2 dynamic arrays of
286 * strings, parameters and values.
288 * ap must have the following format : param1 val1 param2 val2 param3 val3 ...
289 * arguments will be extracted to create 2 arrays:
292 * <li>params : param1 param2 param3 ...</li>
293 * <li>vals : val1 val2 val3 ...</li>
296 * The address of these arrays are stored in params_ptr and vals_ptr. It
297 * is the responsibility of the caller to release the memory of these arrays.
298 * It is considered an error that va_list has a null or odd number of strings.
300 * \param ap the va_list object to parse
301 * \param params_ptr where the address of the params array is stored
302 * \param vals_ptr where the address of the vals array is stored
304 * \retval the number of elements in the arrays (which have the same size).
305 * \retval 0 if an error occurred.
307 static size_t get_params(va_list ap, const char ***params_ptr,
308 const char ***vals_ptr, int warn);
311 * \brief SQLite callback function for RealTime configuration.
313 * This function is passed to the SQLite engine as a callback function to
314 * parse a row and store it in a linked list of struct ast_variable objects.
316 * \param arg a pointer to a struct rt_cfg_entry_args object
317 * \param argc number of columns
318 * \param argv values in the row
319 * \param columnNames names and types of the columns
320 * \retval 0 on success.
321 * \retval 1 if an error occurred.
322 * \see rt_cfg_entry_args
323 * \see realtime_handler()
325 static int add_rt_cfg_entry(void *arg, int argc, char **argv,
329 * \brief Asterisk callback function for RealTime configuration.
331 * Asterisk will call this function each time it requires a variable
332 * through the RealTime architecture. ap is a list of parameters and
333 * values used to find a specific row, e.g one parameter "name" and
334 * one value "123" so that the SQL query becomes <code>SELECT * FROM
335 * table WHERE name = '123';</code>.
337 * \param database the database to use (ignored)
338 * \param table the table to use
339 * \param ap list of parameters and values to match
341 * \retval a linked list of struct ast_variable objects
342 * \retval NULL if an error occurred
343 * \see add_rt_cfg_entry()
345 static struct ast_variable * realtime_handler(const char *database,
346 const char *table, va_list ap);
349 * \brief SQLite callback function for RealTime configuration.
351 * This function performs the same actions as add_rt_cfg_entry() except
352 * that the rt_multi_cfg_entry_args structure is designed to store
353 * categories in addition to variables.
355 * \param arg a pointer to a struct rt_multi_cfg_entry_args object
356 * \param argc number of columns
357 * \param argv values in the row
358 * \param columnNames names and types of the columns
359 * \retval 0 on success.
360 * \retval 1 if an error occurred.
361 * \see rt_multi_cfg_entry_args
362 * \see realtime_multi_handler()
364 static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv,
368 * \brief Asterisk callback function for RealTime configuration.
370 * This function performs the same actions as realtime_handler() except
371 * that it can store variables per category, and can return several
374 * \param database the database to use (ignored)
375 * \param table the table to use
376 * \param ap list of parameters and values to match
377 * \retval a struct ast_config object storing categories and variables.
378 * \retval NULL if an error occurred.
380 * \see add_rt_multi_cfg_entry()
382 static struct ast_config * realtime_multi_handler(const char *database,
383 const char *table, va_list ap);
386 * \brief Asterisk callback function for RealTime configuration (variable
389 * Asterisk will call this function each time a variable has been modified
390 * internally and must be updated in the backend engine. keyfield and entity
391 * are used to find the row to update, e.g. <code>UPDATE table SET ... WHERE
392 * keyfield = 'entity';</code>. ap is a list of parameters and values with the
393 * same format as the other realtime functions.
395 * \param database the database to use (ignored)
396 * \param table the table to use
397 * \param keyfield the column of the matching cell
398 * \param entity the value of the matching cell
399 * \param ap list of parameters and new values to update in the database
400 * \retval the number of affected rows.
401 * \retval -1 if an error occurred.
403 static int realtime_update_handler(const char *database, const char *table,
404 const char *keyfield, const char *entity, va_list ap);
405 static int realtime_update2_handler(const char *database, const char *table,
409 * \brief Asterisk callback function for RealTime configuration (variable
412 * Asterisk will call this function each time a variable has been created
413 * internally and must be stored in the backend engine.
414 * are used to find the row to update, e.g. ap is a list of parameters and
415 * values with the same format as the other realtime functions.
417 * \param database the database to use (ignored)
418 * \param table the table to use
419 * \param ap list of parameters and new values to insert into the database
420 * \retval the rowid of inserted row.
421 * \retval -1 if an error occurred.
423 static int realtime_store_handler(const char *database, const char *table,
427 * \brief Asterisk callback function for RealTime configuration (destroys
430 * Asterisk will call this function each time a variable has been destroyed
431 * internally and must be removed from the backend engine. keyfield and entity
432 * are used to find the row to delete, e.g. <code>DELETE FROM table WHERE
433 * keyfield = 'entity';</code>. ap is a list of parameters and values with the
434 * same format as the other realtime functions.
436 * \param database the database to use (ignored)
437 * \param table the table to use
438 * \param keyfield the column of the matching cell
439 * \param entity the value of the matching cell
440 * \param ap list of additional parameters for cell matching
441 * \retval the number of affected rows.
442 * \retval -1 if an error occurred.
444 static int realtime_destroy_handler(const char *database, const char *table,
445 const char *keyfield, const char *entity, va_list ap);
448 * \brief Asterisk callback function for the CLI status command.
450 * \param e CLI command
452 * \param a CLI argument list
453 * \return RESULT_SUCCESS
455 static char *handle_cli_show_sqlite_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
456 static char *handle_cli_sqlite_show_tables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
458 static int realtime_require_handler(const char *database, const char *table, va_list ap);
459 static int realtime_unload_handler(const char *unused, const char *tablename);
461 /*! The SQLite database object. */
464 /*! Set to 1 if CDR support is enabled. */
467 /*! Set to 1 if the CDR callback function was registered. */
468 static int cdr_registered;
470 /*! Set to 1 if the CLI status command callback function was registered. */
471 static int cli_status_registered;
473 /*! The path of the database file. */
476 /*! The name of the static configuration table. */
477 static char *config_table;
479 /*! The name of the table used to store CDR entries. */
480 static char *cdr_table;
483 * The structure specifying all callback functions used by Asterisk for static
484 * and RealTime configuration.
486 static struct ast_config_engine sqlite_engine =
488 .name = RES_CONFIG_SQLITE_DRIVER,
489 .load_func = config_handler,
490 .realtime_func = realtime_handler,
491 .realtime_multi_func = realtime_multi_handler,
492 .store_func = realtime_store_handler,
493 .destroy_func = realtime_destroy_handler,
494 .update_func = realtime_update_handler,
495 .update2_func = realtime_update2_handler,
496 .require_func = realtime_require_handler,
497 .unload_func = realtime_unload_handler,
501 * The mutex used to prevent simultaneous access to the SQLite database.
503 AST_MUTEX_DEFINE_STATIC(mutex);
506 * Structure containing details and callback functions for the CLI status
509 static struct ast_cli_entry cli_status[] = {
510 AST_CLI_DEFINE(handle_cli_show_sqlite_status, "Show status information about the SQLite 2 driver"),
511 AST_CLI_DEFINE(handle_cli_sqlite_show_tables, "Cached table information about the SQLite 2 driver"),
514 struct sqlite_cache_columns {
517 unsigned char isint; /*!< By definition, only INTEGER PRIMARY KEY is an integer; everything else is a string. */
518 AST_RWLIST_ENTRY(sqlite_cache_columns) list;
521 struct sqlite_cache_tables {
523 AST_RWLIST_HEAD(_columns, sqlite_cache_columns) columns;
524 AST_RWLIST_ENTRY(sqlite_cache_tables) list;
527 static AST_RWLIST_HEAD_STATIC(sqlite_tables, sqlite_cache_tables);
530 * Taken from Asterisk 1.2 cdr_sqlite.so.
533 /*! SQL query format to create the CDR table if non existent. */
534 static char *sql_create_cdr_table =
535 "CREATE TABLE '%q' (\n"
537 " clid VARCHAR(80) NOT NULL DEFAULT '',\n"
538 " src VARCHAR(80) NOT NULL DEFAULT '',\n"
539 " dst VARCHAR(80) NOT NULL DEFAULT '',\n"
540 " dcontext VARCHAR(80) NOT NULL DEFAULT '',\n"
541 " channel VARCHAR(80) NOT NULL DEFAULT '',\n"
542 " dstchannel VARCHAR(80) NOT NULL DEFAULT '',\n"
543 " lastapp VARCHAR(80) NOT NULL DEFAULT '',\n"
544 " lastdata VARCHAR(80) NOT NULL DEFAULT '',\n"
545 " start DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n"
546 " answer DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n"
547 " end DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n"
548 " duration INT(11) NOT NULL DEFAULT 0,\n"
549 " billsec INT(11) NOT NULL DEFAULT 0,\n"
550 " disposition VARCHAR(45) NOT NULL DEFAULT '',\n"
551 " amaflags INT(11) NOT NULL DEFAULT 0,\n"
552 " accountcode VARCHAR(20) NOT NULL DEFAULT '',\n"
553 " uniqueid VARCHAR(32) NOT NULL DEFAULT '',\n"
554 " userfield VARCHAR(255) NOT NULL DEFAULT '',\n"
555 " PRIMARY KEY (id)\n"
559 * SQL query format to describe the table structure
561 #define sql_table_structure "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'"
564 * SQL query format to fetch the static configuration of a file.
565 * Rows must be sorted by category.
567 * \see add_cfg_entry()
569 #define sql_get_config_table \
572 " WHERE filename = '%q' AND commented = 0" \
573 " ORDER BY cat_metric ASC, var_metric ASC;"
575 static void free_table(struct sqlite_cache_tables *tblptr)
577 struct sqlite_cache_columns *col;
579 /* Obtain a write lock to ensure there are no read locks outstanding */
580 AST_RWLIST_WRLOCK(&(tblptr->columns));
581 while ((col = AST_RWLIST_REMOVE_HEAD(&(tblptr->columns), list))) {
584 AST_RWLIST_UNLOCK(&(tblptr->columns));
585 AST_RWLIST_HEAD_DESTROY(&(tblptr->columns));
589 static int find_table_cb(void *vtblptr, int argc, char **argv, char **columnNames)
591 struct sqlite_cache_tables *tblptr = vtblptr;
592 char *sql = ast_strdupa(argv[0]), *start, *end, *type, *remainder;
594 AST_DECLARE_APP_ARGS(fie,
595 AST_APP_ARG(ld)[100]; /* This means we support up to 100 columns per table */
597 struct sqlite_cache_columns *col;
599 /* This is really fun. We get to parse an SQL statement to figure out
600 * what columns are in the table.
602 if ((start = strchr(sql, '(')) && (end = strrchr(sql, ')'))) {
610 AST_STANDARD_APP_ARGS(fie, start);
611 for (i = 0; i < fie.argc; i++) {
612 fie.ld[i] = ast_skip_blanks(fie.ld[i]);
613 ast_debug(5, "Found field: %s\n", fie.ld[i]);
614 if (strncasecmp(fie.ld[i], "PRIMARY KEY", 11) == 0 && (start = strchr(fie.ld[i], '(')) && (end = strchr(fie.ld[i], ')'))) {
616 AST_RWLIST_TRAVERSE(&(tblptr->columns), col, list) {
617 if (strcasecmp(start + 1, col->name) == 0 && strcasestr(col->type, "INTEGER")) {
623 /* type delimiter could be any space character */
624 for (type = fie.ld[i]; *type > 32; type++);
626 type = ast_skip_blanks(type);
627 for (remainder = type; *remainder > 32; remainder++);
629 if (!(col = ast_calloc(1, sizeof(*col) + strlen(fie.ld[i]) + strlen(type) + 2))) {
632 col->name = (char *)col + sizeof(*col);
633 col->type = (char *)col + sizeof(*col) + strlen(fie.ld[i]) + 1;
634 strcpy(col->name, fie.ld[i]); /* SAFE */
635 strcpy(col->type, type); /* SAFE */
636 if (strcasestr(col->type, "INTEGER") && strcasestr(col->type, "PRIMARY KEY")) {
639 AST_LIST_INSERT_TAIL(&(tblptr->columns), col, list);
644 static struct sqlite_cache_tables *find_table(const char *tablename)
646 struct sqlite_cache_tables *tblptr;
648 char *sql, *errstr = NULL;
650 AST_RWLIST_RDLOCK(&sqlite_tables);
652 for (i = 0; i < 2; i++) {
653 AST_RWLIST_TRAVERSE(&sqlite_tables, tblptr, list) {
654 if (strcmp(tblptr->name, tablename) == 0) {
659 AST_RWLIST_RDLOCK(&(tblptr->columns));
660 AST_RWLIST_UNLOCK(&sqlite_tables);
665 AST_RWLIST_UNLOCK(&sqlite_tables);
666 AST_RWLIST_WRLOCK(&sqlite_tables);
670 /* Table structure not cached; build the structure now */
671 if (asprintf(&sql, sql_table_structure, tablename) < 0) {
672 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
675 if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
676 AST_RWLIST_UNLOCK(&sqlite_tables);
677 ast_log(LOG_ERROR, "Memory error. Cannot cache table '%s'\n", tablename);
680 tblptr->name = (char *)tblptr + sizeof(*tblptr);
681 strcpy(tblptr->name, tablename); /* SAFE */
682 AST_RWLIST_HEAD_INIT(&(tblptr->columns));
684 ast_debug(1, "About to query table structure: %s\n", sql);
686 ast_mutex_lock(&mutex);
687 if ((err = sqlite_exec(db, sql, find_table_cb, tblptr, &errstr))) {
688 ast_mutex_unlock(&mutex);
689 ast_log(LOG_WARNING, "SQLite error %d: %s\n", err, errstr);
692 AST_RWLIST_UNLOCK(&sqlite_tables);
695 ast_mutex_unlock(&mutex);
697 if (AST_LIST_EMPTY(&(tblptr->columns))) {
699 AST_RWLIST_UNLOCK(&sqlite_tables);
703 AST_RWLIST_INSERT_TAIL(&sqlite_tables, tblptr, list);
704 AST_RWLIST_RDLOCK(&(tblptr->columns));
705 AST_RWLIST_UNLOCK(&sqlite_tables);
709 #define release_table(a) AST_RWLIST_UNLOCK(&((a)->columns))
711 static int set_var(char **var, const char *name, const char *value)
716 *var = ast_strdup(value);
719 ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
726 static int check_vars(void)
729 ast_log(LOG_ERROR, "Required parameter undefined: dbfile\n");
733 use_cdr = (cdr_table != NULL);
738 static int load_config(void)
740 struct ast_config *config;
741 struct ast_variable *var;
743 struct ast_flags config_flags = { 0 };
745 config = ast_config_load(RES_CONFIG_SQLITE_CONF_FILE, config_flags);
747 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
748 ast_log(LOG_ERROR, "Unable to load " RES_CONFIG_SQLITE_CONF_FILE "\n");
752 for (var = ast_variable_browse(config, "general"); var; var = var->next) {
753 if (!strcasecmp(var->name, "dbfile"))
754 SET_VAR(config, dbfile, var);
755 else if (!strcasecmp(var->name, "config_table"))
756 SET_VAR(config, config_table, var);
757 else if (!strcasecmp(var->name, "cdr_table")) {
758 SET_VAR(config, cdr_table, var);
760 ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
763 ast_config_destroy(config);
764 error = check_vars();
774 static void unload_config(void)
776 struct sqlite_cache_tables *tbl;
779 ast_free(config_table);
783 AST_RWLIST_WRLOCK(&sqlite_tables);
784 while ((tbl = AST_RWLIST_REMOVE_HEAD(&sqlite_tables, list))) {
787 AST_RWLIST_UNLOCK(&sqlite_tables);
790 static int cdr_handler(struct ast_cdr *cdr)
792 char *errormsg = NULL, *tmp, workspace[500];
794 struct sqlite_cache_tables *tbl = find_table(cdr_table);
795 struct sqlite_cache_columns *col;
796 struct ast_str *sql1 = ast_str_create(160), *sql2 = ast_str_create(16);
800 ast_log(LOG_WARNING, "No such table: %s\n", cdr_table);
804 ast_str_set(&sql1, 0, "INSERT INTO %s (", cdr_table);
805 ast_str_set(&sql2, 0, ") VALUES (");
807 AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
809 ast_cdr_getvar(cdr, col->name, &tmp, workspace, sizeof(workspace), 0, 1);
813 if (sscanf(tmp, "%30d", &scannum) == 1) {
814 ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
815 ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", scannum);
818 ast_cdr_getvar(cdr, col->name, &tmp, workspace, sizeof(workspace), 0, 0);
822 ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
823 tmp = sqlite_mprintf("%Q", tmp);
824 ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", tmp);
831 ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
834 ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql1));
836 ast_mutex_lock(&mutex);
838 RES_CONFIG_SQLITE_BEGIN
839 error = sqlite_exec(db, ast_str_buffer(sql1), NULL, NULL, &errormsg);
840 RES_CONFIG_SQLITE_END(error)
842 ast_mutex_unlock(&mutex);
847 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
848 sqlite_freemem(errormsg);
851 sqlite_freemem(errormsg);
856 static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
858 struct cfg_entry_args *args;
859 struct ast_variable *var;
861 if (argc != RES_CONFIG_SQLITE_CONFIG_COLUMNS) {
862 ast_log(LOG_WARNING, "Corrupt table\n");
868 if (!strcmp(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], "#include")) {
869 struct ast_config *cfg;
872 val = argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL];
873 cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked);
876 ast_log(LOG_WARNING, "Unable to include %s\n", val);
884 if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
885 args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
888 ast_log(LOG_WARNING, "Unable to allocate category\n");
892 ast_free(args->cat_name);
893 args->cat_name = ast_strdup(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
895 if (!args->cat_name) {
896 ast_category_destroy(args->cat);
900 ast_category_append(args->cfg, args->cat);
903 var = ast_variable_new(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL], "");
906 ast_log(LOG_WARNING, "Unable to allocate variable");
910 ast_variable_append(args->cat, var);
915 static struct ast_config *config_handler(const char *database, const char *table, const char *file,
916 struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl, const char *who_asked)
918 struct cfg_entry_args args;
919 char *query, *errormsg = NULL;
924 ast_log(LOG_ERROR, "Table name unspecified\n");
928 table = config_table;
930 query = sqlite_mprintf(sql_get_config_table, table, file);
933 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
937 ast_debug(1, "SQL query: %s\n", query);
940 args.cat_name = NULL;
942 args.who_asked = who_asked;
944 ast_mutex_lock(&mutex);
946 RES_CONFIG_SQLITE_BEGIN
947 error = sqlite_exec(db, query, add_cfg_entry, &args, &errormsg);
948 RES_CONFIG_SQLITE_END(error)
950 ast_mutex_unlock(&mutex);
952 ast_free(args.cat_name);
953 sqlite_freemem(query);
956 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
957 sqlite_freemem(errormsg);
960 sqlite_freemem(errormsg);
965 static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
967 const char **tmp, *param, *val, **params, **vals;
974 while ((param = va_arg(ap, const char *)) && (val = va_arg(ap, const char *))) {
975 if (!(tmp = ast_realloc(params, (params_count + 1) * sizeof(char *)))) {
982 if (!(tmp = ast_realloc(vals, (params_count + 1) * sizeof(char *)))) {
989 params[params_count] = param;
990 vals[params_count] = val;
994 if (params_count > 0) {
995 *params_ptr = params;
998 ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
1001 return params_count;
1004 static int add_rt_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
1006 struct rt_cfg_entry_args *args;
1007 struct ast_variable *var;
1012 for (i = 0; i < argc; i++) {
1016 if (!(var = ast_variable_new(columnNames[i], argv[i], "")))
1025 args->last->next = var;
1033 static struct ast_variable * realtime_handler(const char *database, const char *table, va_list ap)
1035 char *query, *errormsg = NULL, *op, *tmp_str;
1036 struct rt_cfg_entry_args args;
1037 const char **params, **vals;
1038 size_t params_count;
1042 ast_log(LOG_WARNING, "Table name unspecified\n");
1046 params_count = get_params(ap, ¶ms, &vals, 1);
1048 if (params_count == 0)
1051 op = (strchr(params[0], ' ') == NULL) ? " =" : "";
1053 /* \cond DOXYGEN_CAN_PARSE_THIS */
1055 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1058 query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", params[0], op, vals[0]);
1061 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1067 if (params_count > 1) {
1070 for (i = 1; i < params_count; i++) {
1071 op = (strchr(params[i], ' ') == NULL) ? " =" : "";
1072 tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
1073 sqlite_freemem(query);
1076 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1089 tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
1090 sqlite_freemem(query);
1093 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1098 ast_debug(1, "SQL query: %s\n", query);
1102 ast_mutex_lock(&mutex);
1104 RES_CONFIG_SQLITE_BEGIN
1105 error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
1106 RES_CONFIG_SQLITE_END(error)
1108 ast_mutex_unlock(&mutex);
1110 sqlite_freemem(query);
1113 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1114 sqlite_freemem(errormsg);
1115 ast_variables_destroy(args.var);
1118 sqlite_freemem(errormsg);
1123 static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
1125 struct rt_multi_cfg_entry_args *args;
1126 struct ast_category *cat;
1127 struct ast_variable *var;
1135 * cat_name should always be set here, since initfield is forged from
1136 * params[0] in realtime_multi_handler(), which is a search parameter
1139 for (i = 0; i < argc; i++) {
1140 if (!strcmp(args->initfield, columnNames[i]))
1145 ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
1149 if (!(cat = ast_category_new(cat_name, "", 99999))) {
1150 ast_log(LOG_WARNING, "Unable to allocate category\n");
1154 ast_category_append(args->cfg, cat);
1156 for (i = 0; i < argc; i++) {
1161 if (!(var = ast_variable_new(columnNames[i], argv[i], ""))) {
1162 ast_log(LOG_WARNING, "Unable to allocate variable\n");
1166 ast_variable_append(cat, var);
1172 static struct ast_config *realtime_multi_handler(const char *database,
1173 const char *table, va_list ap)
1175 char *query, *errormsg = NULL, *op, *tmp_str, *initfield;
1176 struct rt_multi_cfg_entry_args args;
1177 const char **params, **vals;
1178 struct ast_config *cfg;
1179 size_t params_count;
1183 ast_log(LOG_WARNING, "Table name unspecified\n");
1187 if (!(cfg = ast_config_new())) {
1188 ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
1192 if (!(params_count = get_params(ap, ¶ms, &vals, 1))) {
1193 ast_config_destroy(cfg);
1197 if (!(initfield = ast_strdup(params[0]))) {
1198 ast_config_destroy(cfg);
1204 tmp_str = strchr(initfield, ' ');
1209 op = (!strchr(params[0], ' ')) ? " =" : "";
1212 * Asterisk sends us an already escaped string when searching for
1213 * "exten LIKE" (uh!). Handle it separately.
1215 tmp_str = (!strcmp(vals[0], "\\_%")) ? "_%" : (char *)vals[0];
1217 /* \cond DOXYGEN_CAN_PARSE_THIS */
1219 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1222 if (!(query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", params[0], op, tmp_str))) {
1223 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1224 ast_config_destroy(cfg);
1227 ast_free(initfield);
1231 if (params_count > 1) {
1234 for (i = 1; i < params_count; i++) {
1235 op = (!strchr(params[i], ' ')) ? " =" : "";
1236 tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
1237 sqlite_freemem(query);
1240 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1241 ast_config_destroy(cfg);
1244 ast_free(initfield);
1255 if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
1256 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1257 sqlite_freemem(query);
1258 ast_config_destroy(cfg);
1259 ast_free(initfield);
1263 sqlite_freemem(query);
1265 ast_debug(1, "SQL query: %s\n", query);
1267 args.initfield = initfield;
1269 ast_mutex_lock(&mutex);
1271 RES_CONFIG_SQLITE_BEGIN
1272 error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
1273 RES_CONFIG_SQLITE_END(error)
1275 ast_mutex_unlock(&mutex);
1277 sqlite_freemem(query);
1278 ast_free(initfield);
1281 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1282 sqlite_freemem(errormsg);
1283 ast_config_destroy(cfg);
1286 sqlite_freemem(errormsg);
1291 static int realtime_update_handler(const char *database, const char *table,
1292 const char *keyfield, const char *entity, va_list ap)
1294 char *query, *errormsg = NULL, *tmp_str;
1295 const char **params, **vals;
1296 size_t params_count;
1297 int error, rows_num;
1300 ast_log(LOG_WARNING, "Table name unspecified\n");
1304 if (!(params_count = get_params(ap, ¶ms, &vals, 1)))
1307 /* \cond DOXYGEN_CAN_PARSE_THIS */
1309 #define QUERY "UPDATE '%q' SET %q = '%q'"
1312 if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
1313 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1319 if (params_count > 1) {
1322 for (i = 1; i < params_count; i++) {
1323 tmp_str = sqlite_mprintf("%s, %q = '%q'", query, params[i], vals[i]);
1324 sqlite_freemem(query);
1327 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1340 if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
1341 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1342 sqlite_freemem(query);
1346 sqlite_freemem(query);
1348 ast_debug(1, "SQL query: %s\n", query);
1350 ast_mutex_lock(&mutex);
1352 RES_CONFIG_SQLITE_BEGIN
1353 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1354 RES_CONFIG_SQLITE_END(error)
1357 rows_num = sqlite_changes(db);
1361 ast_mutex_unlock(&mutex);
1363 sqlite_freemem(query);
1366 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1368 sqlite_freemem(errormsg);
1373 static int realtime_update2_handler(const char *database, const char *table,
1376 char *errormsg = NULL, *tmp1, *tmp2;
1377 int error, rows_num, first = 1;
1378 struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
1379 struct ast_str *where = ast_str_thread_get(&where_buf, 100);
1380 const char *param, *value;
1383 ast_log(LOG_WARNING, "Table name unspecified\n");
1391 ast_str_set(&sql, 0, "UPDATE %s SET", table);
1392 ast_str_set(&where, 0, " WHERE");
1394 while ((param = va_arg(ap, const char *))) {
1395 value = va_arg(ap, const char *);
1396 ast_str_append(&where, 0, "%s %s = %s",
1397 first ? "" : " AND",
1398 tmp1 = sqlite_mprintf("%q", param),
1399 tmp2 = sqlite_mprintf("%Q", value));
1400 sqlite_freemem(tmp1);
1401 sqlite_freemem(tmp2);
1406 ast_log(LOG_ERROR, "No criteria specified on update to '%s@%s'!\n", table, database);
1411 while ((param = va_arg(ap, const char *))) {
1412 value = va_arg(ap, const char *);
1413 ast_str_append(&sql, 0, "%s %s = %s",
1415 tmp1 = sqlite_mprintf("%q", param),
1416 tmp2 = sqlite_mprintf("%Q", value));
1417 sqlite_freemem(tmp1);
1418 sqlite_freemem(tmp2);
1422 ast_str_append(&sql, 0, " %s", ast_str_buffer(where));
1423 ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql));
1425 ast_mutex_lock(&mutex);
1427 RES_CONFIG_SQLITE_BEGIN
1428 error = sqlite_exec(db, ast_str_buffer(sql), NULL, NULL, &errormsg);
1429 RES_CONFIG_SQLITE_END(error)
1432 rows_num = sqlite_changes(db);
1437 ast_mutex_unlock(&mutex);
1440 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1442 sqlite_freemem(errormsg);
1447 static int realtime_store_handler(const char *database, const char *table, va_list ap)
1449 char *errormsg = NULL, *tmp_str, *tmp_keys = NULL, *tmp_keys2 = NULL, *tmp_vals = NULL, *tmp_vals2 = NULL;
1450 const char **params, **vals;
1451 size_t params_count;
1456 ast_log(LOG_WARNING, "Table name unspecified\n");
1460 if (!(params_count = get_params(ap, ¶ms, &vals, 1)))
1463 /* \cond DOXYGEN_CAN_PARSE_THIS */
1465 #define QUERY "INSERT into '%q' (%s) VALUES (%s);"
1468 for (i = 0; i < params_count; i++) {
1470 tmp_keys = sqlite_mprintf("%s, %q", tmp_keys2, params[i]);
1471 sqlite_freemem(tmp_keys2);
1473 tmp_keys = sqlite_mprintf("%q", params[i]);
1476 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1477 sqlite_freemem(tmp_vals);
1484 tmp_vals = sqlite_mprintf("%s, '%q'", tmp_vals2, vals[i]);
1485 sqlite_freemem(tmp_vals2);
1487 tmp_vals = sqlite_mprintf("'%q'", vals[i]);
1490 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1491 sqlite_freemem(tmp_keys);
1498 tmp_keys2 = tmp_keys;
1499 tmp_vals2 = tmp_vals;
1505 if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
1506 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1507 sqlite_freemem(tmp_keys);
1508 sqlite_freemem(tmp_vals);
1512 sqlite_freemem(tmp_keys);
1513 sqlite_freemem(tmp_vals);
1515 ast_debug(1, "SQL query: %s\n", tmp_str);
1517 ast_mutex_lock(&mutex);
1519 RES_CONFIG_SQLITE_BEGIN
1520 error = sqlite_exec(db, tmp_str, NULL, NULL, &errormsg);
1521 RES_CONFIG_SQLITE_END(error)
1524 rows_id = sqlite_last_insert_rowid(db);
1529 ast_mutex_unlock(&mutex);
1531 sqlite_freemem(tmp_str);
1534 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1536 sqlite_freemem(errormsg);
1541 static int realtime_destroy_handler(const char *database, const char *table,
1542 const char *keyfield, const char *entity, va_list ap)
1544 char *query, *errormsg = NULL, *tmp_str;
1545 const char **params = NULL, **vals = NULL;
1546 size_t params_count;
1547 int error, rows_num;
1551 ast_log(LOG_WARNING, "Table name unspecified\n");
1555 params_count = get_params(ap, ¶ms, &vals, 0);
1557 /* \cond DOXYGEN_CAN_PARSE_THIS */
1559 #define QUERY "DELETE FROM '%q' WHERE"
1562 if (!(query = sqlite_mprintf(QUERY, table))) {
1563 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1569 for (i = 0; i < params_count; i++) {
1570 tmp_str = sqlite_mprintf("%s %q = '%q' AND", query, params[i], vals[i]);
1571 sqlite_freemem(query);
1574 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1585 if (!(tmp_str = sqlite_mprintf("%s %q = '%q';", query, keyfield, entity))) {
1586 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1587 sqlite_freemem(query);
1590 sqlite_freemem(query);
1592 ast_debug(1, "SQL query: %s\n", query);
1594 ast_mutex_lock(&mutex);
1596 RES_CONFIG_SQLITE_BEGIN
1597 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1598 RES_CONFIG_SQLITE_END(error)
1601 rows_num = sqlite_changes(db);
1606 ast_mutex_unlock(&mutex);
1608 sqlite_freemem(query);
1611 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1613 sqlite_freemem(errormsg);
1618 static int realtime_require_handler(const char *unused, const char *tablename, va_list ap)
1620 struct sqlite_cache_tables *tbl = find_table(tablename);
1621 struct sqlite_cache_columns *col;
1629 while ((elm = va_arg(ap, char *))) {
1630 type = va_arg(ap, require_type);
1632 /* Check if the field matches the criteria */
1633 AST_RWLIST_TRAVERSE(&tbl->columns, col, list) {
1634 if (strcmp(col->name, elm) == 0) {
1635 /* SQLite only has two types - the 32-bit integer field that
1636 * is the key column, and everything else (everything else
1639 if (col->isint && !ast_rq_is_int(type)) {
1640 ast_log(LOG_WARNING, "Realtime table %s: column '%s' is an integer field, but Asterisk requires that it not be!\n", tablename, col->name);
1647 ast_log(LOG_WARNING, "Realtime table %s requires column '%s', but that column does not exist!\n", tablename, elm);
1650 AST_RWLIST_UNLOCK(&(tbl->columns));
1654 static int realtime_unload_handler(const char *unused, const char *tablename)
1656 struct sqlite_cache_tables *tbl;
1657 AST_RWLIST_WRLOCK(&sqlite_tables);
1658 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&sqlite_tables, tbl, list) {
1659 if (!strcasecmp(tbl->name, tablename)) {
1660 AST_RWLIST_REMOVE_CURRENT(list);
1664 AST_RWLIST_TRAVERSE_SAFE_END
1665 AST_RWLIST_UNLOCK(&sqlite_tables);
1669 static char *handle_cli_show_sqlite_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1673 e->command = "sqlite show status";
1675 "Usage: sqlite show status\n"
1676 " Show status information about the SQLite 2 driver\n";
1683 return CLI_SHOWUSAGE;
1685 ast_cli(a->fd, "SQLite database path: %s\n", dbfile);
1686 ast_cli(a->fd, "config_table: ");
1689 ast_cli(a->fd, "unspecified, must be present in extconfig.conf\n");
1691 ast_cli(a->fd, "%s\n", config_table);
1693 ast_cli(a->fd, "cdr_table: ");
1696 ast_cli(a->fd, "unspecified, CDR support disabled\n");
1698 ast_cli(a->fd, "%s\n", cdr_table);
1703 static char *handle_cli_sqlite_show_tables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1705 struct sqlite_cache_tables *tbl;
1706 struct sqlite_cache_columns *col;
1711 e->command = "sqlite show tables";
1713 "Usage: sqlite show tables\n"
1714 " Show table information about the SQLite 2 driver\n";
1721 return CLI_SHOWUSAGE;
1723 AST_RWLIST_RDLOCK(&sqlite_tables);
1724 AST_RWLIST_TRAVERSE(&sqlite_tables, tbl, list) {
1726 ast_cli(a->fd, "Table %s:\n", tbl->name);
1727 AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
1728 fprintf(stderr, "%s\n", col->name);
1729 ast_cli(a->fd, " %20.20s %-30.30s\n", col->name, col->type);
1732 AST_RWLIST_UNLOCK(&sqlite_tables);
1735 ast_cli(a->fd, "No tables currently in cache\n");
1741 static int unload_module(void)
1743 if (cli_status_registered)
1744 ast_cli_unregister_multiple(cli_status, ARRAY_LEN(cli_status));
1747 ast_cdr_unregister(RES_CONFIG_SQLITE_NAME);
1749 ast_config_engine_deregister(&sqlite_engine);
1759 static int load_module(void)
1761 char *errormsg = NULL;
1766 cli_status_registered = 0;
1768 config_table = NULL;
1770 error = load_config();
1773 return AST_MODULE_LOAD_DECLINE;
1775 if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
1776 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1777 sqlite_freemem(errormsg);
1782 sqlite_freemem(errormsg);
1784 ast_config_engine_register(&sqlite_engine);
1789 /* \cond DOXYGEN_CAN_PARSE_THIS */
1791 #define QUERY "SELECT COUNT(id) FROM %Q;"
1794 query = sqlite_mprintf(QUERY, cdr_table);
1797 ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1802 ast_debug(1, "SQL query: %s\n", query);
1804 RES_CONFIG_SQLITE_BEGIN
1805 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1806 RES_CONFIG_SQLITE_END(error)
1808 sqlite_freemem(query);
1814 if (error != SQLITE_ERROR) {
1815 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1816 sqlite_freemem(errormsg);
1821 sqlite_freemem(errormsg);
1823 query = sqlite_mprintf(sql_create_cdr_table, cdr_table);
1826 ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1831 ast_debug(1, "SQL query: %s\n", query);
1833 RES_CONFIG_SQLITE_BEGIN
1834 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1835 RES_CONFIG_SQLITE_END(error)
1837 sqlite_freemem(query);
1840 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1841 sqlite_freemem(errormsg);
1846 sqlite_freemem(errormsg);
1849 error = ast_cdr_register(RES_CONFIG_SQLITE_NAME, RES_CONFIG_SQLITE_DESCRIPTION, cdr_handler);
1859 error = ast_cli_register_multiple(cli_status, ARRAY_LEN(cli_status));
1866 cli_status_registered = 1;
1871 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Realtime SQLite configuration",
1872 .load = load_module,
1873 .unload = unload_module,
1874 .load_pri = AST_MODPRI_REALTIME_DRIVER,