Wed Aug 11 01:09:56 2010

Asterisk developer's documentation


res_config_sqlite.c File Reference

res_config_sqlite module. More...

#include "asterisk.h"
#include <sqlite.h>
#include "asterisk/logger.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/cdr.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/linkedlists.h"

Include dependency graph for res_config_sqlite.c:

Go to the source code of this file.

Data Structures

struct  _columns
struct  cfg_entry_args
struct  rt_cfg_entry_args
struct  rt_multi_cfg_entry_args
struct  sqlite_cache_columns
struct  sqlite_cache_tables
struct  sqlite_tables

Defines

#define MACRO_BEGIN   do {
#define MACRO_END   } while (0)
#define release_table(a)   AST_RWLIST_UNLOCK(&((a)->columns))
#define RES_CONFIG_SQLITE_BEGIN
#define RES_CONFIG_SQLITE_CONF_FILE   "res_config_sqlite.conf"
#define RES_CONFIG_SQLITE_DESCRIPTION   "Resource Module for SQLite 2"
#define RES_CONFIG_SQLITE_DRIVER   "sqlite"
#define RES_CONFIG_SQLITE_END(error)
#define RES_CONFIG_SQLITE_MAX_LOOPS   10
#define RES_CONFIG_SQLITE_NAME   "res_config_sqlite"
#define SET_VAR(config, to, from)
#define sql_get_config_table
#define sql_table_structure   "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'"

Enumerations

enum  {
  RES_CONFIG_SQLITE_CONFIG_ID, RES_CONFIG_SQLITE_CONFIG_CAT_METRIC, RES_CONFIG_SQLITE_CONFIG_VAR_METRIC, RES_CONFIG_SQLITE_CONFIG_COMMENTED,
  RES_CONFIG_SQLITE_CONFIG_FILENAME, RES_CONFIG_SQLITE_CONFIG_CATEGORY, RES_CONFIG_SQLITE_CONFIG_VAR_NAME, RES_CONFIG_SQLITE_CONFIG_VAR_VAL,
  RES_CONFIG_SQLITE_CONFIG_COLUMNS
}

Functions

static void __init_sql_buf (void)
static void __init_where_buf (void)
static void __reg_module (void)
static void __unreg_module (void)
static int add_cfg_entry (void *arg, int argc, char **argv, char **columnNames)
 SQLite callback function for static configuration.
static int add_rt_cfg_entry (void *arg, int argc, char **argv, char **columnNames)
 SQLite callback function for RealTime configuration.
static int add_rt_multi_cfg_entry (void *arg, int argc, char **argv, char **columnNames)
 SQLite callback function for RealTime configuration.
static int cdr_handler (struct ast_cdr *cdr)
 Asterisk callback function for CDR support.
static int check_vars (void)
static struct ast_config * config_handler (const char *database, const char *table, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl, const char *who_asked)
 Asterisk callback function for static configuration.
static struct sqlite_cache_tables * find_table (const char *tablename)
static int find_table_cb (void *vtblptr, int argc, char **argv, char **columnNames)
static void free_table (struct sqlite_cache_tables *tblptr)
static size_t get_params (va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
 Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values.
static char * handle_cli_show_sqlite_status (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 Asterisk callback function for the CLI status command.
static char * handle_cli_sqlite_show_tables (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int load_config (void)
 Load the configuration file.
static int load_module (void)
static int realtime_destroy_handler (const char *database, const char *table, const char *keyfield, const char *entity, va_list ap)
 Asterisk callback function for RealTime configuration (destroys variable).
static struct ast_variable * realtime_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration.
static struct ast_config * realtime_multi_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration.
static int realtime_require_handler (const char *database, const char *table, va_list ap)
static int realtime_store_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration (variable create/store).
static int realtime_unload_handler (const char *unused, const char *tablename)
static int realtime_update2_handler (const char *database, const char *table, va_list ap)
static int realtime_update_handler (const char *database, const char *table, const char *keyfield, const char *entity, va_list ap)
 Asterisk callback function for RealTime configuration (variable update).
static int set_var (char **var, const char *name, const char *value)
 Allocate a variable.
static void unload_config (void)
 Free resources related to configuration.
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Realtime SQLite configuration" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static struct ast_module_info * ast_module_info = &__mod_info
static int cdr_registered
static char * cdr_table
static struct ast_cli_entry cli_status []
static int cli_status_registered
static char * config_table
static sqlite * db
static char * dbfile
static ast_mutex_t mutex = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP )
static struct ast_threadstorage sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , }
static char * sql_create_cdr_table
static struct ast_config_engine sqlite_engine
static int use_cdr
static struct ast_threadstorage where_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_where_buf , .custom_init = NULL , }


Detailed Description

res_config_sqlite module.

Definition in file res_config_sqlite.c.


Define Documentation

#define MACRO_BEGIN   do {

Definition at line 93 of file res_config_sqlite.c.

#define MACRO_END   } while (0)

Definition at line 94 of file res_config_sqlite.c.

#define release_table ( a   )     AST_RWLIST_UNLOCK(&((a)->columns))

Definition at line 706 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_BEGIN

#define RES_CONFIG_SQLITE_CONF_FILE   "res_config_sqlite.conf"

Definition at line 99 of file res_config_sqlite.c.

Referenced by load_config().

#define RES_CONFIG_SQLITE_DESCRIPTION   "Resource Module for SQLite 2"

Definition at line 98 of file res_config_sqlite.c.

Referenced by load_module().

#define RES_CONFIG_SQLITE_DRIVER   "sqlite"

Definition at line 97 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_END ( error   ) 

Value:

if (error != SQLITE_BUSY)  \
         break;                  \
      usleep(1000);                 \
   }                       \
MACRO_END;
Macro used after executing a query.

See also:
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 163 of file res_config_sqlite.c.

Referenced by cdr_handler(), config_handler(), load_module(), realtime_destroy_handler(), realtime_handler(), realtime_multi_handler(), realtime_store_handler(), realtime_update2_handler(), and realtime_update_handler().

#define RES_CONFIG_SQLITE_MAX_LOOPS   10

Maximum number of loops before giving up executing a query. Calls to sqlite_xxx() functions which can return SQLITE_BUSY are enclosed by RES_CONFIG_SQLITE_BEGIN and RES_CONFIG_SQLITE_END, e.g.

 char *errormsg;
 int error;

 RES_CONFIG_SQLITE_BEGIN
	 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
 RES_CONFIG_SQLITE_END(error)

 if (error)
	 ...;
 

Definition at line 145 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_NAME   "res_config_sqlite"

Definition at line 96 of file res_config_sqlite.c.

Referenced by load_module(), and unload_module().

#define SET_VAR ( config,
to,
from   ) 

Definition at line 113 of file res_config_sqlite.c.

Referenced by load_config().

#define sql_get_config_table

Value:

"SELECT *" \
   "  FROM '%q'" \
   "  WHERE filename = '%q' AND commented = 0" \
   "  ORDER BY cat_metric ASC, var_metric ASC;"
SQL query format to fetch the static configuration of a file. Rows must be sorted by category.

See also:
add_cfg_entry()

Definition at line 566 of file res_config_sqlite.c.

Referenced by config_handler().

#define sql_table_structure   "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'"

SQL query format to describe the table structure

Definition at line 558 of file res_config_sqlite.c.

Referenced by find_table().


Enumeration Type Documentation

anonymous enum

Enumerator:
RES_CONFIG_SQLITE_CONFIG_ID 
RES_CONFIG_SQLITE_CONFIG_CAT_METRIC 
RES_CONFIG_SQLITE_CONFIG_VAR_METRIC 
RES_CONFIG_SQLITE_CONFIG_COMMENTED 
RES_CONFIG_SQLITE_CONFIG_FILENAME 
RES_CONFIG_SQLITE_CONFIG_CATEGORY 
RES_CONFIG_SQLITE_CONFIG_VAR_NAME 
RES_CONFIG_SQLITE_CONFIG_VAR_VAL 
RES_CONFIG_SQLITE_CONFIG_COLUMNS 

