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.sample 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.
74 /*! \li \ref res_config_sqlite.c uses the configuration file \ref res_config_sqlite.conf
75 * \addtogroup configuration_file Configuration Files
79 * \page res_config_sqlite.conf res_config_sqlite.conf
80 * \verbinclude res_config_sqlite.conf.sample
84 <depend>sqlite</depend>
85 <support_level>extended</support_level>
89 ASTERISK_REGISTER_FILE()
93 #include "asterisk/logger.h"
94 #include "asterisk/app.h"
95 #include "asterisk/pbx.h"
96 #include "asterisk/cdr.h"
97 #include "asterisk/cli.h"
98 #include "asterisk/lock.h"
99 #include "asterisk/config.h"
100 #include "asterisk/module.h"
101 #include "asterisk/linkedlists.h"
103 #define MACRO_BEGIN do {
104 #define MACRO_END } while (0)
106 #define RES_CONFIG_SQLITE_NAME "res_config_sqlite"
107 #define RES_CONFIG_SQLITE_DRIVER "sqlite"
108 #define RES_CONFIG_SQLITE_DESCRIPTION "Resource Module for SQLite 2"
109 #define RES_CONFIG_SQLITE_CONF_FILE "res_config_sqlite.conf"
112 RES_CONFIG_SQLITE_CONFIG_ID,
113 RES_CONFIG_SQLITE_CONFIG_CAT_METRIC,
114 RES_CONFIG_SQLITE_CONFIG_VAR_METRIC,
115 RES_CONFIG_SQLITE_CONFIG_COMMENTED,
116 RES_CONFIG_SQLITE_CONFIG_FILENAME,
117 RES_CONFIG_SQLITE_CONFIG_CATEGORY,
118 RES_CONFIG_SQLITE_CONFIG_VAR_NAME,
119 RES_CONFIG_SQLITE_CONFIG_VAR_VAL,
120 RES_CONFIG_SQLITE_CONFIG_COLUMNS,
123 #define SET_VAR(config, to, from) \
127 __error = set_var(&to, #to, from->value); \
130 ast_config_destroy(config); \
136 AST_THREADSTORAGE(sql_buf);
137 AST_THREADSTORAGE(where_buf);
140 * Maximum number of loops before giving up executing a query. Calls to
141 * sqlite_xxx() functions which can return SQLITE_BUSY
142 * are enclosed by RES_CONFIG_SQLITE_BEGIN and RES_CONFIG_SQLITE_END, e.g.
147 * RES_CONFIG_SQLITE_BEGIN
148 * error = sqlite_exec(db, query, NULL, NULL, &errormsg);
149 * RES_CONFIG_SQLITE_END(error)
155 #define RES_CONFIG_SQLITE_MAX_LOOPS 10
158 * Macro used before executing a query.
160 * \see RES_CONFIG_SQLITE_MAX_LOOPS.
162 #define RES_CONFIG_SQLITE_BEGIN \
166 for (__i = 0; __i < RES_CONFIG_SQLITE_MAX_LOOPS; __i++) {
169 * Macro used after executing a query.
171 * \see RES_CONFIG_SQLITE_MAX_LOOPS.
173 #define RES_CONFIG_SQLITE_END(error) \
174 if (error != SQLITE_BUSY) \
181 * Structure sent to the SQLite callback function for static configuration.
183 * \see add_cfg_entry()
185 struct cfg_entry_args {
186 struct ast_config *cfg;
187 struct ast_category *cat;
189 struct ast_flags flags;
190 const char *who_asked;
194 * Structure sent to the SQLite callback function for RealTime configuration.
196 * \see add_rt_cfg_entry()
198 struct rt_cfg_entry_args {
199 struct ast_variable *var;
200 struct ast_variable *last;
204 * Structure sent to the SQLite callback function for RealTime configuration
205 * (realtime_multi_handler()).
207 * \see add_rt_multi_cfg_entry()
209 struct rt_multi_cfg_entry_args {
210 struct ast_config *cfg;
215 * \brief Allocate a variable.
216 * \param var the address of the variable to set (it will be allocated)
217 * \param name the name of the variable (for error handling)
218 * \param value the value to store in var
219 * \retval 0 on success
220 * \retval 1 if an allocation error occurred
222 static int set_var(char **var, const char *name, const char *value);
225 * \brief Load the configuration file.
226 * \see unload_config()
228 * This function sets dbfile, config_table, and cdr_table. It calls
229 * check_vars() before returning, and unload_config() if an error occurred.
231 * \retval 0 on success
232 * \retval 1 if an error occurred
234 static int load_config(void);
237 * \brief Free resources related to configuration.
240 static void unload_config(void);
243 * \brief Asterisk callback function for CDR support.
244 * \param cdr the CDR entry Asterisk sends us.
246 * Asterisk will call this function each time a CDR entry must be logged if
247 * CDR support is enabled.
249 * \retval 0 on success
250 * \retval 1 if an error occurred
252 static int cdr_handler(struct ast_cdr *cdr);
255 * \brief SQLite callback function for static configuration.
257 * This function is passed to the SQLite engine as a callback function to
258 * parse a row and store it in a struct ast_config object. It relies on
259 * resulting rows being sorted by category.
261 * \param arg a pointer to a struct cfg_entry_args object
262 * \param argc number of columns
263 * \param argv values in the row
264 * \param columnNames names and types of the columns
265 * \retval 0 on success
266 * \retval 1 if an error occurred
267 * \see cfg_entry_args
268 * \see sql_get_config_table
269 * \see config_handler()
271 static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames);
274 * \brief Asterisk callback function for static configuration.
276 * Asterisk will call this function when it loads its static configuration,
277 * which usually happens at startup and reload.
279 * \param database the database to use (ignored)
280 * \param table the table to use
281 * \param file the file to load from the database
282 * \param cfg the struct ast_config object to use when storing variables
283 * \param flags Optional flags. Not used.
284 * \param suggested_incl suggest include.
287 * \retval NULL if an error occurred
288 * \see add_cfg_entry()
290 static struct ast_config * config_handler(const char *database, const char *table, const char *file,
291 struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl, const char *who_asked);
294 * \brief SQLite callback function for RealTime configuration.
296 * This function is passed to the SQLite engine as a callback function to
297 * parse a row and store it in a linked list of struct ast_variable objects.
299 * \param arg a pointer to a struct rt_cfg_entry_args object
300 * \param argc number of columns
301 * \param argv values in the row
302 * \param columnNames names and types of the columns
303 * \retval 0 on success.
304 * \retval 1 if an error occurred.
305 * \see rt_cfg_entry_args
306 * \see realtime_handler()
308 static int add_rt_cfg_entry(void *arg, int argc, char **argv,
312 * \brief Asterisk callback function for RealTime configuration.
314 * Asterisk will call this function each time it requires a variable
315 * through the RealTime architecture. ap is a list of parameters and
316 * values used to find a specific row, e.g one parameter "name" and
317 * one value "123" so that the SQL query becomes <code>SELECT * FROM
318 * table WHERE name = '123';</code>.
320 * \param database the database to use (ignored)
321 * \param table the table to use
322 * \param fields list of parameters and values to match
324 * \retval a linked list of struct ast_variable objects
325 * \retval NULL if an error occurred
326 * \see add_rt_cfg_entry()
328 static struct ast_variable * realtime_handler(const char *database,
329 const char *table, const struct ast_variable *fields);
332 * \brief SQLite callback function for RealTime configuration.
334 * This function performs the same actions as add_rt_cfg_entry() except
335 * that the rt_multi_cfg_entry_args structure is designed to store
336 * categories in addition to variables.
338 * \param arg a pointer to a struct rt_multi_cfg_entry_args object
339 * \param argc number of columns
340 * \param argv values in the row
341 * \param columnNames names and types of the columns
342 * \retval 0 on success.
343 * \retval 1 if an error occurred.
344 * \see rt_multi_cfg_entry_args
345 * \see realtime_multi_handler()
347 static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv,
351 * \brief Asterisk callback function for RealTime configuration.
353 * This function performs the same actions as realtime_handler() except
354 * that it can store variables per category, and can return several
357 * \param database the database to use (ignored)
358 * \param table the table to use
359 * \param fields list of parameters and values to match
360 * \retval a struct ast_config object storing categories and variables.
361 * \retval NULL if an error occurred.
363 * \see add_rt_multi_cfg_entry()
365 static struct ast_config * realtime_multi_handler(const char *database,
366 const char *table, const struct ast_variable *fields);
369 * \brief Asterisk callback function for RealTime configuration (variable
372 * Asterisk will call this function each time a variable has been modified
373 * internally and must be updated in the backend engine. keyfield and entity
374 * are used to find the row to update, e.g. <code>UPDATE table SET ... WHERE
375 * keyfield = 'entity';</code>. ap is a list of parameters and values with the
376 * same format as the other realtime functions.
378 * \param database the database to use (ignored)
379 * \param table the table to use
380 * \param keyfield the column of the matching cell
381 * \param entity the value of the matching cell
382 * \param fields list of parameters and new values to update in the database
383 * \retval the number of affected rows.
384 * \retval -1 if an error occurred.
386 static int realtime_update_handler(const char *database, const char *table,
387 const char *keyfield, const char *entity, const struct ast_variable *fields);
388 static int realtime_update2_handler(const char *database, const char *table,
389 const struct ast_variable *lookup_fields, const struct ast_variable *update_fields);
392 * \brief Asterisk callback function for RealTime configuration (variable
395 * Asterisk will call this function each time a variable has been created
396 * internally and must be stored in the backend engine.
397 * are used to find the row to update, e.g. ap is a list of parameters and
398 * values with the same format as the other realtime functions.
400 * \param database the database to use (ignored)
401 * \param table the table to use
402 * \param fields list of parameters and new values to insert into the database
403 * \retval the rowid of inserted row.
404 * \retval -1 if an error occurred.
406 static int realtime_store_handler(const char *database, const char *table,
407 const struct ast_variable *fields);
410 * \brief Asterisk callback function for RealTime configuration (destroys
413 * Asterisk will call this function each time a variable has been destroyed
414 * internally and must be removed from the backend engine. keyfield and entity
415 * are used to find the row to delete, e.g. <code>DELETE FROM table WHERE
416 * keyfield = 'entity';</code>. ap is a list of parameters and values with the
417 * same format as the other realtime functions.
419 * \param database the database to use (ignored)
420 * \param table the table to use
421 * \param keyfield the column of the matching cell
422 * \param entity the value of the matching cell
423 * \param fields list of additional parameters for cell matching
424 * \retval the number of affected rows.
425 * \retval -1 if an error occurred.
427 static int realtime_destroy_handler(const char *database, const char *table,
428 const char *keyfield, const char *entity, const struct ast_variable *fields);
431 * \brief Asterisk callback function for the CLI status command.
433 * \param e CLI command
435 * \param a CLI argument list
436 * \return RESULT_SUCCESS
438 static char *handle_cli_show_sqlite_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
439 static char *handle_cli_sqlite_show_tables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
441 static int realtime_require_handler(const char *database, const char *table, va_list ap);
442 static int realtime_unload_handler(const char *unused, const char *tablename);
444 /*! The SQLite database object. */
447 /*! Set to 1 if CDR support is enabled. */
450 /*! Set to 1 if the CDR callback function was registered. */
451 static int cdr_registered;
453 /*! Set to 1 if the CLI status command callback function was registered. */
454 static int cli_status_registered;
456 /*! The path of the database file. */
459 /*! The name of the static configuration table. */
460 static char *config_table;
462 /*! The name of the table used to store CDR entries. */
463 static char *cdr_table;
466 * The structure specifying all callback functions used by Asterisk for static
467 * and RealTime configuration.
469 static struct ast_config_engine sqlite_engine =
471 .name = RES_CONFIG_SQLITE_DRIVER,
472 .load_func = config_handler,
473 .realtime_func = realtime_handler,
474 .realtime_multi_func = realtime_multi_handler,
475 .store_func = realtime_store_handler,
476 .destroy_func = realtime_destroy_handler,
477 .update_func = realtime_update_handler,
478 .update2_func = realtime_update2_handler,
479 .require_func = realtime_require_handler,
480 .unload_func = realtime_unload_handler,
484 * The mutex used to prevent simultaneous access to the SQLite database.
486 AST_MUTEX_DEFINE_STATIC(mutex);
489 * Structure containing details and callback functions for the CLI status
492 static struct ast_cli_entry cli_status[] = {
493 AST_CLI_DEFINE(handle_cli_show_sqlite_status, "Show status information about the SQLite 2 driver"),
494 AST_CLI_DEFINE(handle_cli_sqlite_show_tables, "Cached table information about the SQLite 2 driver"),
497 struct sqlite_cache_columns {
500 unsigned char isint; /*!< By definition, only INTEGER PRIMARY KEY is an integer; everything else is a string. */
501 AST_RWLIST_ENTRY(sqlite_cache_columns) list;
504 struct sqlite_cache_tables {
506 AST_RWLIST_HEAD(_columns, sqlite_cache_columns) columns;
507 AST_RWLIST_ENTRY(sqlite_cache_tables) list;
510 static AST_RWLIST_HEAD_STATIC(sqlite_tables, sqlite_cache_tables);
513 * Taken from Asterisk 1.2 cdr_sqlite.so.
516 /*! SQL query format to create the CDR table if non existent. */
517 static char *sql_create_cdr_table =
518 "CREATE TABLE '%q' (\n"
520 " clid VARCHAR(80) NOT NULL DEFAULT '',\n"
521 " src VARCHAR(80) NOT NULL DEFAULT '',\n"
522 " dst VARCHAR(80) NOT NULL DEFAULT '',\n"
523 " dcontext VARCHAR(80) NOT NULL DEFAULT '',\n"
524 " channel VARCHAR(80) NOT NULL DEFAULT '',\n"
525 " dstchannel VARCHAR(80) NOT NULL DEFAULT '',\n"
526 " lastapp VARCHAR(80) NOT NULL DEFAULT '',\n"
527 " lastdata VARCHAR(80) NOT NULL DEFAULT '',\n"
528 " start DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n"
529 " answer DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n"
530 " end DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n"
531 " duration INT(11) NOT NULL DEFAULT 0,\n"
532 " billsec INT(11) NOT NULL DEFAULT 0,\n"
533 " disposition VARCHAR(45) NOT NULL DEFAULT '',\n"
534 " amaflags INT(11) NOT NULL DEFAULT 0,\n"
535 " accountcode VARCHAR(20) NOT NULL DEFAULT '',\n"
536 " uniqueid VARCHAR(32) NOT NULL DEFAULT '',\n"
537 " userfield VARCHAR(255) NOT NULL DEFAULT '',\n"
538 " PRIMARY KEY (id)\n"
542 * SQL query format to describe the table structure
544 #define sql_table_structure "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'"
547 * SQL query format to fetch the static configuration of a file.
548 * Rows must be sorted by category.
550 * \see add_cfg_entry()
552 #define sql_get_config_table \
555 " WHERE filename = '%q' AND commented = 0" \
556 " ORDER BY cat_metric ASC, var_metric ASC;"
558 static void free_table(struct sqlite_cache_tables *tblptr)
560 struct sqlite_cache_columns *col;
562 /* Obtain a write lock to ensure there are no read locks outstanding */
563 AST_RWLIST_WRLOCK(&(tblptr->columns));
564 while ((col = AST_RWLIST_REMOVE_HEAD(&(tblptr->columns), list))) {
567 AST_RWLIST_UNLOCK(&(tblptr->columns));
568 AST_RWLIST_HEAD_DESTROY(&(tblptr->columns));
572 static int find_table_cb(void *vtblptr, int argc, char **argv, char **columnNames)
574 struct sqlite_cache_tables *tblptr = vtblptr;
575 char *sql = ast_strdupa(argv[0]), *start, *end, *type, *remainder;
577 AST_DECLARE_APP_ARGS(fie,
578 AST_APP_ARG(ld)[100]; /* This means we support up to 100 columns per table */
580 struct sqlite_cache_columns *col;
582 /* This is really fun. We get to parse an SQL statement to figure out
583 * what columns are in the table.
585 if ((start = strchr(sql, '(')) && (end = strrchr(sql, ')'))) {
593 AST_STANDARD_APP_ARGS(fie, start);
594 for (i = 0; i < fie.argc; i++) {
595 fie.ld[i] = ast_skip_blanks(fie.ld[i]);
596 ast_debug(5, "Found field: %s\n", fie.ld[i]);
597 if (strncasecmp(fie.ld[i], "PRIMARY KEY", 11) == 0 && (start = strchr(fie.ld[i], '(')) && (end = strchr(fie.ld[i], ')'))) {
599 AST_RWLIST_TRAVERSE(&(tblptr->columns), col, list) {
600 if (strcasecmp(start + 1, col->name) == 0 && strcasestr(col->type, "INTEGER")) {
606 /* type delimiter could be any space character */
607 for (type = fie.ld[i]; *type > 32; type++);
609 type = ast_skip_blanks(type);
610 for (remainder = type; *remainder > 32; remainder++);
612 if (!(col = ast_calloc(1, sizeof(*col) + strlen(fie.ld[i]) + strlen(type) + 2))) {
615 col->name = (char *)col + sizeof(*col);
616 col->type = (char *)col + sizeof(*col) + strlen(fie.ld[i]) + 1;
617 strcpy(col->name, fie.ld[i]); /* SAFE */
618 strcpy(col->type, type); /* SAFE */
619 if (strcasestr(col->type, "INTEGER") && strcasestr(col->type, "PRIMARY KEY")) {
622 AST_LIST_INSERT_TAIL(&(tblptr->columns), col, list);
627 static struct sqlite_cache_tables *find_table(const char *tablename)
629 struct sqlite_cache_tables *tblptr;
631 char *sql, *errstr = NULL;
633 AST_RWLIST_RDLOCK(&sqlite_tables);
635 for (i = 0; i < 2; i++) {
636 AST_RWLIST_TRAVERSE(&sqlite_tables, tblptr, list) {
637 if (strcmp(tblptr->name, tablename) == 0) {
642 AST_RWLIST_RDLOCK(&(tblptr->columns));
643 AST_RWLIST_UNLOCK(&sqlite_tables);
648 AST_RWLIST_UNLOCK(&sqlite_tables);
649 AST_RWLIST_WRLOCK(&sqlite_tables);
653 /* Table structure not cached; build the structure now */
654 if (ast_asprintf(&sql, sql_table_structure, tablename) < 0) {
657 if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
658 AST_RWLIST_UNLOCK(&sqlite_tables);
659 ast_log(LOG_ERROR, "Memory error. Cannot cache table '%s'\n", tablename);
663 tblptr->name = (char *)tblptr + sizeof(*tblptr);
664 strcpy(tblptr->name, tablename); /* SAFE */
665 AST_RWLIST_HEAD_INIT(&(tblptr->columns));
667 ast_debug(1, "About to query table structure: %s\n", sql);
669 ast_mutex_lock(&mutex);
670 if ((err = sqlite_exec(db, sql, find_table_cb, tblptr, &errstr))) {
671 ast_mutex_unlock(&mutex);
672 ast_log(LOG_WARNING, "SQLite error %d: %s\n", err, errstr);
675 AST_RWLIST_UNLOCK(&sqlite_tables);
679 ast_mutex_unlock(&mutex);
682 if (AST_LIST_EMPTY(&(tblptr->columns))) {
684 AST_RWLIST_UNLOCK(&sqlite_tables);
688 AST_RWLIST_INSERT_TAIL(&sqlite_tables, tblptr, list);
689 AST_RWLIST_RDLOCK(&(tblptr->columns));
690 AST_RWLIST_UNLOCK(&sqlite_tables);
694 #define release_table(a) AST_RWLIST_UNLOCK(&((a)->columns))
696 static int set_var(char **var, const char *name, const char *value)
701 *var = ast_strdup(value);
704 ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
711 static int check_vars(void)
714 ast_log(LOG_ERROR, "Required parameter undefined: dbfile\n");
718 use_cdr = (cdr_table != NULL);
723 static int load_config(void)
725 struct ast_config *config;
726 struct ast_variable *var;
728 struct ast_flags config_flags = { 0 };
730 config = ast_config_load(RES_CONFIG_SQLITE_CONF_FILE, config_flags);
732 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
733 ast_log(LOG_ERROR, "Unable to load " RES_CONFIG_SQLITE_CONF_FILE "\n");
737 for (var = ast_variable_browse(config, "general"); var; var = var->next) {
738 if (!strcasecmp(var->name, "dbfile"))
739 SET_VAR(config, dbfile, var);
740 else if (!strcasecmp(var->name, "config_table"))
741 SET_VAR(config, config_table, var);
742 else if (!strcasecmp(var->name, "cdr_table")) {
743 SET_VAR(config, cdr_table, var);
745 ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
748 ast_config_destroy(config);
749 error = check_vars();
759 static void unload_config(void)
761 struct sqlite_cache_tables *tbl;
764 ast_free(config_table);
768 AST_RWLIST_WRLOCK(&sqlite_tables);
769 while ((tbl = AST_RWLIST_REMOVE_HEAD(&sqlite_tables, list))) {
772 AST_RWLIST_UNLOCK(&sqlite_tables);
775 static int cdr_handler(struct ast_cdr *cdr)
777 char *errormsg = NULL, *tmp, workspace[500];
779 struct sqlite_cache_tables *tbl = find_table(cdr_table);
780 struct sqlite_cache_columns *col;
781 struct ast_str *sql1 = ast_str_create(160), *sql2 = ast_str_create(16);
784 if (!sql1 || !sql2) {
791 ast_log(LOG_WARNING, "No such table: %s\n", cdr_table);
797 ast_str_set(&sql1, 0, "INSERT INTO %s (", cdr_table);
798 ast_str_set(&sql2, 0, ") VALUES (");
800 AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
802 ast_cdr_format_var(cdr, col->name, &tmp, workspace, sizeof(workspace), 1);
806 if (sscanf(tmp, "%30d", &scannum) == 1) {
807 ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
808 ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", scannum);
811 ast_cdr_format_var(cdr, col->name, &tmp, workspace, sizeof(workspace), 0);
815 ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
816 tmp = sqlite_mprintf("%Q", tmp);
817 ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", tmp);
824 ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
827 ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql1));
829 ast_mutex_lock(&mutex);
831 RES_CONFIG_SQLITE_BEGIN
832 error = sqlite_exec(db, ast_str_buffer(sql1), NULL, NULL, &errormsg);
833 RES_CONFIG_SQLITE_END(error)
835 ast_mutex_unlock(&mutex);
840 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
841 sqlite_freemem(errormsg);
844 sqlite_freemem(errormsg);
849 static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
851 struct cfg_entry_args *args;
852 struct ast_variable *var;
854 if (argc != RES_CONFIG_SQLITE_CONFIG_COLUMNS) {
855 ast_log(LOG_WARNING, "Corrupt table\n");
861 if (!strcmp(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], "#include")) {
862 struct ast_config *cfg;
865 val = argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL];
866 cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked);
869 ast_log(LOG_WARNING, "Unable to include %s\n", val);
877 if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
878 args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
881 ast_log(LOG_WARNING, "Unable to allocate category\n");
885 ast_free(args->cat_name);
886 args->cat_name = ast_strdup(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
888 if (!args->cat_name) {
889 ast_category_destroy(args->cat);
893 ast_category_append(args->cfg, args->cat);
896 var = ast_variable_new(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL], "");
899 ast_log(LOG_WARNING, "Unable to allocate variable\n");
903 ast_variable_append(args->cat, var);
908 static struct ast_config *config_handler(const char *database, const char *table, const char *file,
909 struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl, const char *who_asked)
911 struct cfg_entry_args args;
912 char *query, *errormsg = NULL;
917 ast_log(LOG_ERROR, "Table name unspecified\n");
921 table = config_table;
923 query = sqlite_mprintf(sql_get_config_table, table, file);
926 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
930 ast_debug(1, "SQL query: %s\n", query);
933 args.cat_name = NULL;
935 args.who_asked = who_asked;
937 ast_mutex_lock(&mutex);
939 RES_CONFIG_SQLITE_BEGIN
940 error = sqlite_exec(db, query, add_cfg_entry, &args, &errormsg);
941 RES_CONFIG_SQLITE_END(error)
943 ast_mutex_unlock(&mutex);
945 ast_free(args.cat_name);
946 sqlite_freemem(query);
949 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
950 sqlite_freemem(errormsg);
953 sqlite_freemem(errormsg);
958 static int add_rt_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
960 struct rt_cfg_entry_args *args;
961 struct ast_variable *var;
966 for (i = 0; i < argc; i++) {
970 if (!(var = ast_variable_new(columnNames[i], argv[i], "")))
979 args->last->next = var;
987 static struct ast_variable * realtime_handler(const char *database, const char *table, const struct ast_variable *fields)
989 char *query, *errormsg = NULL, *op, *tmp_str;
990 struct rt_cfg_entry_args args;
991 const struct ast_variable *field = fields;
995 ast_log(LOG_WARNING, "Table name unspecified\n");
1003 op = (strchr(field->name, ' ') == NULL) ? " =" : "";
1005 /* \cond DOXYGEN_CAN_PARSE_THIS */
1007 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1010 query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", field->name, op, field->value);
1013 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1017 while ((field = field->next)) {
1018 op = (strchr(field->name, ' ') == NULL) ? " =" : "";
1019 tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, field->name, op, field->value);
1020 sqlite_freemem(query);
1023 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1030 tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
1031 sqlite_freemem(query);
1034 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1039 ast_debug(1, "SQL query: %s\n", query);
1043 ast_mutex_lock(&mutex);
1045 RES_CONFIG_SQLITE_BEGIN
1046 error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
1047 RES_CONFIG_SQLITE_END(error)
1049 ast_mutex_unlock(&mutex);
1051 sqlite_freemem(query);
1054 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1055 sqlite_freemem(errormsg);
1056 ast_variables_destroy(args.var);
1059 sqlite_freemem(errormsg);
1064 static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
1066 struct rt_multi_cfg_entry_args *args;
1067 struct ast_category *cat;
1068 struct ast_variable *var;
1076 * cat_name should always be set here, since initfield is forged from
1077 * params[0] in realtime_multi_handler(), which is a search parameter
1080 for (i = 0; i < argc; i++) {
1081 if (!strcmp(args->initfield, columnNames[i]))
1086 ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
1090 if (!(cat = ast_category_new(cat_name, "", 99999))) {
1091 ast_log(LOG_WARNING, "Unable to allocate category\n");
1095 ast_category_append(args->cfg, cat);
1097 for (i = 0; i < argc; i++) {
1102 if (!(var = ast_variable_new(columnNames[i], argv[i], ""))) {
1103 ast_log(LOG_WARNING, "Unable to allocate variable\n");
1107 ast_variable_append(cat, var);
1113 static struct ast_config *realtime_multi_handler(const char *database,
1114 const char *table, const struct ast_variable *fields)
1116 char *query, *errormsg = NULL, *op, *tmp_str, *initfield;
1117 struct rt_multi_cfg_entry_args args;
1118 const struct ast_variable *field = fields;
1119 struct ast_config *cfg;
1123 ast_log(LOG_WARNING, "Table name unspecified\n");
1131 if (!(cfg = ast_config_new())) {
1132 ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
1136 if (!(initfield = ast_strdup(field->name))) {
1137 ast_config_destroy(cfg);
1141 tmp_str = strchr(initfield, ' ');
1146 op = (!strchr(field->name, ' ')) ? " =" : "";
1149 * Asterisk sends us an already escaped string when searching for
1150 * "exten LIKE" (uh!). Handle it separately.
1152 tmp_str = (!strcmp(field->value, "\\_%")) ? "_%" : (char *)field->value;
1154 /* \cond DOXYGEN_CAN_PARSE_THIS */
1156 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1159 if (!(query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", field->name, op, tmp_str))) {
1160 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1161 ast_config_destroy(cfg);
1162 ast_free(initfield);
1166 while ((field = field->next)) {
1167 op = (!strchr(field->name, ' ')) ? " =" : "";
1168 tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, field->name, op, field->value);
1169 sqlite_freemem(query);
1172 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1173 ast_config_destroy(cfg);
1174 ast_free(initfield);
1181 if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
1182 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1183 sqlite_freemem(query);
1184 ast_config_destroy(cfg);
1185 ast_free(initfield);
1189 sqlite_freemem(query);
1191 ast_debug(1, "SQL query: %s\n", query);
1193 args.initfield = initfield;
1195 ast_mutex_lock(&mutex);
1197 RES_CONFIG_SQLITE_BEGIN
1198 error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
1199 RES_CONFIG_SQLITE_END(error)
1201 ast_mutex_unlock(&mutex);
1203 sqlite_freemem(query);
1204 ast_free(initfield);
1207 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1208 sqlite_freemem(errormsg);
1209 ast_config_destroy(cfg);
1212 sqlite_freemem(errormsg);
1217 static int realtime_update_handler(const char *database, const char *table,
1218 const char *keyfield, const char *entity, const struct ast_variable *fields)
1220 char *query, *errormsg = NULL, *tmp_str;
1221 const struct ast_variable *field = fields;
1222 int error, rows_num;
1225 ast_log(LOG_WARNING, "Table name unspecified\n");
1233 /* \cond DOXYGEN_CAN_PARSE_THIS */
1235 #define QUERY "UPDATE '%q' SET %q = '%q'"
1238 if (!(query = sqlite_mprintf(QUERY, table, field->name, field->value))) {
1239 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1243 while ((field = field->next)) {
1244 tmp_str = sqlite_mprintf("%s, %q = '%q'", query, field->name, field->value);
1245 sqlite_freemem(query);
1248 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1255 if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
1256 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1257 sqlite_freemem(query);
1261 sqlite_freemem(query);
1263 ast_debug(1, "SQL query: %s\n", query);
1265 ast_mutex_lock(&mutex);
1267 RES_CONFIG_SQLITE_BEGIN
1268 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1269 RES_CONFIG_SQLITE_END(error)
1272 rows_num = sqlite_changes(db);
1276 ast_mutex_unlock(&mutex);
1278 sqlite_freemem(query);
1281 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1283 sqlite_freemem(errormsg);
1288 static int realtime_update2_handler(const char *database, const char *table,
1289 const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
1291 char *errormsg = NULL, *tmp1, *tmp2;
1292 int error, rows_num, first = 1;
1293 struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
1294 struct ast_str *where = ast_str_thread_get(&where_buf, 100);
1295 const struct ast_variable *field;
1298 ast_log(LOG_WARNING, "Table name unspecified\n");
1306 ast_str_set(&sql, 0, "UPDATE %s SET", table);
1307 ast_str_set(&where, 0, " WHERE");
1309 for (field = lookup_fields; field; field = field->next) {
1310 ast_str_append(&where, 0, "%s %s = %s",
1311 first ? "" : " AND",
1312 tmp1 = sqlite_mprintf("%q", field->name),
1313 tmp2 = sqlite_mprintf("%Q", field->value));
1314 sqlite_freemem(tmp1);
1315 sqlite_freemem(tmp2);
1320 ast_log(LOG_ERROR, "No criteria specified on update to '%s@%s'!\n", table, database);
1325 for (field = update_fields; field; field = field->next) {
1326 ast_str_append(&sql, 0, "%s %s = %s",
1328 tmp1 = sqlite_mprintf("%q", field->name),
1329 tmp2 = sqlite_mprintf("%Q", field->value));
1330 sqlite_freemem(tmp1);
1331 sqlite_freemem(tmp2);
1335 ast_str_append(&sql, 0, " %s", ast_str_buffer(where));
1336 ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql));
1338 ast_mutex_lock(&mutex);
1340 RES_CONFIG_SQLITE_BEGIN
1341 error = sqlite_exec(db, ast_str_buffer(sql), NULL, NULL, &errormsg);
1342 RES_CONFIG_SQLITE_END(error)
1345 rows_num = sqlite_changes(db);
1350 ast_mutex_unlock(&mutex);
1353 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1355 sqlite_freemem(errormsg);
1360 static int realtime_store_handler(const char *database, const char *table, const struct ast_variable *fields)
1362 char *errormsg = NULL, *tmp_str, *tmp_keys = NULL, *tmp_keys2 = NULL, *tmp_vals = NULL, *tmp_vals2 = NULL;
1363 const struct ast_variable *field = fields;
1367 ast_log(LOG_WARNING, "Table name unspecified\n");
1375 /* \cond DOXYGEN_CAN_PARSE_THIS */
1377 #define QUERY "INSERT into '%q' (%s) VALUES (%s);"
1382 tmp_keys = sqlite_mprintf("%s, %q", tmp_keys2, field->name);
1383 sqlite_freemem(tmp_keys2);
1385 tmp_keys = sqlite_mprintf("%q", field->name);
1388 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1389 sqlite_freemem(tmp_vals);
1394 tmp_vals = sqlite_mprintf("%s, '%q'", tmp_vals2, field->value);
1395 sqlite_freemem(tmp_vals2);
1397 tmp_vals = sqlite_mprintf("'%q'", field->value);
1400 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1401 sqlite_freemem(tmp_keys);
1406 tmp_keys2 = tmp_keys;
1407 tmp_vals2 = tmp_vals;
1408 } while ((field = field->next));
1410 if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
1411 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1412 sqlite_freemem(tmp_keys);
1413 sqlite_freemem(tmp_vals);
1417 sqlite_freemem(tmp_keys);
1418 sqlite_freemem(tmp_vals);
1420 ast_debug(1, "SQL query: %s\n", tmp_str);
1422 ast_mutex_lock(&mutex);
1424 RES_CONFIG_SQLITE_BEGIN
1425 error = sqlite_exec(db, tmp_str, NULL, NULL, &errormsg);
1426 RES_CONFIG_SQLITE_END(error)
1429 rows_id = sqlite_last_insert_rowid(db);
1434 ast_mutex_unlock(&mutex);
1436 sqlite_freemem(tmp_str);
1439 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1441 sqlite_freemem(errormsg);
1446 static int realtime_destroy_handler(const char *database, const char *table,
1447 const char *keyfield, const char *entity, const struct ast_variable *fields)
1449 char *query, *errormsg = NULL, *tmp_str;
1450 const struct ast_variable *field;
1451 int error, rows_num;
1454 ast_log(LOG_WARNING, "Table name unspecified\n");
1458 /* \cond DOXYGEN_CAN_PARSE_THIS */
1460 #define QUERY "DELETE FROM '%q' WHERE"
1463 if (!(query = sqlite_mprintf(QUERY, table))) {
1464 ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1468 for (field = fields; field; field = field->next) {
1469 tmp_str = sqlite_mprintf("%s %q = '%q' AND", query, field->name, field->value);
1470 sqlite_freemem(query);
1473 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1480 if (!(tmp_str = sqlite_mprintf("%s %q = '%q';", query, keyfield, entity))) {
1481 ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1482 sqlite_freemem(query);
1485 sqlite_freemem(query);
1487 ast_debug(1, "SQL query: %s\n", query);
1489 ast_mutex_lock(&mutex);
1491 RES_CONFIG_SQLITE_BEGIN
1492 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1493 RES_CONFIG_SQLITE_END(error)
1496 rows_num = sqlite_changes(db);
1501 ast_mutex_unlock(&mutex);
1503 sqlite_freemem(query);
1506 ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1508 sqlite_freemem(errormsg);
1513 static int realtime_require_handler(const char *unused, const char *tablename, va_list ap)
1515 struct sqlite_cache_tables *tbl = find_table(tablename);
1516 struct sqlite_cache_columns *col;
1524 while ((elm = va_arg(ap, char *))) {
1525 type = va_arg(ap, require_type);
1527 /* Check if the field matches the criteria */
1528 AST_RWLIST_TRAVERSE(&tbl->columns, col, list) {
1529 if (strcmp(col->name, elm) == 0) {
1530 /* SQLite only has two types - the 32-bit integer field that
1531 * is the key column, and everything else (everything else
1534 if (col->isint && !ast_rq_is_int(type)) {
1535 ast_log(LOG_WARNING, "Realtime table %s: column '%s' is an integer field, but Asterisk requires that it not be!\n", tablename, col->name);
1542 ast_log(LOG_WARNING, "Realtime table %s requires column '%s', but that column does not exist!\n", tablename, elm);
1545 AST_RWLIST_UNLOCK(&(tbl->columns));
1549 static int realtime_unload_handler(const char *unused, const char *tablename)
1551 struct sqlite_cache_tables *tbl;
1552 AST_RWLIST_WRLOCK(&sqlite_tables);
1553 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&sqlite_tables, tbl, list) {
1554 if (!strcasecmp(tbl->name, tablename)) {
1555 AST_RWLIST_REMOVE_CURRENT(list);
1559 AST_RWLIST_TRAVERSE_SAFE_END
1560 AST_RWLIST_UNLOCK(&sqlite_tables);
1564 static char *handle_cli_show_sqlite_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1568 e->command = "sqlite show status";
1570 "Usage: sqlite show status\n"
1571 " Show status information about the SQLite 2 driver\n";
1578 return CLI_SHOWUSAGE;
1580 ast_cli(a->fd, "SQLite database path: %s\n", dbfile);
1581 ast_cli(a->fd, "config_table: ");
1584 ast_cli(a->fd, "unspecified, must be present in extconfig.conf\n");
1586 ast_cli(a->fd, "%s\n", config_table);
1588 ast_cli(a->fd, "cdr_table: ");
1591 ast_cli(a->fd, "unspecified, CDR support disabled\n");
1593 ast_cli(a->fd, "%s\n", cdr_table);
1598 static char *handle_cli_sqlite_show_tables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1600 struct sqlite_cache_tables *tbl;
1601 struct sqlite_cache_columns *col;
1606 e->command = "sqlite show tables";
1608 "Usage: sqlite show tables\n"
1609 " Show table information about the SQLite 2 driver\n";
1616 return CLI_SHOWUSAGE;
1618 AST_RWLIST_RDLOCK(&sqlite_tables);
1619 AST_RWLIST_TRAVERSE(&sqlite_tables, tbl, list) {
1621 ast_cli(a->fd, "Table %s:\n", tbl->name);
1622 AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
1623 fprintf(stderr, "%s\n", col->name);
1624 ast_cli(a->fd, " %20.20s %-30.30s\n", col->name, col->type);
1627 AST_RWLIST_UNLOCK(&sqlite_tables);
1630 ast_cli(a->fd, "No tables currently in cache\n");
1636 static int unload_module(void)
1638 if (cdr_registered && ast_cdr_unregister(RES_CONFIG_SQLITE_NAME)) {
1642 if (cli_status_registered) {
1643 ast_cli_unregister_multiple(cli_status, ARRAY_LEN(cli_status));
1646 ast_config_engine_deregister(&sqlite_engine);
1657 * \brief Load the module
1659 * Module loading including tests for configuration or dependencies.
1660 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
1661 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
1662 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
1663 * configuration file or other non-critical problem return
1664 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
1666 static int load_module(void)
1668 char *errormsg = NULL;
1673 cli_status_registered = 0;
1675 config_table = NULL;
1677 error = load_config();
1680 return AST_MODULE_LOAD_DECLINE;
1682 if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
1683 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1684 sqlite_freemem(errormsg);
1689 sqlite_freemem(errormsg);
1691 ast_config_engine_register(&sqlite_engine);
1696 /* \cond DOXYGEN_CAN_PARSE_THIS */
1698 #define QUERY "SELECT COUNT(id) FROM %Q;"
1701 query = sqlite_mprintf(QUERY, cdr_table);
1704 ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1709 ast_debug(1, "SQL query: %s\n", query);
1711 RES_CONFIG_SQLITE_BEGIN
1712 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1713 RES_CONFIG_SQLITE_END(error)
1715 sqlite_freemem(query);
1721 if (error != SQLITE_ERROR) {
1722 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1723 sqlite_freemem(errormsg);
1728 sqlite_freemem(errormsg);
1730 query = sqlite_mprintf(sql_create_cdr_table, cdr_table);
1733 ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1738 ast_debug(1, "SQL query: %s\n", query);
1740 RES_CONFIG_SQLITE_BEGIN
1741 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1742 RES_CONFIG_SQLITE_END(error)
1744 sqlite_freemem(query);
1747 ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1748 sqlite_freemem(errormsg);
1753 sqlite_freemem(errormsg);
1756 error = ast_cdr_register(RES_CONFIG_SQLITE_NAME, RES_CONFIG_SQLITE_DESCRIPTION, cdr_handler);
1766 error = ast_cli_register_multiple(cli_status, ARRAY_LEN(cli_status));
1773 cli_status_registered = 1;
1778 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Realtime SQLite configuration",
1779 .support_level = AST_MODULE_SUPPORT_EXTENDED,
1780 .load = load_module,
1781 .unload = unload_module,
1782 .load_pri = AST_MODPRI_REALTIME_DRIVER,