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 #include "asterisk.h"
00032
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 283319 $")
00034
00035 #include <sys/types.h>
00036 #include <time.h>
00037
00038 #include <sql.h>
00039 #include <sqlext.h>
00040 #include <sqltypes.h>
00041
00042 #include "asterisk/config.h"
00043 #include "asterisk/channel.h"
00044 #include "asterisk/lock.h"
00045 #include "asterisk/linkedlists.h"
00046 #include "asterisk/res_odbc.h"
00047 #include "asterisk/cdr.h"
00048 #include "asterisk/module.h"
00049
00050 #define CONFIG "cdr_adaptive_odbc.conf"
00051
00052 static const char name[] = "Adaptive ODBC";
00053
00054 static int maxsize = 512, maxsize2 = 512;
00055
00056 struct columns {
00057 char *name;
00058 char *cdrname;
00059 char *filtervalue;
00060 char *staticvalue;
00061 SQLSMALLINT type;
00062 SQLINTEGER size;
00063 SQLSMALLINT decimals;
00064 SQLSMALLINT radix;
00065 SQLSMALLINT nullable;
00066 SQLINTEGER octetlen;
00067 AST_LIST_ENTRY(columns) list;
00068 };
00069
00070 struct tables {
00071 char *connection;
00072 char *table;
00073 unsigned int usegmtime:1;
00074 AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
00075 AST_RWLIST_ENTRY(tables) list;
00076 };
00077
00078 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
00079
00080 static int load_config(void)
00081 {
00082 struct ast_config *cfg;
00083 struct ast_variable *var;
00084 const char *tmp, *catg;
00085 struct tables *tableptr;
00086 struct columns *entry;
00087 struct odbc_obj *obj;
00088 char columnname[80];
00089 char connection[40];
00090 char table[40];
00091 int lenconnection, lentable, usegmtime = 0;
00092 SQLLEN sqlptr;
00093 int res = 0;
00094 SQLHSTMT stmt = NULL;
00095 struct ast_flags config_flags = { 0 };
00096
00097 cfg = ast_config_load(CONFIG, config_flags);
00098 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
00099 ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
00100 return -1;
00101 }
00102
00103 for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
00104 var = ast_variable_browse(cfg, catg);
00105 if (!var)
00106 continue;
00107
00108 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
00109 ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
00110 continue;
00111 }
00112 ast_copy_string(connection, tmp, sizeof(connection));
00113 lenconnection = strlen(connection);
00114
00115 if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) {
00116 usegmtime = ast_true(tmp);
00117 }
00118
00119
00120 obj = ast_odbc_request_obj(connection, 1);
00121 if (!obj) {
00122 ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
00123 continue;
00124 }
00125
00126 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
00127 ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
00128 tmp = "cdr";
00129 }
00130 ast_copy_string(table, tmp, sizeof(table));
00131 lentable = strlen(table);
00132
00133 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
00134 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00135 ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
00136 ast_odbc_release_obj(obj);
00137 continue;
00138 }
00139
00140 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
00141 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00142 ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
00143 ast_odbc_release_obj(obj);
00144 continue;
00145 }
00146
00147 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
00148 if (!tableptr) {
00149 ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
00150 ast_odbc_release_obj(obj);
00151 res = -1;
00152 break;
00153 }
00154
00155 tableptr->usegmtime = usegmtime;
00156 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
00157 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
00158 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
00159 ast_copy_string(tableptr->table, table, lentable + 1);
00160
00161 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
00162
00163
00164 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
00165 if (strncmp(var->name, "filter", 6) == 0) {
00166 char *cdrvar = ast_strdupa(var->name + 6);
00167 cdrvar = ast_strip(cdrvar);
00168 ast_verb(3, "Found filter %s for cdr variable %s in %s@%s\n", var->value, cdrvar, tableptr->table, tableptr->connection);
00169
00170 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(cdrvar) + 1 + strlen(var->value) + 1);
00171 if (!entry) {
00172 ast_log(LOG_ERROR, "Out of memory creating filter entry for CDR variable '%s' in table '%s' on connection '%s'\n", cdrvar, table, connection);
00173 res = -1;
00174 break;
00175 }
00176
00177
00178 entry->name = NULL;
00179 entry->cdrname = (char *)entry + sizeof(*entry);
00180 entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(cdrvar) + 1;
00181 strcpy(entry->cdrname, cdrvar);
00182 strcpy(entry->filtervalue, var->value);
00183
00184 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
00185 }
00186 }
00187
00188 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
00189 char *cdrvar = "", *staticvalue = "";
00190
00191 SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
00192
00193
00194
00195
00196
00197
00198
00199 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
00200 if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) {
00201 char *alias = ast_strdupa(var->name + 5);
00202 cdrvar = ast_strip(alias);
00203 ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
00204 break;
00205 } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) {
00206 char *item = ast_strdupa(var->name + 6);
00207 item = ast_strip(item);
00208 if (item[0] == '"' && item[strlen(item) - 1] == '"') {
00209
00210 item[strlen(item) - 1] = '\0';
00211 item++;
00212 }
00213 staticvalue = item;
00214 }
00215 }
00216
00217 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1);
00218 if (!entry) {
00219 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
00220 res = -1;
00221 break;
00222 }
00223 entry->name = (char *)entry + sizeof(*entry);
00224 strcpy(entry->name, columnname);
00225
00226 if (!ast_strlen_zero(cdrvar)) {
00227 entry->cdrname = entry->name + strlen(columnname) + 1;
00228 strcpy(entry->cdrname, cdrvar);
00229 } else {
00230 entry->cdrname = (char *)entry + sizeof(*entry);
00231 }
00232
00233 if (!ast_strlen_zero(staticvalue)) {
00234 entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
00235 strcpy(entry->staticvalue, staticvalue);
00236 }
00237
00238 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
00239 SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
00240 SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
00241 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
00242 SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
00243 SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
00244
00245
00246
00247
00248 if (entry->octetlen == 0)
00249 entry->octetlen = entry->size;
00250
00251 ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
00252
00253 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
00254 res = 0;
00255 }
00256
00257 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00258 ast_odbc_release_obj(obj);
00259
00260 if (AST_LIST_FIRST(&(tableptr->columns)))
00261 AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
00262 else
00263 ast_free(tableptr);
00264 }
00265 return res;
00266 }
00267
00268 static int free_config(void)
00269 {
00270 struct tables *table;
00271 struct columns *entry;
00272 while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
00273 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
00274 ast_free(entry);
00275 }
00276 ast_free(table);
00277 }
00278 return 0;
00279 }
00280
00281 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
00282 {
00283 int res, i;
00284 SQLHSTMT stmt;
00285 SQLINTEGER nativeerror = 0, numfields = 0;
00286 SQLSMALLINT diagbytes = 0;
00287 unsigned char state[10], diagnostic[256];
00288
00289 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
00290 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00291 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
00292 return NULL;
00293 }
00294
00295 res = SQLPrepare(stmt, (unsigned char *) data, SQL_NTS);
00296 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00297 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", (char *) data);
00298 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
00299 for (i = 0; i < numfields; i++) {
00300 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
00301 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
00302 if (i > 10) {
00303 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
00304 break;
00305 }
00306 }
00307 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
00308 return NULL;
00309 }
00310
00311 return stmt;
00312 }
00313
00314 #define LENGTHEN_BUF1(size) \
00315 do { \
00316 \
00317 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
00318 if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 1) / 512 + 1) * 512) != 0) { \
00319 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
00320 ast_free(sql); \
00321 ast_free(sql2); \
00322 AST_RWLIST_UNLOCK(&odbc_tables); \
00323 return -1; \
00324 } \
00325 } \
00326 } while (0)
00327
00328 #define LENGTHEN_BUF2(size) \
00329 do { \
00330 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
00331 if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
00332 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
00333 ast_free(sql); \
00334 ast_free(sql2); \
00335 AST_RWLIST_UNLOCK(&odbc_tables); \
00336 return -1; \
00337 } \
00338 } \
00339 } while (0)
00340
00341 static int odbc_log(struct ast_cdr *cdr)
00342 {
00343 struct tables *tableptr;
00344 struct columns *entry;
00345 struct odbc_obj *obj;
00346 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
00347 char *tmp;
00348 char colbuf[1024], *colptr;
00349 SQLHSTMT stmt = NULL;
00350 SQLLEN rows = 0;
00351
00352 if (!sql || !sql2) {
00353 if (sql)
00354 ast_free(sql);
00355 if (sql2)
00356 ast_free(sql2);
00357 return -1;
00358 }
00359
00360 if (AST_RWLIST_RDLOCK(&odbc_tables)) {
00361 ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
00362 ast_free(sql);
00363 ast_free(sql2);
00364 return -1;
00365 }
00366
00367 AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
00368 int first = 1;
00369 ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
00370 ast_str_set(&sql2, 0, " VALUES (");
00371
00372
00373 if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
00374 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
00375 continue;
00376 }
00377
00378 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
00379 int datefield = 0;
00380 if (strcasecmp(entry->cdrname, "start") == 0) {
00381 datefield = 1;
00382 } else if (strcasecmp(entry->cdrname, "answer") == 0) {
00383 datefield = 2;
00384 } else if (strcasecmp(entry->cdrname, "end") == 0) {
00385 datefield = 3;
00386 }
00387
00388
00389 if (entry->staticvalue) {
00390 colptr = ast_strdupa(entry->staticvalue);
00391 } else if (datefield && tableptr->usegmtime) {
00392 struct timeval date_tv = (datefield == 1) ? cdr->start : (datefield == 2) ? cdr->answer : cdr->end;
00393 struct ast_tm tm = { 0, };
00394 ast_localtime(&date_tv, &tm, "UTC");
00395 ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S", &tm);
00396 colptr = colbuf;
00397 } else {
00398 ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0, datefield ? 0 : 1);
00399 }
00400
00401 if (colptr) {
00402
00403
00404
00405
00406 if (entry->filtervalue && strcasecmp(colptr, entry->filtervalue) != 0) {
00407 ast_verb(4, "CDR column '%s' with value '%s' does not match filter of"
00408 " '%s'. Cancelling this CDR.\n",
00409 entry->cdrname, colptr, entry->filtervalue);
00410 goto early_release;
00411 }
00412
00413
00414 if (ast_strlen_zero(entry->name))
00415 continue;
00416
00417 LENGTHEN_BUF1(strlen(entry->name));
00418
00419 switch (entry->type) {
00420 case SQL_CHAR:
00421 case SQL_VARCHAR:
00422 case SQL_LONGVARCHAR:
00423 case SQL_BINARY:
00424 case SQL_VARBINARY:
00425 case SQL_LONGVARBINARY:
00426 case SQL_GUID:
00427
00428
00429
00430 if (strcasecmp(entry->name, "disposition") == 0) {
00431 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
00432 } else if (strcasecmp(entry->name, "amaflags") == 0) {
00433 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
00434 }
00435
00436
00437 if (entry->type != SQL_GUID) {
00438 if (strlen(colptr) > entry->octetlen) {
00439 colptr[entry->octetlen] = '\0';
00440 }
00441 }
00442
00443 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00444 LENGTHEN_BUF2(strlen(colptr));
00445
00446
00447 ast_str_append(&sql2, 0, "%s'", first ? "" : ",");
00448 for (tmp = colptr; *tmp; tmp++) {
00449 if (*tmp == '\'') {
00450 ast_str_append(&sql2, 0, "''");
00451 } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
00452 ast_str_append(&sql2, 0, "\\\\");
00453 } else {
00454 ast_str_append(&sql2, 0, "%c", *tmp);
00455 }
00456 }
00457 ast_str_append(&sql2, 0, "'");
00458 break;
00459 case SQL_TYPE_DATE:
00460 if (ast_strlen_zero(colptr)) {
00461 continue;
00462 } else {
00463 int year = 0, month = 0, day = 0;
00464 if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 ||
00465 month <= 0 || month > 12 || day < 0 || day > 31 ||
00466 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
00467 (month == 2 && year % 400 == 0 && day > 29) ||
00468 (month == 2 && year % 100 == 0 && day > 28) ||
00469 (month == 2 && year % 4 == 0 && day > 29) ||
00470 (month == 2 && year % 4 != 0 && day > 28)) {
00471 ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
00472 continue;
00473 }
00474
00475 if (year > 0 && year < 100) {
00476 year += 2000;
00477 }
00478
00479 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00480 LENGTHEN_BUF2(17);
00481 ast_str_append(&sql2, 0, "%s{ d '%04d-%02d-%02d' }", first ? "" : ",", year, month, day);
00482 }
00483 break;
00484 case SQL_TYPE_TIME:
00485 if (ast_strlen_zero(colptr)) {
00486 continue;
00487 } else {
00488 int hour = 0, minute = 0, second = 0;
00489 int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second);
00490
00491 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
00492 ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
00493 continue;
00494 }
00495
00496 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00497 LENGTHEN_BUF2(15);
00498 ast_str_append(&sql2, 0, "%s{ t '%02d:%02d:%02d' }", first ? "" : ",", hour, minute, second);
00499 }
00500 break;
00501 case SQL_TYPE_TIMESTAMP:
00502 case SQL_TIMESTAMP:
00503 if (ast_strlen_zero(colptr)) {
00504 continue;
00505 } else {
00506 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
00507 int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second);
00508
00509 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
00510 month <= 0 || month > 12 || day < 0 || day > 31 ||
00511 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
00512 (month == 2 && year % 400 == 0 && day > 29) ||
00513 (month == 2 && year % 100 == 0 && day > 28) ||
00514 (month == 2 && year % 4 == 0 && day > 29) ||
00515 (month == 2 && year % 4 != 0 && day > 28) ||
00516 hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
00517 ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
00518 continue;
00519 }
00520
00521 if (year > 0 && year < 100) {
00522 year += 2000;
00523 }
00524
00525 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00526 LENGTHEN_BUF2(26);
00527 ast_str_append(&sql2, 0, "%s{ ts '%04d-%02d-%02d %02d:%02d:%02d' }", first ? "" : ",", year, month, day, hour, minute, second);
00528 }
00529 break;
00530 case SQL_INTEGER:
00531 if (ast_strlen_zero(colptr)) {
00532 continue;
00533 } else {
00534 int integer = 0;
00535 if (sscanf(colptr, "%30d", &integer) != 1) {
00536 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
00537 continue;
00538 }
00539
00540 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00541 LENGTHEN_BUF2(12);
00542 ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
00543 }
00544 break;
00545 case SQL_BIGINT:
00546 if (ast_strlen_zero(colptr)) {
00547 continue;
00548 } else {
00549 long long integer = 0;
00550 if (sscanf(colptr, "%30lld", &integer) != 1) {
00551 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
00552 continue;
00553 }
00554
00555 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00556 LENGTHEN_BUF2(24);
00557 ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", integer);
00558 }
00559 break;
00560 case SQL_SMALLINT:
00561 if (ast_strlen_zero(colptr)) {
00562 continue;
00563 } else {
00564 short integer = 0;
00565 if (sscanf(colptr, "%30hd", &integer) != 1) {
00566 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
00567 continue;
00568 }
00569
00570 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00571 LENGTHEN_BUF2(6);
00572 ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
00573 }
00574 break;
00575 case SQL_TINYINT:
00576 if (ast_strlen_zero(colptr)) {
00577 continue;
00578 } else {
00579 char integer = 0;
00580 if (sscanf(colptr, "%30hhd", &integer) != 1) {
00581 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
00582 continue;
00583 }
00584
00585 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00586 LENGTHEN_BUF2(4);
00587 ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
00588 }
00589 break;
00590 case SQL_BIT:
00591 if (ast_strlen_zero(colptr)) {
00592 continue;
00593 } else {
00594 char integer = 0;
00595 if (sscanf(colptr, "%30hhd", &integer) != 1) {
00596 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
00597 continue;
00598 }
00599 if (integer != 0)
00600 integer = 1;
00601
00602 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00603 LENGTHEN_BUF2(2);
00604 ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
00605 }
00606 break;
00607 case SQL_NUMERIC:
00608 case SQL_DECIMAL:
00609 if (ast_strlen_zero(colptr)) {
00610 continue;
00611 } else {
00612 double number = 0.0;
00613
00614 if (!strcasecmp(entry->cdrname, "billsec")) {
00615 if (!ast_tvzero(cdr->answer)) {
00616 snprintf(colbuf, sizeof(colbuf), "%lf",
00617 (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
00618 } else {
00619 ast_copy_string(colbuf, "0", sizeof(colbuf));
00620 }
00621 } else if (!strcasecmp(entry->cdrname, "duration")) {
00622 snprintf(colbuf, sizeof(colbuf), "%lf",
00623 (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
00624
00625 if (!ast_strlen_zero(colbuf)) {
00626 colptr = colbuf;
00627 }
00628 }
00629
00630 if (sscanf(colptr, "%30lf", &number) != 1) {
00631 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
00632 continue;
00633 }
00634
00635 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00636 LENGTHEN_BUF2(entry->decimals);
00637 ast_str_append(&sql2, 0, "%s%*.*lf", first ? "" : ",", entry->decimals, entry->radix, number);
00638 }
00639 break;
00640 case SQL_FLOAT:
00641 case SQL_REAL:
00642 case SQL_DOUBLE:
00643 if (ast_strlen_zero(colptr)) {
00644 continue;
00645 } else {
00646 double number = 0.0;
00647
00648 if (!strcasecmp(entry->cdrname, "billsec")) {
00649 if (!ast_tvzero(cdr->answer)) {
00650 snprintf(colbuf, sizeof(colbuf), "%lf",
00651 (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
00652 } else {
00653 ast_copy_string(colbuf, "0", sizeof(colbuf));
00654 }
00655 } else if (!strcasecmp(entry->cdrname, "duration")) {
00656 snprintf(colbuf, sizeof(colbuf), "%lf",
00657 (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
00658
00659 if (!ast_strlen_zero(colbuf)) {
00660 colptr = colbuf;
00661 }
00662 }
00663
00664 if (sscanf(colptr, "%30lf", &number) != 1) {
00665 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
00666 continue;
00667 }
00668
00669 ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
00670 LENGTHEN_BUF2(entry->decimals);
00671 ast_str_append(&sql2, 0, "%s%lf", first ? "" : ",", number);
00672 }
00673 break;
00674 default:
00675 ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name);
00676 continue;
00677 }
00678 first = 0;
00679 }
00680 }
00681
00682
00683 LENGTHEN_BUF1(ast_str_strlen(sql2));
00684 ast_str_append(&sql, 0, ")");
00685 ast_str_append(&sql2, 0, ")");
00686 ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2));
00687
00688 ast_verb(11, "[%s]\n", ast_str_buffer(sql));
00689
00690 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, ast_str_buffer(sql));
00691 if (stmt) {
00692 SQLRowCount(stmt, &rows);
00693 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00694 }
00695 if (rows == 0) {
00696 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
00697 }
00698 early_release:
00699 ast_odbc_release_obj(obj);
00700 }
00701 AST_RWLIST_UNLOCK(&odbc_tables);
00702
00703
00704 if (ast_str_strlen(sql) > maxsize) {
00705 maxsize = ast_str_strlen(sql);
00706 }
00707 if (ast_str_strlen(sql2) > maxsize2) {
00708 maxsize2 = ast_str_strlen(sql2);
00709 }
00710
00711 ast_free(sql);
00712 ast_free(sql2);
00713 return 0;
00714 }
00715
00716 static int unload_module(void)
00717 {
00718 ast_cdr_unregister(name);
00719 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
00720 ast_cdr_register(name, ast_module_info->description, odbc_log);
00721 ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
00722 return -1;
00723 }
00724
00725 free_config();
00726 AST_RWLIST_UNLOCK(&odbc_tables);
00727 return 0;
00728 }
00729
00730 static int load_module(void)
00731 {
00732 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
00733 ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
00734 return 0;
00735 }
00736
00737 load_config();
00738 AST_RWLIST_UNLOCK(&odbc_tables);
00739 ast_cdr_register(name, ast_module_info->description, odbc_log);
00740 return 0;
00741 }
00742
00743 static int reload(void)
00744 {
00745 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
00746 ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
00747 return -1;
00748 }
00749
00750 free_config();
00751 load_config();
00752 AST_RWLIST_UNLOCK(&odbc_tables);
00753 return 0;
00754 }
00755
00756 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Adaptive ODBC CDR backend",
00757 .load = load_module,
00758 .unload = unload_module,
00759 .reload = reload,
00760 .load_pri = AST_MODPRI_CDR_DRIVER,
00761 );
00762