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