Fix static db problem
[asterisk/asterisk.git] / res / res_config_odbc.c
index 786e14b..2a27c06 100755 (executable)
@@ -174,6 +174,8 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
        char sql[1024];
        char coltitle[256];
        char rowdata[2048];
+       char *title=NULL;
+       const char *initfield=NULL;
        char *op;
        const char *newparam, *newval;
        char *stringp;
@@ -213,6 +215,9 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                SQLFreeHandle (SQL_HANDLE_STMT, stmt);
                return NULL;
        }
+       initfield = ast_strdupa(newparam);
+       if (initfield && (op = strchr(initfield, ' '))) 
+               *op = '\0';
        newval = va_arg(aq, const char *);
        if (!strchr(newparam, ' ')) op = " ="; else op = "";
        snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
@@ -221,6 +226,8 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
                newval = va_arg(aq, const char *);
        }
+       if (initfield)
+               snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
        va_end(aq);
        res = SQLPrepare(stmt, sql, SQL_NTS);
        if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
@@ -259,7 +266,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                return NULL;
        }
 
-       while (rowcount) {
+       while (rowcount--) {
                var = NULL;
                prev = NULL;
                res = SQLFetch(stmt);
@@ -289,6 +296,8 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                        while(stringp) {
                                chunk = strsep(&stringp, ";");
                                if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
+                                       if (initfield && !strcmp(initfield, coltitle) && !title)
+                                               title = ast_strdupa(chunk);
                                        if (prev) {
                                                prev->next = ast_new_variable(coltitle, chunk);
                                                if (prev->next)
@@ -298,20 +307,20 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                                        
                                }
                        }
-                       if (var) {
-                               cat = ast_new_category("");
-                               if (cat) {
-                                       cat->root = var;
-                                       if (!cfg) 
-                                               cfg = ast_new_config();
-                                       if (cfg)
-                                               ast_category_append(cfg, cat);
-                                       else 
-                                               ast_category_destroy(cat);
-                               } else {
-                                       ast_log(LOG_WARNING, "Out of memory!\n");
-                                       ast_destroy_realtime(var);
-                               }
+               }
+               if (var) {
+                       cat = ast_new_category(title ? title : "");
+                       if (cat) {
+                               cat->root = var;
+                               if (!cfg) 
+                                       cfg = ast_new_config();
+                               if (cfg)
+                                       ast_category_append(cfg, cat);
+                               else 
+                                       ast_category_destroy(cat);
+                       } else {
+                               ast_log(LOG_WARNING, "Out of memory!\n");
+                               ast_destroy_realtime(var);
                        }
                }
        }
@@ -398,7 +407,7 @@ static int update_odbc(const char *database, const char *table, const char *keyf
        return -1;
 }
 
-static struct ast_config *config_odbc (const char *database, const char *table, const char *file, struct ast_config *new_config_s, struct ast_category **new_cat_p, struct ast_variable **new_v_p, int recur)
+static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *new_config_s, struct ast_category **new_cat_p, int recur)
 {
        struct ast_config *new;
        struct ast_variable *cur_v, *new_v;
@@ -407,7 +416,7 @@ static struct ast_config *config_odbc (const char *database, const char *table,
        odbc_obj *obj;
        SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
        SQLBIGINT id;
-       char sql[255] = "", filename[128], category[128], var_name[128], var_val[128];
+       char sql[255] = "", filename[128], category[128], var_name[128], var_val[512];
        SQLSMALLINT rowcount=0;
        SQLHSTMT stmt;
        char last[80] = "";
@@ -416,7 +425,7 @@ static struct ast_config *config_odbc (const char *database, const char *table,
 
 
        if (!file || !strcmp (file, "res_config_odbc.conf"))
-               return NULL;            // cant configure myself with myself !
+               return NULL;            /* cant configure myself with myself ! */
 
        obj = fetch_odbc_obj(database);
        if (!obj)
@@ -468,7 +477,7 @@ static struct ast_config *config_odbc (const char *database, const char *table,
                cat_started = 0;
 
                cur_cat = *new_cat_p;
-               cur_v = *new_v_p;
+               cur_v = NULL;
 
                if (cur_cat)
                        cat_started = 1;
@@ -478,7 +487,7 @@ static struct ast_config *config_odbc (const char *database, const char *table,
                while (res != SQL_NO_DATA) {
                        if (!strcmp (var_name, "#include") && recur < MAX_INCLUDE_LEVEL) {
 
-                               config_odbc(database, table, var_val, new, &cur_cat, &cur_v, recur + 1);
+                               config_odbc(database, table, var_val, new, &cur_cat, recur + 1);
                        } else {
                                if (strcmp (last, category) || last_cat_metric != cat_metric) {
                                        strncpy(last, category, sizeof(last) - 1);
@@ -509,7 +518,7 @@ static struct ast_config *config_odbc (const char *database, const char *table,
                                }
                        }
 
-               // next row 
+               /* next row  */
                        res = SQLFetch (stmt);
                }