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