#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/res_odbc.h"
#include "asterisk/utils.h"
#include "asterisk/stringfields.h"

Go to the source code of this file.
Data Structures | |
| struct | config_odbc_obj |
| struct | custom_prepare_struct |
| struct | update2_prepare_struct |
Defines | |
| #define | CHECK_SIZE(n) |
| #define | warn_length(col, size) ast_log(LOG_WARNING, "Realtime table %s@%s: column '%s' is not long enough to contain realtime data (needs %d)\n", table, database, col->name, size) |
| #define | warn_type(col, type) ast_log(LOG_WARNING, "Realtime table %s@%s: column '%s' is of the incorrect type (%d) to contain the required realtime data\n", table, database, col->name, col->type) |
| #define | WARN_TYPE_OR_LENGTH(n) |
Functions | |
| static void | __init_sql_buf (void) |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static struct ast_config * | config_odbc (const char *database, const char *table, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *sugg_incl, const char *who_asked) |
| static SQLHSTMT | config_odbc_prepare (struct odbc_obj *obj, void *data) |
| static SQLHSTMT | custom_prepare (struct odbc_obj *obj, void *data) |
| static void | decode_chunk (char *chunk) |
| static int | destroy_odbc (const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap) |
| Excute an DELETE query. | |
| static int | load_module (void) |
| static struct ast_config * | realtime_multi_odbc (const char *database, const char *table, va_list ap) |
| Excute an Select query and return ast_config list. | |
| static struct ast_variable * | realtime_odbc (const char *database, const char *table, va_list ap) |
| Excute an SQL query and return ast_variable list. | |
| static int | reload_module (void) |
| static int | require_odbc (const char *database, const char *table, va_list ap) |
| static int | store_odbc (const char *database, const char *table, va_list ap) |
| Excute an INSERT query. | |
| static int | unload_module (void) |
| static int | update2_odbc (const char *database, const char *table, va_list ap) |
| Execute an UPDATE query. | |
| static SQLHSTMT | update2_prepare (struct odbc_obj *obj, void *data) |
| static int | update_odbc (const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap) |
| Excute an UPDATE query. | |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime ODBC 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, .reload = reload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, } |
| static struct ast_module_info * | ast_module_info = &__mod_info |
| static struct ast_config_engine | odbc_engine |
| static struct ast_threadstorage | sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , } |
Definition in file res_config_odbc.c.
| #define CHECK_SIZE | ( | n | ) |
| #define WARN_TYPE_OR_LENGTH | ( | n | ) |
Value:
if (!ast_rq_is_int(type)) { \ warn_type(col, type); \ } else { \ warn_length(col, n); \ }
Referenced by require_odbc().
| static void __init_sql_buf | ( | void | ) | [static] |
| static void __reg_module | ( | void | ) | [static] |
Definition at line 1160 of file res_config_odbc.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 1160 of file res_config_odbc.c.
| static struct ast_config* config_odbc | ( | const char * | database, | |
| const char * | table, | |||
| const char * | file, | |||
| struct ast_config * | cfg, | |||
| struct ast_flags | flags, | |||
| const char * | sugg_incl, | |||
| const char * | who_asked | |||
| ) | [static, read] |
Definition at line 876 of file res_config_odbc.c.
References ast_build_string(), ast_category_append(), ast_category_new(), ast_config_get_current_category(), ast_config_internal_load(), ast_log(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_request_obj2, ast_variable_append(), ast_variable_new(), config_odbc_obj::cat_metric, config_odbc_obj::category, config_odbc_prepare(), last, LOG_NOTICE, LOG_WARNING, RES_ODBC_CONNECTED, config_odbc_obj::sql, config_odbc_obj::var_name, and config_odbc_obj::var_val.
00877 { 00878 struct ast_variable *new_v; 00879 struct ast_category *cur_cat; 00880 int res = 0; 00881 struct odbc_obj *obj; 00882 char sqlbuf[1024] = ""; 00883 char *sql = sqlbuf; 00884 size_t sqlleft = sizeof(sqlbuf); 00885 unsigned int last_cat_metric = 0; 00886 SQLSMALLINT rowcount = 0; 00887 SQLHSTMT stmt; 00888 char last[128] = ""; 00889 struct config_odbc_obj q; 00890 struct ast_flags loader_flags = { 0 }; 00891 struct ast_flags connected_flag = { RES_ODBC_CONNECTED }; 00892 00893 memset(&q, 0, sizeof(q)); 00894 00895 if (!file || !strcmp (file, "res_config_odbc.conf")) 00896 return NULL; /* cant configure myself with myself ! */ 00897 00898 obj = ast_odbc_request_obj2(database, connected_flag); 00899 if (!obj) 00900 return NULL; 00901 00902 ast_build_string(&sql, &sqlleft, "SELECT cat_metric, category, var_name, var_val FROM %s ", table); 00903 ast_build_string(&sql, &sqlleft, "WHERE filename='%s' AND commented=0 ", file); 00904 ast_build_string(&sql, &sqlleft, "ORDER BY cat_metric DESC, var_metric ASC, category, var_name "); 00905 q.sql = sqlbuf; 00906 00907 stmt = ast_odbc_prepare_and_execute(obj, config_odbc_prepare, &q); 00908 00909 if (!stmt) { 00910 ast_log(LOG_WARNING, "SQL select error!\n[%s]\n\n", sql); 00911 ast_odbc_release_obj(obj); 00912 return NULL; 00913 } 00914 00915 res = SQLNumResultCols(stmt, &rowcount); 00916 00917 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00918 ast_log(LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql); 00919 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00920 ast_odbc_release_obj(obj); 00921 return NULL; 00922 } 00923 00924 if (!rowcount) { 00925 ast_log(LOG_NOTICE, "found nothing\n"); 00926 ast_odbc_release_obj(obj); 00927 return cfg; 00928 } 00929 00930 cur_cat = ast_config_get_current_category(cfg); 00931 00932 while ((res = SQLFetch(stmt)) != SQL_NO_DATA) { 00933 if (!strcmp (q.var_name, "#include")) { 00934 if (!ast_config_internal_load(q.var_val, cfg, loader_flags, "", who_asked)) { 00935 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00936 ast_odbc_release_obj(obj); 00937 return NULL; 00938 } 00939 continue; 00940 } 00941 if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) { 00942 cur_cat = ast_category_new(q.category, "", 99999); 00943 if (!cur_cat) { 00944 ast_log(LOG_WARNING, "Out of memory!\n"); 00945 break; 00946 } 00947 strcpy(last, q.category); 00948 last_cat_metric = q.cat_metric; 00949 ast_category_append(cfg, cur_cat); 00950 } 00951 00952 new_v = ast_variable_new(q.var_name, q.var_val, ""); 00953 ast_variable_append(cur_cat, new_v); 00954 } 00955 00956 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00957 ast_odbc_release_obj(obj); 00958 return cfg; 00959 }
| static SQLHSTMT config_odbc_prepare | ( | struct odbc_obj * | obj, | |
| void * | data | |||
| ) | [static] |
Definition at line 849 of file res_config_odbc.c.
References ast_verb, config_odbc_obj::cat_metric, config_odbc_obj::category, odbc_obj::con, config_odbc_obj::err, config_odbc_obj::sql, config_odbc_obj::var_name, and config_odbc_obj::var_val.
Referenced by config_odbc().
00850 { 00851 struct config_odbc_obj *q = data; 00852 SQLHSTMT sth; 00853 int res; 00854 00855 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &sth); 00856 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00857 ast_verb(4, "Failure in AllocStatement %d\n", res); 00858 return NULL; 00859 } 00860 00861 res = SQLPrepare(sth, (unsigned char *)q->sql, SQL_NTS); 00862 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00863 ast_verb(4, "Error in PREPARE %d\n", res); 00864 SQLFreeHandle(SQL_HANDLE_STMT, sth); 00865 return NULL; 00866 } 00867 00868 SQLBindCol(sth, 1, SQL_C_ULONG, &q->cat_metric, sizeof(q->cat_metric), &q->err); 00869 SQLBindCol(sth, 2, SQL_C_CHAR, q->category, sizeof(q->category), &q->err); 00870 SQLBindCol(sth, 3, SQL_C_CHAR, q->var_name, sizeof(q->var_name), &q->err); 00871 SQLBindCol(sth, 4, SQL_C_CHAR, q->var_val, sizeof(q->var_val), &q->err); 00872 00873 return sth; 00874 }
| static SQLHSTMT custom_prepare | ( | struct odbc_obj * | obj, | |
| void * | data | |||
| ) | [static] |
Definition at line 71 of file res_config_odbc.c.
References custom_prepare_struct::ap, ast_debug, ast_log(), ast_string_field_set, ast_strlen_zero(), odbc_obj::con, custom_prepare_struct::encoding, encoding, custom_prepare_struct::extra, LOG_WARNING, custom_prepare_struct::skip, and custom_prepare_struct::sql.
Referenced by destroy_odbc(), realtime_multi_odbc(), realtime_odbc(), store_odbc(), and update_odbc().
00072 { 00073 int res, x = 1, count = 0; 00074 struct custom_prepare_struct *cps = data; 00075 const char *newparam, *newval; 00076 char encodebuf[1024]; 00077 SQLHSTMT stmt; 00078 va_list ap; 00079 00080 va_copy(ap, cps->ap); 00081 00082 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); 00083 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00084 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); 00085 return NULL; 00086 } 00087 00088 ast_debug(1, "Skip: %lld; SQL: %s\n", cps->skip, cps->sql); 00089 00090 res = SQLPrepare(stmt, (unsigned char *)cps->sql, SQL_NTS); 00091 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00092 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", cps->sql); 00093 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00094 return NULL; 00095 } 00096 00097 while ((newparam = va_arg(ap, const char *))) { 00098 newval = va_arg(ap, const char *); 00099 if ((1LL << count++) & cps->skip) { 00100 ast_debug(1, "Skipping field '%s'='%s' (%llo/%llo)\n", newparam, newval, 1LL << (count - 1), cps->skip); 00101 continue; 00102 } 00103 ast_debug(1, "Parameter %d ('%s') = '%s'\n", x, newparam, newval); 00104 if (strchr(newval, ';') || strchr(newval, '^')) { 00105 char *eptr = encodebuf; 00106 const char *vptr = newval; 00107 for (; *vptr && eptr < encodebuf + sizeof(encodebuf); vptr++) { 00108 if (strchr("^;", *vptr)) { 00109 /* We use ^XX, instead of %XX because '%' is a special character in SQL */ 00110 snprintf(eptr, encodebuf + sizeof(encodebuf) - eptr, "^%02hhX", *vptr); 00111 eptr += 3; 00112 vptr++; 00113 } else { 00114 *eptr++ = *vptr++; 00115 } 00116 } 00117 if (eptr < encodebuf + sizeof(encodebuf)) { 00118 *eptr = '\0'; 00119 } else { 00120 encodebuf[sizeof(encodebuf) - 1] = '\0'; 00121 } 00122 ast_string_field_set(cps, encoding[x], encodebuf); 00123 newval = cps->encoding[x]; 00124 } 00125 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL); 00126 } 00127 va_end(ap); 00128 00129 if (!ast_strlen_zero(cps->extra)) 00130 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(cps->extra), 0, (void *)cps->extra, 0, NULL); 00131 return stmt; 00132 }
| static void decode_chunk | ( | char * | chunk | ) | [static] |
Definition at line 61 of file res_config_odbc.c.
Referenced by realtime_multi_odbc(), and realtime_odbc().
00062 { 00063 for (; *chunk; chunk++) { 00064 if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) { 00065 sscanf(chunk + 1, "%02hhX", chunk); 00066 memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1); 00067 } 00068 } 00069 }
| static int destroy_odbc | ( | const char * | database, | |
| const char * | table, | |||
| const char * | keyfield, | |||
| const char * | lookup, | |||
| va_list | ap | |||
| ) | [static] |
Excute an DELETE query.
| database | ||
| table | ||
| keyfield | where clause field | |
| lookup | value of field for where clause | |
| ap | list containing one or more field/value set(s) |
| number | of rows affected | |
| -1 | on failure |
Definition at line 787 of file res_config_odbc.c.
References custom_prepare_struct::ap, ast_log(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_request_obj2, custom_prepare(), LOG_WARNING, RES_ODBC_CONNECTED, and custom_prepare_struct::sql.
00788 { 00789 struct odbc_obj *obj; 00790 SQLHSTMT stmt; 00791 char sql[256]; 00792 SQLLEN rowcount=0; 00793 const char *newparam, *newval; 00794 int res; 00795 va_list aq; 00796 struct custom_prepare_struct cps = { .sql = sql, .extra = lookup }; 00797 struct ast_flags connected_flag = { RES_ODBC_CONNECTED }; 00798 00799 va_copy(cps.ap, ap); 00800 va_copy(aq, ap); 00801 00802 if (!table) 00803 return -1; 00804 00805 obj = ast_odbc_request_obj2(database, connected_flag); 00806 if (!obj) 00807 return -1; 00808 00809 snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE ", table); 00810 while((newparam = va_arg(aq, const char *))) { 00811 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s=? AND ", newparam); 00812 newval = va_arg(aq, const char *); 00813 } 00814 va_end(aq); 00815 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s=?", keyfield); 00816 00817 stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps); 00818 00819 if (!stmt) { 00820 ast_odbc_release_obj(obj); 00821 return -1; 00822 } 00823 00824 res = SQLRowCount(stmt, &rowcount); 00825 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00826 ast_odbc_release_obj(obj); 00827 00828 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00829 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql); 00830 return -1; 00831 } 00832 00833 if (rowcount >= 0) 00834 return (int)rowcount; 00835 00836 return -1; 00837 }
| static int load_module | ( | void | ) | [static] |
Definition at line 1143 of file res_config_odbc.c.
References ast_config_engine_register(), and ast_verb.
01144 { 01145 ast_config_engine_register(&odbc_engine); 01146 ast_verb(1, "res_config_odbc loaded.\n"); 01147 return 0; 01148 }
| static struct ast_config* realtime_multi_odbc | ( | const char * | database, | |
| const char * | table, | |||
| va_list | ap | |||
| ) | [static, read] |
Excute an Select query and return ast_config list.
| database | ||
| table | ||
| ap | list containing one or more field/operator/value set. |
| var | on success | |
| NULL | on failure |
Definition at line 311 of file res_config_odbc.c.
References custom_prepare_struct::ap, ast_category_append(), ast_category_destroy(), ast_category_new(), ast_category_rename(), ast_config_new(), ast_log(), ast_odbc_backslash_is_escape(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_request_obj2, ast_strdupa, ast_string_field_free_memory, ast_string_field_init, ast_strip(), ast_strlen_zero(), ast_variable_append(), ast_variable_new(), custom_prepare(), decode_chunk(), LOG_WARNING, RES_ODBC_CONNECTED, custom_prepare_struct::sql, strcasestr(), strsep(), and var.
00312 { 00313 struct odbc_obj *obj; 00314 SQLHSTMT stmt; 00315 char sql[1024]; 00316 char coltitle[256]; 00317 char rowdata[2048]; 00318 const char *initfield=NULL; 00319 char *op; 00320 const char *newparam, *newval; 00321 char *stringp; 00322 char *chunk; 00323 SQLSMALLINT collen; 00324 int res; 00325 int x; 00326 struct ast_variable *var=NULL; 00327 struct ast_config *cfg=NULL; 00328 struct ast_category *cat=NULL; 00329 struct ast_flags connected_flag = { RES_ODBC_CONNECTED }; 00330 SQLULEN colsize; 00331 SQLSMALLINT colcount=0; 00332 SQLSMALLINT datatype; 00333 SQLSMALLINT decimaldigits; 00334 SQLSMALLINT nullable; 00335 SQLLEN indicator; 00336 struct custom_prepare_struct cps = { .sql = sql }; 00337 va_list aq; 00338 00339 if (!table || ast_string_field_init(&cps, 256)) { 00340 return NULL; 00341 } 00342 va_copy(cps.ap, ap); 00343 va_copy(aq, ap); 00344 00345 00346 obj = ast_odbc_request_obj2(database, connected_flag); 00347 if (!obj) { 00348 ast_string_field_free_memory(&cps); 00349 return NULL; 00350 } 00351 00352 newparam = va_arg(aq, const char *); 00353 if (!newparam) { 00354 ast_odbc_release_obj(obj); 00355 ast_string_field_free_memory(&cps); 00356 return NULL; 00357 } 00358 initfield = ast_strdupa(newparam); 00359 if ((op = strchr(initfield, ' '))) 00360 *op = '\0'; 00361 newval = va_arg(aq, const char *); 00362 op = !strchr(newparam, ' ') ? " =" : ""; 00363 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?%s", table, newparam, op, 00364 strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); 00365 while((newparam = va_arg(aq, const char *))) { 00366 op = !strchr(newparam, ' ') ? " =" : ""; 00367 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?%s", newparam, op, 00368 strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); 00369 newval = va_arg(aq, const char *); 00370 } 00371 if (initfield) 00372 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield); 00373 va_end(aq); 00374 00375 stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps); 00376 00377 if (!stmt) { 00378 ast_odbc_release_obj(obj); 00379 ast_string_field_free_memory(&cps); 00380 return NULL; 00381 } 00382 00383 res = SQLNumResultCols(stmt, &colcount); 00384 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00385 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); 00386 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00387 ast_odbc_release_obj(obj); 00388 ast_string_field_free_memory(&cps); 00389 return NULL; 00390 } 00391 00392 cfg = ast_config_new(); 00393 if (!cfg) { 00394 ast_log(LOG_WARNING, "Out of memory!\n"); 00395 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00396 ast_odbc_release_obj(obj); 00397 ast_string_field_free_memory(&cps); 00398 return NULL; 00399 } 00400 00401 while ((res=SQLFetch(stmt)) != SQL_NO_DATA) { 00402 var = NULL; 00403 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00404 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); 00405 continue; 00406 } 00407 cat = ast_category_new("","",99999); 00408 if (!cat) { 00409 ast_log(LOG_WARNING, "Out of memory!\n"); 00410 continue; 00411 } 00412 for (x=0;x<colcount;x++) { 00413 rowdata[0] = '\0'; 00414 collen = sizeof(coltitle); 00415 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen, 00416 &datatype, &colsize, &decimaldigits, &nullable); 00417 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00418 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql); 00419 ast_category_destroy(cat); 00420 continue; 00421 } 00422 00423 indicator = 0; 00424 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator); 00425 if (indicator == SQL_NULL_DATA) 00426 continue; 00427 00428 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00429 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); 00430 ast_category_destroy(cat); 00431 continue; 00432 } 00433 stringp = rowdata; 00434 while (stringp) { 00435 chunk = strsep(&stringp, ";"); 00436 if (!ast_strlen_zero(ast_strip(chunk))) { 00437 if (strchr(chunk, '^')) { 00438 decode_chunk(chunk); 00439 } 00440 if (initfield && !strcmp(initfield, coltitle)) { 00441 ast_category_rename(cat, chunk); 00442 } 00443 var = ast_variable_new(coltitle, chunk, ""); 00444 ast_variable_append(cat, var); 00445 } 00446 } 00447 } 00448 ast_category_append(cfg, cat); 00449 } 00450 00451 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00452 ast_odbc_release_obj(obj); 00453 ast_string_field_free_memory(&cps); 00454 return cfg; 00455 }
| static struct ast_variable* realtime_odbc | ( | const char * | database, | |
| const char * | table, | |||
| va_list | ap | |||
| ) | [static, read] |
Excute an SQL query and return ast_variable list.
| database | ||
| table | ||
| ap | list containing one or more field/operator/value set. |
| var | on success | |
| NULL | on failure |
Definition at line 147 of file res_config_odbc.c.
References custom_prepare_struct::ap, ast_copy_string(), ast_log(), ast_odbc_backslash_is_escape(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_request_obj2, ast_string_field_free_memory, ast_string_field_init, ast_strip(), ast_strlen_zero(), ast_variable_new(), ast_variables_destroy(), custom_prepare(), decode_chunk(), LOG_ERROR, LOG_WARNING, RES_ODBC_CONNECTED, custom_prepare_struct::sql, strcasestr(), strsep(), and var.
00148 { 00149 struct odbc_obj *obj; 00150 SQLHSTMT stmt; 00151 char sql[1024]; 00152 char coltitle[256]; 00153 char rowdata[2048]; 00154 char *op; 00155 const char *newparam, *newval; 00156 char *stringp; 00157 char *chunk; 00158 SQLSMALLINT collen; 00159 int res; 00160 int x; 00161 struct ast_variable *var=NULL, *prev=NULL; 00162 SQLULEN colsize; 00163 SQLSMALLINT colcount=0; 00164 SQLSMALLINT datatype; 00165 SQLSMALLINT decimaldigits; 00166 SQLSMALLINT nullable; 00167 SQLLEN indicator; 00168 va_list aq; 00169 struct custom_prepare_struct cps = { .sql = sql }; 00170 struct ast_flags connected_flag = { RES_ODBC_CONNECTED }; 00171 00172 if (ast_string_field_init(&cps, 256)) { 00173 return NULL; 00174 } 00175 va_copy(cps.ap, ap); 00176 va_copy(aq, ap); 00177 00178 if (!table) { 00179 ast_string_field_free_memory(&cps); 00180 return NULL; 00181 } 00182 00183 obj = ast_odbc_request_obj2(database, connected_flag); 00184 00185 if (!obj) { 00186 ast_log(LOG_ERROR, "No database handle available with the name of '%s' (check res_odbc.conf)\n", database); 00187 ast_string_field_free_memory(&cps); 00188 return NULL; 00189 } 00190 00191 newparam = va_arg(aq, const char *); 00192 if (!newparam) { 00193 ast_odbc_release_obj(obj); 00194 ast_string_field_free_memory(&cps); 00195 return NULL; 00196 } 00197 newval = va_arg(aq, const char *); 00198 op = !strchr(newparam, ' ') ? " =" : ""; 00199 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?%s", table, newparam, op, 00200 strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); 00201 while((newparam = va_arg(aq, const char *))) { 00202 op = !strchr(newparam, ' ') ? " =" : ""; 00203 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?%s", newparam, op, 00204 strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); 00205 newval = va_arg(aq, const char *); 00206 } 00207 va_end(aq); 00208 00209 stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps); 00210 00211 if (!stmt) { 00212 ast_odbc_release_obj(obj); 00213 ast_string_field_free_memory(&cps); 00214 return NULL; 00215 } 00216 00217 res = SQLNumResultCols(stmt, &colcount); 00218 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00219 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); 00220 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00221 ast_odbc_release_obj(obj); 00222 ast_string_field_free_memory(&cps); 00223 return NULL; 00224 } 00225 00226 res = SQLFetch(stmt); 00227 if (res == SQL_NO_DATA) { 00228 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00229 ast_odbc_release_obj(obj); 00230 ast_string_field_free_memory(&cps); 00231 return NULL; 00232 } 00233 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00234 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); 00235 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00236 ast_odbc_release_obj(obj); 00237 ast_string_field_free_memory(&cps); 00238 return NULL; 00239 } 00240 for (x = 0; x < colcount; x++) { 00241 rowdata[0] = '\0'; 00242 collen = sizeof(coltitle); 00243 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen, 00244 &datatype, &colsize, &decimaldigits, &nullable); 00245 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00246 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql); 00247 if (var) 00248 ast_variables_destroy(var); 00249 ast_odbc_release_obj(obj); 00250 ast_string_field_free_memory(&cps); 00251 return NULL; 00252 } 00253 00254 indicator = 0; 00255 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator); 00256 if (indicator == SQL_NULL_DATA) 00257 rowdata[0] = '\0'; 00258 else if (ast_strlen_zero(rowdata)) { 00259 /* Because we encode the empty string for a NULL, we will encode 00260 * actual empty strings as a string containing a single whitespace. */ 00261 ast_copy_string(rowdata, " ", sizeof(rowdata)); 00262 } 00263 00264 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00265 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); 00266 if (var) 00267 ast_variables_destroy(var); 00268 ast_odbc_release_obj(obj); 00269 return NULL; 00270 } 00271 stringp = rowdata; 00272 while (stringp) { 00273 chunk = strsep(&stringp, ";"); 00274 if (!ast_strlen_zero(ast_strip(chunk))) { 00275 if (strchr(chunk, '^')) { 00276 decode_chunk(chunk); 00277 } 00278 if (prev) { 00279 prev->next = ast_variable_new(coltitle, chunk, ""); 00280 if (prev->next) { 00281 prev = prev->next; 00282 } 00283 } else { 00284 prev = var = ast_variable_new(coltitle, chunk, ""); 00285 } 00286 } 00287 } 00288 } 00289 00290 00291 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00292 ast_odbc_release_obj(obj); 00293 ast_string_field_free_memory(&cps); 00294 return var; 00295 }
| static int reload_module | ( | void | ) | [static] |
| static int require_odbc | ( | const char * | database, | |
| const char * | table, | |||
| va_list | ap | |||
| ) | [static] |
Definition at line 964 of file res_config_odbc.c.
References ast_log(), ast_odbc_find_table(), ast_rq_is_int(), AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CHECK_SIZE, odbc_cache_tables::columns, odbc_cache_columns::list, LOG_WARNING, odbc_cache_columns::name, RQ_CHAR, RQ_DATE, RQ_DATETIME, RQ_FLOAT, RQ_INTEGER1, RQ_INTEGER2, RQ_INTEGER3, RQ_INTEGER4, RQ_INTEGER8, RQ_UINTEGER1, RQ_UINTEGER2, RQ_UINTEGER3, RQ_UINTEGER4, RQ_UINTEGER8, odbc_cache_columns::size, odbc_cache_columns::type, type, warn_length, warn_type, and WARN_TYPE_OR_LENGTH.
00965 { 00966 struct odbc_cache_tables *tableptr = ast_odbc_find_table(database, table); 00967 struct odbc_cache_columns *col; 00968 char *elm; 00969 int type, size; 00970 00971 if (!tableptr) { 00972 return -1; 00973 } 00974 00975 while ((elm = va_arg(ap, char *))) { 00976 type = va_arg(ap, require_type); 00977 size = va_arg(ap, int); 00978 /* Check if the field matches the criteria */ 00979 AST_RWLIST_TRAVERSE(&tableptr->columns, col, list) { 00980 if (strcmp(col->name, elm) == 0) { 00981 /* Type check, first. Some fields are more particular than others */ 00982 switch (col->type) { 00983 case SQL_CHAR: 00984 case SQL_VARCHAR: 00985 case SQL_LONGVARCHAR: 00986 #ifdef HAVE_ODBC_WCHAR 00987 case SQL_WCHAR: 00988 case SQL_WVARCHAR: 00989 case SQL_WLONGVARCHAR: 00990 #endif 00991 case SQL_BINARY: 00992 case SQL_VARBINARY: 00993 case SQL_LONGVARBINARY: 00994 case SQL_GUID: 00995 #define CHECK_SIZE(n) \ 00996 if (col->size < n) { \ 00997 warn_length(col, n); \ 00998 } \ 00999 break; 01000 switch (type) { 01001 case RQ_UINTEGER1: CHECK_SIZE(3) /* 255 */ 01002 case RQ_INTEGER1: CHECK_SIZE(4) /* -128 */ 01003 case RQ_UINTEGER2: CHECK_SIZE(5) /* 65535 */ 01004 case RQ_INTEGER2: CHECK_SIZE(6) /* -32768 */ 01005 case RQ_UINTEGER3: /* 16777215 */ 01006 case RQ_INTEGER3: CHECK_SIZE(8) /* -8388608 */ 01007 case RQ_DATE: /* 2008-06-09 */ 01008 case RQ_UINTEGER4: CHECK_SIZE(10) /* 4200000000 */ 01009 case RQ_INTEGER4: CHECK_SIZE(11) /* -2100000000 */ 01010 case RQ_DATETIME: /* 2008-06-09 16:03:47 */ 01011 case RQ_UINTEGER8: CHECK_SIZE(19) /* trust me */ 01012 case RQ_INTEGER8: CHECK_SIZE(20) /* ditto */ 01013 case RQ_FLOAT: 01014 case RQ_CHAR: CHECK_SIZE(size) 01015 } 01016 #undef CHECK_SIZE 01017 break; 01018 case SQL_TYPE_DATE: 01019 if (type != RQ_DATE) { 01020 warn_type(col, type); 01021 } 01022 break; 01023 case SQL_TYPE_TIMESTAMP: 01024 case SQL_TIMESTAMP: 01025 if (type != RQ_DATE && type != RQ_DATETIME) { 01026 warn_type(col, type); 01027 } 01028 break; 01029 case SQL_BIT: 01030 warn_length(col, size); 01031 break; 01032 #define WARN_TYPE_OR_LENGTH(n) \ 01033 if (!ast_rq_is_int(type)) { \ 01034 warn_type(col, type); \ 01035 } else { \ 01036 warn_length(col, n); \ 01037 } 01038 case SQL_TINYINT: 01039 if (type != RQ_UINTEGER1) { 01040 WARN_TYPE_OR_LENGTH(size) 01041 } 01042 break; 01043 case SQL_C_STINYINT: 01044 if (type != RQ_INTEGER1) { 01045 WARN_TYPE_OR_LENGTH(size) 01046 } 01047 break; 01048 case SQL_C_USHORT: 01049 if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && type != RQ_UINTEGER2) { 01050 WARN_TYPE_OR_LENGTH(size) 01051 } 01052 break; 01053 case SQL_SMALLINT: 01054 case SQL_C_SSHORT: 01055 if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && type != RQ_INTEGER2) { 01056 WARN_TYPE_OR_LENGTH(size) 01057 } 01058 break; 01059 case SQL_C_ULONG: 01060 if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && 01061 type != RQ_UINTEGER2 && type != RQ_INTEGER2 && 01062 type != RQ_UINTEGER3 && type != RQ_INTEGER3 && 01063 type != RQ_INTEGER4) { 01064 WARN_TYPE_OR_LENGTH(size) 01065 } 01066 break; 01067 case SQL_INTEGER: 01068 case SQL_C_SLONG: 01069 if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && 01070 type != RQ_UINTEGER2 && type != RQ_INTEGER2 && 01071 type != RQ_UINTEGER3 && type != RQ_INTEGER3 && 01072 type != RQ_INTEGER4) { 01073 WARN_TYPE_OR_LENGTH(size) 01074 } 01075 break; 01076 case SQL_C_UBIGINT: 01077 if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && 01078 type != RQ_UINTEGER2 && type != RQ_INTEGER2 && 01079 type != RQ_UINTEGER3 && type != RQ_INTEGER3 && 01080 type != RQ_UINTEGER4 && type != RQ_INTEGER4 && 01081 type != RQ_INTEGER8) { 01082 WARN_TYPE_OR_LENGTH(size) 01083 } 01084 break; 01085 case SQL_BIGINT: 01086 case SQL_C_SBIGINT: 01087 if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && 01088 type != RQ_UINTEGER2 && type != RQ_INTEGER2 && 01089 type != RQ_UINTEGER3 && type != RQ_INTEGER3 && 01090 type != RQ_UINTEGER4 && type != RQ_INTEGER4 && 01091 type != RQ_INTEGER8) { 01092 WARN_TYPE_OR_LENGTH(size) 01093 } 01094 break; 01095 #undef WARN_TYPE_OR_LENGTH 01096 case SQL_NUMERIC: 01097 case SQL_DECIMAL: 01098 case SQL_FLOAT: 01099 case SQL_REAL: 01100 case SQL_DOUBLE: 01101 if (!ast_rq_is_int(type) && type != RQ_FLOAT) { 01102 warn_type(col, type); 01103 } 01104 break; 01105 default: 01106 ast_log(LOG_WARNING, "Realtime table %s@%s: column type (%d) unrecognized for column '%s'\n", table, database, col->type, elm); 01107 } 01108 break; 01109 } 01110 } 01111 if (!col) { 01112 ast_log(LOG_WARNING, "Realtime table %s@%s requires column '%s', but that column does not exist!\n", table, database, elm); 01113 } 01114 } 01115 va_end(ap); 01116 AST_RWLIST_UNLOCK(&tableptr->columns); 01117 return 0; 01118 }
| static int store_odbc | ( | const char * | database, | |
| const char * | table, | |||
| va_list | ap | |||
| ) | [static] |
Excute an INSERT query.
| database | ||
| table | ||
| ap | list containing one or more field/value set(s) |
| number | of rows affected | |
| -1 | on failure |
Definition at line 710 of file res_config_odbc.c.
References custom_prepare_struct::ap, ast_copy_string(), ast_log(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_request_obj2, custom_prepare(), LOG_WARNING, RES_ODBC_CONNECTED, and custom_prepare_struct::sql.
00711 { 00712 struct odbc_obj *obj; 00713 SQLHSTMT stmt; 00714 char sql[256]; 00715 char keys[256]; 00716 char vals[256]; 00717 SQLLEN rowcount=0; 00718 const char *newparam, *newval; 00719 int res; 00720 va_list aq; 00721 struct custom_prepare_struct cps = { .sql = sql, .extra = NULL }; 00722 struct ast_flags connected_flag = { RES_ODBC_CONNECTED }; 00723 00724 va_copy(cps.ap, ap); 00725 va_copy(aq, ap); 00726 00727 if (!table) 00728 return -1; 00729 00730 obj = ast_odbc_request_obj2(database, connected_flag); 00731 if (!obj) 00732 return -1; 00733 00734 newparam = va_arg(aq, const char *); 00735 if (!newparam) { 00736 ast_odbc_release_obj(obj); 00737 return -1; 00738 } 00739 newval = va_arg(aq, const char *); 00740 snprintf(keys, sizeof(keys), "%s", newparam); 00741 ast_copy_string(vals, "?", sizeof(vals)); 00742 while ((newparam = va_arg(aq, const char *))) { 00743 snprintf(keys + strlen(keys), sizeof(keys) - strlen(keys), ", %s", newparam); 00744 snprintf(vals + strlen(vals), sizeof(vals) - strlen(vals), ", ?"); 00745 newval = va_arg(aq, const char *); 00746 } 00747 va_end(aq); 00748 snprintf(sql, sizeof(sql), "INSERT INTO %s (%s) VALUES (%s)", table, keys, vals); 00749 00750 stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps); 00751 00752 if (!stmt) { 00753 ast_odbc_release_obj(obj); 00754 return -1; 00755 } 00756 00757 res = SQLRowCount(stmt, &rowcount); 00758 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00759 ast_odbc_release_obj(obj); 00760 00761 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00762 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql); 00763 return -1; 00764 } 00765 00766 if (rowcount >= 0) 00767 return (int)rowcount; 00768 00769 return -1; 00770 }
| static int unload_module | ( | void | ) | [static] |
Definition at line 1135 of file res_config_odbc.c.
References ast_config_engine_deregister(), and ast_verb.
01136 { 01137 ast_config_engine_deregister(&odbc_engine); 01138 01139 ast_verb(1, "res_config_odbc unloaded.\n"); 01140 return 0; 01141 }
| static int update2_odbc | ( | const char * | database, | |
| const char * | table, | |||
| va_list | ap | |||
| ) | [static] |
Execute an UPDATE query.
| database | ||
| table | ||
| ap | list containing one or more field/value set(s). |
| number | of rows affected | |
| -1 | on failure |
Definition at line 659 of file res_config_odbc.c.
References update2_prepare_struct::ap, ast_log(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_request_obj, ast_str_buffer(), ast_str_thread_get(), update2_prepare_struct::database, LOG_WARNING, sql_buf, and update2_prepare().
00660 { 00661 struct odbc_obj *obj; 00662 SQLHSTMT stmt; 00663 struct update2_prepare_struct ups = { .database = database, .table = table, }; 00664 struct ast_str *sql; 00665 int res; 00666 SQLLEN rowcount = 0; 00667 00668 va_copy(ups.ap, ap); 00669 00670 if (!(obj = ast_odbc_request_obj(database, 0))) { 00671 return -1; 00672 } 00673 00674 if (!(stmt = ast_odbc_prepare_and_execute(obj, update2_prepare, &ups))) { 00675 ast_odbc_release_obj(obj); 00676 return -1; 00677 } 00678 00679 res = SQLRowCount(stmt, &rowcount); 00680 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00681 ast_odbc_release_obj(obj); 00682 00683 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00684 /* Since only a single thread can access this memory, we can retrieve what would otherwise be lost. */ 00685 sql = ast_str_thread_get(&sql_buf, 16); 00686 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n", ast_str_buffer(sql)); 00687 return -1; 00688 } 00689 00690 if (rowcount >= 0) { 00691 return (int)rowcount; 00692 } 00693 00694 return -1; 00695 }
| static SQLHSTMT update2_prepare | ( | struct odbc_obj * | obj, | |
| void * | data | |||
| ) | [static] |
Definition at line 562 of file res_config_odbc.c.
References update2_prepare_struct::ap, ast_log(), ast_odbc_find_column(), ast_odbc_find_table(), ast_odbc_release_table, ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_thread_get(), odbc_obj::con, update2_prepare_struct::database, first, LOG_ERROR, LOG_NOTICE, LOG_WARNING, sql_buf, and update2_prepare_struct::table.
Referenced by update2_odbc().
00563 { 00564 int res, x = 1, first = 1; 00565 struct update2_prepare_struct *ups = data; 00566 const char *newparam, *newval; 00567 struct ast_str *sql = ast_str_thread_get(&sql_buf, 16); 00568 SQLHSTMT stmt; 00569 va_list ap; 00570 struct odbc_cache_tables *tableptr = ast_odbc_find_table(ups->database, ups->table); 00571 struct odbc_cache_columns *column; 00572 00573 if (!sql) { 00574 if (tableptr) { 00575 ast_odbc_release_table(tableptr); 00576 } 00577 return NULL; 00578 } 00579 00580 if (!tableptr) { 00581 ast_log(LOG_ERROR, "Could not retrieve metadata for table '%s@%s'. Update will fail!\n", ups->table, ups->database); 00582 return NULL; 00583 } 00584 00585 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); 00586 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00587 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); 00588 ast_odbc_release_table(tableptr); 00589 return NULL; 00590 } 00591 00592 ast_str_set(&sql, 0, "UPDATE %s SET ", ups->table); 00593 00594 /* Start by finding the second set of parameters */ 00595 va_copy(ap, ups->ap); 00596 00597 while ((newparam = va_arg(ap, const char *))) { 00598 newval = va_arg(ap, const char *); 00599 } 00600 00601 while ((newparam = va_arg(ap, const char *))) { 00602 newval = va_arg(ap, const char *); 00603 if ((column = ast_odbc_find_column(tableptr, newparam))) { 00604 ast_str_append(&sql, 0, "%s%s=? ", first ? "" : ", ", newparam); 00605 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL); 00606 first = 0; 00607 } else { 00608 ast_log(LOG_NOTICE, "Not updating column '%s' in '%s@%s' because that column does not exist!\n", newparam, ups->table, ups->database); 00609 } 00610 } 00611 va_end(ap); 00612 00613 /* Restart search, because we need to add the search parameters */ 00614 va_copy(ap, ups->ap); 00615 ast_str_append(&sql, 0, "WHERE"); 00616 first = 1; 00617 00618 while ((newparam = va_arg(ap, const char *))) { 00619 newval = va_arg(ap, const char *); 00620 if (!(column = ast_odbc_find_column(tableptr, newparam))) { 00621 ast_log(LOG_ERROR, "One or more of the criteria columns '%s' on '%s@%s' for this update does not exist!\n", newparam, ups->table, ups->database); 00622 ast_odbc_release_table(tableptr); 00623 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00624 return NULL; 00625 } 00626 ast_str_append(&sql, 0, "%s %s=?", first ? "" : " AND", newparam); 00627 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL); 00628 first = 0; 00629 } 00630 va_end(ap); 00631 00632 /* Done with the table metadata */ 00633 ast_odbc_release_table(tableptr); 00634 00635 res = SQLPrepare(stmt, (unsigned char *)ast_str_buffer(sql), SQL_NTS); 00636 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00637 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", ast_str_buffer(sql)); 00638 SQLFreeHandle(SQL_HANDLE_STMT, stmt); 00639 return NULL; 00640 } 00641 00642 return stmt; 00643 }
| static int update_odbc | ( | const char * | database, | |
| const char * | table, | |||
| const char * | keyfield, | |||
| const char * | lookup, | |||
| va_list | ap | |||
| ) | [static] |
Excute an UPDATE query.
| database | ||
| table | ||
| keyfield | where clause field | |
| lookup | value of field for where clause | |
| ap | list containing one or more field/value set(s). |
| number | of rows affected | |
| -1 | on failure |
Definition at line 472 of file res_config_odbc.c.
References custom_prepare_struct::ap, ast_log(), ast_odbc_find_column(), ast_odbc_find_table(), ast_odbc_prepare_and_execute(), ast_odbc_release_obj(), ast_odbc_release_table, ast_odbc_request_obj2, ast_string_field_free_memory, ast_string_field_init, custom_prepare(), LOG_WARNING, RES_ODBC_CONNECTED, custom_prepare_struct::skip, and custom_prepare_struct::sql.
00473 { 00474 struct odbc_obj *obj; 00475 SQLHSTMT stmt; 00476 char sql[256]; 00477 SQLLEN rowcount=0; 00478 const char *newparam, *newval; 00479 int res, count = 1; 00480 va_list aq; 00481 struct custom_prepare_struct cps = { .sql = sql, .extra = lookup }; 00482 struct odbc_cache_tables *tableptr; 00483 struct odbc_cache_columns *column; 00484 struct ast_flags connected_flag = { RES_ODBC_CONNECTED }; 00485 00486 if (!table) { 00487 return -1; 00488 } 00489 00490 va_copy(cps.ap, ap); 00491 va_copy(aq, ap); 00492 00493 if (ast_string_field_init(&cps, 256)) { 00494 return -1; 00495 } 00496 00497 tableptr = ast_odbc_find_table(database, table); 00498 if (!(obj = ast_odbc_request_obj2(database, connected_flag))) { 00499 ast_odbc_release_table(tableptr); 00500 ast_string_field_free_memory(&cps); 00501 return -1; 00502 } 00503 00504 newparam = va_arg(aq, const char *); 00505 if (!newparam) { 00506 ast_odbc_release_obj(obj); 00507 ast_odbc_release_table(tableptr); 00508 ast_string_field_free_memory(&cps); 00509 return -1; 00510 } 00511 newval = va_arg(aq, const char *); 00512 00513 if (tableptr && !(column = ast_odbc_find_column(tableptr, newparam))) { 00514 ast_log(LOG_WARNING, "Key field '%s' does not exist in table '%s@%s'. Update will fail\n", newparam, table, database); 00515 } 00516 00517 snprintf(sql, sizeof(sql), "UPDATE %s SET %s=?", table, newparam); 00518 while((newparam = va_arg(aq, const char *))) { 00519 newval = va_arg(aq, const char *); 00520 if ((tableptr && (column = ast_odbc_find_column(tableptr, newparam))) || count > 63) { 00521 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s=?", newparam); 00522 } else { /* the column does not exist in the table */ 00523 cps.skip |= (1LL << count); 00524 } 00525 count++; 00526 } 00527 va_end(aq); 00528 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s=?", keyfield); 00529 ast_odbc_release_table(tableptr); 00530 00531 stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps); 00532 00533 if (!stmt) { 00534 ast_odbc_release_obj(obj); 00535 ast_string_field_free_memory(&cps); 00536 return -1; 00537 } 00538 00539 res = SQLRowCount(stmt, &rowcount); 00540 SQLFreeHandle (SQL_HANDLE_STMT, stmt); 00541 ast_odbc_release_obj(obj); 00542 ast_string_field_free_memory(&cps); 00543 00544 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { 00545 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql); 00546 return -1; 00547 } 00548 00549 if (rowcount >= 0) { 00550 return (int) rowcount; 00551 } 00552 00553 return -1; 00554 }
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime ODBC 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, .reload = reload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, } [static] |
Definition at line 1160 of file res_config_odbc.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 1160 of file res_config_odbc.c.
struct ast_config_engine odbc_engine [static] |
Definition at line 1122 of file res_config_odbc.c.
struct ast_threadstorage sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , } [static] |
Definition at line 49 of file res_config_odbc.c.
Referenced by update2_odbc(), and update2_prepare().
1.5.9