Wed Oct 28 11:46:17 2009

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/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  cfg_entry_args
struct  rt_cfg_entry_args
struct  rt_multi_cfg_entry_args

Defines

#define MACRO_BEGIN   do {
#define MACRO_END   } while (0)
#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_add_cdr_entry
#define sql_get_config_table

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 __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_configconfig_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 size_t get_params (va_list ap, const char ***params_ptr, const char ***vals_ptr)
 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 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_variablerealtime_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration.
static struct ast_configrealtime_multi_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration.
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_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 const 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_MUTEX_INITIALIZER )
static char * sql_create_cdr_table
static struct ast_config_engine sqlite_engine
static int use_cdr


Detailed Description

res_config_sqlite module.

Definition in file res_config_sqlite.c.


Define Documentation

#define MACRO_BEGIN   do {

Definition at line 91 of file res_config_sqlite.c.

#define MACRO_END   } while (0)

Definition at line 92 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_BEGIN

Value:

MACRO_BEGIN                      \
   int __i;                   \
                           \
   for (__i = 0; __i < RES_CONFIG_SQLITE_MAX_LOOPS; __i++)  {
Macro used before executing a query.

See also:
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 147 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(), and realtime_update_handler().

#define RES_CONFIG_SQLITE_CONF_FILE   "res_config_sqlite.conf"

Definition at line 97 of file res_config_sqlite.c.

Referenced by load_config().

#define RES_CONFIG_SQLITE_DESCRIPTION   "Resource Module for SQLite 2"

Definition at line 96 of file res_config_sqlite.c.

Referenced by load_module().

#define RES_CONFIG_SQLITE_DRIVER   "sqlite"

Definition at line 95 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_END ( error   ) 

Value:

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

See also:
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 158 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(), 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 or SQLITE_LOCKED 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 140 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_NAME   "res_config_sqlite"

Definition at line 94 of file res_config_sqlite.c.

Referenced by load_module(), and unload_module().

#define SET_VAR ( config,
to,
from   ) 

Definition at line 111 of file res_config_sqlite.c.

Referenced by load_config().

#define sql_add_cdr_entry

SQL query format to insert a CDR entry.

Definition at line 526 of file res_config_sqlite.c.

Referenced by cdr_handler().

#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 573 of file res_config_sqlite.c.

Referenced by config_handler().


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 99 of file res_config_sqlite.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 1536 of file res_config_sqlite.c.

static void __unreg_module ( void   )  [static]

Definition at line 1536 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 691 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().

00692 {
00693    struct cfg_entry_args *args;
00694    struct ast_variable *var;
00695 
00696    if (argc != RES_CONFIG_SQLITE_CONFIG_COLUMNS) {
00697       ast_log(LOG_WARNING, "Corrupt table\n");
00698       return 1;
00699    }
00700 
00701    args = arg;
00702 
00703    if (!strcmp(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], "#include")) {
00704       struct ast_config *cfg;
00705       char *val;
00706 
00707       val = argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL];
00708       cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked);
00709 
00710       if (!cfg) {
00711          ast_log(LOG_WARNING, "Unable to include %s\n", val);
00712          return 1;
00713       } else {
00714          args->cfg = cfg;
00715          return 0;
00716       }
00717    }
00718 
00719    if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
00720       args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
00721 
00722       if (!args->cat) {
00723          ast_log(LOG_WARNING, "Unable to allocate category\n");
00724          return 1;
00725       }
00726 
00727       ast_free(args->cat_name);
00728       args->cat_name = ast_strdup(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
00729 
00730       if (!args->cat_name) {
00731          ast_category_destroy(args->cat);
00732          return 1;
00733       }
00734 
00735       ast_category_append(args->cfg, args->cat);
00736    }
00737 
00738    var = ast_variable_new(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL], "");
00739 
00740    if (!var) {
00741       ast_log(LOG_WARNING, "Unable to allocate variable");
00742       return 1;
00743    }
00744 
00745    ast_variable_append(args->cat, var);
00746 
00747    return 0;
00748 }

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 837 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().

00838 {
00839    struct rt_cfg_entry_args *args;
00840    struct ast_variable *var;
00841    int i;
00842 
00843    args = arg;
00844 
00845    for (i = 0; i < argc; i++) {
00846       if (!argv[i])
00847          continue;
00848 
00849       if (!(var = ast_variable_new(columnNames[i], argv[i], "")))
00850          return 1;
00851 
00852       if (!args->var)
00853          args->var = var;
00854 
00855       if (!args->last)
00856          args->last = var;
00857       else {
00858          args->last->next = var;
00859          args->last = var;
00860       }
00861    }
00862 
00863    return 0;
00864 }

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 955 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().