Definition at line 101 of file res_config_sqlite.c.


Function Documentation

static void __init_sql_buf ( void   )  [static]

Definition at line 126 of file res_config_sqlite.c.

00156 {

static void __init_where_buf ( void   )  [static]

Definition at line 127 of file res_config_sqlite.c.

00156 {

static void __reg_module ( void   )  [static]

Definition at line 1870 of file res_config_sqlite.c.

static void __unreg_module ( void   )  [static]

Definition at line 1870 of file res_config_sqlite.c.

static int add_cfg_entry ( void *  arg,
int  argc,
char **  argv,
char **  columnNames 
) [static]

SQLite callback function for static configuration.

This function is passed to the SQLite engine as a callback function to parse a row and store it in a struct ast_config object. It relies on resulting rows being sorted by category.

Parameters:
arg a pointer to a struct cfg_entry_args object
argc number of columns
argv values in the row
columnNames names and types of the columns
Return values:
0 on success
1 if an error occurred
See also:
cfg_entry_args

sql_get_config_table

config_handler()

Definition at line 853 of file res_config_sqlite.c.

References ast_category_append(), ast_category_destroy(), ast_category_new(), ast_config_internal_load(), ast_free, ast_log(), ast_strdup, ast_variable_append(), ast_variable_new(), cfg_entry_args::cat, cfg_entry_args::cat_name, cfg_entry_args::cfg, cfg_entry_args::flags, LOG_WARNING, RES_CONFIG_SQLITE_CONFIG_CATEGORY, RES_CONFIG_SQLITE_CONFIG_COLUMNS, RES_CONFIG_SQLITE_CONFIG_VAR_NAME, RES_CONFIG_SQLITE_CONFIG_VAR_VAL, var, and cfg_entry_args::who_asked.

Referenced by config_handler().

00854 {
00855    struct cfg_entry_args *args;
00856    struct ast_variable *var;
00857 
00858    if (argc != RES_CONFIG_SQLITE_CONFIG_COLUMNS) {
00859       ast_log(LOG_WARNING, "Corrupt table\n");
00860       return 1;
00861    }
00862 
00863    args = arg;
00864 
00865    if (!strcmp(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], "#include")) {
00866       struct ast_config *cfg;
00867       char *val;
00868 
00869       val = argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL];
00870       cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked);
00871 
00872       if (!cfg) {
00873          ast_log(LOG_WARNING, "Unable to include %s\n", val);
00874          return 1;
00875       } else {
00876          args->cfg = cfg;
00877          return 0;
00878       }
00879    }
00880 
00881    if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
00882       args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
00883 
00884       if (!args->cat) {
00885          ast_log(LOG_WARNING, "Unable to allocate category\n");
00886          return 1;
00887       }
00888 
00889       ast_free(args->cat_name);
00890       args->cat_name = ast_strdup(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
00891 
00892       if (!args->cat_name) {
00893          ast_category_destroy(args->cat);
00894          return 1;
00895       }
00896 
00897       ast_category_append(args->cfg, args->cat);
00898    }
00899 
00900    var = ast_variable_new(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL], "");
00901 
00902    if (!var) {
00903       ast_log(LOG_WARNING, "Unable to allocate variable");
00904       return 1;
00905    }
00906 
00907    ast_variable_append(args->cat, var);
00908 
00909    return 0;
00910 }

static int add_rt_cfg_entry ( void *  arg,
int  argc,
char **  argv,
char **  columnNames 
) [static]

SQLite callback function for RealTime configuration.

This function is passed to the SQLite engine as a callback function to parse a row and store it in a linked list of struct ast_variable objects.

Parameters:
arg a pointer to a struct rt_cfg_entry_args object
argc number of columns
argv values in the row
columnNames names and types of the columns
Return values:
0 on success.
1 if an error occurred.
See also:
rt_cfg_entry_args

realtime_handler()

Definition at line 1001 of file res_config_sqlite.c.

References ast_variable_new(), rt_cfg_entry_args::last, ast_variable::next, rt_cfg_entry_args::var, and var.

Referenced by realtime_handler().

01002 {
01003    struct rt_cfg_entry_args *args;
01004    struct ast_variable *var;
01005    int i;
01006 
01007    args = arg;
01008 
01009    for (i = 0; i < argc; i++) {
01010       if (!argv[i])
01011          continue;
01012 
01013       if (!(var = ast_variable_new(columnNames[i], argv[i], "")))
01014          return 1;
01015 
01016       if (!args->var)
01017          args->var = var;
01018 
01019       if (!args->last)
01020          args->last = var;
01021       else {
01022          args->last->next = var;
01023          args->last = var;
01024       }
01025    }
01026 
01027    return 0;
01028 }

static int add_rt_multi_cfg_entry ( void *  arg,
int  argc,
char **  argv,
char **  columnNames 
) [static]

SQLite callback function for RealTime configuration.

This function performs the same actions as add_rt_cfg_entry() except that the rt_multi_cfg_entry_args structure is designed to store categories in addition to variables.

Parameters:
arg a pointer to a struct rt_multi_cfg_entry_args object
argc number of columns
argv values in the row
columnNames names and types of the columns
Return values:
0 on success.
1 if an error occurred.
See also:
rt_multi_cfg_entry_args

realtime_multi_handler()

Definition at line 1120 of file res_config_sqlite.c.

References ast_category_append(), ast_category_new(), ast_log(), ast_variable_append(), ast_variable_new(), rt_multi_cfg_entry_args::cfg, rt_multi_cfg_entry_args::initfield, LOG_ERROR, LOG_WARNING, and var.

Referenced by realtime_multi_handler().

01121 {
01122    struct rt_multi_cfg_entry_args *args;
01123    struct ast_category *cat;
01124    struct ast_variable *var;
01125    char *cat_name;
01126    size_t i;
01127 
01128    args = arg;
01129    cat_name = NULL;
01130 
01131    /*
01132     * cat_name should always be set here, since initfield is forged from
01133     * params[0] in realtime_multi_handler(), which is a search parameter
01134     * of the SQL query.
01135     */
01136    for (i = 0; i < argc; i++) {
01137       if (!strcmp(args->initfield, columnNames[i]))
01138          cat_name = argv[i];
01139    }
01140 
01141    if (!cat_name) {
01142       ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
01143       return 1;
01144    }
01145 
01146    if (!(cat = ast_category_new(cat_name, "", 99999))) {
01147       ast_log(LOG_WARNING, "Unable to allocate category\n");
01148       return 1;
01149    }
01150 
01151    ast_category_append(args->cfg, cat);
01152 
01153    for (i = 0; i < argc; i++) {
01154       if (!argv[i] || !strcmp(args->initfield, columnNames[i]))
01155          continue;
01156 
01157       if (!(var = ast_variable_new(columnNames[i], argv[i], ""))) {
01158          ast_log(LOG_WARNING, "Unable to allocate variable\n");
01159          return 1;
01160       }
01161 
01162       ast_variable_append(cat, var);
01163    }
01164 
01165    return 0;
01166 }

static int cdr_handler ( struct ast_cdr *  cdr  )  [static]

Asterisk callback function for CDR support.

Parameters:
cdr the CDR entry Asterisk sends us.
Asterisk will call this function each time a CDR entry must be logged if CDR support is enabled.

Return values:
0 on success
1 if an error occurred

Definition at line 787 of file res_config_sqlite.c.

References ast_cdr_getvar(), ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_RWLIST_TRAVERSE, ast_str_append(), ast_str_buffer(), ast_str_create(), ast_str_set(), sqlite_cache_tables::columns, find_table(), first, sqlite_cache_columns::isint, LOG_ERROR, LOG_WARNING, mutex, sqlite_cache_columns::name, release_table, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

Referenced by load_module().

