Blocked revisions 94543 via svnmerge
[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                 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
331                 if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
332                         ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
333                         continue;
334                 }
335
336                 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
337                         /* Check if we have a similarly named variable */
338                         ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0,
339                                 (strcasecmp(entry->cdrname, "start") == 0 ||
340                                  strcasecmp(entry->cdrname, "answer") == 0 ||
341                                  strcasecmp(entry->cdrname, "end") == 0) ? 0 : 1);
342
343                         if (colptr) {
344                                 LENGTHEN_BUF1(strlen(entry->name));
345
346                                 switch (entry->type) {
347                                 case SQL_CHAR:
348                                 case SQL_VARCHAR:
349                                 case SQL_LONGVARCHAR:
350                                 case SQL_BINARY:
351                                 case SQL_VARBINARY:
352                                 case SQL_LONGVARBINARY:
353                                 case SQL_GUID:
354                                         /* For these two field names, get the rendered form, instead of the raw
355                                          * form (but only when we're dealing with a character-based field).
356                                          */
357                                         if (strcasecmp(entry->name, "disposition") == 0)
358                                                 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
359                                         else if (strcasecmp(entry->name, "amaflags") == 0)
360                                                 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
361
362                                         /* Truncate too-long fields */
363                                         if (entry->type != SQL_GUID) {
364                                                 if (strlen(colptr) > entry->octetlen)
365                                                         colptr[entry->octetlen] = '\0';
366                                         }
367
368                                         lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
369                                         LENGTHEN_BUF2(strlen(colptr));
370
371                                         /* Encode value, with escaping */
372                                         strcpy(sql2 + lensql2, "'");
373                                         lensql2++;
374                                         for (tmp = colptr; *tmp; tmp++) {
375                                                 if (*tmp == '\'') {
376                                                         strcpy(sql2 + lensql2, "''");
377                                                         lensql2 += 2;
378                                                 } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
379                                                         strcpy(sql2 + lensql2, "\\\\");
380                                                         lensql2 += 2;
381                                                 } else {
382                                                         sql2[lensql2++] = *tmp;
383                                                         sql2[lensql2] = '\0';
384                                                 }
385                                         }
386                                         strcpy(sql2 + lensql2, "',");
387                                         lensql2 += 2;
388                                         break;
389                                 case SQL_TYPE_DATE:
390                                         {
391                                                 int year = 0, month = 0, day = 0;
392                                                 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
393                                                         month <= 0 || month > 12 || day < 0 || day > 31 ||
394                                                         ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
395                                                         (month == 2 && year % 400 == 0 && day > 29) ||
396                                                         (month == 2 && year % 100 == 0 && day > 28) ||
397                                                         (month == 2 && year % 4 == 0 && day > 29) ||
398                                                         (month == 2 && year % 4 != 0 && day > 28)) {
399                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
400                                                         break;
401                                                 }
402
403                                                 if (year > 0 && year < 100)
404                                                         year += 2000;
405
406                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
407                                                 LENGTHEN_BUF2(10);
408                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d',", year, month, day);
409                                         }
410                                         break;
411                                 case SQL_TYPE_TIME:
412                                         {
413                                                 int hour = 0, minute = 0, second = 0;
414                                                 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
415
416                                                 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
417                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
418                                                         break;
419                                                 }
420
421                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
422                                                 LENGTHEN_BUF2(8);
423                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%02d:%02d:%02d',", hour, minute, second);
424                                         }
425                                         break;
426                                 case SQL_TYPE_TIMESTAMP:
427                                 case SQL_TIMESTAMP:
428                                         {
429                                                 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
430                                                 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
431
432                                                 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
433                                                         month <= 0 || month > 12 || day < 0 || day > 31 ||
434                                                         ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
435                                                         (month == 2 && year % 400 == 0 && day > 29) ||
436                                                         (month == 2 && year % 100 == 0 && day > 28) ||
437                                                         (month == 2 && year % 4 == 0 && day > 29) ||
438                                                         (month == 2 && year % 4 != 0 && day > 28) ||
439                                                         hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
440                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
441                                                         break;
442                                                 }
443
444                                                 if (year > 0 && year < 100)
445                                                         year += 2000;
446
447                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
448                                                 LENGTHEN_BUF2(19);
449                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d %02d:%02d:%02d',", year, month, day, hour, minute, second);
450                                         }
451                                         break;
452                                 case SQL_INTEGER:
453                                         {
454                                                 int integer = 0;
455                                                 if (sscanf(colptr, "%d", &integer) != 1) {
456                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
457                                                         break;
458                                                 }
459
460                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
461                                                 LENGTHEN_BUF2(12);
462                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
463                                         }
464                                         break;
465                                 case SQL_BIGINT:
466                                         {
467                                                 long long integer = 0;
468                                                 if (sscanf(colptr, "%lld", &integer) != 1) {
469                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
470                                                         break;
471                                                 }
472
473                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
474                                                 LENGTHEN_BUF2(24);
475                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
476                                         }
477                                         break;
478                                 case SQL_SMALLINT:
479                                         {
480                                                 short integer = 0;
481                                                 if (sscanf(colptr, "%hd", &integer) != 1) {
482                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
483                                                         break;
484                                                 }
485
486                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
487                                                 LENGTHEN_BUF2(6);
488                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
489                                         }
490                                         break;
491                                 case SQL_TINYINT:
492                                         {
493                                                 char integer = 0;
494                                                 if (sscanf(colptr, "%hhd", &integer) != 1) {
495                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
496                                                         break;
497                                                 }
498
499                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
500                                                 LENGTHEN_BUF2(4);
501                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
502                                         }
503                                         break;
504                                 case SQL_BIT:
505                                         {
506                                                 char integer = 0;
507                                                 if (sscanf(colptr, "%hhd", &integer) != 1) {
508                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
509                                                         break;
510                                                 }
511                                                 if (integer != 0)
512                                                         integer = 1;
513
514                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
515                                                 LENGTHEN_BUF2(2);
516                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
517                                         }
518                                         break;
519                                 case SQL_NUMERIC:
520                                 case SQL_DECIMAL:
521                                         {
522                                                 double number = 0.0;
523                                                 if (sscanf(colptr, "%lf", &number) != 1) {
524                                                         ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
525                                                         break;
526                                                 }
527
528                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
529                                                 LENGTHEN_BUF2(entry->decimals);
530                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
531                                         }
532                                         break;
533                                 case SQL_FLOAT:
534                                 case SQL_REAL:
535                                 case SQL_DOUBLE:
536                                         {
537                                                 double number = 0.0;
538                                                 if (sscanf(colptr, "%lf", &number) != 1) {
539                                                         ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
540                                                         break;
541                                                 }
542
543                                                 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
544                                                 LENGTHEN_BUF2(entry->decimals);
545                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
546                                         }
547                                         break;
548                                 default:
549                                         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);
550                                 }
551                         }
552                 }
553
554                 /* Concatenate the two constructed buffers */
555                 LENGTHEN_BUF1(lensql2);
556                 sql[lensql - 1] = ')';
557                 sql2[lensql2 - 1] = ')';
558                 strcat(sql + lensql, sql2);
559
560                 ast_verb(11, "[%s]\n", sql);
561
562                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
563                 if (stmt) {
564                         SQLRowCount(stmt, &rows);
565                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
566                 }
567                 if (rows == 0) {
568                         ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
569                 }
570                 ast_odbc_release_obj(obj);
571         }
572         AST_RWLIST_UNLOCK(&odbc_tables);
573
574         /* Next time, just allocate buffers that are that big to start with. */
575         if (sizesql > maxsize)
576                 maxsize = sizesql;
577         if (sizesql2 > maxsize2)
578                 maxsize2 = sizesql2;
579
580         ast_free(sql);
581         ast_free(sql2);
582         return 0;
583 }
584
585 static int unload_module(void)
586 {
587         ast_cdr_unregister(name);
588         usleep(1);
589         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
590                 ast_cdr_register(name, ast_module_info->description, odbc_log);
591                 ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
592                 return -1;
593         }
594
595         free_config();
596         AST_RWLIST_UNLOCK(&odbc_tables);
597         return 0;
598 }
599
600 static int load_module(void)
601 {
602         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
603                 ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
604                 return 0;
605         }
606
607         load_config();
608         AST_RWLIST_UNLOCK(&odbc_tables);
609         ast_cdr_register(name, ast_module_info->description, odbc_log);
610         return 0;
611 }
612
613 static int reload(void)
614 {
615         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
616                 ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
617                 return -1;
618         }
619
620         free_config();
621         load_config();
622         AST_RWLIST_UNLOCK(&odbc_tables);
623         return 0;
624 }
625
626 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
627         .load = load_module,
628         .unload = unload_module,
629         .reload = reload,
630 );
631