00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "asterisk.h"
00036
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 283882 $")
00038
00039 #include "asterisk/file.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/lock.h"
00045 #include "asterisk/res_odbc.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/stringfields.h"
00048
00049 AST_THREADSTORAGE(sql_buf);
00050
00051 struct custom_prepare_struct {
00052 const char *sql;
00053 const char *extra;
00054 AST_DECLARE_STRING_FIELDS(
00055 AST_STRING_FIELD(encoding)[256];
00056 );
00057 va_list ap;
00058 unsigned long long skip;
00059 };
00060
00061 static void decode_chunk(char *chunk)
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 }
00070
00071 static SQLHSTMT custom_prepare(struct odbc_obj *obj, void *data)
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
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 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap)
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
00260
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 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
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 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
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 {
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 }
00555
00556 struct update2_prepare_struct {
00557 const char *database;
00558 const char *table;
00559 va_list ap;
00560 };
00561
00562 static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)
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
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
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
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 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 static int update2_odbc(const char *database, const char *table, va_list ap)
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
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 }
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 static int store_odbc(const char *database, const char *table, va_list ap)
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 }
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 static int destroy_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
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 }
00838
00839
00840 struct config_odbc_obj {
00841 char *sql;
00842 unsigned long cat_metric;
00843 char category[128];
00844 char var_name[128];
00845 char var_val[1024];
00846 SQLLEN err;
00847 };
00848
00849 static SQLHSTMT config_odbc_prepare(struct odbc_obj *obj, void *data)
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 }
00875
00876 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)
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;
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 }
00960
00961 #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)
00962 #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)
00963
00964 static int require_odbc(const char *database, const char *table, va_list ap)
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
00979 AST_RWLIST_TRAVERSE(&tableptr->columns, col, list) {
00980 if (strcmp(col->name, elm) == 0) {
00981
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)
01002 case RQ_INTEGER1: CHECK_SIZE(4)
01003 case RQ_UINTEGER2: CHECK_SIZE(5)
01004 case RQ_INTEGER2: CHECK_SIZE(6)
01005 case RQ_UINTEGER3:
01006 case RQ_INTEGER3: CHECK_SIZE(8)
01007 case RQ_DATE:
01008 case RQ_UINTEGER4: CHECK_SIZE(10)
01009 case RQ_INTEGER4: CHECK_SIZE(11)
01010 case RQ_DATETIME:
01011 case RQ_UINTEGER8: CHECK_SIZE(19)
01012 case RQ_INTEGER8: CHECK_SIZE(20)
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 }
01119 #undef warn_length
01120 #undef warn_type
01121
01122 static struct ast_config_engine odbc_engine = {
01123 .name = "odbc",
01124 .load_func = config_odbc,
01125 .realtime_func = realtime_odbc,
01126 .realtime_multi_func = realtime_multi_odbc,
01127 .store_func = store_odbc,
01128 .destroy_func = destroy_odbc,
01129 .update_func = update_odbc,
01130 .update2_func = update2_odbc,
01131 .require_func = require_odbc,
01132 .unload_func = ast_odbc_clear_cache,
01133 };
01134
01135 static int unload_module (void)
01136 {
01137 ast_config_engine_deregister(&odbc_engine);
01138
01139 ast_verb(1, "res_config_odbc unloaded.\n");
01140 return 0;
01141 }
01142
01143 static int load_module (void)
01144 {
01145 ast_config_engine_register(&odbc_engine);
01146 ast_verb(1, "res_config_odbc loaded.\n");
01147 return 0;
01148 }
01149
01150 static int reload_module(void)
01151 {
01152 return 0;
01153 }
01154
01155 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Realtime ODBC configuration",
01156 .load = load_module,
01157 .unload = unload_module,
01158 .reload = reload_module,
01159 .load_pri = AST_MODPRI_REALTIME_DRIVER,
01160 );