00788 {
00789    char *errormsg = NULL, *tmp, workspace[500];
00790    int error, scannum;
00791    struct sqlite_cache_tables *tbl = find_table(cdr_table);
00792    struct sqlite_cache_columns *col;
00793    struct ast_str *sql1 = ast_str_create(160), *sql2 = ast_str_create(16);
00794    int first = 1;
00795 
00796    if (!tbl) {
00797       ast_log(LOG_WARNING, "No such table: %s\n", cdr_table);
00798       return -1;
00799    }
00800 
00801    ast_str_set(&sql1, 0, "INSERT INTO %s (", cdr_table);
00802    ast_str_set(&sql2, 0, ") VALUES (");
00803 
00804    AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
00805       if (col->isint) {
00806          ast_cdr_getvar(cdr, col->name, &tmp, workspace, sizeof(workspace), 0, 1);
00807          if (!tmp) {
00808             continue;
00809          }
00810          if (sscanf(tmp, "%30d", &scannum) == 1) {
00811             ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
00812             ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", scannum);
00813          }
00814       } else {
00815          ast_cdr_getvar(cdr, col->name, &tmp, workspace, sizeof(workspace), 0, 0);
00816          if (!tmp) {
00817             continue;
00818          }
00819          ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
00820          tmp = sqlite_mprintf("%Q", tmp);
00821          ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", tmp);
00822          sqlite_freemem(tmp);
00823       }
00824       first = 0;
00825    }
00826    release_table(tbl);
00827 
00828    ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
00829    ast_free(sql2);
00830 
00831    ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql1));
00832 
00833    ast_mutex_lock(&mutex);
00834 
00835    RES_CONFIG_SQLITE_BEGIN
00836       error = sqlite_exec(db, ast_str_buffer(sql1), NULL, NULL, &errormsg);
00837    RES_CONFIG_SQLITE_END(error)
00838 
00839    ast_mutex_unlock(&mutex);
00840 
00841    ast_free(sql1);
00842 
00843    if (error) {
00844       ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
00845       sqlite_freemem(errormsg);
00846       return 1;
00847    }
00848    sqlite_freemem(errormsg);
00849 
00850    return 0;
00851 }

static int check_vars ( void   )  [static]

Definition at line 723 of file res_config_sqlite.c.

References ast_log(), and LOG_ERROR.

Referenced by load_config().

00724 {
00725    if (!dbfile) {
00726       ast_log(LOG_ERROR, "Required parameter undefined: dbfile\n");
00727       return 1;
00728    }
00729 
00730    use_cdr = (cdr_table != NULL);
00731 
00732    return 0;
00733 }

static struct ast_config * config_handler ( const char *  database,
const char *  table,
const char *  file,
struct ast_config *  cfg,
struct ast_flags  flags,
const char *  suggested_incl,
const char *  who_asked 
) [static, read]

Asterisk callback function for static configuration.

Asterisk will call this function when it loads its static configuration, which usually happens at startup and reload.

Parameters:
database the database to use (ignored)
table the table to use
file the file to load from the database
cfg the struct ast_config object to use when storing variables
flags Optional flags. Not used.
suggested_incl suggest include.
Return values:
cfg object
NULL if an error occurred
See also:
add_cfg_entry()

Definition at line 912 of file res_config_sqlite.c.

References add_cfg_entry(), ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), cfg_entry_args::cat, cfg_entry_args::cat_name, cfg_entry_args::cfg, cfg_entry_args::flags, LOG_ERROR, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, sql_get_config_table, and cfg_entry_args::who_asked.

00914 {
00915    struct cfg_entry_args args;
00916    char *query, *errormsg = NULL;
00917    int error;
00918 
00919    if (!config_table) {
00920       if (!table) {
00921          ast_log(LOG_ERROR, "Table name unspecified\n");
00922          return NULL;
00923       }
00924    } else
00925       table = config_table;
00926 
00927    query = sqlite_mprintf(sql_get_config_table, table, file);
00928 
00929    if (!query) {
00930       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
00931       return NULL;
00932    }
00933 
00934    ast_debug(1, "SQL query: %s\n", query);
00935    args.cfg = cfg;
00936    args.cat = NULL;
00937    args.cat_name = NULL;
00938    args.flags = flags;
00939    args.who_asked = who_asked;
00940 
00941    ast_mutex_lock(&mutex);
00942 
00943    RES_CONFIG_SQLITE_BEGIN
00944       error = sqlite_exec(db, query, add_cfg_entry, &args, &errormsg);
00945    RES_CONFIG_SQLITE_END(error)
00946 
00947    ast_mutex_unlock(&mutex);
00948 
00949    ast_free(args.cat_name);
00950    sqlite_freemem(query);
00951 
00952    if (error) {
00953       ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
00954       sqlite_freemem(errormsg);
00955       return NULL;
00956    }
00957    sqlite_freemem(errormsg);
00958 
00959    return cfg;
00960 }

static struct sqlite_cache_tables* find_table ( const char *  tablename  )  [static, read]

Definition at line 641 of file res_config_sqlite.c.

References asprintf, ast_calloc, ast_debug, ast_free, AST_LIST_EMPTY, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_RWLIST_HEAD_INIT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, sqlite_cache_tables::columns, errno, find_table_cb(), free_table(), LOG_ERROR, LOG_WARNING, mutex, sqlite_cache_tables::name, and sql_table_structure.

Referenced by cdr_handler(), and realtime_require_handler().

00642 {
00643    struct sqlite_cache_tables *tblptr;
00644    int i, err;
00645    char *sql, *errstr = NULL;
00646 
00647    AST_RWLIST_RDLOCK(&sqlite_tables);
00648 
00649    for (i = 0; i < 2; i++) {
00650       AST_RWLIST_TRAVERSE(&sqlite_tables, tblptr, list) {
00651          if (strcmp(tblptr->name, tablename) == 0) {
00652             break;
00653          }
00654       }
00655       if (tblptr) {
00656          AST_RWLIST_RDLOCK(&(tblptr->columns));
00657          AST_RWLIST_UNLOCK(&sqlite_tables);
00658          return tblptr;
00659       }
00660 
00661       if (i == 0) {
00662          AST_RWLIST_UNLOCK(&sqlite_tables);
00663          AST_RWLIST_WRLOCK(&sqlite_tables);
00664       }
00665    }
00666 
00667    /* Table structure not cached; build the structure now */
00668    if (asprintf(&sql, sql_table_structure, tablename) < 0) {
00669       ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00670       sql = NULL;
00671    }
00672    if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
00673       AST_RWLIST_UNLOCK(&sqlite_tables);
00674       ast_log(LOG_ERROR, "Memory error.  Cannot cache table '%s'\n", tablename);
00675       return NULL;
00676    }
00677    tblptr->name = (char *)tblptr + sizeof(*tblptr);
00678    strcpy(tblptr->name, tablename); /* SAFE */
00679    AST_RWLIST_HEAD_INIT(&(tblptr->columns));
00680 
00681    ast_debug(1, "About to query table structure: %s\n", sql);
00682 
00683    ast_mutex_lock(&mutex);
00684    if ((err = sqlite_exec(db, sql, find_table_cb, tblptr, &errstr))) {
00685       ast_mutex_unlock(&mutex);
00686       ast_log(LOG_WARNING, "SQLite error %d: %s\n", err, errstr);
00687       ast_free(errstr);
00688       free_table(tblptr);
00689       AST_RWLIST_UNLOCK(&sqlite_tables);
00690       return NULL;
00691    }
00692    ast_mutex_unlock(&mutex);
00693 
00694    if (AST_LIST_EMPTY(&(tblptr->columns))) {
00695       free_table(tblptr);
00696       AST_RWLIST_UNLOCK(&sqlite_tables);
00697       return NULL;
00698    }
00699 
00700    AST_RWLIST_INSERT_TAIL(&sqlite_tables, tblptr, list);
00701    AST_RWLIST_RDLOCK(&(tblptr->columns));
00702    AST_RWLIST_UNLOCK(&sqlite_tables);
00703    return tblptr;
00704 }