00956 {
00957    struct rt_multi_cfg_entry_args *args;
00958    struct ast_category *cat;
00959    struct ast_variable *var;
00960    char *cat_name;
00961    size_t i;
00962 
00963    args = arg;
00964    cat_name = NULL;
00965 
00966    /*
00967     * cat_name should always be set here, since initfield is forged from
00968     * params[0] in realtime_multi_handler(), which is a search parameter
00969     * of the SQL query.
00970     */
00971    for (i = 0; i < argc; i++) {
00972       if (!strcmp(args->initfield, columnNames[i]))
00973          cat_name = argv[i];
00974    }
00975 
00976    if (!cat_name) {
00977       ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
00978       return 1;
00979    }
00980 
00981    if (!(cat = ast_category_new(cat_name, "", 99999))) {
00982       ast_log(LOG_WARNING, "Unable to allocate category\n");
00983       return 1;
00984    }
00985 
00986    ast_category_append(args->cfg, cat);
00987 
00988    for (i = 0; i < argc; i++) {
00989       if (!argv[i] || !strcmp(args->initfield, columnNames[i]))
00990          continue;
00991 
00992       if (!(var = ast_variable_new(columnNames[i], argv[i], ""))) {
00993          ast_log(LOG_WARNING, "Unable to allocate variable\n");
00994          return 1;
00995       }
00996 
00997       ast_variable_append(cat, var);
00998    }
00999 
01000    return 0;
01001 }

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 652 of file res_config_sqlite.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_debug, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, sql_add_cdr_entry, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_module().

00653 {
00654    char *query, *errormsg;
00655    int error;
00656 
00657    query = sqlite_mprintf(sql_add_cdr_entry, cdr_table, cdr->clid,
00658          cdr->src, cdr->dst, cdr->dcontext, cdr->channel,
00659          cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00660          cdr->start.tv_sec, cdr->answer.tv_sec,
00661          cdr->end.tv_sec, cdr->duration, cdr->billsec,
00662          cdr->disposition, cdr->amaflags, cdr->accountcode,
00663          cdr->uniqueid, cdr->userfield);
00664 
00665    if (!query) {
00666       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
00667       return 1;
00668    }
00669 
00670    ast_debug(1, "SQL query: %s\n", query);
00671 
00672    ast_mutex_lock(&mutex);
00673 
00674    RES_CONFIG_SQLITE_BEGIN
00675       error = sqlite_exec(db, query, NULL, NULL, &errormsg);
00676    RES_CONFIG_SQLITE_END(error)
00677 
00678    ast_mutex_unlock(&mutex);
00679 
00680    sqlite_freemem(query);
00681 
00682    if (error) {
00683       ast_log(LOG_ERROR, "%s\n", errormsg);
00684       sqlite_freemem(errormsg);
00685       return 1;
00686    }
00687 
00688    return 0;
00689 }

static int check_vars ( void   )  [static]

Definition at line 594 of file res_config_sqlite.c.

References ast_log(), and LOG_ERROR.

Referenced by load_config().

00595 {
00596    if (!dbfile) {
00597       ast_log(LOG_ERROR, "Required parameter undefined: dbfile\n");
00598       return 1;
00599    }
00600 
00601    use_cdr = (cdr_table != NULL);
00602 
00603    return 0;
00604 }

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 750 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, sql_get_config_table, and cfg_entry_args::who_asked.

