diff options
author | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-04-18 18:16:32 +0000 |
---|---|---|
committer | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-04-18 18:16:32 +0000 |
commit | 184bfd1c047b2e20da7480e5695eda31014a34f3 (patch) | |
tree | 983b8bac092c5027e21a0903506b67d526b4d6a3 /res/res_config_odbc.c | |
parent | ac1154dd8290a6a2e8ef49258678d80fbc738b5a (diff) |
update res_odbc to support pooled connections
(from tilghman's developer branch, res_odbc_rewrite)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@21181 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'res/res_config_odbc.c')
-rw-r--r-- | res/res_config_odbc.c | 162 |
1 files changed, 113 insertions, 49 deletions
diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c index f2634a93c..e48b01c02 100644 --- a/res/res_config_odbc.c +++ b/res/res_config_odbc.c @@ -52,7 +52,7 @@ LOCAL_USER_DECL; static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap) { - odbc_obj *obj; + struct odbc_obj *obj; SQLHSTMT stmt; char sql[1024]; char coltitle[256]; @@ -79,19 +79,21 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl if (!table) return NULL; - obj = fetch_odbc_obj(database, 0); + obj = odbc_request_obj(database, 0); if (!obj) return NULL; res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); + odbc_release_obj(obj); return NULL; } newparam = va_arg(aq, const char *); if (!newparam) { SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } newval = va_arg(aq, const char *); @@ -107,6 +109,7 @@ 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 Prepare failed![%s]\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } @@ -123,6 +126,7 @@ 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 Execute error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } @@ -130,20 +134,23 @@ 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 Column Count error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } res = SQLFetch(stmt); if (res == SQL_NO_DATA) { SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } - for (x=0;x<colcount;x++) { + for (x = 0; x < colcount; x++) { rowdata[0] = '\0'; collen = sizeof(coltitle); res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen, @@ -152,6 +159,7 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql); if (var) ast_variables_destroy(var); + odbc_release_obj(obj); return NULL; } @@ -164,6 +172,7 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); if (var) ast_variables_destroy(var); + odbc_release_obj(obj); return NULL; } stringp = rowdata; @@ -183,12 +192,13 @@ static struct ast_variable *realtime_odbc(const char *database, const char *tabl SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return var; } static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap) { - odbc_obj *obj; + struct odbc_obj *obj; SQLHSTMT stmt; char sql[1024]; char coltitle[256]; @@ -220,19 +230,21 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * return NULL; memset(&ra, 0, sizeof(ra)); - obj = fetch_odbc_obj(database, 0); + obj = odbc_request_obj(database, 0); if (!obj) return NULL; res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); + odbc_release_obj(obj); return NULL; } newparam = va_arg(aq, const char *); if (!newparam) { - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } initfield = ast_strdupa(newparam); @@ -252,7 +264,8 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql); - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } @@ -268,21 +281,24 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql); - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } res = SQLNumResultCols(stmt, &colcount); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } cfg = ast_config_new(); if (!cfg) { ast_log(LOG_WARNING, "Out of memory!\n"); - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } @@ -332,13 +348,14 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char * ast_category_append(cfg, cat); } - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return cfg; } static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap) { - odbc_obj *obj; + struct odbc_obj *obj; SQLHSTMT stmt; char sql[256]; SQLLEN rowcount=0; @@ -352,19 +369,21 @@ static int update_odbc(const char *database, const char *table, const char *keyf if (!table) return -1; - obj = fetch_odbc_obj (database, 0); + obj = odbc_request_obj(database, 0); if (!obj) return -1; res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); + odbc_release_obj(obj); return -1; } newparam = va_arg(aq, const char *); if (!newparam) { SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return -1; } newval = va_arg(aq, const char *); @@ -380,6 +399,7 @@ static int update_odbc(const char *database, const char *table, const char *keyf if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return -1; } @@ -398,103 +418,147 @@ static int update_odbc(const char *database, const char *table, const char *keyf if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return -1; } res = SQLRowCount(stmt, &rowcount); SQLFreeHandle (SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql); return -1; } - if (rowcount >= 0) - return (int)rowcount; + if (rowcount >= 0) + return (int)rowcount; return -1; } +struct config_odbc_obj { + char *sql; + unsigned long id; + unsigned long cat_metric; + unsigned long var_metric; + unsigned long commented; + char filename[128]; + char category[128]; + char var_name[128]; + char var_val[128]; + SQLINTEGER err; +}; + +static SQLHSTMT config_odbc_prepare(struct odbc_obj *obj, void *data) +{ + struct config_odbc_obj *q = data; + SQLHSTMT sth; + int res; + + res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &sth); + if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + if (option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "Failure in AllocStatement %d\n", res); + return NULL; + } + + res = SQLPrepare(sth, (unsigned char *)q->sql, SQL_NTS); + if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + if (option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "Error in PREPARE %d\n", res); + SQLFreeHandle(SQL_HANDLE_STMT, sth); + return NULL; + } + + SQLBindCol(sth, 1, SQL_C_ULONG, &q->id, sizeof(q->id), &q->err); + SQLBindCol(sth, 2, SQL_C_ULONG, &q->cat_metric, sizeof(q->cat_metric), &q->err); + SQLBindCol(sth, 3, SQL_C_ULONG, &q->var_metric, sizeof(q->var_metric), &q->err); + SQLBindCol(sth, 4, SQL_C_ULONG, &q->commented, sizeof(q->commented), &q->err); + SQLBindCol(sth, 5, SQL_C_CHAR, q->filename, sizeof(q->filename), &q->err); + SQLBindCol(sth, 6, SQL_C_CHAR, q->category, sizeof(q->category), &q->err); + SQLBindCol(sth, 7, SQL_C_CHAR, q->var_name, sizeof(q->var_name), &q->err); + SQLBindCol(sth, 8, SQL_C_CHAR, q->var_val, sizeof(q->var_val), &q->err); + + return sth; +} + static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg) { 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; - SQLBIGINT id; - char sql[255] = "", filename[128], category[128], var_name[128], var_val[512]; + struct odbc_obj *obj; + char sql[255] = ""; + unsigned int last_cat_metric = 0; SQLSMALLINT rowcount=0; SQLHSTMT stmt; char last[128] = ""; + struct config_odbc_obj q; + + memset(&q, 0, sizeof(q)); if (!file || !strcmp (file, "res_config_odbc.conf")) return NULL; /* cant configure myself with myself ! */ - obj = fetch_odbc_obj(database, 0); + obj = odbc_request_obj(database, 0); if (!obj) return NULL; - res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); - - SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err); - SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err); - SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err); - SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err); - SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err); - SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err); - SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err); - SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err); - snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE filename='%s' and commented=0 ORDER BY filename,cat_metric desc,var_metric asc,category,var_name,var_val,id", table, file); + q.sql = sql; - res = odbc_smart_direct_execute(obj, stmt, sql); - - 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); + stmt = odbc_prepare_and_execute(obj, config_odbc_prepare, &q); + + if (!stmt) { + ast_log(LOG_WARNING, "SQL select error!\n[%s]\n\n", sql); + odbc_release_obj(obj); return NULL; } - res = SQLNumResultCols (stmt, &rowcount); + res = SQLNumResultCols(stmt, &rowcount); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { - ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql); - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + ast_log(LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } if (!rowcount) { - ast_log (LOG_NOTICE, "found nothing\n"); + ast_log(LOG_NOTICE, "found nothing\n"); + odbc_release_obj(obj); return cfg; } cur_cat = ast_config_get_current_category(cfg); 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); + if (!strcmp (q.var_name, "#include")) { + if (!ast_config_internal_load(q.var_val, cfg)) { + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return NULL; } continue; } - if (strcmp(last, category) || last_cat_metric != cat_metric) { - cur_cat = ast_category_new(category); + if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) { + cur_cat = ast_category_new(q.category); if (!cur_cat) { ast_log(LOG_WARNING, "Out of memory!\n"); break; } - strcpy(last, category); - last_cat_metric = cat_metric; + strcpy(last, q.category); + last_cat_metric = q.cat_metric; ast_category_append(cfg, cur_cat); } - new_v = ast_variable_new(var_name, var_val); + new_v = ast_variable_new(q.var_name, q.var_val); ast_variable_append(cur_cat, new_v); } - SQLFreeHandle (SQL_HANDLE_STMT, stmt); + SQLFreeHandle(SQL_HANDLE_STMT, stmt); + odbc_release_obj(obj); return cfg; } |