static int find_table_cb ( void *  vtblptr,
int  argc,
char **  argv,
char **  columnNames 
) [static]

Definition at line 586 of file res_config_sqlite.c.

References AST_APP_ARG, ast_calloc, ast_debug, AST_DECLARE_APP_ARGS, AST_LIST_INSERT_TAIL, AST_RWLIST_TRAVERSE, ast_skip_blanks(), AST_STANDARD_APP_ARGS, ast_strdupa, sqlite_cache_tables::columns, sqlite_cache_columns::isint, sqlite_cache_columns::name, strcasestr(), and sqlite_cache_columns::type.

Referenced by find_table().

00587 {
00588    struct sqlite_cache_tables *tblptr = vtblptr;
00589    char *sql = ast_strdupa(argv[0]), *start, *end, *type, *remainder;
00590    int i;
00591    AST_DECLARE_APP_ARGS(fie,
00592       AST_APP_ARG(ld)[100]; /* This means we support up to 100 columns per table */
00593    );
00594    struct sqlite_cache_columns *col;
00595 
00596    /* This is really fun.  We get to parse an SQL statement to figure out
00597     * what columns are in the table.
00598     */
00599    if ((start = strchr(sql, '(')) && (end = strrchr(sql, ')'))) {
00600       start++;
00601       *end = '\0';
00602    } else {
00603       /* Abort */
00604       return -1;
00605    }
00606 
00607    AST_STANDARD_APP_ARGS(fie, start);
00608    for (i = 0; i < fie.argc; i++) {
00609       fie.ld[i] = ast_skip_blanks(fie.ld[i]);
00610       ast_debug(5, "Found field: %s\n", fie.ld[i]);
00611       if (strncasecmp(fie.ld[i], "PRIMARY KEY", 11) == 0 && (start = strchr(fie.ld[i], '(')) && (end = strchr(fie.ld[i], ')'))) {
00612          *end = '\0';
00613          AST_RWLIST_TRAVERSE(&(tblptr->columns), col, list) {
00614             if (strcasecmp(start + 1, col->name) == 0 && strcasestr(col->type, "INTEGER")) {
00615                col->isint = 1;
00616             }
00617          }
00618          continue;
00619       }
00620       /* type delimiter could be any space character */
00621       for (type = fie.ld[i]; *type > 32; type++);
00622       *type++ = '\0';
00623       type = ast_skip_blanks(type);
00624       for (remainder = type; *remainder > 32; remainder++);
00625       *remainder = '\0';
00626       if (!(col = ast_calloc(1, sizeof(*col) + strlen(fie.ld[i]) + strlen(type) + 2))) {
00627          return -1;
00628       }
00629       col->name = (char *)col + sizeof(*col);
00630       col->type = (char *)col + sizeof(*col) + strlen(fie.ld[i]) + 1;
00631       strcpy(col->name, fie.ld[i]); /* SAFE */
00632       strcpy(col->type, type); /* SAFE */
00633       if (strcasestr(col->type, "INTEGER") && strcasestr(col->type, "PRIMARY KEY")) {
00634          col->isint = 1;
00635       }
00636       AST_LIST_INSERT_TAIL(&(tblptr->columns), col, list);
00637    }
00638    return 0;
00639 }

static void free_table ( struct sqlite_cache_tables *  tblptr  )  [static]

Definition at line 572 of file res_config_sqlite.c.

References ast_free, AST_RWLIST_HEAD_DESTROY, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and sqlite_cache_tables::columns.

Referenced by find_table(), realtime_unload_handler(), and unload_config().

00573 {
00574    struct sqlite_cache_columns *col;
00575 
00576    /* Obtain a write lock to ensure there are no read locks outstanding */
00577    AST_RWLIST_WRLOCK(&(tblptr->columns));
00578    while ((col = AST_RWLIST_REMOVE_HEAD(&(tblptr->columns), list))) {
00579       ast_free(col);
00580    }
00581    AST_RWLIST_UNLOCK(&(tblptr->columns));
00582    AST_RWLIST_HEAD_DESTROY(&(tblptr->columns));
00583    ast_free(tblptr);
00584 }

static size_t get_params ( va_list  ap,
const char ***  params_ptr,
const char ***  vals_ptr,
int  warn 
) [static]

Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values.

ap must have the following format : param1 val1 param2 val2 param3 val3 ... arguments will be extracted to create 2 arrays:

  • params : param1 param2 param3 ...
  • vals : val1 val2 val3 ...

The address of these arrays are stored in params_ptr and vals_ptr. It is the responsibility of the caller to release the memory of these arrays. It is considered an error that va_list has a null or odd number of strings.

Parameters:
ap the va_list object to parse
params_ptr where the address of the params array is stored
vals_ptr where the address of the vals array is stored
Return values:
the number of elements in the arrays (which have the same size).
0 if an error occurred.

Definition at line 962 of file res_config_sqlite.c.

References ast_free, ast_log(), ast_realloc, and LOG_WARNING.

Referenced by realtime_destroy_handler(), realtime_handler(), realtime_multi_handler(), realtime_store_handler(), and realtime_update_handler().

00963 {
00964    const char **tmp, *param, *val, **params, **vals;
00965    size_t params_count;
00966 
00967    params = NULL;
00968    vals = NULL;
00969    params_count = 0;
00970 
00971    while ((param = va_arg(ap, const char *)) && (val = va_arg(ap, const char *))) {
00972       if (!(tmp = ast_realloc(params, (params_count + 1) * sizeof(char *)))) {
00973          ast_free(params);
00974          ast_free(vals);
00975          return 0;
00976       }
00977       params = tmp;
00978 
00979       if (!(tmp = ast_realloc(vals, (params_count + 1) * sizeof(char *)))) {
00980          ast_free(params);
00981          ast_free(vals);
00982          return 0;
00983       }
00984       vals = tmp;
00985 
00986       params[params_count] = param;
00987       vals[params_count] = val;
00988       params_count++;
00989    }
00990 
00991    if (params_count > 0) {
00992       *params_ptr = params;
00993       *vals_ptr = vals;
00994    } else if (warn) {
00995       ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
00996    }
00997 
00998    return params_count;
00999 }

static char * handle_cli_show_sqlite_status ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Asterisk callback function for the CLI status command.

Parameters:
e CLI command
cmd 
a CLI argument list
Returns:
RESULT_SUCCESS

Definition at line 1665 of file res_config_sqlite.c.

References ast_cli_args::argc, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

01666 {
01667    switch (cmd) {
01668    case CLI_INIT:
01669       e->command = "sqlite show status";
01670       e->usage =
01671          "Usage: sqlite show status\n"
01672          "       Show status information about the SQLite 2 driver\n";
01673       return NULL;
01674    case CLI_GENERATE:
01675       return NULL;
01676    }
01677 
01678    if (a->argc != 3)
01679       return CLI_SHOWUSAGE;
01680 
01681    ast_cli(a->fd, "SQLite database path: %s\n", dbfile);
01682    ast_cli(a->fd, "config_table: ");
01683 
01684    if (!config_table)
01685       ast_cli(a->fd, "unspecified, must be present in extconfig.conf\n");
01686    else
01687       ast_cli(a->fd, "%s\n", config_table);
01688 
01689    ast_cli(a->fd, "cdr_table: ");
01690 
01691    if (!cdr_table)
01692       ast_cli(a->fd, "unspecified, CDR support disabled\n");
01693    else
01694       ast_cli(a->fd, "%s\n", cdr_table);
01695 
01696    return CLI_SUCCESS;
01697 }

static char * handle_cli_sqlite_show_tables ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1699 of file res_config_sqlite.c.

References ast_cli_args::argc, ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, sqlite_cache_tables::columns, ast_cli_entry::command, ast_cli_args::fd, sqlite_cache_columns::name, sqlite_cache_tables::name, sqlite_cache_columns::type, and ast_cli_entry::usage.