00752 {
00753    struct cfg_entry_args args;
00754    char *query, *errormsg;
00755    int error;
00756 
00757    if (!config_table) {
00758       if (!table) {
00759          ast_log(LOG_ERROR, "Table name unspecified\n");
00760          return NULL;
00761       }
00762    } else
00763       table = config_table;
00764 
00765    query = sqlite_mprintf(sql_get_config_table, table, file);
00766 
00767    if (!query) {
00768       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
00769       return NULL;
00770    }
00771 
00772    ast_debug(1, "SQL query: %s\n", query);
00773    args.cfg = cfg;
00774    args.cat = NULL;
00775    args.cat_name = NULL;
00776    args.flags = flags;
00777    args.who_asked = who_asked;
00778 
00779    ast_mutex_lock(&mutex);
00780 
00781    RES_CONFIG_SQLITE_BEGIN
00782       error = sqlite_exec(db, query, add_cfg_entry, &args, &errormsg);
00783    RES_CONFIG_SQLITE_END(error)
00784 
00785    ast_mutex_unlock(&mutex);
00786 
00787    ast_free(args.cat_name);
00788    sqlite_freemem(query);
00789 
00790    if (error) {
00791       ast_log(LOG_ERROR, "%s\n", errormsg);
00792       sqlite_freemem(errormsg);
00793       return NULL;
00794    }
00795 
00796    return cfg;
00797 }

static size_t get_params ( va_list  ap,
const char ***  params_ptr,
const char ***  vals_ptr 
) [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 799 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().

00800 {
00801    const char **tmp, *param, *val, **params, **vals;
00802    size_t params_count;
00803 
00804    params = NULL;
00805    vals = NULL;
00806    params_count = 0;
00807 
00808    while ((param = va_arg(ap, const char *)) && (val = va_arg(ap, const char *))) {
00809       if (!(tmp = ast_realloc(params, (params_count + 1) * sizeof(char *)))) {
00810          ast_free(params);
00811          ast_free(vals);
00812          return 0;
00813       }
00814       params = tmp;
00815 
00816       if (!(tmp = ast_realloc(vals, (params_count + 1) * sizeof(char *)))) {
00817          ast_free(params);
00818          ast_free(vals);
00819          return 0;
00820       }
00821       vals = tmp;
00822 
00823       params[params_count] = param;
00824       vals[params_count] = val;
00825       params_count++;
00826    }
00827 
00828    if (params_count > 0) {
00829       *params_ptr = params;
00830       *vals_ptr = vals;
00831    } else
00832       ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
00833 
00834    return params_count;
00835 }

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 1374 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.

01375 {
01376    switch (cmd) {
01377    case CLI_INIT:
01378       e->command = "sqlite show status";
01379       e->usage =
01380          "Usage: sqlite show status\n"
01381          "       Show status information about the SQLite 2 driver\n";
01382       return NULL;
01383    case CLI_GENERATE:
01384       return NULL;
01385    }
01386 
01387    if (a->argc != 3)
01388       return CLI_SHOWUSAGE;
01389 
01390    ast_cli(a->fd, "SQLite database path: %s\n", dbfile);
01391    ast_cli(a->fd, "config_table: ");
01392 
01393    if (!config_table)
01394       ast_cli(a->fd, "unspecified, must be present in extconfig.conf\n");
01395    else
01396       ast_cli(a->fd, "%s\n", config_table);
01397 
01398    ast_cli(a->fd, "cdr_table: ");
01399 
01400    if (!cdr_table)
01401       ast_cli(a->fd, "unspecified, CDR support disabled\n");
01402    else
01403       ast_cli(a->fd, "%s\n", cdr_table);
01404 
01405    return CLI_SUCCESS;
01406 }

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 606 of file res_config_sqlite.c.

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

00607 {
00608    struct ast_config *config;
00609    struct ast_variable *var;
00610    int error;
00611    struct ast_flags config_flags = { 0 };
00612 
00613    config = ast_config_load(RES_CONFIG_SQLITE_CONF_FILE, config_flags);
00614 
00615    if (!config) {
00616       ast_log(LOG_ERROR, "Unable to load " RES_CONFIG_SQLITE_CONF_FILE "\n");
00617       return 1;
00618    }
00619 
00620    for (var = ast_variable_browse(config, "general"); var; var = var->next) {
00621       if (!strcasecmp(var->name, "dbfile"))
00622          SET_VAR(config, dbfile, var);
00623       else if (!strcasecmp(var->name, "config_table"))
00624          SET_VAR(config, config_table, var);
00625       else if (!strcasecmp(var->name, "cdr_table"))
00626          SET_VAR(config, cdr_table, var);
00627       else
00628          ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
00629    }
00630 
00631    ast_config_destroy(config);
00632    error = check_vars();
00633 
00634    if (error) {
00635       unload_config();
00636       return 1;
00637    }
00638 
00639    return 0;
00640 }

static int load_module ( void   )  [static]

Definition at line 1426 of file res_config_sqlite.c.

References 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, and unload_module().

01427 {
01428    char *errormsg;
01429    int error;
01430 
01431    db = NULL;
01432    cdr_registered = 0;
01433    cli_status_registered = 0;
01434    dbfile = NULL;
01435    config_table = NULL;
01436    cdr_table = NULL;
01437    error = load_config();
01438 
01439    if (error)
01440       return AST_MODULE_LOAD_DECLINE;
01441 
01442    if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
01443       ast_log(LOG_ERROR, "%s\n", errormsg);
01444       sqlite_freemem(errormsg);
01445       unload_module();
01446       return 1;
01447    }
01448 
01449    ast_config_engine_register(&sqlite_engine);
01450 
01451    if (use_cdr) {
01452       char *query;
01453 
01454 /* \cond DOXYGEN_CAN_PARSE_THIS */
01455 #undef QUERY
01456 #define QUERY "SELECT COUNT(id) FROM %Q;"
01457 /* \endcond */
01458 
01459       query = sqlite_mprintf(QUERY, cdr_table);
01460 
01461       if (!query) {
01462          ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
01463          unload_module();
01464          return 1;
01465       }
01466 
01467       ast_debug(1, "SQL query: %s\n", query);
01468 
01469       RES_CONFIG_SQLITE_BEGIN
01470          error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01471       RES_CONFIG_SQLITE_END(error)
01472 
01473       sqlite_freemem(query);
01474 
01475       if (error) {
01476          /*
01477           * Unexpected error.
01478           */
01479          if (error != SQLITE_ERROR) {
01480             ast_log(LOG_ERROR, "%s\n", errormsg);
01481             sqlite_freemem(errormsg);
01482             unload_module();
01483             return 1;
01484          }
01485 
01486          sqlite_freemem(errormsg);
01487          query = sqlite_mprintf(sql_create_cdr_table, cdr_table);
01488 
01489          if (!query) {
01490             ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
01491             unload_module();
01492             return 1;
01493          }
01494 
01495          ast_debug(1, "SQL query: %s\n", query);
01496 
01497          RES_CONFIG_SQLITE_BEGIN
01498             error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01499          RES_CONFIG_SQLITE_END(error)
01500 
01501          sqlite_freemem(query);
01502 
01503          if (error) {
01504             ast_log(LOG_ERROR, "%s\n", errormsg);
01505             sqlite_freemem(errormsg);
01506             unload_module();
01507             return 1;
01508          }
01509       }
01510 
01511       error = ast_cdr_register(RES_CONFIG_SQLITE_NAME, RES_CONFIG_SQLITE_DESCRIPTION, cdr_handler);
01512 
01513       if (error) {
01514          unload_module();
01515          return 1;
01516       }
01517 
01518       cdr_registered = 1;
01519    }
01520 
01521    error = ast_cli_register_multiple(cli_status, sizeof(cli_status) / sizeof(struct ast_cli_entry));
01522 
01523    if (error) {
01524       unload_module();
01525       return 1;
01526    }
01527 
01528    cli_status_registered = 1;
01529 
01530    return 0;
01531 }

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 1297 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, and RES_CONFIG_SQLITE_END.

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

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 866 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, and rt_cfg_entry_args::var.

