Some ODBC drivers don't set the CHAR_OCTET_LENGTH field correctly.
[asterisk/asterisk.git] / cdr / cdr_adaptive_odbc.c
index 7e5a7f8..12e9820 100644 (file)
@@ -155,8 +155,7 @@ static int load_config(void)
                ast_copy_string(tableptr->connection, connection, lenconnection + 1);
                ast_copy_string(tableptr->table, table, lentable + 1);
 
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
+               ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
 
                while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
                        char *cdrvar = "";
@@ -173,8 +172,7 @@ static int load_config(void)
                                if (strcasecmp(var->value, columnname) == 0) {
                                        char *tmp = ast_strdupa(var->name + 5);
                                        cdrvar = ast_strip(tmp);
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
+                                       ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
                                        break;
                                }
                        }
@@ -200,17 +198,25 @@ static int load_config(void)
                        SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
                        SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
 
+                       /* Specification states that the octenlen should be the maximum number of bytes
+                        * returned in a char or binary column, but it seems that some drivers just set
+                        * it to NULL. (Bad Postgres! No biscuit!) */
+                       if (entry->octetlen == 0)
+                               entry->octetlen = entry->size;
+
+                       ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, entry->size, entry->octetlen, entry->decimals, entry->radix);
                        /* Insert column info into column list */
                        AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
                        res = 0;
                }
 
+               SQLFreeHandle(SQL_HANDLE_STMT, stmt);
                ast_odbc_release_obj(obj);
 
                if (AST_LIST_FIRST(&(tableptr->columns)))
                        AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
                else
-                       free(tableptr);
+                       ast_free(tableptr);
        }
        return res;
 }
@@ -540,8 +546,7 @@ static int odbc_log(struct ast_cdr *cdr)
                sql2[lensql2 - 1] = ')';
                strcat(sql + lensql, sql2);
 
-               if (option_verbose > 10)
-                       ast_verbose(VERBOSE_PREFIX_4 "[%s]\n", sql);
+               ast_verb(11, "[%s]\n", sql);
                /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
                obj = ast_odbc_request_obj(tableptr->connection, 0);
                if (obj) {