01700 {
01701    struct sqlite_cache_tables *tbl;
01702    struct sqlite_cache_columns *col;
01703    int found = 0;
01704 
01705    switch (cmd) {
01706    case CLI_INIT:
01707       e->command = "sqlite show tables";
01708       e->usage =
01709          "Usage: sqlite show tables\n"
01710          "       Show table information about the SQLite 2 driver\n";
01711       return NULL;
01712    case CLI_GENERATE:
01713       return NULL;
01714    }
01715 
01716    if (a->argc != 3)
01717       return CLI_SHOWUSAGE;
01718 
01719    AST_RWLIST_RDLOCK(&sqlite_tables);
01720    AST_RWLIST_TRAVERSE(&sqlite_tables, tbl, list) {
01721       found++;
01722       ast_cli(a->fd, "Table %s:\n", tbl->name);
01723       AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
01724          fprintf(stderr, "%s\n", col->name);
01725          ast_cli(a->fd, "  %20.20s  %-30.30s\n", col->name, col->type);
01726       }
01727    }
01728    AST_RWLIST_UNLOCK(&sqlite_tables);
01729 
01730    if (!found) {
01731       ast_cli(a->fd, "No tables currently in cache\n");
01732    }
01733 
01734    return CLI_SUCCESS;
01735 }

static int load_config ( void   )  [static]

Load the configuration file.

See also:
unload_config()
This function sets dbfile, config_table, and cdr_table. It calls check_vars() before returning, and unload_config() if an error occurred.

Return values:
0 on success
1 if an error occurred

Definition at line 735 of file res_config_sqlite.c.

References ast_config_destroy(), ast_config_load, ast_log(), ast_variable_browse(), check_vars(), config, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, LOG_ERROR, LOG_WARNING, ast_variable::name, ast_variable::next, RES_CONFIG_SQLITE_CONF_FILE, SET_VAR, unload_config(), and var.

Referenced by load_module().

00736 {
00737    struct ast_config *config;
00738    struct ast_variable *var;
00739    int error;
00740    struct ast_flags config_flags = { 0 };
00741 
00742    config = ast_config_load(RES_CONFIG_SQLITE_CONF_FILE, config_flags);
00743 
00744    if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
00745       ast_log(LOG_ERROR, "Unable to load " RES_CONFIG_SQLITE_CONF_FILE "\n");
00746       return 1;
00747    }
00748 
00749    for (var = ast_variable_browse(config, "general"); var; var = var->next) {
00750       if (!strcasecmp(var->name, "dbfile"))
00751          SET_VAR(config, dbfile, var);
00752       else if (!strcasecmp(var->name, "config_table"))
00753          SET_VAR(config, config_table, var);
00754       else if (!strcasecmp(var->name, "cdr_table")) {
00755          SET_VAR(config, cdr_table, var);
00756       } else
00757          ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
00758    }
00759 
00760    ast_config_destroy(config);
00761    error = check_vars();
00762 
00763    if (error) {
00764       unload_config();
00765       return 1;
00766    }
00767 
00768    return 0;
00769 }

static int load_module ( void   )  [static]

Definition at line 1755 of file res_config_sqlite.c.

References ARRAY_LEN, ast_cdr_register(), ast_cli_register_multiple(), ast_config_engine_register(), ast_debug, ast_log(), AST_MODULE_LOAD_DECLINE, cdr_handler(), load_config(), LOG_ERROR, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_DESCRIPTION, RES_CONFIG_SQLITE_END, RES_CONFIG_SQLITE_NAME, S_OR, sql_create_cdr_table, and unload_module.

01756 {
01757    char *errormsg = NULL;
01758    int error;
01759 
01760    db = NULL;
01761    cdr_registered = 0;
01762    cli_status_registered = 0;
01763    dbfile = NULL;
01764    config_table = NULL;
01765    cdr_table = NULL;
01766    error = load_config();
01767 
01768    if (error)
01769       return AST_MODULE_LOAD_DECLINE;
01770 
01771    if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
01772       ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01773       sqlite_freemem(errormsg);
01774       unload_module();
01775       return 1;
01776    }
01777 
01778    sqlite_freemem(errormsg);
01779    errormsg = NULL;
01780    ast_config_engine_register(&sqlite_engine);
01781 
01782    if (use_cdr) {
01783       char *query;
01784 
01785 /* \cond DOXYGEN_CAN_PARSE_THIS */
01786 #undef QUERY
01787 #define QUERY "SELECT COUNT(id) FROM %Q;"
01788 /* \endcond */
01789 
01790       query = sqlite_mprintf(QUERY, cdr_table);
01791 
01792       if (!query) {
01793          ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
01794          unload_module();
01795          return 1;
01796       }
01797 
01798       ast_debug(1, "SQL query: %s\n", query);
01799 
01800       RES_CONFIG_SQLITE_BEGIN
01801          error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01802       RES_CONFIG_SQLITE_END(error)
01803 
01804       sqlite_freemem(query);
01805 
01806       if (error) {
01807          /*
01808           * Unexpected error.
01809           */
01810          if (error != SQLITE_ERROR) {
01811             ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01812             sqlite_freemem(errormsg);
01813             unload_module();
01814             return 1;
01815          }
01816 
01817          sqlite_freemem(errormsg);
01818          errormsg = NULL;
01819          query = sqlite_mprintf(sql_create_cdr_table, cdr_table);
01820 
01821          if (!query) {
01822             ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
01823             unload_module();
01824             return 1;
01825          }
01826 
01827          ast_debug(1, "SQL query: %s\n", query);
01828 
01829          RES_CONFIG_SQLITE_BEGIN
01830             error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01831          RES_CONFIG_SQLITE_END(error)
01832 
01833          sqlite_freemem(query);
01834 
01835          if (error) {
01836             ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01837             sqlite_freemem(errormsg);
01838             unload_module();
01839             return 1;
01840          }
01841       }
01842       sqlite_freemem(errormsg);
01843       errormsg = NULL;
01844 
01845       error = ast_cdr_register(RES_CONFIG_SQLITE_NAME, RES_CONFIG_SQLITE_DESCRIPTION, cdr_handler);
01846 
01847       if (error) {
01848          unload_module();
01849          return 1;
01850       }
01851 
01852       cdr_registered = 1;
01853    }
01854 
01855    error = ast_cli_register_multiple(cli_status, ARRAY_LEN(cli_status));
01856 
01857    if (error) {
01858       unload_module();
01859       return 1;
01860    }
01861 
01862    cli_status_registered = 1;
01863 
01864    return 0;
01865 }

static int realtime_destroy_handler ( const char *  database,
const char *  table,
const char *  keyfield,
const char *  entity,
va_list  ap 
) [static]

Asterisk callback function for RealTime configuration (destroys variable).

Asterisk will call this function each time a variable has been destroyed internally and must be removed from the backend engine. keyfield and entity are used to find the row to delete, e.g. DELETE FROM table WHERE keyfield = 'entity';. ap is a list of parameters and values with the same format as the other realtime functions.

Parameters:
database the database to use (ignored)
table the table to use
keyfield the column of the matching cell
entity the value of the matching cell
ap list of additional parameters for cell matching
Return values:
the number of affected rows.
-1 if an error occurred.

Definition at line 1537 of file res_config_sqlite.c.

References ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), get_params(), LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

