diff options
author | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-01-25 06:10:20 +0000 |
---|---|---|
committer | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-01-25 06:10:20 +0000 |
commit | 2207b9a515214bf72785eabf75bd587bd7df23e8 (patch) | |
tree | 5e740f79246c281ef1bc43bcffc87fd21f676005 /res/res_config_odbc.c | |
parent | c7f2030d7977ded782c6430a33e57bbbb69cd6df (diff) |
Merge config updates (bug #3406)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@4889 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'res/res_config_odbc.c')
-rwxr-xr-x | res/res_config_odbc.c | 184 |
1 files changed, 65 insertions, 119 deletions
diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c index 246dd80a8..d83a4b5e2 100755 --- a/res/res_config_odbc.c +++ b/res/res_config_odbc.c @@ -14,7 +14,6 @@ #include <asterisk/channel.h> #include <asterisk/pbx.h> #include <asterisk/config.h> -#include <asterisk/config_pvt.h> #include <asterisk/module.h> #include <asterisk/lock.h> #include <asterisk/options.h> @@ -25,8 +24,6 @@ #include <asterisk/utils.h> static char *tdesc = "ODBC Configuration"; -static struct ast_config_reg reg1; - STANDARD_LOCAL_USER; LOCAL_USER_DECL; @@ -136,14 +133,14 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql); if (var) - ast_destroy_realtime(var); + ast_variables_destroy(var); return NULL; } res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); if (var) - ast_destroy_realtime(var); + ast_variables_destroy(var); return NULL; } stringp = rowdata; @@ -151,11 +148,11 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl chunk = strsep(&stringp, ";"); if (chunk && !ast_strlen_zero(ast_strip(chunk))) { if (prev) { - prev->next = ast_new_variable(coltitle, chunk); + prev->next = ast_variable_new(coltitle, chunk); if (prev->next) prev = prev->next; } else - prev = var = ast_new_variable(coltitle, chunk); + prev = var = ast_variable_new(coltitle, chunk); } } @@ -174,7 +171,6 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * char sql[1024]; char coltitle[256]; char rowdata[2048]; - char *title=NULL; const char *initfield=NULL; char *op; const char *newparam, *newval; @@ -183,7 +179,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * SQLSMALLINT collen; int res; int x; - struct ast_variable *var=NULL, *prev=NULL; + struct ast_variable *var=NULL; struct ast_config *cfg=NULL; struct ast_category *cat=NULL; struct ast_realloca ra; @@ -268,15 +264,25 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * return NULL; } + cfg = ast_config_new(); + if (!cfg) { + ast_log(LOG_WARNING, "Out of memory!\n"); + SQLFreeHandle (SQL_HANDLE_STMT, stmt); + return NULL; + } + while (rowcount--) { var = NULL; - prev = NULL; - title = NULL; res = SQLFetch(stmt); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); continue; } + cat = ast_category_new(""); + if (!cat) { + ast_log(LOG_WARNING, "Out of memory!\n"); + continue; + } for (x=0;x<colcount;x++) { rowdata[0] = '\0'; collen = sizeof(coltitle); @@ -284,48 +290,27 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * &datatype, &colsize, &decimaldigits, &nullable); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql); - if (var) - ast_destroy_realtime(var); + ast_category_destroy(cat); continue; } res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); - if (var) - ast_destroy_realtime(var); + ast_category_destroy(cat); continue; } stringp = rowdata; while(stringp) { chunk = strsep(&stringp, ";"); if (chunk && !ast_strlen_zero(ast_strip(chunk))) { - if (initfield && !strcmp(initfield, coltitle) && !title) - title = ast_restrdupa(&ra, chunk); - if (prev) { - prev->next = ast_new_variable(coltitle, chunk); - if (prev->next) - prev = prev->next; - } else - prev = var = ast_new_variable(coltitle, chunk); - + if (initfield && !strcmp(initfield, coltitle)) + ast_category_rename(cat, chunk); + var = ast_variable_new(coltitle, chunk); + ast_variable_append(cat, var); } } } - if (var) { - cat = ast_new_category(title ? title : ""); - if (cat) { - cat->root = var; - if (!cfg) - cfg = ast_new_config(); - if (cfg) - ast_category_append(cfg, cat); - else - ast_category_destroy(cat); - } else { - ast_log(LOG_WARNING, "Out of memory!\n"); - ast_destroy_realtime(var); - } - } + ast_category_append(cfg, cat); } SQLFreeHandle (SQL_HANDLE_STMT, stmt); @@ -410,11 +395,10 @@ static int update_odbc(const char *database, const char *table, const char *keyf return -1; } -static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *new_config_s, struct ast_category **new_cat_p, int recur) +static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg) { - struct ast_config *new; - struct ast_variable *cur_v, *new_v; - struct ast_category *cur_cat, *new_cat; + struct ast_variable *new_v; + struct ast_category *cur_cat; int res = 0; odbc_obj *obj; SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0; @@ -422,10 +406,7 @@ static struct ast_config *config_odbc(const char *database, const char *table, c char sql[255] = "", filename[128], category[128], var_name[128], var_val[512]; SQLSMALLINT rowcount=0; SQLHSTMT stmt; - char last[80] = ""; - int cat_started = 0; - int var_started = 0; - + char last[128] = ""; if (!file || !strcmp (file, "res_config_odbc.conf")) return NULL; /* cant configure myself with myself ! */ @@ -434,8 +415,6 @@ static struct ast_config *config_odbc(const char *database, const char *table, c if (!obj) return NULL; - last[0] = '\0'; - res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err); @@ -452,90 +431,63 @@ static struct ast_config *config_odbc(const char *database, const char *table, c if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql); + SQLFreeHandle (SQL_HANDLE_STMT, stmt); return NULL; } res = SQLNumResultCols (stmt, &rowcount); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { - ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql); - return NULL; - } - - if (new_config_s) { - new = new_config_s; - cat_started++; - } else { - new = ast_new_config (); - } - - if (!new) { - ast_log(LOG_WARNING, "Out of memory!\n"); + ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); return NULL; } - if (rowcount) { - res = SQLFetch (stmt); - cat_started = 0; - - cur_cat = *new_cat_p; - cur_v = NULL; - - if (cur_cat) - cat_started = 1; - if (cur_v) - var_started = 1; - - while (res != SQL_NO_DATA) { - if (!strcmp (var_name, "#include") && recur < MAX_INCLUDE_LEVEL) { - - config_odbc(database, table, var_val, new, &cur_cat, recur + 1); - } else { - if (strcmp (last, category) || last_cat_metric != cat_metric) { - strncpy(last, category, sizeof(last) - 1); - last_cat_metric = cat_metric; - new_cat = (struct ast_category *) ast_new_category (category); - - if (!cat_started) { - cat_started++; - new->root = new_cat; - cur_cat = new->root; - } else { - cur_cat->next = new_cat; - cur_cat = cur_cat->next; - } - var_started = 0; - - } + if (!rowcount) { + ast_log (LOG_NOTICE, "found nothing\n"); + return cfg; + } - new_v = ast_new_variable (var_name, var_val); + cur_cat = ast_config_get_current_category(cfg); - if (!var_started) { - var_started++; - cur_cat->root = new_v; - cur_v = cur_cat->root; - } else { - cur_v->next = new_v; - cur_v = cur_v->next; - } + while ((res = SQLFetch(stmt)) != SQL_NO_DATA) { + if (!strcmp (var_name, "#include")) { + if (!ast_config_internal_load(var_val, cfg)) { + SQLFreeHandle (SQL_HANDLE_STMT, stmt); + return NULL; } - - /* next row */ - res = SQLFetch (stmt); + continue; + } + if (strcmp(last, category) || last_cat_metric != cat_metric) { + cur_cat = ast_category_new(category); + if (!cur_cat) { + ast_log(LOG_WARNING, "Out of memory!\n"); + break; + } + strcpy(last, category); + last_cat_metric = cat_metric; + ast_category_append(cfg, cur_cat); } - SQLFreeHandle (SQL_HANDLE_STMT, stmt); - } else { - ast_log (LOG_NOTICE, "found nothing\n"); + new_v = ast_variable_new(var_name, var_val); + ast_variable_append(cur_cat, new_v); } - return new; + SQLFreeHandle (SQL_HANDLE_STMT, stmt); + return cfg; } +static struct ast_config_engine odbc_engine = { + .name = "odbc", + .load_func = config_odbc, + .realtime_func = realtime_odbc, + .realtime_multi_func = realtime_multi_odbc, + .update_func = update_odbc +}; + int unload_module (void) { - ast_cust_config_deregister (®1); + ast_config_engine_deregister(&odbc_engine); if (option_verbose) ast_verbose("res_config_odbc unloaded.\n"); STANDARD_HANGUP_LOCALUSERS; @@ -544,13 +496,7 @@ int unload_module (void) int load_module (void) { - memset (®1, 0, sizeof (struct ast_config_reg)); - strncpy(reg1.name, "odbc", sizeof(reg1.name) - 1); - reg1.static_func = config_odbc; - reg1.realtime_func = realtime_odbc; - reg1.realtime_multi_func = realtime_multi_odbc; - reg1.update_func = update_odbc; - ast_cust_config_register (®1); + ast_config_engine_register(&odbc_engine); if (option_verbose) ast_verbose("res_config_odbc loaded.\n"); return 0; |