Fix memory leak
[asterisk/asterisk.git] / cdr / cdr_adaptive_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Tilghman Lesher
5  *
6  * Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Adaptive ODBC CDR backend
22  * 
23  * \author Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
24  * \ingroup cdr_drivers
25  */
26
27 /*** MODULEINFO
28         <depend>unixodbc</depend>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <time.h>
41
42 #include <sql.h>
43 #include <sqlext.h>
44 #include <sqltypes.h>
45
46 #include "asterisk/config.h"
47 #include "asterisk/options.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/res_odbc.h"
52 #include "asterisk/cdr.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
55
56 #define CONFIG  "cdr_adaptive_odbc.conf"
57
58 static char *name = "Adaptive ODBC";
59 /* Optimization to reduce number of memory allocations */
60 static int maxsize = 512, maxsize2 = 512;
61
62 struct columns {
63         char *name;
64         char *cdrname;
65         SQLSMALLINT type;
66         SQLINTEGER size;
67         SQLSMALLINT decimals;
68         SQLSMALLINT radix;
69         SQLSMALLINT nullable;
70         SQLINTEGER octetlen;
71         AST_LIST_ENTRY(columns) list;
72 };
73
74 struct tables {
75         char *connection;
76         char *table;
77         AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
78         AST_RWLIST_ENTRY(tables) list;
79 };
80
81 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
82
83 static int load_config(void)
84 {
85         struct ast_config *cfg;
86         struct ast_variable *var;
87         const char *tmp, *catg;
88         struct tables *tableptr;
89         struct columns *entry;
90         struct odbc_obj *obj;
91         char columnname[80];
92         char connection[40];
93         char table[40];
94         int lenconnection, lentable;
95         SQLLEN sqlptr;
96         int res = 0;
97         SQLHSTMT stmt = NULL;
98         struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
99
100         cfg = ast_config_load(CONFIG, config_flags);
101         if (!cfg) {
102                 ast_log(LOG_WARNING, "Unable to load " CONFIG ".  No adaptive ODBC CDRs.\n");
103                 return -1;
104         }
105
106         for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
107                 var = ast_variable_browse(cfg, catg);
108                 if (!var)
109                         continue;
110
111                 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
112                         ast_log(LOG_WARNING, "No connection parameter found in '%s'.  Skipping.\n", catg);
113                         continue;
114                 }
115                 ast_copy_string(connection, tmp, sizeof(connection));
116                 lenconnection = strlen(connection);
117
118                 /* When loading, we want to be sure we can connect. */
119                 obj = ast_odbc_request_obj(connection, 1);
120                 if (!obj) {
121                         ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ".  Check res_odbc.conf.\n", connection, catg);
122                         continue;
123                 }
124
125                 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
126                         ast_log(LOG_NOTICE, "No table name found.  Assuming 'cdr'.\n");
127                         tmp = "cdr";
128                 }
129                 ast_copy_string(table, tmp, sizeof(table));
130                 lentable = strlen(table);
131
132                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
133                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
134                         ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
135                         ast_odbc_release_obj(obj);
136                         continue;
137                 }
138
139                 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
140                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
141                         ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.  Skipping.\n", connection);
142                         ast_odbc_release_obj(obj);
143                         continue;
144                 }
145
146                 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
147                 if (!tableptr) {
148                         ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
149                         ast_odbc_release_obj(obj);
150                         res = -1;
151                         break;
152                 }
153
154                 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
155                 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
156                 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
157                 ast_copy_string(tableptr->table, table, lentable + 1);
158
159                 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
160
161                 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
162                         char *cdrvar = "";
163
164                         SQLGetData(stmt,  4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
165
166                         /* Is there an alias for this column? */
167
168                         /* NOTE: This seems like a non-optimal parse method, but I'm going
169                          * for user configuration readability, rather than fast parsing. We
170                          * really don't parse this file all that often, anyway.
171                          */
172                         for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
173                                 if (strcasecmp(var->value, columnname) == 0) {
174                                         char *tmp = ast_strdupa(var->name + 5);
175                                         cdrvar = ast_strip(tmp);
176                                         ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
177                                         break;
178                                 }
179                         }
180                         entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1);
181                         if (!entry) {
182                                 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
183                                 res = -1;
184                                 break;
185                         }
186                         entry->name = (char *)entry + sizeof(*entry);
187                         strcpy(entry->name, columnname);
188
189                         if (!ast_strlen_zero(cdrvar)) {
190                                 entry->cdrname = entry->name + strlen(columnname) + 1;
191                                 strcpy(entry->cdrname, cdrvar);
192                         } else /* Point to same place as the column name */
193                                 entry->cdrname = (char *)entry + sizeof(*entry);
194
195                         SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
196                         SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
197                         SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
198                         SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
199                         SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
200                         SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
201
202                         /* Specification states that the octenlen should be the maximum number of bytes
203                          * returned in a char or binary column, but it seems that some drivers just set
204                          * it to NULL. (Bad Postgres! No biscuit!) */
205                         if (entry->octetlen == 0)
206                                 entry->octetlen = entry->size;
207
208                         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);
209                         /* Insert column info into column list */
210                         AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
211                         res = 0;
212                 }
213
214                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
215                 ast_odbc_release_obj(obj);
216
217                 if (AST_LIST_FIRST(&(tableptr->columns)))
218                         AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
219                 else
220                         ast_free(tableptr);
221         }
222         return res;
223 }
224
225 static int free_config(void)
226 {
227         struct tables *table;
228         struct columns *entry;
229         while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
230                 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
231                         ast_free(entry);
232                 }
233                 ast_free(table);
234         }
235         return 0;
236 }
237
238 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
239 {
240         int res, i;
241         char *sql = data;
242         SQLHSTMT stmt;
243         SQLINTEGER nativeerror = 0, numfields = 0;
244         SQLSMALLINT diagbytes = 0;
245         unsigned char state[10], diagnostic[256];
246
247         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
248         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
249                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
250                 return NULL;
251         }
252
253         res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
254         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
255                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
256                 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
257                 for (i = 0; i < numfields; i++) {
258                         SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
259                         ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
260                         if (i > 10) {
261                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
262                                 break;
263                         }
264                 }
265                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
266                 return NULL;
267         }
268
269         return stmt;
270 }
271
272 #define LENGTHEN_BUF1(size)                                                                                                             \
273                         do {                                                                                                                            \
274                                 /* Lengthen buffer, if necessary */                                                             \
275                                 if ((newsize = lensql + (size) + 3) > sizesql) {        \
276                                         if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) {      \
277                                                 sql = tmp;                                                                                              \
278                                                 sizesql = (newsize / 512 + 1) * 512;                                    \
279                                         } else {                                                                                                        \
280                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
281                                                 ast_free(sql);                                                                                  \
282                                                 ast_free(sql2);                                                                                 \
283                                                 AST_RWLIST_UNLOCK(&odbc_tables);                                                \
284                                                 return -1;                                                                                              \
285                                         }                                                                                                                       \
286                                 }                                                                                                                               \
287                         } while (0)
288
289 #define LENGTHEN_BUF2(size)                                                                                                             \
290                         do {                                                                                                                            \
291                                 if ((newsize = lensql2 + (size) + 3) > sizesql2) {                              \
292                                         if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) {     \
293                                                 sql2 = tmp;                                                                                             \
294                                                 sizesql2 = (newsize / 512 + 1) * 512;                                   \
295                                         } else {                                                                                                        \
296                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table);       \
297                                                 ast_free(sql);                                                                                  \
298                                                 ast_free(sql2);                                                                                 \
299                                                 AST_RWLIST_UNLOCK(&odbc_tables);                                                \
300                                                 return -1;                                                                                              \
301                                         }                                                                                                                       \
302                                 }                                                                                                                               \
303                         } while (0)
304
305 static int odbc_log(struct ast_cdr *cdr)
306 {
307         struct tables *tableptr;
308         struct columns *entry;
309         struct odbc_obj *obj;
310         int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
311         /* Allocated, so we can realloc() */
312         char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp;
313         char colbuf[1024], *colptr;
314         SQLHSTMT stmt = NULL;
315         SQLLEN rows = 0;
316
317         if (!sql || !sql2) {
318                 if (sql)
319                         ast_free(sql);
320                 if (sql2)
321                         ast_free(sql2);
322                 return -1;
323         }
324
325         if (AST_RWLIST_RDLOCK(&odbc_tables)) {
326                 ast_log(LOG_ERROR, "Unable to lock table list.  Insert CDR(s) failed.\n");
327                 ast_free(sql);
328                 ast_free(sql2);
329                 return -1;
330         }
331
332         AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
333                 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", tableptr->table);
334                 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
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 == '\\') {
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                 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
562                 obj = ast_odbc_request_obj(tableptr->connection, 0);
563                 if (obj) {
564                         stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
565                         if (stmt) {
566                                 SQLRowCount(stmt, &rows);
567                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
568                         }
569                         if (rows == 0) {
570                                 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
571                         }
572                         ast_odbc_release_obj(obj);
573                 } else {
574                         ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
575                 }
576         }
577         AST_RWLIST_UNLOCK(&odbc_tables);
578
579         /* Next time, just allocate buffers that are that big to start with. */
580         if (sizesql > maxsize)
581                 maxsize = sizesql;
582         if (sizesql2 > maxsize2)
583                 maxsize2 = sizesql2;
584
585         ast_free(sql);
586         ast_free(sql2);
587         return 0;
588 }
589
590 static int unload_module(void)
591 {
592         ast_cdr_unregister(name);
593         usleep(1);
594         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
595                 ast_cdr_register(name, ast_module_info->description, odbc_log);
596                 ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
597                 return -1;
598         }
599
600         free_config();
601         AST_RWLIST_UNLOCK(&odbc_tables);
602         return 0;
603 }
604
605 static int load_module(void)
606 {
607         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
608                 ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
609                 return 0;
610         }
611
612         load_config();
613         AST_RWLIST_UNLOCK(&odbc_tables);
614         ast_cdr_register(name, ast_module_info->description, odbc_log);
615         return 0;
616 }
617
618 static int reload(void)
619 {
620         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
621                 ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
622                 return -1;
623         }
624
625         free_config();
626         load_config();
627         AST_RWLIST_UNLOCK(&odbc_tables);
628         return 0;
629 }
630
631 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
632         .load = load_module,
633         .unload = unload_module,
634         .reload = reload,
635 );
636