01539 {
01540    char *query, *errormsg = NULL, *tmp_str;
01541    const char **params = NULL, **vals = NULL;
01542    size_t params_count;
01543    int error, rows_num;
01544    size_t i;
01545 
01546    if (!table) {
01547       ast_log(LOG_WARNING, "Table name unspecified\n");
01548       return -1;
01549    }
01550 
01551    params_count = get_params(ap, &params, &vals, 0);
01552 
01553 /* \cond DOXYGEN_CAN_PARSE_THIS */
01554 #undef QUERY
01555 #define QUERY "DELETE FROM '%q' WHERE"
01556 /* \endcond */
01557 
01558    if (!(query = sqlite_mprintf(QUERY, table))) {
01559       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
01560       ast_free(params);
01561       ast_free(vals);
01562       return -1;
01563    }
01564 
01565    for (i = 0; i < params_count; i++) {
01566       tmp_str = sqlite_mprintf("%s %q = '%q' AND", query, params[i], vals[i]);
01567       sqlite_freemem(query);
01568 
01569       if (!tmp_str) {
01570          ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01571          ast_free(params);
01572          ast_free(vals);
01573          return -1;
01574       }
01575 
01576       query = tmp_str;
01577    }
01578 
01579    ast_free(params);
01580    ast_free(vals);
01581    if (!(tmp_str = sqlite_mprintf("%s %q = '%q';", query, keyfield, entity))) {
01582       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01583       sqlite_freemem(query);
01584       return -1;
01585    }
01586    sqlite_freemem(query);
01587    query = tmp_str;
01588    ast_debug(1, "SQL query: %s\n", query);
01589 
01590    ast_mutex_lock(&mutex);
01591 
01592    RES_CONFIG_SQLITE_BEGIN
01593       error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01594    RES_CONFIG_SQLITE_END(error)
01595 
01596    if (!error) {
01597       rows_num = sqlite_changes(db);
01598    } else {
01599       rows_num = -1;
01600    }
01601 
01602    ast_mutex_unlock(&mutex);
01603 
01604    sqlite_freemem(query);
01605 
01606    if (error) {
01607       ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01608    }
01609    sqlite_freemem(errormsg);
01610 
01611    return rows_num;
01612 }

static struct ast_variable * realtime_handler ( const char *  database,
const char *  table,
va_list  ap 
) [static, read]

Asterisk callback function for RealTime configuration.

Asterisk will call this function each time it requires a variable through the RealTime architecture. ap is a list of parameters and values used to find a specific row, e.g one parameter "name" and one value "123" so that the SQL query becomes SELECT * FROM table WHERE name = '123';.

Parameters:
database the database to use (ignored)
table the table to use
ap list of parameters and values to match
Return values:
a linked list of struct ast_variable objects
NULL if an error occurred
See also:
add_rt_cfg_entry()

Definition at line 1030 of file res_config_sqlite.c.

References add_rt_cfg_entry(), ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_variables_destroy(), get_params(), rt_cfg_entry_args::last, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and rt_cfg_entry_args::var.

01031 {
01032    char *query, *errormsg = NULL, *op, *tmp_str;
01033    struct rt_cfg_entry_args args;
01034    const char **params, **vals;
01035    size_t params_count;
01036    int error;
01037 
01038    if (!table) {
01039       ast_log(LOG_WARNING, "Table name unspecified\n");
01040       return NULL;
01041    }
01042 
01043    params_count = get_params(ap, &params, &vals, 1);
01044 
01045    if (params_count == 0)
01046       return NULL;
01047 
01048    op = (strchr(params[0], ' ') == NULL) ? " =" : "";
01049 
01050 /* \cond DOXYGEN_CAN_PARSE_THIS */
01051 #undef QUERY
01052 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
01053 /* \endcond */
01054 
01055    query = sqlite_mprintf(QUERY, table, !strcmp(config_table, table) ? " commented = 0 AND" : "", params[0], op, vals[0]);
01056 
01057    if (!query) {
01058       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
01059       ast_free(params);
01060       ast_free(vals);
01061       return NULL;
01062    }
01063 
01064    if (params_count > 1) {
01065       size_t i;
01066 
01067       for (i = 1; i < params_count; i++) {
01068          op = (strchr(params[i], ' ') == NULL) ? " =" : "";
01069          tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
01070          sqlite_freemem(query);
01071 
01072          if (!tmp_str) {
01073             ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01074             ast_free(params);
01075             ast_free(vals);
01076             return NULL;
01077          }
01078 
01079          query = tmp_str;
01080       }
01081    }
01082 
01083    ast_free(params);
01084    ast_free(vals);
01085 
01086    tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
01087    sqlite_freemem(query);
01088 
01089    if (!tmp_str) {
01090       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01091       return NULL;
01092    }
01093 
01094    query = tmp_str;
01095    ast_debug(1, "SQL query: %s\n", query);
01096    args.var = NULL;
01097    args.last = NULL;
01098 
01099    ast_mutex_lock(&mutex);
01100 
01101    RES_CONFIG_SQLITE_BEGIN
01102       error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
01103    RES_CONFIG_SQLITE_END(error)
01104 
01105    ast_mutex_unlock(&mutex);
01106 
01107    sqlite_freemem(query);
01108 
01109    if (error) {
01110       ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01111       sqlite_freemem(errormsg);
01112       ast_variables_destroy(args.var);
01113       return NULL;
01114    }
01115    sqlite_freemem(errormsg);
01116 
01117    return args.var;
01118 }

static struct ast_config * realtime_multi_handler ( const char *  database,
const char *  table,
va_list  ap 
) [static, read]

Asterisk callback function for RealTime configuration.

This function performs the same actions as realtime_handler() except that it can store variables per category, and can return several categories.

Parameters:
database the database to use (ignored)
table the table to use
ap list of parameters and values to match
Return values:
a struct ast_config object storing categories and variables.
NULL if an error occurred.
See also:
add_rt_multi_cfg_entry()

Definition at line 1168 of file res_config_sqlite.c.

References add_rt_multi_cfg_entry(), ast_config_destroy(), ast_config_new(), ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_strdup, rt_multi_cfg_entry_args::cfg, get_params(), rt_multi_cfg_entry_args::initfield, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

01170 {
01171    char *query, *errormsg = NULL, *op, *tmp_str, *initfield;
01172    struct rt_multi_cfg_entry_args args;
01173    const char **params, **vals;
01174    struct ast_config *cfg;
01175    size_t params_count;
01176    int error;
01177 
01178    if (!table) {
01179       ast_log(LOG_WARNING, "Table name unspecified\n");
01180       return NULL;
01181    }
01182 
01183    if (!(cfg = ast_config_new())) {
01184       ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
01185       return NULL;
01186    }
01187 
01188    if (!(params_count = get_params(ap, &params, &vals, 1))) {
01189       ast_config_destroy(cfg);
01190       return NULL;
01191    }
01192 
01193    if (!(initfield = ast_strdup(params[0]))) {
01194       ast_config_destroy(cfg);
01195       ast_free(params);
01196       ast_free(vals);
01197       return NULL;
01198    }
01199 
01200    tmp_str = strchr(initfield, ' ');
01201 
01202    if (tmp_str)
01203       *tmp_str = '\0';
01204 
01205    op = (!strchr(params[0], ' ')) ? " =" : "";
01206 
01207    /*
01208     * Asterisk sends us an already escaped string when searching for
01209     * "exten LIKE" (uh!). Handle it separately.
01210     */
01211    tmp_str = (!strcmp(vals[0], "\\_%")) ? "_%" : (char *)vals[0];
01212 
01213 /* \cond DOXYGEN_CAN_PARSE_THIS */
01214 #undef QUERY
01215 #define QUERY "SELECT * FROM '%q' WHERE commented = 0 AND %q%s '%q'"
01216 /* \endcond */
01217 
01218    if (!(query = sqlite_mprintf(QUERY, table, params[0], op, tmp_str))) {
01219       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
01220       ast_config_destroy(cfg);
01221       ast_free(params);
01222       ast_free(vals);
01223       ast_free(initfield);
01224       return NULL;
01225    }
01226 
01227    if (params_count > 1) {
01228       size_t i;
01229 
01230       for (i = 1; i < params_count; i++) {
01231          op = (!strchr(params[i], ' ')) ? " =" : "";
01232          tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
01233          sqlite_freemem(query);
01234 
01235          if (!tmp_str) {
01236             ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01237             ast_config_destroy(cfg);
01238             ast_free(params);
01239             ast_free(vals);
01240             ast_free(initfield);
01241             return NULL;
01242          }
01243 
01244          query = tmp_str;
01245       }
01246    }
01247 
01248    ast_free(params);
01249    ast_free(vals);
01250 
01251    if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
01252       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01253       sqlite_freemem(query);
01254       ast_config_destroy(cfg);
01255       ast_free(initfield);
01256       return NULL;
01257    }
01258 
01259    sqlite_freemem(query);
01260    query = tmp_str;
01261    ast_debug(1, "SQL query: %s\n", query);
01262    args.cfg = cfg;
01263    args.initfield = initfield;
01264 
01265    ast_mutex_lock(&mutex);
01266 
01267    RES_CONFIG_SQLITE_BEGIN
01268       error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
01269    RES_CONFIG_SQLITE_END(error)
01270 
01271    ast_mutex_unlock(&mutex);
01272 
01273    sqlite_freemem(query);
01274    ast_free(initfield);
01275 
01276    if (error) {
01277       ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01278       sqlite_freemem(errormsg);
01279       ast_config_destroy(cfg);
01280       return NULL;
01281    }
01282    sqlite_freemem(errormsg);
01283 
01284    return cfg;
01285 }

