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