00867 {
00868    char *query, *errormsg, *op, *tmp_str;
00869    struct rt_cfg_entry_args args;
00870    const char **params, **vals;
00871    size_t params_count;
00872    int error;
00873 
00874    if (!table) {
00875       ast_log(LOG_WARNING, "Table name unspecified\n");
00876       return NULL;
00877    }
00878 
00879    params_count = get_params(ap, &params, &vals);
00880 
00881    if (params_count == 0)
00882       return NULL;
00883 
00884    op = (strchr(params[0], ' ') == NULL) ? " =" : "";
00885 
00886 /* \cond DOXYGEN_CAN_PARSE_THIS */
00887 #undef QUERY
00888 #define QUERY "SELECT * FROM '%q' WHERE commented = 0 AND %q%s '%q'"
00889 /* \endcond */
00890 
00891    query = sqlite_mprintf(QUERY, table, params[0], op, vals[0]);
00892 
00893    if (!query) {
00894       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
00895       ast_free(params);
00896       ast_free(vals);
00897       return NULL;
00898    }
00899 
00900    if (params_count > 1) {
00901       size_t i;
00902 
00903       for (i = 1; i < params_count; i++) {
00904          op = (strchr(params[i], ' ') == NULL) ? " =" : "";
00905          tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
00906          sqlite_freemem(query);
00907 
00908          if (!tmp_str) {
00909             ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
00910             ast_free(params);
00911             ast_free(vals);
00912             return NULL;
00913          }
00914 
00915          query = tmp_str;
00916       }
00917    }
00918 
00919    ast_free(params);
00920    ast_free(vals);
00921 
00922    tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
00923    sqlite_freemem(query);
00924 
00925    if (!tmp_str) {
00926       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
00927       return NULL;
00928    }
00929 
00930    query = tmp_str;
00931    ast_debug(1, "SQL query: %s\n", query);
00932    args.var = NULL;
00933    args.last = NULL;
00934 
00935    ast_mutex_lock(&mutex);
00936 
00937    RES_CONFIG_SQLITE_BEGIN
00938       error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
00939    RES_CONFIG_SQLITE_END(error)
00940 
00941    ast_mutex_unlock(&mutex);
00942 
00943    sqlite_freemem(query);
00944 
00945    if (error) {
00946       ast_log(LOG_WARNING, "%s\n", errormsg);
00947       sqlite_freemem(errormsg);
00948       ast_variables_destroy(args.var);
00949       return NULL;
00950    }
00951 
00952    return args.var;
00953 }

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 1003 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, and RES_CONFIG_SQLITE_END.

