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