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