static int realtime_require_handler ( const char *  database,
const char *  table,
va_list  ap 
) [static]

Definition at line 1614 of file res_config_sqlite.c.

References ast_log(), ast_rq_is_int(), AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, sqlite_cache_tables::columns, find_table(), sqlite_cache_columns::isint, LOG_WARNING, sqlite_cache_columns::name, and sqlite_cache_columns::type.

01615 {
01616    struct sqlite_cache_tables *tbl = find_table(tablename);
01617    struct sqlite_cache_columns *col;
01618    char *elm;
01619    int type, size, res = 0;
01620 
01621    if (!tbl) {
01622       return -1;
01623    }
01624 
01625    while ((elm = va_arg(ap, char *))) {
01626       type = va_arg(ap, require_type);
01627       size = va_arg(ap, int);
01628       /* Check if the field matches the criteria */
01629       AST_RWLIST_TRAVERSE(&tbl->columns, col, list) {
01630          if (strcmp(col->name, elm) == 0) {
01631             /* SQLite only has two types - the 32-bit integer field that
01632              * is the key column, and everything else (everything else
01633              * being a string).
01634              */
01635             if (col->isint && !ast_rq_is_int(type)) {
01636                ast_log(LOG_WARNING, "Realtime table %s: column '%s' is an integer field, but Asterisk requires that it not be!\n", tablename, col->name);
01637                res = -1;
01638             }
01639             break;
01640          }
01641       }
01642       if (!col) {
01643          ast_log(LOG_WARNING, "Realtime table %s requires column '%s', but that column does not exist!\n", tablename, elm);
01644       }
01645    }
01646    AST_RWLIST_UNLOCK(&(tbl->columns));
01647    return res;
01648 }

static int realtime_store_handler ( const char *  database,
const char *  table,
va_list  ap 
) [static]

Asterisk callback function for RealTime configuration (variable create/store).

Asterisk will call this function each time a variable has been created internally and must be stored in the backend engine. are used to find the row to update, e.g. ap is a list of parameters and values with the same format as the other realtime functions.

Parameters:
database the database to use (ignored)
table the table to use
ap list of parameters and new values to insert into the database
Return values:
the rowid of inserted row.
-1 if an error occurred.

Definition at line 1443 of file res_config_sqlite.c.

References ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), get_params(), LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

01444 {
01445    char *errormsg = NULL, *tmp_str, *tmp_keys = NULL, *tmp_keys2 = NULL, *tmp_vals = NULL, *tmp_vals2 = NULL;
01446    const char **params, **vals;
01447    size_t params_count;
01448    int error, rows_id;
01449    size_t i;
01450 
01451    if (!table) {
01452       ast_log(LOG_WARNING, "Table name unspecified\n");
01453       return -1;
01454    }
01455 
01456    if (!(params_count = get_params(ap, &params, &vals, 1)))
01457       return -1;
01458 
01459 /* \cond DOXYGEN_CAN_PARSE_THIS */
01460 #undef QUERY
01461 #define QUERY "INSERT into '%q' (%s) VALUES (%s);"
01462 /* \endcond */
01463 
01464    for (i = 0; i < params_count; i++) {
01465       if ( tmp_keys2 ) {
01466          tmp_keys = sqlite_mprintf("%s, %q", tmp_keys2, params[i]);
01467          sqlite_freemem(tmp_keys2);
01468       } else {
01469          tmp_keys = sqlite_mprintf("%q", params[i]);
01470       }
01471       if (!tmp_keys) {
01472          ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01473          sqlite_freemem(tmp_vals);
01474          ast_free(params);
01475          ast_free(vals);
01476          return -1;
01477       }
01478 
01479       if ( tmp_vals2 ) {
01480          tmp_vals = sqlite_mprintf("%s, '%q'", tmp_vals2, vals[i]);
01481          sqlite_freemem(tmp_vals2);
01482       } else {
01483          tmp_vals = sqlite_mprintf("'%q'", vals[i]);
01484       }
01485       if (!tmp_vals) {
01486          ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01487          sqlite_freemem(tmp_keys);
01488          ast_free(params);
01489          ast_free(vals);
01490          return -1;
01491       }
01492 
01493 
01494       tmp_keys2 = tmp_keys;
01495       tmp_vals2 = tmp_vals;
01496    }
01497 
01498    ast_free(params);
01499    ast_free(vals);
01500 
01501    if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
01502       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01503       sqlite_freemem(tmp_keys);
01504       sqlite_freemem(tmp_vals);
01505       return -1;
01506    }
01507 
01508    sqlite_freemem(tmp_keys);
01509    sqlite_freemem(tmp_vals);
01510 
01511    ast_debug(1, "SQL query: %s\n", tmp_str);
01512 
01513    ast_mutex_lock(&mutex);
01514 
01515    RES_CONFIG_SQLITE_BEGIN
01516       error = sqlite_exec(db, tmp_str, NULL, NULL, &errormsg);
01517    RES_CONFIG_SQLITE_END(error)
01518 
01519    if (!error) {
01520       rows_id = sqlite_last_insert_rowid(db);
01521    } else {
01522       rows_id = -1;
01523    }
01524 
01525    ast_mutex_unlock(&mutex);
01526 
01527    sqlite_freemem(tmp_str);
01528 
01529    if (error) {
01530       ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01531    }
01532    sqlite_freemem(errormsg);
01533 
01534    return rows_id;
01535 }

static int realtime_unload_handler ( const char *  unused,
const char *  tablename 
) [static]

static int realtime_update2_handler ( const char *  database,
const char *  table,
va_list  ap 
) [static]

Definition at line 1369 of file res_config_sqlite.c.

References ast_debug, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_thread_get(), first, LOG_ERROR, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, sql_buf, and where_buf.

