Merged revisions 73316 via svnmerge from
[asterisk/asterisk.git] / res / res_config_sqlite.c
index b3a44a9..9aa37d5 100644 (file)
  */
 
 /*!
- * \mainpage res_sqlite
+ * \mainpage res_config_sqlite
  * 
  * \section intro_sec Presentation
  * 
- * res_sqlite is a module for the Asterisk Open Source PBX to support SQLite 2
- * databases. It can be used to fetch configuration from a database (static
- * configuration files and/or using the Asterisk RealTime Architecture - ARA).
+ * res_config_sqlite is a module for the Asterisk Open Source PBX to 
+ * support SQLite 2 databases. It can be used to fetch configuration 
+ * from a database (static configuration files and/or using the Asterisk 
+ * RealTime Architecture - ARA).
  * It can also be used to log CDR entries. Finally, it can be used for simple
  * queries in the Dialplan. Note that Asterisk already comes with a module
  * named cdr_sqlite. There are two reasons for including it in res_sqlite:
  * PHP 4 in our embedded systems, and PHP 4 has no stable support for SQLite 3
  * at this time. We also needed RealTime support.
  * 
- * \section build_install_sec Building and installing
- * 
- * To build res_sqlite, simply enter <code>make</code>. To install it,
- * enter make install. The Makefile has been slightly designed for
- * cross compilation and installation in non standard locations, to ease
- * the work of packagers. Read it for more details.
- * 
  * \section conf_sec Configuration
  * 
  * The main configuration file is res_config_sqlite.conf. It must be readable or
  * 
  * \section credits_sec Credits
  * 
- * res_sqlite was developed by Richard Braun at the Proformatique company.
+ * res_config_sqlite was developed by Richard Braun at the Proformatique company.
  */
 
 /*!
- * \file res_sqlite.c
+ * \file 
  * \brief res_sqlite module.
  */
 
@@ -304,6 +298,7 @@ static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames);
  * \param table                the table to use
  * \param file          the file to load from the database
  * \param cfg                  the struct ast_config object to use when storing variables
+ * \param withcomments Integer. Flag
  * \return NULL if an error occurred, cfg otherwise
  * \see add_cfg_entry()
  */
@@ -599,7 +594,7 @@ static char *sql_get_config_table =
 static int set_var(char **var, char *name, char *value)
 {
        if (*var)
-               free(*var);
+               ast_free(*var);
 
        *var = ast_strdup(value);
 
@@ -660,11 +655,11 @@ static int load_config(void)
 
 static void unload_config(void)
 {
-       free(dbfile);
+       ast_free(dbfile);
        dbfile = NULL;
-       free(config_table);
+       ast_free(config_table);
        config_table = NULL;
-       free(cdr_table);
+       ast_free(cdr_table);
        cdr_table = NULL;
 }
 
@@ -690,7 +685,7 @@ static int cdr_handler(struct ast_cdr *cdr)
 
        if (error) {
                ast_log(LOG_ERROR, "%s\n", errormsg);
-               free(errormsg);
+               ast_free(errormsg);
                return 1;
        }
 
@@ -717,7 +712,7 @@ static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
                        return 1;
                }
 
-               free(args->cat_name);
+               ast_free(args->cat_name);
                args->cat_name = ast_strdup(argv[RES_SQLITE_CONFIG_CATEGORY]);
 
                if (!args->cat_name) {
@@ -769,11 +764,11 @@ static struct ast_config *config_handler(const char *database,
 
        ast_mutex_unlock(&mutex);
 
-       free(args.cat_name);
+       ast_free(args.cat_name);
 
        if (error) {
                ast_log(LOG_ERROR, "%s\n", errormsg);
-               free(errormsg);
+               ast_free(errormsg);
                return NULL;
        }
 
@@ -791,15 +786,15 @@ static size_t get_params(va_list ap, const char ***params_ptr, const char ***val
 
        while ((param = va_arg(ap, const char *)) && (val = va_arg(ap, const char *))) {
                if (!(tmp = ast_realloc(params, (params_count + 1) * sizeof(char *)))) {
-                       free(params);
-                       free(vals);
+                       ast_free(params);
+                       ast_free(vals);
                        return 0;
                }
                params = tmp;
 
                if (!(tmp = ast_realloc(vals, (params_count + 1) * sizeof(char *)))) {
-                       free(params);
-                       free(vals);
+                       ast_free(params);
+                       ast_free(vals);
                        return 0;
                }
                vals = tmp;
@@ -809,12 +804,11 @@ static size_t get_params(va_list ap, const char ***params_ptr, const char ***val
                params_count++;
        }
 
-       if (params_count)
-               ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
-       else {
+       if (params_count > 0) {
                *params_ptr = params;
                *vals_ptr = vals;
-       }
+       } else
+               ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
 
        return params_count;
 }
@@ -878,8 +872,8 @@ realtime_handler(const char *database, const char *table, va_list ap)
 
        if (!query) {
                ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
-               free(params);
-               free(vals);
+               ast_free(params);
+               ast_free(vals);
                return NULL;
        }
 
@@ -894,8 +888,8 @@ realtime_handler(const char *database, const char *table, va_list ap)
 
                        if (!tmp_str) {
                                ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
-                               free(params);
-                               free(vals);
+                               ast_free(params);
+                               ast_free(vals);
                                return NULL;
                        }
 
@@ -903,8 +897,8 @@ realtime_handler(const char *database, const char *table, va_list ap)
                }
        }
 
-       free(params);
-       free(vals);
+       ast_free(params);
+       ast_free(vals);
 
        tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
        sqlite_freemem(query);
@@ -915,7 +909,7 @@ realtime_handler(const char *database, const char *table, va_list ap)
        }
 
        query = tmp_str;
