Fix output delimiters and add prefix parameter to func_odbc #7025 (Corydon76)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@25234 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
BJ Weschke
2006-05-06 13:36:29 +00:00
parent 41b1a82a38
commit 3e2079e46c
2 changed files with 136 additions and 166 deletions

View File

@@ -34,4 +34,5 @@ read=SELECT COUNT(*) FROM exgirlfriends WHERE callerid='${SQL_ESC(${ARG1})}'
dsn=mysql1 dsn=mysql1
read=SELECT location FROM presence WHERE id='${SQL_ESC(${ARG1})}' read=SELECT location FROM presence WHERE id='${SQL_ESC(${ARG1})}'
write=UPDATE presence SET location='${SQL_ESC(${VAL1})}' WHERE id='${SQL_ESC(${ARG1})}' write=UPDATE presence SET location='${SQL_ESC(${VAL1})}' WHERE id='${SQL_ESC(${ARG1})}'
;prefix=OFFICE ; Changes this function from ODBC_PRESENCE to OFFICE_PRESENCE

View File

@@ -47,23 +47,21 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/config.h" #include "asterisk/config.h"
#include "asterisk/res_odbc.h" #include "asterisk/res_odbc.h"
#include "asterisk/app.h"
static char *tdesc = "ODBC lookups"; static char *tdesc = "ODBC lookups";
static char *config = "func_odbc.conf"; static char *config = "func_odbc.conf";
struct acf_odbc_query { struct acf_odbc_query {
char name[30]; AST_LIST_ENTRY(acf_odbc_query) list;
char dsn[30]; char dsn[30];
char sql_read[2048]; char sql_read[2048];
char sql_write[2048]; char sql_write[2048];
struct ast_custom_function *acf; struct ast_custom_function *acf;
unsigned int deleteme:1;
struct acf_odbc_query *next;
}; };
static struct acf_odbc_query *queries = NULL; AST_LIST_HEAD_STATIC(queries, acf_odbc_query);
AST_MUTEX_DEFINE_STATIC(query_lock);
#ifdef NEEDTRACE #ifdef NEEDTRACE
static void acf_odbc_error(SQLHSTMT stmt, int res) static void acf_odbc_error(SQLHSTMT stmt, int res)
@@ -95,16 +93,16 @@ static int acf_odbc_write(struct ast_channel *chan, char *cmd, char *s, const ch
char *tracefile = "/tmp/odbc.trace"; char *tracefile = "/tmp/odbc.trace";
#endif #endif
ast_mutex_lock(&query_lock); AST_LIST_LOCK(&queries);
for (query=queries; query; query = query->next) { AST_LIST_TRAVERSE(&queries, query, list) {
if (!strcasecmp(query->name, cmd + 5)) { if (!strcmp(query->acf->name, cmd)) {
break; break;
} }
} }
if (!query) { if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd); ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return -1; return -1;
} }
@@ -112,7 +110,7 @@ static int acf_odbc_write(struct ast_channel *chan, char *cmd, char *s, const ch
if (!obj) { if (!obj) {
ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn); ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return -1; return -1;
} }
@@ -121,7 +119,7 @@ static int acf_odbc_write(struct ast_channel *chan, char *cmd, char *s, const ch
if (!s || !t) { if (!s || !t) {
ast_log(LOG_ERROR, "Out of memory\n"); ast_log(LOG_ERROR, "Out of memory\n");
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return -1; return -1;
} }
@@ -171,7 +169,7 @@ static int acf_odbc_write(struct ast_channel *chan, char *cmd, char *s, const ch
pbx_builtin_setvar_helper(chan, "VALUE", pbx_builtin_getvar_helper(ast, "VALUE")); pbx_builtin_setvar_helper(chan, "VALUE", pbx_builtin_getvar_helper(ast, "VALUE"));
ast_channel_free(ast); ast_channel_free(ast);
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
retry_write: retry_write:
#ifdef NEEDTRACE #ifdef NEEDTRACE
@@ -242,7 +240,7 @@ static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf
struct odbc_obj *obj; struct odbc_obj *obj;
struct acf_odbc_query *query; struct acf_odbc_query *query;
char *arg, sql[2048] = "", varname[15]; char *arg, sql[2048] = "", varname[15];
int count=0, res, x; int count=0, res, x, buflen = 0;
SQLHSTMT stmt; SQLHSTMT stmt;
SQLSMALLINT colcount=0; SQLSMALLINT colcount=0;
SQLINTEGER indicator; SQLINTEGER indicator;
@@ -251,16 +249,16 @@ static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf
char *tracefile = "/tmp/odbc.trace"; char *tracefile = "/tmp/odbc.trace";
#endif #endif
ast_mutex_lock(&query_lock); AST_LIST_LOCK(&queries);
for (query=queries; query; query = query->next) { AST_LIST_TRAVERSE(&queries, query, list) {
if (!strcasecmp(query->name, cmd + 5)) { if (!strcmp(query->acf->name, cmd)) {
break; break;
} }
} }
if (!query) { if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd); ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return -1; return -1;
} }
@@ -268,7 +266,7 @@ static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf
if (!obj) { if (!obj) {
ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn); ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return -1; return -1;
} }
@@ -292,7 +290,7 @@ static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf
pbx_builtin_setvar_helper(chan, varname, NULL); pbx_builtin_setvar_helper(chan, varname, NULL);
} }
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
@@ -336,7 +334,7 @@ static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf
} }
for (x = 0; x < colcount; x++) { for (x = 0; x < colcount; x++) {
int buflen, coldatalen; int i;
char coldata[256]; char coldata[256];
buflen = strlen(buf); buflen = strlen(buf);
@@ -352,12 +350,24 @@ static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf
return -1; return -1;
} }
strncat(buf + buflen, coldata, len - buflen); /* Copy data, encoding '\' and ',' for the argument parser */
coldatalen = strlen(coldata); for (i = 0; i < sizeof(coldata); i++) {
strncat(buf + buflen + coldatalen, ",", len - buflen - coldatalen); if (coldata[i] == '\\' || coldata[i] == ',') {
buf[buflen++] = '\\';
}
buf[buflen++] = coldata[i];
if (buflen >= len - 2)
break;
if (coldata[i] == '\0')
break;
}
buf[buflen - 1] = ',';
} }
/* Trim trailing comma */ /* Trim trailing comma */
buf[strlen(buf) - 1] = '\0'; buf[buflen - 1] = '\0';
acf_out: acf_out:
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
@@ -400,12 +410,10 @@ static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_qu
return -1; return -1;
} }
*query = calloc(1, sizeof(struct acf_odbc_query)); *query = ast_calloc(1, sizeof(struct acf_odbc_query));
if (! (*query)) if (! (*query))
return -1; return -1;
ast_copy_string((*query)->name, catg, sizeof((*query)->name));
if ((tmp = ast_variable_retrieve(cfg, catg, "dsn"))) { if ((tmp = ast_variable_retrieve(cfg, catg, "dsn"))) {
ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn)); ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn));
} else { } else {
@@ -420,54 +428,80 @@ static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_qu
ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write)); ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
} }
(*query)->acf = calloc(1, sizeof(struct ast_custom_function)); (*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
if ((*query)->acf) { if (! (*query)->acf) {
asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg); free(*query);
asprintf((char **)&((*query)->acf->syntax), "ODBC_%s(<arg1>[...[,<argN>]])", catg);
(*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
asprintf((char **)&((*query)->acf->desc),
"Runs the following query, as defined in func_odbc.conf, performing\n"
"substitution of the arguments into the query as specified by ${ARG1},\n"
"${ARG2}, ... ${ARGn}. When setting the function, the values are provided\n"
"either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
"\nRead:\n%s\n\nWrite:\n%s\n",
(*query)->sql_read,
(*query)->sql_write);
} else if (!ast_strlen_zero((*query)->sql_read)) {
asprintf((char **)&((*query)->acf->desc),
"Runs the following query, as defined in func_odbc.conf, performing\n"
"substitution of the arguments into the query as specified by ${ARG1},\n"
"${ARG2}, ... ${ARGn}. This function may only be read, not set.\n\nSQL:\n%s\n",
(*query)->sql_read);
} else if (!ast_strlen_zero((*query)->sql_write)) {
asprintf((char **)&((*query)->acf->desc),
"Runs the following query, as defined in func_odbc.conf, performing\n"
"substitution of the arguments into the query as specified by ${ARG1},\n"
"${ARG2}, ... ${ARGn}. The values are provided either in whole as\n"
"${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
"This function may only be set.\nSQL:\n%s\n",
(*query)->sql_write);
}
if (ast_strlen_zero((*query)->sql_read)) {
(*query)->acf->read = NULL;
} else {
(*query)->acf->read = acf_odbc_read;
}
if (ast_strlen_zero((*query)->sql_write)) {
(*query)->acf->write = NULL;
} else {
(*query)->acf->write = acf_odbc_write;
}
if (! (*query)->acf->name || ! (*query)->acf->syntax || ! (*query)->acf->desc) {
return -1;
}
} else {
return -1; return -1;
} }
if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg);
} else {
asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg);
}
if (!((*query)->acf->name)) {
free((*query)->acf);
free(*query);
return -1;
}
asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name);
if (!((*query)->acf->syntax)) {
free((char *)(*query)->acf->name);
free((*query)->acf);
free(*query);
return -1;
}
(*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
asprintf((char **)&((*query)->acf->desc),
"Runs the following query, as defined in func_odbc.conf, performing\n"
"substitution of the arguments into the query as specified by ${ARG1},\n"
"${ARG2}, ... ${ARGn}. When setting the function, the values are provided\n"
"either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
"\nRead:\n%s\n\nWrite:\n%s\n",
(*query)->sql_read,
(*query)->sql_write);
} else if (!ast_strlen_zero((*query)->sql_read)) {
asprintf((char **)&((*query)->acf->desc),
"Runs the following query, as defined in func_odbc.conf, performing\n"
"substitution of the arguments into the query as specified by ${ARG1},\n"
"${ARG2}, ... ${ARGn}. This function may only be read, not set.\n\nSQL:\n%s\n",
(*query)->sql_read);
} else if (!ast_strlen_zero((*query)->sql_write)) {
asprintf((char **)&((*query)->acf->desc),
"Runs the following query, as defined in func_odbc.conf, performing\n"
"substitution of the arguments into the query as specified by ${ARG1},\n"
"${ARG2}, ... ${ARGn}. The values are provided either in whole as\n"
"${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
"This function may only be set.\nSQL:\n%s\n",
(*query)->sql_write);
}
/* Could be out of memory, or could be we have neither sql_read nor sql_write */
if (! ((*query)->acf->desc)) {
free((char *)(*query)->acf->syntax);
free((char *)(*query)->acf->name);
free((*query)->acf);
free(*query);
return -1;
}
if (ast_strlen_zero((*query)->sql_read)) {
(*query)->acf->read = NULL;
} else {
(*query)->acf->read = acf_odbc_read;
}
if (ast_strlen_zero((*query)->sql_write)) {
(*query)->acf->write = NULL;
} else {
(*query)->acf->write = acf_odbc_write;
}
return 0; return 0;
} }
@@ -494,64 +528,54 @@ static int odbc_load_module(void)
struct ast_config *cfg; struct ast_config *cfg;
char *catg; char *catg;
ast_mutex_lock(&query_lock); AST_LIST_LOCK(&queries);
cfg = ast_config_load(config); cfg = ast_config_load(config);
if (!cfg) { if (!cfg) {
ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config); ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
goto out; AST_LIST_UNLOCK(&queries);
return -1;
} }
for (catg = ast_category_browse(cfg, NULL); for (catg = ast_category_browse(cfg, NULL);
catg; catg;
catg = ast_category_browse(cfg, catg)) { catg = ast_category_browse(cfg, catg)) {
struct acf_odbc_query *query=NULL; struct acf_odbc_query *query = NULL;
if (init_acf_query(cfg, catg, &query)) { if (init_acf_query(cfg, catg, &query)) {
ast_log(LOG_ERROR, "Out of memory\n"); ast_log(LOG_ERROR, "Out of memory\n");
free_acf_query(query); free_acf_query(query);
} else { } else {
query->next = queries; AST_LIST_INSERT_HEAD(&queries, query, list);
queries = query;
ast_custom_function_register(query->acf); ast_custom_function_register(query->acf);
} }
} }
ast_config_destroy(cfg); ast_config_destroy(cfg);
ast_custom_function_register(&escape_function); ast_custom_function_register(&escape_function);
out:
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return res; return res;
} }
static int odbc_unload_module(void) static int odbc_unload_module(void)
{ {
struct acf_odbc_query *query, *lastquery = NULL; struct acf_odbc_query *query;
ast_mutex_lock(&query_lock); AST_LIST_LOCK(&queries);
for (query = queries; query; query = query->next) { while (!AST_LIST_EMPTY(&queries)) {
if (lastquery) query = AST_LIST_REMOVE_HEAD(&queries, list);
free_acf_query(lastquery); ast_custom_function_unregister(query->acf);
if (ast_custom_function_unregister(query->acf)) { free_acf_query(query);
ast_log(LOG_ERROR, "Cannot unregister function '%s'?\n", query->acf->name);
/* Keep state valid */
queries = query;
ast_mutex_unlock(&query_lock);
return -1;
} else {
/* If anything is waiting on this lock, this will let it pass (avoids a race) */
ast_mutex_unlock(&query_lock);
ast_mutex_lock(&query_lock);
lastquery = query;
}
} }
if (lastquery)
free(lastquery);
queries = NULL;
ast_custom_function_unregister(&escape_function); ast_custom_function_unregister(&escape_function);
ast_mutex_unlock(&query_lock); /* Allow any threads waiting for this lock to pass (avoids a race) */
AST_LIST_UNLOCK(&queries);
AST_LIST_LOCK(&queries);
AST_LIST_UNLOCK(&queries);
return 0; return 0;
} }
@@ -559,13 +583,15 @@ static int reload(void *mod)
{ {
int res = 0; int res = 0;
struct ast_config *cfg; struct ast_config *cfg;
struct acf_odbc_query *q, *prevq = NULL, *qdel = NULL; struct acf_odbc_query *oldquery;
char *catg; char *catg;
ast_mutex_lock(&query_lock); AST_LIST_LOCK(&queries);
for (q = queries; q; q = q->next) { while (!AST_LIST_EMPTY(&queries)) {
q->deleteme = 1; oldquery = AST_LIST_REMOVE_HEAD(&queries, list);
ast_custom_function_unregister(oldquery->acf);
free_acf_query(oldquery);
} }
cfg = ast_config_load(config); cfg = ast_config_load(config);
@@ -579,74 +605,17 @@ static int reload(void *mod)
catg = ast_category_browse(cfg, catg)) { catg = ast_category_browse(cfg, catg)) {
struct acf_odbc_query *query = NULL; struct acf_odbc_query *query = NULL;
/* We do this piecemeal, so that we stay in a consistent state, if there's ever an error */
for (q = queries, prevq=NULL; q; prevq=q, q = q->next) {
if (!strcasecmp(catg, q->name)) {
break;
}
}
if (init_acf_query(cfg, catg, &query)) { if (init_acf_query(cfg, catg, &query)) {
ast_log(LOG_ERROR, "Cannot initialize query ODBC_%s\n", catg); ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
free_acf_query(query);
} else { } else {
if (q) { AST_LIST_INSERT_HEAD(&queries, query, list);
/* Replacement */ ast_custom_function_register(query->acf);
if (ast_custom_function_unregister(q->acf)) {
ast_log(LOG_ERROR, "Cannot reload query %s\n", query->acf->name);
free_acf_query(query);
} else {
ast_custom_function_register(query->acf);
/* Add it to the list */
if (prevq)
prevq->next = query;
else
queries = query;
query->next = q->next;
/* Get rid of the old record */
free_acf_query(q);
}
} else {
/* New */
query->next = queries;
queries = query;
ast_custom_function_register(query->acf);
}
} }
} }
/* Any remaining sets will now be destroyed */
for (q = queries; q; q = q->next) {
if (qdel) {
free_acf_query(qdel);
qdel = NULL;
}
if (q->deleteme) {
if (ast_custom_function_unregister(q->acf)) {
ast_log(LOG_ERROR, "Cannot unregister function? Refusing to make 'ODBC_%s' go away.\n", q->name);
} else {
/* If anything is waiting on the lock to execute, this will dispose of it (without a race) */
ast_mutex_unlock(&query_lock);
ast_mutex_lock(&query_lock);
if (prevq) {
prevq->next = q->next;
} else {
queries = q->next;
}
qdel = q;
}
} else {
prevq = q;
}
}
if (qdel)
free_acf_query(qdel);
ast_config_destroy(cfg); ast_config_destroy(cfg);
reload_out: reload_out:
ast_mutex_unlock(&query_lock); AST_LIST_UNLOCK(&queries);
return res; return res;
} }