01005 {
01006    char *query, *errormsg, *op, *tmp_str, *initfield;
01007    struct rt_multi_cfg_entry_args args;
01008    const char **params, **vals;
01009    struct ast_config *cfg;
01010    size_t params_count;
01011    int error;
01012 
01013    if (!table) {
01014       ast_log(LOG_WARNING, "Table name unspecified\n");
01015       return NULL;
01016    }
01017 
01018    if (!(cfg = ast_config_new())) {
01019       ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
01020       return NULL;
01021    }
01022 
01023    if (!(params_count = get_params(ap, &params, &vals))) {
01024       ast_config_destroy(cfg);
01025       return NULL;
01026    }
01027 
01028    if (!(initfield = ast_strdup(params[0]))) {
01029       ast_config_destroy(cfg);
01030       ast_free(params);
01031       ast_free(vals);
01032       return NULL;
01033    }
01034 
01035    tmp_str = strchr(initfield, ' ');
01036 
01037    if (tmp_str)
01038       *tmp_str = '\0';
01039 
01040    op = (!strchr(params[0], ' ')) ? " =" : "";
01041 
01042    /*
01043     * Asterisk sends us an already escaped string when searching for
01044     * "exten LIKE" (uh!). Handle it separately.
01045     */
01046    tmp_str = (!strcmp(vals[0], "\\_%")) ? "_%" : (char *)vals[0];
01047 
01048 /* \cond DOXYGEN_CAN_PARSE_THIS */
01049 #undef QUERY
01050 #define QUERY "SELECT * FROM '%q' WHERE commented = 0 AND %q%s '%q'"
01051 /* \endcond */
01052 
01053    if (!(query = sqlite_mprintf(QUERY, table, params[0], op, tmp_str))) {
01054       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
01055       ast_config_destroy(cfg);
01056       ast_free(params);
01057       ast_free(vals);
01058       ast_free(initfield);
01059       return NULL;
01060    }
01061 
01062    if (params_count > 1) {
01063       size_t i;
01064 
01065       for (i = 1; i < params_count; i++) {
01066          op = (!strchr(params[i], ' ')) ? " =" : "";
01067          tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
01068          sqlite_freemem(query);
01069 
01070          if (!tmp_str) {
01071             ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01072             ast_config_destroy(cfg);
01073             ast_free(params);
01074             ast_free(vals);
01075             ast_free(initfield);
01076             return NULL;
01077          }
01078 
01079          query = tmp_str;
01080       }
01081    }
01082 
01083    ast_free(params);
01084    ast_free(vals);
01085 
01086    if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
01087       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01088       sqlite_freemem(query);
01089       ast_config_destroy(cfg);
01090       ast_free(initfield);
01091       return NULL;
01092    }
01093 
01094    sqlite_freemem(query);
01095    query = tmp_str;
01096    ast_debug(1, "SQL query: %s\n", query);
01097    args.cfg = cfg;
01098    args.initfield = initfield;
01099 
01100    ast_mutex_lock(&mutex);
01101 
01102    RES_CONFIG_SQLITE_BEGIN
01103       error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
01104    RES_CONFIG_SQLITE_END(error)
01105 
01106    ast_mutex_unlock(&mutex);
01107 
01108    sqlite_freemem(query);
01109    ast_free(initfield);
01110 
01111    if (error) {
01112       ast_log(LOG_WARNING, "%s\n", errormsg);
01113       sqlite_freemem(errormsg);
01114       ast_config_destroy(cfg);
01115       return NULL;
01116    }
01117 
01118    return cfg;
01119 }

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 1203 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, and RES_CONFIG_SQLITE_END.

