Merged revisions 80360 via svnmerge from
[asterisk/asterisk.git] / cdr / cdr_adaptive_odbc.c
index 474d7fc..66f704d 100644 (file)
@@ -95,8 +95,9 @@ static int load_config(void)
        SQLLEN sqlptr;
        int res = 0;
        SQLHSTMT stmt = NULL;
+       struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
 
-       cfg = ast_config_load(CONFIG);
+       cfg = ast_config_load(CONFIG, config_flags);
        if (!cfg) {
                ast_log(LOG_WARNING, "Unable to load " CONFIG ".  No adaptive ODBC CDRs.\n");
                return -1;
@@ -114,6 +115,7 @@ static int load_config(void)
                ast_copy_string(connection, tmp, sizeof(connection));
                lenconnection = strlen(connection);
 
+               /* When loading, we want to be sure we can connect. */
                obj = ast_odbc_request_obj(connection, 1);
                if (!obj) {
                        ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ".  Check res_odbc.conf.\n", connection, catg);
@@ -154,6 +156,8 @@ static int load_config(void)
                ast_copy_string(tableptr->connection, connection, lenconnection + 1);
                ast_copy_string(tableptr->table, table, lentable + 1);
 
+               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 = "";
 
@@ -169,7 +173,7 @@ static int load_config(void)
                                if (strcasecmp(var->value, columnname) == 0) {
                                        char *tmp = ast_strdupa(var->name + 5);
                                        cdrvar = ast_strip(tmp);
-                                       ast_verbose(VERBOSE_PREFIX_4 "Found alias %s for column %s\n", cdrvar, columnname);
+                                       ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
                                        break;
                                }
                        }
@@ -195,17 +199,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, (long) entry->size, (long) 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;
 }
@@ -300,7 +312,7 @@ static int odbc_log(struct ast_cdr *cdr)
        char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp;
        char colbuf[1024], *colptr;
        SQLHSTMT stmt = NULL;
-       SQLINTEGER rows = 0;
+       SQLLEN rows = 0;
 
        if (AST_RWLIST_RDLOCK(&odbc_tables)) {
                ast_log(LOG_ERROR, "Unable to lock table list.  Insert CDR(s) failed.\n");
@@ -535,9 +547,9 @@ 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);
-               obj = ast_odbc_request_obj(tableptr->connection, 1);
+               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) {
                        stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
                        if (stmt) {