01371 {
01372    char *errormsg = NULL, *tmp1, *tmp2;
01373    int error, rows_num, first = 1;
01374    struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
01375    struct ast_str *where = ast_str_thread_get(&where_buf, 100);
01376    const char *param, *value;
01377 
01378    if (!table) {
01379       ast_log(LOG_WARNING, "Table name unspecified\n");
01380       return -1;
01381    }
01382 
01383    if (!sql) {
01384       return -1;
01385    }
01386 
01387    ast_str_set(&sql, 0, "UPDATE %s SET", table);
01388    ast_str_set(&where, 0, " WHERE");
01389 
01390    while ((param = va_arg(ap, const char *))) {
01391       value = va_arg(ap, const char *);
01392       ast_str_append(&where, 0, "%s %s = %s",
01393          first ? "" : " AND",
01394          tmp1 = sqlite_mprintf("%q", param),
01395          tmp2 = sqlite_mprintf("%Q", value));
01396       sqlite_freemem(tmp1);
01397       sqlite_freemem(tmp2);
01398       first = 0;
01399    }
01400 
01401    if (first) {
01402       ast_log(LOG_ERROR, "No criteria specified on update to '%s@%s'!\n", table, database);
01403       return -1;
01404    }
01405 
01406    first = 1;
01407    while ((param = va_arg(ap, const char *))) {
01408       value = va_arg(ap, const char *);
01409       ast_str_append(&sql, 0, "%s %s = %s",
01410          first ? "" : ",",
01411          tmp1 = sqlite_mprintf("%q", param),
01412          tmp2 = sqlite_mprintf("%Q", value));
01413       sqlite_freemem(tmp1);
01414       sqlite_freemem(tmp2);
01415       first = 0;
01416    }
01417 
01418    ast_str_append(&sql, 0, " %s", ast_str_buffer(where));
01419    ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql));
01420 
01421    ast_mutex_lock(&mutex);
01422 
01423    RES_CONFIG_SQLITE_BEGIN
01424       error = sqlite_exec(db, ast_str_buffer(sql), NULL, NULL, &errormsg);
01425    RES_CONFIG_SQLITE_END(error)
01426 
01427    if (!error) {
01428       rows_num = sqlite_changes(db);
01429    } else {
01430       rows_num = -1;
01431    }
01432 
01433    ast_mutex_unlock(&mutex);
01434 
01435    if (error) {
01436       ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01437    }
01438    sqlite_freemem(errormsg);
01439 
01440    return rows_num;
01441 }

static int realtime_update_handler ( const char *  database,
const char *  table,
const char *  keyfield,
const char *  entity,
va_list  ap 
) [static]

Asterisk callback function for RealTime configuration (variable update).

Asterisk will call this function each time a variable has been modified internally and must be updated in the backend engine. keyfield and entity are used to find the row to update, e.g. UPDATE table SET ... WHERE keyfield = 'entity';. ap is a list of parameters and values with the same format as the other realtime functions.

Parameters:
database the database to use (ignored)
table the table to use
keyfield the column of the matching cell
entity the value of the matching cell
ap list of parameters and new values to update in the database
Return values:
the number of affected rows.
-1 if an error occurred.

Definition at line 1287 of file res_config_sqlite.c.

References ast_debug, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), get_params(), LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

01289 {
01290    char *query, *errormsg = NULL, *tmp_str;
01291    const char **params, **vals;
01292    size_t params_count;
01293    int error, rows_num;
01294 
01295    if (!table) {
01296       ast_log(LOG_WARNING, "Table name unspecified\n");
01297       return -1;
01298    }
01299 
01300    if (!(params_count = get_params(ap, &params, &vals, 1)))
01301       return -1;
01302 
01303 /* \cond DOXYGEN_CAN_PARSE_THIS */
01304 #undef QUERY
01305 #define QUERY "UPDATE '%q' SET %q = '%q'"
01306 /* \endcond */
01307 
01308    if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
01309       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
01310       ast_free(params);
01311       ast_free(vals);
01312       return -1;
01313    }
01314 
01315    if (params_count > 1) {
01316       size_t i;
01317 
01318       for (i = 1; i < params_count; i++) {
01319          tmp_str = sqlite_mprintf("%s, %q = '%q'", query, params[i], vals[i]);
01320          sqlite_freemem(query);
01321 
01322          if (!tmp_str) {
01323             ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01324             ast_free(params);
01325             ast_free(vals);
01326             return -1;
01327          }
01328 
01329          query = tmp_str;
01330       }
01331    }
01332 
01333    ast_free(params);
01334    ast_free(vals);
01335 
01336    if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
01337       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01338       sqlite_freemem(query);
01339       return -1;
01340    }
01341 
01342    sqlite_freemem(query);
01343    query = tmp_str;
01344    ast_debug(1, "SQL query: %s\n", query);
01345 
01346    ast_mutex_lock(&mutex);
01347 
01348    RES_CONFIG_SQLITE_BEGIN
01349       error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01350    RES_CONFIG_SQLITE_END(error)
01351 
01352    if (!error)
01353       rows_num = sqlite_changes(db);
01354    else
01355       rows_num = -1;
01356 
01357    ast_mutex_unlock(&mutex);
01358 
01359    sqlite_freemem(query);
01360 
01361    if (error) {
01362       ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
01363    }
01364    sqlite_freemem(errormsg);
01365 
01366    return rows_num;
01367 }

static int set_var ( char **  var,
const char *  name,
const char *  value 
) [static]

Allocate a variable.

Parameters:
var the address of the variable to set (it will be allocated)
name the name of the variable (for error handling)
value the value to store in var
Return values:
0 on success
1 if an allocation error occurred

Definition at line 708 of file res_config_sqlite.c.

References ast_free, ast_log(), ast_strdup, and LOG_WARNING.

00709 {
00710    if (*var)
00711       ast_free(*var);
00712 
00713    *var = ast_strdup(value);
00714 
00715    if (!*var) {
00716       ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
00717       return 1;
00718    }
00719 
00720    return 0;
00721 }

static void unload_config ( void   )  [static]

Free resources related to configuration.

See also:
load_config()

Definition at line 771 of file res_config_sqlite.c.

References ast_free, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and free_table().

Referenced by load_config(), and unload_module().

00772 {
00773    struct sqlite_cache_tables *tbl;
00774    ast_free(dbfile);
00775    dbfile = NULL;
00776    ast_free(config_table);
00777    config_table = NULL;
00778    ast_free(cdr_table);
00779    cdr_table = NULL;
00780    AST_RWLIST_WRLOCK(&sqlite_tables);
00781    while ((tbl = AST_RWLIST_REMOVE_HEAD(&sqlite_tables, list))) {
00782       free_table(tbl);
00783    }
00784    AST_RWLIST_UNLOCK(&sqlite_tables);
00785 }

static int unload_module ( void   )  [static]


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Realtime SQLite configuration" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 1870 of file res_config_sqlite.c.

Definition at line 1870 of file res_config_sqlite.c.

int cdr_registered [static]

Set to 1 if the CDR callback function was registered.

Definition at line 465 of file res_config_sqlite.c.

char* cdr_table [static]

The name of the table used to store CDR entries.

Definition at line 477 of file res_config_sqlite.c.

struct ast_cli_entry cli_status[] [static]

Initial value:

 {
   AST_CLI_DEFINE(handle_cli_show_sqlite_status, "Show status information about the SQLite 2 driver"),
   AST_CLI_DEFINE(handle_cli_sqlite_show_tables, "Cached table information about the SQLite 2 driver"),
}
Structure containing details and callback functions for the CLI status command.

Definition at line 506 of file res_config_sqlite.c.

int cli_status_registered [static]

Set to 1 if the CLI status command callback function was registered.

Definition at line 468 of file res_config_sqlite.c.

char* config_table [static]

The name of the static configuration table.

Definition at line 474 of file res_config_sqlite.c.

sqlite* db [static]

The SQLite database object.

Definition at line 459 of file res_config_sqlite.c.

char* dbfile [static]

The path of the database file.

Definition at line 471 of file res_config_sqlite.c.

ast_mutex_t mutex = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ) [static]

struct ast_threadstorage sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , } [static]

Definition at line 126 of file res_config_sqlite.c.

Referenced by realtime_update2_handler().

char* sql_create_cdr_table [static]

SQL query format to create the CDR table if non existent.

Definition at line 531 of file res_config_sqlite.c.

Referenced by load_module().

The structure specifying all callback functions used by Asterisk for static and RealTime configuration.

Definition at line 483 of file res_config_sqlite.c.

int use_cdr [static]

Set to 1 if CDR support is enabled.

Definition at line 462 of file res_config_sqlite.c.

struct ast_threadstorage where_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_where_buf , .custom_init = NULL , } [static]

Definition at line 127 of file res_config_sqlite.c.

Referenced by realtime_update2_handler().


Generated on Wed Aug 11 01:09:57 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9