01204 {
01205    char *errormsg, *tmp_str, *tmp_keys = NULL, *tmp_keys2 = NULL, *tmp_vals = NULL, *tmp_vals2 = NULL;
01206    const char **params, **vals;
01207    size_t params_count;
01208    int error, rows_id;
01209    size_t i;
01210 
01211    if (!table) {
01212       ast_log(LOG_WARNING, "Table name unspecified\n");
01213       return -1;
01214    }
01215 
01216    if (!(params_count = get_params(ap, &params, &vals)))
01217       return -1;
01218 
01219 /* \cond DOXYGEN_CAN_PARSE_THIS */
01220 #undef QUERY
01221 #define QUERY "INSERT into '%q' (%s) VALUES (%s);"
01222 /* \endcond */
01223 
01224    for (i = 0; i < params_count; i++) {
01225       if ( tmp_keys2 ) {
01226          tmp_keys = sqlite_mprintf("%s, %q", tmp_keys2, params[i]);
01227          sqlite_freemem(tmp_keys2);
01228       } else {
01229          tmp_keys = sqlite_mprintf("%q", params[i]);
01230       }
01231       if (!tmp_keys) {
01232          ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01233          sqlite_freemem(tmp_vals);
01234          ast_free(params);
01235          ast_free(vals);
01236          return -1;
01237       }
01238 
01239       if ( tmp_vals2 ) {
01240          tmp_vals = sqlite_mprintf("%s, '%q'", tmp_vals2, params[i]);
01241          sqlite_freemem(tmp_vals2);
01242       } else {
01243          tmp_vals = sqlite_mprintf("'%q'", params[i]);
01244       }
01245       if (!tmp_vals) {
01246          ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01247          sqlite_freemem(tmp_keys);
01248          ast_free(params);
01249          ast_free(vals);
01250          return -1;
01251       }
01252 
01253 
01254       tmp_keys2 = tmp_keys;
01255       tmp_vals2 = tmp_vals;
01256    }
01257 
01258    ast_free(params);
01259    ast_free(vals);
01260 
01261    if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
01262       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01263       sqlite_freemem(tmp_keys);
01264       sqlite_freemem(tmp_vals);
01265       return -1;
01266    }
01267 
01268    sqlite_freemem(tmp_keys);
01269    sqlite_freemem(tmp_vals);
01270 
01271    ast_debug(1, "SQL query: %s\n", tmp_str);
01272 
01273    ast_mutex_lock(&mutex);
01274 
01275    RES_CONFIG_SQLITE_BEGIN
01276       error = sqlite_exec(db, tmp_str, NULL, NULL, &errormsg);
01277    RES_CONFIG_SQLITE_END(error)
01278 
01279    if (!error) {
01280       rows_id = sqlite_last_insert_rowid(db);
01281    } else {
01282       rows_id = -1;
01283    }
01284 
01285    ast_mutex_unlock(&mutex);
01286 
01287    sqlite_freemem(tmp_str);
01288 
01289    if (error) {
01290       ast_log(LOG_WARNING, "%s\n", errormsg);
01291       sqlite_freemem(errormsg);
01292    }
01293 
01294    return rows_id;
01295 }

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 1121 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, and RES_CONFIG_SQLITE_END.

