Do a massive conversion for using the ast_verb() macro
[asterisk/asterisk.git] / cdr / cdr_adaptive_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Tilghman Lesher
5  *
6  * Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Adaptive ODBC CDR backend
22  * 
23  * \author Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
24  * \ingroup cdr_drivers
25  */
26
27 /*** MODULEINFO
28         <depend>unixodbc</depend>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <time.h>
41
42 #include <sql.h>
43 #include <sqlext.h>
44 #include <sqltypes.h>
45
46 #include "asterisk/config.h"
47 #include "asterisk/options.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/res_odbc.h"
52 #include "asterisk/cdr.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
55
56 #define CONFIG  "cdr_adaptive_odbc.conf"
57
58 static char *name = "Adaptive ODBC";
59 /* Optimization to reduce number of memory allocations */
60 static int maxsize = 512, maxsize2 = 512;
61
62 struct columns {
63         char *name;
64         char *cdrname;
65         SQLSMALLINT type;
66         SQLINTEGER size;
67         SQLSMALLINT decimals;
68         SQLSMALLINT radix;
69         SQLSMALLINT nullable;
70         SQLINTEGER octetlen;
71         AST_LIST_ENTRY(columns) list;
72 };
73
74 struct tables {
75         char *connection;
76         char *table;
77         AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
78         AST_RWLIST_ENTRY(tables) list;
79 };
80
81 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
82
83 static int load_config(void)
84 {
85         struct ast_config *cfg;
86         struct ast_variable *var;
87         const char *tmp, *catg;
88         struct tables *tableptr;
89         struct columns *entry;
90         struct odbc_obj *obj;
91         char columnname[80];
92         char connection[40];
93         char table[40];
94         int lenconnection, lentable;
95         SQLLEN sqlptr;
96         int res = 0;
97         SQLHSTMT stmt = NULL;
98
99         cfg = ast_config_load(CONFIG);
100         if (!cfg) {
101                 ast_log(LOG_WARNING, "Unable to load " CONFIG ".  No adaptive ODBC CDRs.\n");
102                 return -1;
103         }
104
105         for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
106                 var = ast_variable_browse(cfg, catg);
107                 if (!var)
108                         continue;
109
110                 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
111                         ast_log(LOG_WARNING, "No connection parameter found in '%s'.  Skipping.\n", catg);
112                         continue;
113                 }
114                 ast_copy_string(connection, tmp, sizeof(connection));
115                 lenconnection = strlen(connection);
116
117                 /* When loading, we want to be sure we can connect. */
118                 obj = ast_odbc_request_obj(connection, 1);
119                 if (!obj) {
120                         ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ".  Check res_odbc.conf.\n", connection, catg);
121                         continue;
122                 }
123
124                 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
125                         ast_log(LOG_NOTICE, "No table name found.  Assuming 'cdr'.\n");
126                         tmp = "cdr";
127                 }
128                 ast_copy_string(table, tmp, sizeof(table));
129                 lentable = strlen(table);
130
131                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
132                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
133                         ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
134                         ast_odbc_release_obj(obj);
135                         continue;
136                 }
137
138                 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
139                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
140                         ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.  Skipping.\n", connection);
141                         ast_odbc_release_obj(obj);
142                         continue;
143                 }
144
145                 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
146                 if (!tableptr) {
147                         ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
148                         ast_odbc_release_obj(obj);
149                         res = -1;
150                         break;
151                 }
152
153                 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
154                 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
155                 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
156                 ast_copy_string(tableptr->table, table, lentable + 1);
157
158                 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
159
160                 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
161                         char *cdrvar = "";
162
163                         SQLGetData(stmt,  4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
164
165                         /* Is there an alias for this column? */
166
167                         /* NOTE: This seems like a non-optimal parse method, but I'm going
168                          * for user configuration readability, rather than fast parsing. We
169                          * really don't parse this file all that often, anyway.
170                          */
171                         for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
172                                 if (strcasecmp(var->value, columnname) == 0) {
173                                         char *tmp = ast_strdupa(var->name + 5);
174                                         cdrvar = ast_strip(tmp);
175                                         ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
176                                         break;
177                                 }
178                         }
179                         entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1);
180                         if (!entry) {
181                                 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
182                                 res = -1;
183                                 break;
184                         }
185                         entry->name = (char *)entry + sizeof(*entry);
186                         strcpy(entry->name, columnname);
187
188                         if (!ast_strlen_zero(cdrvar)) {
189                                 entry->cdrname = entry->name + strlen(columnname) + 1;
190                                 strcpy(entry->cdrname, cdrvar);
191                         } else /* Point to same place as the column name */
192                                 entry->cdrname = (char *)entry + sizeof(*entry);
193
194                         SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
195                         SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
196                         SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
197                         SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
198                         SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
199                         SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
200
201                         /* Insert column info into column list */
202                         AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
203                         res = 0;
204                 }
205
206                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
207                 ast_odbc_release_obj(obj);
208
209                 if (AST_LIST_FIRST(&(tableptr->columns)))
210                         AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
211                 else
212                         ast_free(tableptr);
213         }
214         return res;
215 }
216
217 static int free_config(void)
218 {
219         struct tables *table;
220         struct columns *entry;
221         while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
222                 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
223                         ast_free(entry);
224                 }
225                 ast_free(table);
226         }
227         return 0;
228 }
229
230 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
231 {
232         int res, i;
233         char *sql = data;
234         SQLHSTMT stmt;
235         SQLINTEGER nativeerror = 0, numfields = 0;
236         SQLSMALLINT diagbytes = 0;
237         unsigned char state[10], diagnostic[256];
238
239         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
240         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
241                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
242                 return NULL;
243         }
244
245         res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
246         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
247                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
248                 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
249                 for (i = 0; i < numfields; i++) {
250                         SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
251                         ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
252                         if (i > 10) {
253                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
254                                 break;
255                         }
256                 }
257                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
258                 return NULL;
259         }
260
261         return stmt;
262 }
263
264 #define LENGTHEN_BUF1(size)                                                                                                             \
265                         do {                                                                                                                            \
266                                 /* Lengthen buffer, if necessary */                                                             \
267                                 if ((newsize = lensql + (size) + 3) > sizesql) {        \
268                                         if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) {      \
269                                                 sql = tmp;                                                                                              \
270                                                 sizesql = (newsize / 512 + 1) * 512;                                    \
271                                         } else {                                                                                                        \
272                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
273                                                 ast_free(sql);                                                                                  \
274                                                 ast_free(sql2);                                                                                 \
275                                                 AST_RWLIST_UNLOCK(&odbc_tables);                                                \
276                                                 return -1;                                                                                              \
277                                         }                                                                                                                       \
278                                 }                                                                                                                               \
279                         } while (0)
280
281 #define LENGTHEN_BUF2(size)                                                                                                             \
282                         do {                                                                                                                            \
283                                 if ((newsize = lensql2 + (size) + 3) > sizesql2) {                              \
284                                         if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) {     \
285                                                 sql2 = tmp;                                                                                             \
286                                                 sizesql2 = (newsize / 512 + 1) * 512;                                   \
287                                         } else {                                                                                                        \
288                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table);       \
289                                                 ast_free(sql);                                                                                  \
290                                                 ast_free(sql2);                                                                                 \
291                                                 AST_RWLIST_UNLOCK(&odbc_tables);                                                \
292                                                 return -1;                                                                                              \
293                                         }                                                                                                                       \
294                                 }                                                                                                                               \
295                         } while (0)
296
297 static int odbc_log(struct ast_cdr *cdr)
298 {
299         struct tables *tableptr;
300         struct columns *entry;
301         struct odbc_obj *obj;
302         int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
303         /* Allocated, so we can realloc() */
304         char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp;
305         char colbuf[1024], *colptr;
306         SQLHSTMT stmt = NULL;
307         SQLLEN rows = 0;
308
309         if (AST_RWLIST_RDLOCK(&odbc_tables)) {
310                 ast_log(LOG_ERROR, "Unable to lock table list.  Insert CDR(s) failed.\n");
311                 return -1;
312         }
313
314         AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
315                 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", tableptr->table);
316                 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
317
318                 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
319                         /* Check if we have a similarly named variable */
320                         ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0,
321                                 (strcasecmp(entry->cdrname, "start") == 0 ||
322                                  strcasecmp(entry->cdrname, "answer") == 0 ||
323                                  strcasecmp(entry->cdrname, "end") == 0) ? 0 : 1);
324
325                         if (colptr) {
326                                 LENGTHEN_BUF1(strlen(entry->name));
327
328                                 switch (entry->type) {
329                                 case SQL_CHAR:
330                                 case SQL_VARCHAR:
331                                 case SQL_LONGVARCHAR:
332                                 case SQL_BINARY:
333                                 case SQL_VARBINARY:
334                                 case SQL_LONGVARBINARY:
335                                 case SQL_GUID:
336                                         /* For these two field names, get the rendered form, instead of the raw
337                                          * form (but only when we're dealing with a character-based field).
338                                          */
339                                         if (strcasecmp(entry->name, "disposition") == 0)
340                                                 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
341                                         else if (strcasecmp(entry->name, "amaflags") == 0)
342                                                 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
343
344                                         /* Truncate too-long fields */
345                                         if (entry->type != SQL_GUID) {
346                                                 if (strlen(colptr) > entry->octetlen)
347                                                         colptr[entry->octetlen] = '\0';
348                                         }
349
350                                         lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
351                                         LENGTHEN_BUF2(strlen(colptr));
352
353                                         /* Encode value, with escaping */
354                                         strcpy(sql2 + lensql2, "'");
355                                         lensql2++;
356                                         for (tmp = colptr; *tmp; tmp++) {
357                                                 if (*tmp == '\'') {
358                                                         strcpy(sql2 + lensql2, "''");
359                                                         lensql2 += 2;
360                                                 } else if (*tmp == '\\') {
361                                                         strcpy(sql2 + lensql2, "\\\\");
362                                                         lensql2 += 2;
363                                                 } else {
364                                                         sql2[lensql2++] = *tmp;
365                                                         sql2[lensql2] = '\0';
366                                                 }
367                                         }
368                                         strcpy(sql2 + lensql2, "',");
369                                         lensql2 += 2;
370                                         break;
371                                 case SQL_TYPE_DATE:
372                                         {
373                                                 int year = 0, month = 0, day = 0;
374                                                 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
375                                                         month <= 0 || month > 12 || day < 0 || day > 31 ||
376                                                         ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
377                                                         (month == 2 && year % 400 == 0 && day > 29) ||
378                                                         (month == 2 && year % 100 == 0 && day > 28) ||
379                                                         (month == 2 && year % 4 == 0 && day > 29) ||
380                                                         (month == 2 && year % 4 != 0 && day > 28)) {
381                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
382                                                         break;
383                                                 }
384
385                                                 if (year > 0 && year < 100)
386                                                         year += 2000;
387
388                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
389                                                 LENGTHEN_BUF2(10);
390                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d',", year, month, day);
391                                         }
392                                         break;
393                                 case SQL_TYPE_TIME:
394                                         {
395                                                 int hour = 0, minute = 0, second = 0;
396                                                 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
397
398                                                 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
399                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
400                                                         break;
401                                                 }
402
403                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
404                                                 LENGTHEN_BUF2(8);
405                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%02d:%02d:%02d',", hour, minute, second);
406                                         }
407                                         break;
408                                 case SQL_TYPE_TIMESTAMP:
409                                 case SQL_TIMESTAMP:
410                                         {
411                                                 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
412                                                 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
413
414                                                 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
415                                                         month <= 0 || month > 12 || day < 0 || day > 31 ||
416                                                         ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
417                                                         (month == 2 && year % 400 == 0 && day > 29) ||
418                                                         (month == 2 && year % 100 == 0 && day > 28) ||
419                                                         (month == 2 && year % 4 == 0 && day > 29) ||
420                                                         (month == 2 && year % 4 != 0 && day > 28) ||
421                                                         hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
422                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
423                                                         break;
424                                                 }
425
426                                                 if (year > 0 && year < 100)
427                                                         year += 2000;
428
429                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
430                                                 LENGTHEN_BUF2(19);
431                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d %02d:%02d:%02d',", year, month, day, hour, minute, second);
432                                         }
433                                         break;
434                                 case SQL_INTEGER:
435                                         {
436                                                 int integer = 0;
437                                                 if (sscanf(colptr, "%d", &integer) != 1) {
438                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
439                                                         break;
440                                                 }
441
442                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
443                                                 LENGTHEN_BUF2(12);
444                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
445                                         }
446                                         break;
447                                 case SQL_BIGINT:
448                                         {
449                                                 long long integer = 0;
450                                                 if (sscanf(colptr, "%lld", &integer) != 1) {
451                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
452                                                         break;
453                                                 }
454
455                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
456                                                 LENGTHEN_BUF2(24);
457                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
458                                         }
459                                         break;
460                                 case SQL_SMALLINT:
461                                         {
462                                                 short integer = 0;
463                                                 if (sscanf(colptr, "%hd", &integer) != 1) {
464                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
465                                                         break;
466                                                 }
467
468                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
469                                                 LENGTHEN_BUF2(6);
470                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
471                                         }
472                                         break;
473                                 case SQL_TINYINT:
474                                         {
475                                                 char integer = 0;
476                                                 if (sscanf(colptr, "%hhd", &integer) != 1) {
477                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
478                                                         break;
479                                                 }
480
481                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
482                                                 LENGTHEN_BUF2(4);
483                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
484                                         }
485                                         break;
486                                 case SQL_BIT:
487                                         {
488                                                 char integer = 0;
489                                                 if (sscanf(colptr, "%hhd", &integer) != 1) {
490                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
491                                                         break;
492                                                 }
493                                                 if (integer != 0)
494                                                         integer = 1;
495
496                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
497                                                 LENGTHEN_BUF2(2);
498                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
499                                         }
500                                         break;
501                                 case SQL_NUMERIC:
502                                 case SQL_DECIMAL:
503                                         {
504                                                 double number = 0.0;
505                                                 if (sscanf(colptr, "%lf", &number) != 1) {
506                                                         ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
507                                                         break;
508                                                 }
509
510                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
511                                                 LENGTHEN_BUF2(entry->decimals);
512                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
513                                         }
514                                         break;
515                                 case SQL_FLOAT:
516                                 case SQL_REAL:
517                                 case SQL_DOUBLE:
518                                         {
519                                                 double number = 0.0;
520                                                 if (sscanf(colptr, "%lf", &number) != 1) {
521                                                         ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
522                                                         break;
523                                                 }
524
525                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
526                                                 LENGTHEN_BUF2(entry->decimals);
527                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
528                                         }
529                                         break;
530                                 default:
531                                         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);
532                                 }
533                         }
534                 }
535
536                 /* Concatenate the two constructed buffers */
537                 LENGTHEN_BUF1(lensql2);
538                 sql[lensql - 1] = ')';
539                 sql2[lensql2 - 1] = ')';
540                 strcat(sql + lensql, sql2);
541
542                 ast_verb(11, "[%s]\n", sql);
543                 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
544                 obj = ast_odbc_request_obj(tableptr->connection, 0);
545                 if (obj) {
546                         stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
547                         if (stmt) {
548                                 SQLRowCount(stmt, &rows);
549                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
550                         }
551                         if (rows == 0) {
552                                 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
553                         }
554                         ast_odbc_release_obj(obj);
555                 } else {
556                         ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
557                 }
558         }
559         AST_RWLIST_UNLOCK(&odbc_tables);
560
561         /* Next time, just allocate buffers that are that big to start with. */
562         if (sizesql > maxsize)
563                 maxsize = sizesql;
564         if (sizesql2 > maxsize2)
565                 maxsize2 = sizesql2;
566
567         ast_free(sql);
568         ast_free(sql2);
569         return 0;
570 }
571
572 static int unload_module(void)
573 {
574         ast_cdr_unregister(name);
575         usleep(1);
576         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
577                 ast_cdr_register(name, ast_module_info->description, odbc_log);
578                 ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
579                 return -1;
580         }
581
582         free_config();
583         AST_RWLIST_UNLOCK(&odbc_tables);
584         return 0;
585 }
586
587 static int load_module(void)
588 {
589         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
590                 ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
591                 return 0;
592         }
593
594         load_config();
595         AST_RWLIST_UNLOCK(&odbc_tables);
596         ast_cdr_register(name, ast_module_info->description, odbc_log);
597         return 0;
598 }
599
600 static int reload(void)
601 {
602         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
603                 ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
604                 return -1;
605         }
606
607         free_config();
608         load_config();
609         AST_RWLIST_UNLOCK(&odbc_tables);
610         return 0;
611 }
612
613 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
614         .load = load_module,
615         .unload = unload_module,
616         .reload = reload,
617 );
618