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