01123 {
01124    char *query, *errormsg, *tmp_str;
01125    const char **params, **vals;
01126    size_t params_count;
01127    int error, rows_num;
01128 
01129    if (!table) {
01130       ast_log(LOG_WARNING, "Table name unspecified\n");
01131       return -1;
01132    }
01133 
01134    if (!(params_count = get_params(ap, &params, &vals)))
01135       return -1;
01136 
01137 /* \cond DOXYGEN_CAN_PARSE_THIS */
01138 #undef QUERY
01139 #define QUERY "UPDATE '%q' SET %q = '%q'"
01140 /* \endcond */
01141 
01142    if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
01143       ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
01144       ast_free(params);
01145       ast_free(vals);
01146       return -1;
01147    }
01148 
01149    if (params_count > 1) {
01150       size_t i;
01151 
01152       for (i = 1; i < params_count; i++) {
01153          tmp_str = sqlite_mprintf("%s, %q = '%q'", query, params[i], vals[i]);
01154          sqlite_freemem(query);
01155 
01156          if (!tmp_str) {
01157             ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01158             ast_free(params);
01159             ast_free(vals);
01160             return -1;
01161          }
01162 
01163          query = tmp_str;
01164       }
01165    }
01166 
01167    ast_free(params);
01168    ast_free(vals);
01169 
01170    if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
01171       ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
01172       sqlite_freemem(query);
01173       return -1;
01174    }
01175 
01176    sqlite_freemem(query);
01177    query = tmp_str;
01178    ast_debug(1, "SQL query: %s\n", query);
01179 
01180    ast_mutex_lock(&mutex);
01181 
01182    RES_CONFIG_SQLITE_BEGIN
01183       error = sqlite_exec(db, query, NULL, NULL, &errormsg);
01184    RES_CONFIG_SQLITE_END(error)
01185 
01186    if (!error)
01187       rows_num = sqlite_changes(db);
01188    else
01189       rows_num = -1;
01190 
01191    ast_mutex_unlock(&mutex);
01192 
01193    sqlite_freemem(query);
01194 
01195    if (error) {
01196       ast_log(LOG_WARNING, "%s\n", errormsg);
01197       sqlite_freemem(errormsg);
01198    }
01199 
01200    return rows_num;
01201 }

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 579 of file res_config_sqlite.c.

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

00580 {
00581    if (*var)
00582       ast_free(*var);
00583 
00584    *var = ast_strdup(value);
00585 
00586    if (!*var) {
00587       ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
00588       return 1;
00589    }
00590 
00591    return 0;
00592 }

static void unload_config ( void   )  [static]

Free resources related to configuration.

See also:
load_config()

Definition at line 642 of file res_config_sqlite.c.

References ast_free.

Referenced by load_config(), and unload_module().

00643 {
00644    ast_free(dbfile);
00645    dbfile = NULL;
00646    ast_free(config_table);
00647    config_table = NULL;
00648    ast_free(cdr_table);
00649    cdr_table = NULL;
00650 }

static int unload_module ( void   )  [static]

Definition at line 1408 of file res_config_sqlite.c.

References ast_cdr_unregister(), ast_cli_unregister_multiple(), ast_config_engine_deregister(), RES_CONFIG_SQLITE_NAME, and unload_config().

01409 {
01410    if (cli_status_registered)
01411       ast_cli_unregister_multiple(cli_status, sizeof(cli_status) / sizeof(struct ast_cli_entry));
01412 
01413    if (cdr_registered)
01414       ast_cdr_unregister(RES_CONFIG_SQLITE_NAME);
01415 
01416    ast_config_engine_deregister(&sqlite_engine);
01417 
01418    if (db)
01419       sqlite_close(db);
01420 
01421    unload_config();
01422 
01423    return 0;
01424 }


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 1536 of file res_config_sqlite.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 1536 of file res_config_sqlite.c.

int cdr_registered [static]

Set to 1 if the CDR callback function was registered.

Definition at line 454 of file res_config_sqlite.c.

char* cdr_table [static]

The name of the table used to store CDR entries.

Definition at line 466 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"),
}
Structure containing details and callback functions for the CLI status command.

Definition at line 492 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 457 of file res_config_sqlite.c.

char* config_table [static]

The name of the static configuration table.

Definition at line 463 of file res_config_sqlite.c.

sqlite* db [static]

The SQLite database object.

Definition at line 448 of file res_config_sqlite.c.

char* dbfile [static]

The path of the database file.

Definition at line 460 of file res_config_sqlite.c.

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

The mutex used to prevent simultaneous access to the SQLite database.

Definition at line 486 of file res_config_sqlite.c.

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

char* sql_create_cdr_table [static]

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

Definition at line 501 of file res_config_sqlite.c.

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

Definition at line 472 of file res_config_sqlite.c.

int use_cdr [static]

Set to 1 if CDR support is enabled.

Definition at line 451 of file res_config_sqlite.c.


Generated on Wed Oct 28 11:46:18 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6