Remove some unused variables
[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 <time.h>
37
38 #include <sql.h>
39 #include <sqlext.h>
40 #include <sqltypes.h>
41
42 #include "asterisk/config.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/res_odbc.h"
47 #include "asterisk/cdr.h"
48 #include "asterisk/module.h"
49
50 #define CONFIG  "cdr_adaptive_odbc.conf"
51
52 static char *name = "Adaptive ODBC";
53 /* Optimization to reduce number of memory allocations */
54 static int maxsize = 512, maxsize2 = 512;
55
56 struct columns {
57         char *name;
58         char *cdrname;
59         char *filtervalue;
60         SQLSMALLINT type;
61         SQLINTEGER size;
62         SQLSMALLINT decimals;
63         SQLSMALLINT radix;
64         SQLSMALLINT nullable;
65         SQLINTEGER octetlen;
66         AST_LIST_ENTRY(columns) list;
67 };
68
69 struct tables {
70         char *connection;
71         char *table;
72         AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
73         AST_RWLIST_ENTRY(tables) list;
74 };
75
76 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
77
78 static int load_config(void)
79 {
80         struct ast_config *cfg;
81         struct ast_variable *var;
82         const char *tmp, *catg;
83         struct tables *tableptr;
84         struct columns *entry;
85         struct odbc_obj *obj;
86         char columnname[80];
87         char connection[40];
88         char table[40];
89         int lenconnection, lentable;
90         SQLLEN sqlptr;
91         int res = 0;
92         SQLHSTMT stmt = NULL;
93         struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
94
95         cfg = ast_config_load(CONFIG, config_flags);
96         if (!cfg) {
97                 ast_log(LOG_WARNING, "Unable to load " CONFIG ".  No adaptive ODBC CDRs.\n");
98                 return -1;
99         }
100
101         for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
102                 var = ast_variable_browse(cfg, catg);
103                 if (!var)
104                         continue;
105
106                 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
107                         ast_log(LOG_WARNING, "No connection parameter found in '%s'.  Skipping.\n", catg);
108                         continue;
109                 }
110                 ast_copy_string(connection, tmp, sizeof(connection));
111                 lenconnection = strlen(connection);
112
113                 /* When loading, we want to be sure we can connect. */
114                 obj = ast_odbc_request_obj(connection, 1);
115                 if (!obj) {
116                         ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ".  Check res_odbc.conf.\n", connection, catg);
117                         continue;
118                 }
119
120                 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
121                         ast_log(LOG_NOTICE, "No table name found.  Assuming 'cdr'.\n");
122                         tmp = "cdr";
123                 }
124                 ast_copy_string(table, tmp, sizeof(table));
125                 lentable = strlen(table);
126
127                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
128                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
129                         ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
130                         ast_odbc_release_obj(obj);
131                         continue;
132                 }
133
134                 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
135                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
136                         ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.  Skipping.\n", connection);
137                         ast_odbc_release_obj(obj);
138                         continue;
139                 }
140
141                 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
142                 if (!tableptr) {
143                         ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
144                         ast_odbc_release_obj(obj);
145                         res = -1;
146                         break;
147                 }
148
149                 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
150                 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
151                 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
152                 ast_copy_string(tableptr->table, table, lentable + 1);
153
154                 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
155
156                 /* Check for filters first */
157                 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
158                         if (strncmp(var->name, "filter", 6) == 0) {
159                                 char *cdrvar = ast_strdupa(var->name + 6);
160                                 cdrvar = ast_strip(cdrvar);
161                                 ast_verb(3, "Found filter %s for cdr variable %s in %s@%s\n", var->value, cdrvar, tableptr->table, tableptr->connection);
162
163                                 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(cdrvar) + 1 + strlen(var->value) + 1);
164                                 if (!entry) {
165                                         ast_log(LOG_ERROR, "Out of memory creating filter entry for CDR variable '%s' in table '%s' on connection '%s'\n", cdrvar, table, connection);
166                                         res = -1;
167                                         break;
168                                 }
169
170                                 /* NULL column entry means this isn't a column in the database */
171                                 entry->name = NULL;
172                                 entry->cdrname = (char *)entry + sizeof(*entry);
173                                 entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(cdrvar) + 1;
174                                 strcpy(entry->cdrname, cdrvar);
175                                 strcpy(entry->filtervalue, var->value);
176
177                                 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
178                         }
179                 }
180
181                 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
182                         char *cdrvar = "";
183
184                         SQLGetData(stmt,  4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
185
186                         /* Is there an alias for this column? */
187
188                         /* NOTE: This seems like a non-optimal parse method, but I'm going
189                          * for user configuration readability, rather than fast parsing. We
190                          * really don't parse this file all that often, anyway.
191                          */
192                         for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
193                                 if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) {
194                                         char *tmp = ast_strdupa(var->name + 5);
195                                         cdrvar = ast_strip(tmp);
196                                         ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
197                                         break;
198                                 }
199                         }
200
201                         entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1);
202                         if (!entry) {
203                                 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
204                                 res = -1;
205                                 break;
206                         }
207                         entry->name = (char *)entry + sizeof(*entry);
208                         strcpy(entry->name, columnname);
209
210                         if (!ast_strlen_zero(cdrvar)) {
211                                 entry->cdrname = entry->name + strlen(columnname) + 1;
212                                 strcpy(entry->cdrname, cdrvar);
213                         } else /* Point to same place as the column name */
214                                 entry->cdrname = (char *)entry + sizeof(*entry);
215
216                         SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
217                         SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
218                         SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
219                         SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
220                         SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
221                         SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
222
223                         /* Specification states that the octenlen should be the maximum number of bytes
224                          * returned in a char or binary column, but it seems that some drivers just set
225                          * it to NULL. (Bad Postgres! No biscuit!) */
226                         if (entry->octetlen == 0)
227                                 entry->octetlen = entry->size;
228
229                         ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
230                         /* Insert column info into column list */
231                         AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
232                         res = 0;
233                 }
234
235                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
236                 ast_odbc_release_obj(obj);
237
238                 if (AST_LIST_FIRST(&(tableptr->columns)))
239                         AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
240                 else
241                         ast_free(tableptr);
242         }
243         return res;
244 }
245
246 static int free_config(void)
247 {
248         struct tables *table;
249         struct columns *entry;
250         while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
251                 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
252                         ast_free(entry);
253                 }
254                 ast_free(table);
255         }
256         return 0;
257 }
258
259 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
260 {
261         int res, i;
262         char *sql = data;
263         SQLHSTMT stmt;
264         SQLINTEGER nativeerror = 0, numfields = 0;
265         SQLSMALLINT diagbytes = 0;
266         unsigned char state[10], diagnostic[256];
267
268         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
269         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
270                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
271                 return NULL;
272         }
273
274         res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
275         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
276                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
277                 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
278                 for (i = 0; i < numfields; i++) {
279                         SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
280                         ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
281                         if (i > 10) {
282                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
283                                 break;
284                         }
285                 }
286                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
287                 return NULL;
288         }
289
290         return stmt;
291 }
292
293 #define LENGTHEN_BUF1(size)                                                                                                             \
294                         do {                                                                                                                            \
295                                 /* Lengthen buffer, if necessary */                                                             \
296                                 if (sql->used + size + 1 > sql->len) {                          \
297                                         if (ast_str_make_space(&sql, ((sql->len + size + 1) / 512 + 1) * 512) != 0) { \
298                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
299                                                 ast_free(sql);                                                                                  \
300                                                 ast_free(sql2);                                                                                 \
301                                                 AST_RWLIST_UNLOCK(&odbc_tables);                                                \
302                                                 return -1;                                                                                              \
303                                         }                                                                                                                       \
304                                 }                                                                                                                               \
305                         } while (0)
306
307 #define LENGTHEN_BUF2(size)                                                                                                             \
308                         do {                                                                                                                            \
309                                 if (sql2->used + size + 1 > sql2->len) {                        \
310                                         if (ast_str_make_space(&sql2, ((sql2->len + size + 3) / 512 + 1) * 512) != 0) { \
311                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
312                                                 ast_free(sql);                                                                                  \
313                                                 ast_free(sql2);                                                                                 \
314                                                 AST_RWLIST_UNLOCK(&odbc_tables);                                                \
315                                                 return -1;                                                                                              \
316                                         }                                                                                                                       \
317                                 }                                                                                                                               \
318                         } while (0)
319
320 static int odbc_log(struct ast_cdr *cdr)
321 {
322         struct tables *tableptr;
323         struct columns *entry;
324         struct odbc_obj *obj;
325         struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
326         char *tmp;
327         char colbuf[1024], *colptr;
328         SQLHSTMT stmt = NULL;
329         SQLLEN rows = 0;
330
331         if (!sql || !sql2) {
332                 if (sql)
333                         ast_free(sql);
334                 if (sql2)
335                         ast_free(sql2);
336                 return -1;
337         }
338
339         if (AST_RWLIST_RDLOCK(&odbc_tables)) {
340                 ast_log(LOG_ERROR, "Unable to lock table list.  Insert CDR(s) failed.\n");
341                 ast_free(sql);
342                 ast_free(sql2);
343                 return -1;
344         }
345
346         AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
347                 ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
348                 ast_str_set(&sql2, 0, " VALUES (");
349
350                 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
351                 if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
352                         ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql->str);
353                         continue;
354                 }
355
356                 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
357                         /* Check if we have a similarly named variable */
358                         ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0,
359                                 (strcasecmp(entry->cdrname, "start") == 0 ||
360                                  strcasecmp(entry->cdrname, "answer") == 0 ||
361                                  strcasecmp(entry->cdrname, "end") == 0) ? 0 : 1);
362
363                         if (colptr) {
364                                 /* Check first if the column filters this entry.  Note that this
365                                  * is very specifically NOT ast_strlen_zero(), because the filter
366                                  * could legitimately specify that the field is blank, which is
367                                  * different from the field being unspecified (NULL). */
368                                 if (entry->filtervalue && strcasecmp(colptr, entry->filtervalue) != 0) {
369                                         ast_verb(4, "CDR column '%s' with value '%s' does not match filter of"
370                                                 " '%s'.  Cancelling this CDR.\n",
371                                                 entry->cdrname, colptr, entry->filtervalue);
372                                         goto early_release;
373                                 }
374
375                                 /* Only a filter? */
376                                 if (ast_strlen_zero(entry->name))
377                                         continue;
378
379                                 LENGTHEN_BUF1(strlen(entry->name));
380
381                                 switch (entry->type) {
382                                 case SQL_CHAR:
383                                 case SQL_VARCHAR:
384                                 case SQL_LONGVARCHAR:
385                                 case SQL_BINARY:
386                                 case SQL_VARBINARY:
387                                 case SQL_LONGVARBINARY:
388                                 case SQL_GUID:
389                                         /* For these two field names, get the rendered form, instead of the raw
390                                          * form (but only when we're dealing with a character-based field).
391                                          */
392                                         if (strcasecmp(entry->name, "disposition") == 0)
393                                                 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
394                                         else if (strcasecmp(entry->name, "amaflags") == 0)
395                                                 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
396
397                                         /* Truncate too-long fields */
398                                         if (entry->type != SQL_GUID) {
399                                                 if (strlen(colptr) > entry->octetlen)
400                                                         colptr[entry->octetlen] = '\0';
401                                         }
402
403                                         ast_str_append(&sql, 0, "%s,", entry->name);
404                                         LENGTHEN_BUF2(strlen(colptr));
405
406                                         /* Encode value, with escaping */
407                                         ast_str_append(&sql2, 0, "'");
408                                         for (tmp = colptr; *tmp; tmp++) {
409                                                 if (*tmp == '\'') {
410                                                         ast_str_append(&sql2, 0, "''");
411                                                 } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
412                                                         ast_str_append(&sql2, 0, "\\\\");
413                                                 } else {
414                                                         ast_str_append(&sql2, 0, "%c", *tmp);
415                                                 }
416                                         }
417                                         ast_str_append(&sql2, 0, "',");
418                                         break;
419                                 case SQL_TYPE_DATE:
420                                         {
421                                                 int year = 0, month = 0, day = 0;
422                                                 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
423                                                         month <= 0 || month > 12 || day < 0 || day > 31 ||
424                                                         ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
425                                                         (month == 2 && year % 400 == 0 && day > 29) ||
426                                                         (month == 2 && year % 100 == 0 && day > 28) ||
427                                                         (month == 2 && year % 4 == 0 && day > 29) ||
428                                                         (month == 2 && year % 4 != 0 && day > 28)) {
429                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
430                                                         break;
431                                                 }
432
433                                                 if (year > 0 && year < 100)
434                                                         year += 2000;
435
436                                                 ast_str_append(&sql, 0, "%s,", entry->name);
437                                                 LENGTHEN_BUF2(17);
438                                                 ast_str_append(&sql2, 0, "{ d '%04d-%02d-%02d' },", year, month, day);
439                                         }
440                                         break;
441                                 case SQL_TYPE_TIME:
442                                         {
443                                                 int hour = 0, minute = 0, second = 0;
444                                                 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
445
446                                                 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
447                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
448                                                         break;
449                                                 }
450
451                                                 ast_str_append(&sql, 0, "%s,", entry->name);
452                                                 LENGTHEN_BUF2(15);
453                                                 ast_str_append(&sql2, 0, "{ t '%02d:%02d:%02d' },", hour, minute, second);
454                                         }
455                                         break;
456                                 case SQL_TYPE_TIMESTAMP:
457                                 case SQL_TIMESTAMP:
458                                         {
459                                                 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
460                                                 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
461
462                                                 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
463                                                         month <= 0 || month > 12 || day < 0 || day > 31 ||
464                                                         ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
465                                                         (month == 2 && year % 400 == 0 && day > 29) ||
466                                                         (month == 2 && year % 100 == 0 && day > 28) ||
467                                                         (month == 2 && year % 4 == 0 && day > 29) ||
468                                                         (month == 2 && year % 4 != 0 && day > 28) ||
469                                                         hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
470                                                         ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
471                                                         break;
472                                                 }
473
474                                                 if (year > 0 && year < 100)
475                                                         year += 2000;
476
477                                                 ast_str_append(&sql, 0, "%s,", entry->name);
478                                                 LENGTHEN_BUF2(26);
479                                                 ast_str_append(&sql2, 0, "{ ts '%04d-%02d-%02d %02d:%02d:%02d' },", year, month, day, hour, minute, second);
480                                         }
481                                         break;
482                                 case SQL_INTEGER:
483                                         {
484                                                 int integer = 0;
485                                                 if (sscanf(colptr, "%d", &integer) != 1) {
486                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
487                                                         break;
488                                                 }
489
490                                                 ast_str_append(&sql, 0, "%s,", entry->name);
491                                                 LENGTHEN_BUF2(12);
492                                                 ast_str_append(&sql2, 0, "%d,", integer);
493                                         }
494                                         break;
495                                 case SQL_BIGINT:
496                                         {
497                                                 long long integer = 0;
498                                                 if (sscanf(colptr, "%lld", &integer) != 1) {
499                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
500                                                         break;
501                                                 }
502
503                                                 ast_str_append(&sql, 0, "%s,", entry->name);
504                                                 LENGTHEN_BUF2(24);
505                                                 ast_str_append(&sql2, 0, "%lld,", integer);
506                                         }
507                                         break;
508                                 case SQL_SMALLINT:
509                                         {
510                                                 short integer = 0;
511                                                 if (sscanf(colptr, "%hd", &integer) != 1) {
512                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
513                                                         break;
514                                                 }
515
516                                                 ast_str_append(&sql, 0, "%s,", entry->name);
517                                                 LENGTHEN_BUF2(6);
518                                                 ast_str_append(&sql2, 0, "%d,", integer);
519                                         }
520                                         break;
521                                 case SQL_TINYINT:
522                                         {
523                                                 char integer = 0;
524                                                 if (sscanf(colptr, "%hhd", &integer) != 1) {
525                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
526                                                         break;
527                                                 }
528
529                                                 ast_str_append(&sql, 0, "%s,", entry->name);
530                                                 LENGTHEN_BUF2(4);
531                                                 ast_str_append(&sql2, 0, "%d,", integer);
532                                         }
533                                         break;
534                                 case SQL_BIT:
535                                         {
536                                                 char integer = 0;
537                                                 if (sscanf(colptr, "%hhd", &integer) != 1) {
538                                                         ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
539                                                         break;
540                                                 }
541                                                 if (integer != 0)
542                                                         integer = 1;
543
544                                                 ast_str_append(&sql, 0, "%s,", entry->name);
545                                                 LENGTHEN_BUF2(2);
546                                                 ast_str_append(&sql2, 0, "%d,", integer);
547                                         }
548                                         break;
549                                 case SQL_NUMERIC:
550                                 case SQL_DECIMAL:
551                                         {
552                                                 double number = 0.0;
553                                                 if (sscanf(colptr, "%lf", &number) != 1) {
554                                                         ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
555                                                         break;
556                                                 }
557
558                                                 ast_str_append(&sql, 0, "%s,", entry->name);
559                                                 LENGTHEN_BUF2(entry->decimals);
560                                                 ast_str_append(&sql2, 0, "%*.*lf,", entry->decimals, entry->radix, number);
561                                         }
562                                         break;
563                                 case SQL_FLOAT:
564                                 case SQL_REAL:
565                                 case SQL_DOUBLE:
566                                         {
567                                                 double number = 0.0;
568                                                 if (sscanf(colptr, "%lf", &number) != 1) {
569                                                         ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
570                                                         break;
571                                                 }
572
573                                                 ast_str_append(&sql, 0, "%s,", entry->name);
574                                                 LENGTHEN_BUF2(entry->decimals);
575                                                 ast_str_append(&sql2, 0, "%lf,", number);
576                                         }
577                                         break;
578                                 default:
579                                         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);
580                                 }
581                         }
582                 }
583
584                 /* Concatenate the two constructed buffers */
585                 LENGTHEN_BUF1(sql2->used);
586                 sql->str[sql->used - 1] = ')';
587                 sql2->str[sql2->used - 1] = ')';
588                 ast_str_append(&sql, 0, "%s", sql2->str);
589
590                 ast_verb(11, "[%s]\n", sql->str);
591
592                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql->str);
593                 if (stmt) {
594                         SQLRowCount(stmt, &rows);
595                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
596                 }
597                 if (rows == 0) {
598                         ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql->str);
599                 }
600 early_release:
601                 ast_odbc_release_obj(obj);
602         }
603         AST_RWLIST_UNLOCK(&odbc_tables);
604
605         /* Next time, just allocate buffers that are that big to start with. */
606         if (sql->used > maxsize)
607                 maxsize = sql->used;
608         if (sql2->used > maxsize2)
609                 maxsize2 = sql2->used;
610
611         ast_free(sql);
612         ast_free(sql2);
613         return 0;
614 }
615
616 static int unload_module(void)
617 {
618         ast_cdr_unregister(name);
619         usleep(1);
620         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
621                 ast_cdr_register(name, ast_module_info->description, odbc_log);
622                 ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
623                 return -1;
624         }
625
626         free_config();
627         AST_RWLIST_UNLOCK(&odbc_tables);
628         return 0;
629 }
630
631 static int load_module(void)
632 {
633         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
634                 ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
635                 return 0;
636         }
637
638         load_config();
639         AST_RWLIST_UNLOCK(&odbc_tables);
640         ast_cdr_register(name, ast_module_info->description, odbc_log);
641         return 0;
642 }
643
644 static int reload(void)
645 {
646         if (AST_RWLIST_WRLOCK(&odbc_tables)) {
647                 ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
648                 return -1;
649         }
650
651         free_config();
652         load_config();
653         AST_RWLIST_UNLOCK(&odbc_tables);
654         return 0;
655 }
656
657 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
658         .load = load_module,
659         .unload = unload_module,
660         .reload = reload,
661 );
662