102 #define MACRO_BEGIN do { 103 #define MACRO_END } while (0) 105 #define RES_CONFIG_SQLITE_NAME "res_config_sqlite" 106 #define RES_CONFIG_SQLITE_DRIVER "sqlite" 107 #define RES_CONFIG_SQLITE_DESCRIPTION "Resource Module for SQLite 2" 108 #define RES_CONFIG_SQLITE_CONF_FILE "res_config_sqlite.conf" 122 #define SET_VAR(config, to, from) \ 126 __error = set_var(&to, #to, from->value); \ 129 ast_config_destroy(config); \ 154 #define RES_CONFIG_SQLITE_MAX_LOOPS 10 161 #define RES_CONFIG_SQLITE_BEGIN \ 165 for (__i = 0; __i < RES_CONFIG_SQLITE_MAX_LOOPS; __i++) { 172 #define RES_CONFIG_SQLITE_END(error) \ 173 if (error != SQLITE_BUSY) \ 270 static int add_cfg_entry(
void *arg,
int argc,
char **argv,
char **columnNames);
517 "CREATE TABLE '%q' (\n" 519 " clid VARCHAR(80) NOT NULL DEFAULT '',\n" 520 " src VARCHAR(80) NOT NULL DEFAULT '',\n" 521 " dst VARCHAR(80) NOT NULL DEFAULT '',\n" 522 " dcontext VARCHAR(80) NOT NULL DEFAULT '',\n" 523 " channel VARCHAR(80) NOT NULL DEFAULT '',\n" 524 " dstchannel VARCHAR(80) NOT NULL DEFAULT '',\n" 525 " lastapp VARCHAR(80) NOT NULL DEFAULT '',\n" 526 " lastdata VARCHAR(80) NOT NULL DEFAULT '',\n" 527 " start DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n" 528 " answer DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n" 529 " end DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\n" 530 " duration INT(11) NOT NULL DEFAULT 0,\n" 531 " billsec INT(11) NOT NULL DEFAULT 0,\n" 532 " disposition VARCHAR(45) NOT NULL DEFAULT '',\n" 533 " amaflags INT(11) NOT NULL DEFAULT 0,\n" 534 " accountcode VARCHAR(20) NOT NULL DEFAULT '',\n" 535 " uniqueid VARCHAR(32) NOT NULL DEFAULT '',\n" 536 " userfield VARCHAR(255) NOT NULL DEFAULT '',\n" 537 " PRIMARY KEY (id)\n" 543 #define sql_table_structure "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'" 551 #define sql_get_config_table \ 554 " WHERE filename = '%q' AND commented = 0" \ 555 " ORDER BY cat_metric ASC, var_metric ASC;" 571 static int find_table_cb(
void *vtblptr,
int argc,
char **argv,
char **columnNames)
584 if ((start = strchr(sql,
'(')) && (end = strrchr(sql,
')'))) {
593 for (i = 0; i < fie.argc; i++) {
595 ast_debug(5,
"Found field: %s\n", fie.ld[i]);
596 if (strncasecmp(fie.ld[i],
"PRIMARY KEY", 11) == 0 && (start = strchr(fie.ld[i],
'(')) && (end = strchr(fie.ld[i],
')'))) {
606 for (type = fie.ld[i]; *type > 32; type++);
609 for (remainder = type; *remainder > 32; remainder++);
611 if (!(col =
ast_calloc(1,
sizeof(*col) + strlen(fie.ld[i]) + strlen(type) + 2))) {
614 col->
name = (
char *)col +
sizeof(*col);
615 col->
type = (
char *)col +
sizeof(*col) + strlen(fie.ld[i]) + 1;
616 strcpy(col->
name, fie.ld[i]);
617 strcpy(col->
type, type);
630 char *sql, *errstr =
NULL;
634 for (i = 0; i < 2; i++) {
636 if (strcmp(tblptr->
name, tablename) == 0) {
656 if (!(tblptr =
ast_calloc(1,
sizeof(*tblptr) + strlen(tablename) + 1))) {
662 tblptr->
name = (
char *)tblptr +
sizeof(*tblptr);
663 strcpy(tblptr->
name, tablename);
666 ast_debug(1,
"About to query table structure: %s\n", sql);
693 #define release_table(a) AST_RWLIST_UNLOCK(&((a)->columns)) 737 if (!strcasecmp(var->
name,
"dbfile"))
739 else if (!strcasecmp(var->
name,
"config_table"))
741 else if (!strcasecmp(var->
name,
"cdr_table")) {
776 char *errormsg =
NULL, *
tmp, workspace[500];
783 if (!sql1 || !sql2) {
805 if (sscanf(tmp,
"%30d", &scannum) == 1) {
815 tmp = sqlite_mprintf(
"%Q", tmp);
840 sqlite_freemem(errormsg);
843 sqlite_freemem(errormsg);
848 static int add_cfg_entry(
void *arg,
int argc,
char **argv,
char **columnNames)
909 char *query, *errormsg =
NULL;
943 sqlite_freemem(query);
947 sqlite_freemem(errormsg);
950 sqlite_freemem(errormsg);
963 for (i = 0; i < argc; i++) {
986 char *query, *errormsg =
NULL, *op, *tmp_str;
1000 op = (strchr(field->
name,
' ') ==
NULL) ?
" =" :
"";
1004 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'" 1014 while ((field = field->
next)) {
1015 op = (strchr(field->
name,
' ') ==
NULL) ?
" =" :
"";
1016 tmp_str = sqlite_mprintf(
"%s AND %q%s '%q'", query, field->
name, op, field->
value);
1017 sqlite_freemem(query);
1027 tmp_str = sqlite_mprintf(
"%s LIMIT 1;", query);
1028 sqlite_freemem(query);
1048 sqlite_freemem(query);
1052 sqlite_freemem(errormsg);
1056 sqlite_freemem(errormsg);
1077 for (i = 0; i < argc; i++) {
1078 if (!strcmp(args->
initfield, columnNames[i]))
1094 for (i = 0; i < argc; i++) {
1113 char *query, *errormsg =
NULL, *op, *tmp_str, *initfield;
1138 tmp_str = strchr(initfield,
' ');
1143 op = (!strchr(field->
name,
' ')) ?
" =" :
"";
1149 tmp_str = (!strcmp(field->
value,
"\\_%")) ?
"_%" : (
char *)field->
value;
1153 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'" 1156 if (!(query = sqlite_mprintf(QUERY, table, (
config_table && !strcmp(
config_table, table)) ?
" commented = 0 AND" :
"", field->
name, op, tmp_str))) {
1163 while ((field = field->
next)) {
1164 op = (!strchr(field->
name,
' ')) ?
" =" :
"";
1165 tmp_str = sqlite_mprintf(
"%s AND %q%s '%q'", query, field->
name, op, field->
value);
1166 sqlite_freemem(query);
1178 if (!(tmp_str = sqlite_mprintf(
"%s ORDER BY %q;", query, initfield))) {
1180 sqlite_freemem(query);
1186 sqlite_freemem(query);
1200 sqlite_freemem(query);
1205 sqlite_freemem(errormsg);
1209 sqlite_freemem(errormsg);
1217 char *query, *errormsg =
NULL, *tmp_str;
1219 int error, rows_num;
1232 #define QUERY "UPDATE '%q' SET %q = '%q'" 1235 if (!(query = sqlite_mprintf(QUERY, table, field->
name, field->
value))) {
1240 while ((field = field->
next)) {
1241 tmp_str = sqlite_mprintf(
"%s, %q = '%q'", query, field->
name, field->
value);
1242 sqlite_freemem(query);
1252 if (!(tmp_str = sqlite_mprintf(
"%s WHERE %q = '%q';", query, keyfield, entity))) {
1254 sqlite_freemem(query);
1258 sqlite_freemem(query);
1265 error = sqlite_exec(
db, query,
NULL,
NULL, &errormsg);
1269 rows_num = sqlite_changes(
db);
1275 sqlite_freemem(query);
1280 sqlite_freemem(errormsg);
1288 char *errormsg =
NULL, *tmp1, *tmp2;
1306 for (field = lookup_fields; field; field = field->
next) {
1308 first ?
"" :
" AND",
1309 tmp1 = sqlite_mprintf(
"%q", field->
name),
1310 tmp2 = sqlite_mprintf(
"%Q", field->
value));
1311 sqlite_freemem(tmp1);
1312 sqlite_freemem(tmp2);
1317 ast_log(
LOG_ERROR,
"No criteria specified on update to '%s@%s'!\n", table, database);
1322 for (field = update_fields; field; field = field->
next) {
1325 tmp1 = sqlite_mprintf(
"%q", field->
name),
1326 tmp2 = sqlite_mprintf(
"%Q", field->
value));
1327 sqlite_freemem(tmp1);
1328 sqlite_freemem(tmp2);
1342 rows_num = sqlite_changes(
db);
1352 sqlite_freemem(errormsg);
1359 char *errormsg =
NULL, *tmp_str, *tmp_keys =
NULL, *tmp_keys2 =
NULL, *tmp_vals =
NULL, *tmp_vals2 =
NULL;
1374 #define QUERY "INSERT into '%q' (%s) VALUES (%s);" 1379 tmp_keys = sqlite_mprintf(
"%s, %q", tmp_keys2, field->
name);
1380 sqlite_freemem(tmp_keys2);
1382 tmp_keys = sqlite_mprintf(
"%q", field->
name);
1386 sqlite_freemem(tmp_vals);
1391 tmp_vals = sqlite_mprintf(
"%s, '%q'", tmp_vals2, field->
value);
1392 sqlite_freemem(tmp_vals2);
1394 tmp_vals = sqlite_mprintf(
"'%q'", field->
value);
1398 sqlite_freemem(tmp_keys);
1403 tmp_keys2 = tmp_keys;
1404 tmp_vals2 = tmp_vals;
1405 }
while ((field = field->
next));
1407 if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
1409 sqlite_freemem(tmp_keys);
1410 sqlite_freemem(tmp_vals);
1414 sqlite_freemem(tmp_keys);
1415 sqlite_freemem(tmp_vals);
1417 ast_debug(1,
"SQL query: %s\n", tmp_str);
1422 error = sqlite_exec(
db, tmp_str,
NULL,
NULL, &errormsg);
1426 rows_id = sqlite_last_insert_rowid(
db);
1433 sqlite_freemem(tmp_str);
1438 sqlite_freemem(errormsg);
1446 char *query, *errormsg =
NULL, *tmp_str;
1448 int error, rows_num;
1457 #define QUERY "DELETE FROM '%q' WHERE" 1460 if (!(query = sqlite_mprintf(QUERY, table))) {
1465 for (field = fields; field; field = field->
next) {
1466 tmp_str = sqlite_mprintf(
"%s %q = '%q' AND", query, field->
name, field->
value);
1467 sqlite_freemem(query);
1477 if (!(tmp_str = sqlite_mprintf(
"%s %q = '%q';", query, keyfield, entity))) {
1479 sqlite_freemem(query);
1482 sqlite_freemem(query);
1489 error = sqlite_exec(
db, query,
NULL,
NULL, &errormsg);
1493 rows_num = sqlite_changes(
db);
1500 sqlite_freemem(query);
1505 sqlite_freemem(errormsg);
1521 while ((elm = va_arg(ap,
char *))) {
1526 if (strcmp(col->
name, elm) == 0) {
1532 ast_log(
LOG_WARNING,
"Realtime table %s: column '%s' is an integer field, but Asterisk requires that it not be!\n", tablename, col->
name);
1539 ast_log(
LOG_WARNING,
"Realtime table %s requires column '%s', but that column does not exist!\n", tablename, elm);
1551 if (!strcasecmp(tbl->
name, tablename)) {
1565 e->
command =
"sqlite show status";
1567 "Usage: sqlite show status\n" 1568 " Show status information about the SQLite 2 driver\n";
1581 ast_cli(a->
fd,
"unspecified, must be present in extconfig.conf\n");
1588 ast_cli(a->
fd,
"unspecified, CDR support disabled\n");
1603 e->
command =
"sqlite show tables";
1605 "Usage: sqlite show tables\n" 1606 " Show table information about the SQLite 2 driver\n";
1620 fprintf(stderr,
"%s\n", col->
name);
1627 ast_cli(a->
fd,
"No tables currently in cache\n");
1665 char *errormsg =
NULL;
1679 if (!(
db = sqlite_open(
dbfile, 0660, &errormsg))) {
1681 sqlite_freemem(errormsg);
1686 sqlite_freemem(errormsg);
1695 #define QUERY "SELECT COUNT(id) FROM %Q;" 1698 query = sqlite_mprintf(QUERY,
cdr_table);
1709 error = sqlite_exec(
db, query,
NULL,
NULL, &errormsg);
1712 sqlite_freemem(query);
1718 if (error != SQLITE_ERROR) {
1720 sqlite_freemem(errormsg);
1725 sqlite_freemem(errormsg);
1738 error = sqlite_exec(
db, query,
NULL,
NULL, &errormsg);
1741 sqlite_freemem(query);
1745 sqlite_freemem(errormsg);
1750 sqlite_freemem(errormsg);
1786 .requires =
"extconfig",
#define RES_CONFIG_SQLITE_CONF_FILE
struct ast_variable * next
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
require_type
Types used in ast_realtime_require_field.
static int find_table_cb(void *vtblptr, int argc, char **argv, char **columnNames)
#define AST_CLI_DEFINE(fn, txt,...)
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
static struct ast_threadstorage where_buf
static int set_var(char **var, const char *name, const char *value)
Allocate a variable.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
#define RES_CONFIG_SQLITE_NAME
static int unload_module(void)
#define RES_CONFIG_SQLITE_DRIVER
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
static struct sqlite_cache_tables * find_table(const char *tablename)
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
static struct ast_variable * realtime_handler(const char *database, const char *table, const struct ast_variable *fields)
Asterisk callback function for RealTime configuration.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
#define RES_CONFIG_SQLITE_BEGIN
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
descriptor for a cli entry.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
static int load_module(void)
Load the module.
#define sql_get_config_table
#define CONFIG_STATUS_FILEINVALID
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.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
static void unload_config(void)
Free resources related to configuration.
Structure for variables, used for configurations and for channel variables.
static void free_table(struct sqlite_cache_tables *tblptr)
static int add_rt_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for RealTime configuration.
int ast_config_engine_deregister(struct ast_config_engine *del)
Deregister config engine.
int ast_config_engine_register(struct ast_config_engine *newconfig)
Register config engine.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
static struct ast_threadstorage sql_buf
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
#define ast_mutex_lock(a)
struct ast_variable * var
#define ast_strdup(str)
A wrapper for strdup()
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
void ast_cli(int fd, const char *fmt,...)
void ast_category_destroy(struct ast_category *cat)
struct ast_config * ast_config_internal_load(const char *configfile, struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl_file, const char *who_asked)
static struct ast_config_engine sqlite_engine
Configuration engine structure, used to define realtime drivers.
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
static int realtime_update_handler(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields)
Asterisk callback function for RealTime configuration (variable update).
static int realtime_destroy_handler(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields)
Asterisk callback function for RealTime configuration (destroys variable).
void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw)
Format a CDR variable from an already posted CDR.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Configuration File Parser.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
struct ast_category * cat
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_config_load(filename, flags)
Load a config file.
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
static int cli_status_registered
static char * config_table
#define AST_RWLIST_TRAVERSE
static struct ast_cli_entry cli_status[]
static struct ast_config * realtime_multi_handler(const char *database, const char *table, const struct ast_variable *fields)
Asterisk callback function for RealTime configuration.
static int cdr_registered
void ast_config_destroy(struct ast_config *config)
Destroys a config.
#define AST_RWLIST_REMOVE_CURRENT
#define ast_strdupa(s)
duplicate a string in memory from the stack
A set of macros to manage forward-linked lists.
#define ast_variable_new(name, value, filename)
#define sql_table_structure
struct ast_config * ast_config_new(void)
Create a new base configuration structure.
static int check_vars(void)
Core PBX routines and definitions.
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
struct sqlite_cache_tables::_columns columns
Responsible for call detail data.
static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for static configuration.
struct ast_variable * last
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
struct sla_ringing_trunk * first
char * strcasestr(const char *, const char *)
static struct columns columns
static char * handle_cli_sqlite_show_tables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
#define ast_calloc(num, len)
A wrapper for calloc()
#define AST_RWLIST_REMOVE_HEAD
#define ast_category_new_dynamic(name)
Create a category that is not backed by a file.
Module has failed to load, may be in an inconsistent state.
static int realtime_store_handler(const char *database, const char *table, const struct ast_variable *fields)
Asterisk callback function for RealTime configuration (variable create/store).
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.
Structure used to handle boolean flags.
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
#define CONFIG_STATUS_FILEMISSING
void ast_category_append(struct ast_config *config, struct ast_category *cat)
Appends a category to a config.
#define AST_RWLIST_INSERT_TAIL
static int realtime_unload_handler(const char *unused, const char *tablename)
static char * sql_create_cdr_table
Standard Command Line Interface.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
static int realtime_update2_handler(const char *database, const char *table, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
#define RES_CONFIG_SQLITE_DESCRIPTION
#define SET_VAR(config, to, from)
static int load_config(void)
Load the configuration file.
static int realtime_require_handler(const char *database, const char *table, va_list ap)
static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for RealTime configuration.
int error(const char *format,...)
#define RES_CONFIG_SQLITE_END(error)
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
int ast_rq_is_int(require_type type)
Check if require type is an integer type.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define AST_RWLIST_TRAVERSE_SAFE_END
#define AST_MUTEX_DEFINE_STATIC(mutex)
#define AST_RWLIST_HEAD(name, type)
Defines a structure to be used to hold a read/write list of specified type.
static int cdr_handler(struct ast_cdr *cdr)
Asterisk callback function for CDR support.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
#define ast_mutex_unlock(a)
#define AST_APP_ARG(name)
Define an application argument.