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