-       ast_log(LOG_DEBUG, "SQL query: %s\n", query);
+       ast_debug(1, "SQL query: %s\n", query);
        args.var = NULL;
        args.last = NULL;
 
@@ -931,7 +925,7 @@ realtime_handler(const char *database, const char *table, va_list ap)
 
        if (error) {
                ast_log(LOG_WARNING, "%s\n", errormsg);
-               free(errormsg);
+               ast_free(errormsg);
                ast_variables_destroy(args.var);
                return NULL;
        }
@@ -1014,8 +1008,8 @@ static struct ast_config *realtime_multi_handler(const char *database,
 
        if (!(initfield = ast_strdup(params[0]))) {
                ast_config_destroy(cfg);
-               free(params);
-               free(vals);
+               ast_free(params);
+               ast_free(vals);
                return NULL;
        }
 
@@ -1040,9 +1034,9 @@ static struct ast_config *realtime_multi_handler(const char *database,
        if (!(query = sqlite_mprintf(QUERY, table, params[0], op, tmp_str))) {
                ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
                ast_config_destroy(cfg);
-               free(params);
-               free(vals);
-               free(initfield);
+               ast_free(params);
+               ast_free(vals);
+               ast_free(initfield);
                return NULL;
        }
 
@@ -1058,9 +1052,9 @@ static struct ast_config *realtime_multi_handler(const char *database,
                        if (!tmp_str) {
                                ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
                                ast_config_destroy(cfg);
-                               free(params);
-                               free(vals);
-                               free(initfield);
+                               ast_free(params);
+                               ast_free(vals);
+                               ast_free(initfield);
                                return NULL;
                        }
 
@@ -1068,19 +1062,19 @@ static struct ast_config *realtime_multi_handler(const char *database,
                }
        }
 
-       free(params);
-       free(vals);
+       ast_free(params);
+       ast_free(vals);
 
        if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
                ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
                ast_config_destroy(cfg);
-               free(initfield);
+               ast_free(initfield);
                return NULL;
        }
 
        sqlite_freemem(query);
        query = tmp_str;
-       ast_log(LOG_DEBUG, "SQL query: %s\n", query);
+       ast_debug(1, "SQL query: %s\n", query);
        args.cfg = cfg;
        args.initfield = initfield;
 
@@ -1093,11 +1087,11 @@ static struct ast_config *realtime_multi_handler(const char *database,
        ast_mutex_unlock(&mutex);
 
        sqlite_freemem(query);
-       free(initfield);
+       ast_free(initfield);
 
        if (error) {
                ast_log(LOG_WARNING, "%s\n", errormsg);
-               free(errormsg);
+               ast_free(errormsg);
                ast_config_destroy(cfg);
                return NULL;
        }
@@ -1129,8 +1123,8 @@ static int realtime_update_handler(const char *database, const char *table,
 
        if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
                ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
-               free(params);
-               free(vals);
+               ast_free(params);
+               ast_free(vals);
                return -1;
        }
 
@@ -1144,8 +1138,8 @@ static int realtime_update_handler(const char *database, const char *table,
 
                        if (!tmp_str) {
                                ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
-                               free(params);
-                               free(vals);
+                               ast_free(params);
+                               ast_free(vals);
                                return -1;
                        }
 
@@ -1153,8 +1147,8 @@ static int realtime_update_handler(const char *database, const char *table,
                }
        }
 
-       free(params);
-       free(vals);
+       ast_free(params);
+       ast_free(vals);
 
        if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
                ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
@@ -1163,7 +1157,7 @@ static int realtime_update_handler(const char *database, const char *table,
 
        sqlite_freemem(query);
        query = tmp_str;
-       ast_log(LOG_DEBUG, "SQL query: %s\n", query);
+       ast_debug(1, "SQL query: %s\n", query);
 
        ast_mutex_lock(&mutex);
 
@@ -1182,7 +1176,7 @@ static int realtime_update_handler(const char *database, const char *table,
 
        if (error) {
                ast_log(LOG_WARNING, "%s\n", errormsg);
-               free(errormsg);
+               ast_free(errormsg);
        }
 
        return rows_num;
@@ -1245,7 +1239,7 @@ static int load_module(void)
 
        if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
                ast_log(LOG_ERROR, "%s\n", errormsg);
-               free(errormsg);
+               ast_free(errormsg);
                unload_module();
                return 1;
        }
@@ -1264,7 +1258,7 @@ static int load_module(void)
                         */
                        if (error != SQLITE_ERROR) {
                                ast_log(LOG_ERROR, "%s\n", errormsg);
-                               free(errormsg);
+                               ast_free(errormsg);
                                unload_module();
                                return 1;
                        }
@@ -1276,7 +1270,7 @@ static int load_module(void)
 
                        if (error) {
                                ast_log(LOG_ERROR, "%s\n", errormsg);
-                               free(errormsg);
+                               ast_free(errormsg);
                                unload_module();
                                return 1;
                        }