]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
realtime: Centralize some common realtime backend code 46/5046/3
authorSean Bright <sean.bright@gmail.com>
Tue, 21 Feb 2017 14:56:54 +0000 (09:56 -0500)
committerSean Bright <sean.bright@gmail.com>
Tue, 21 Feb 2017 16:53:12 +0000 (10:53 -0600)
All of the realtime backends create artificial ast_categorys to pass
back into the core as query results. These categories have no filename
or line number information associated with them and the backends differ
slightly on how they create them. So create a couple helper macros to
help make things more consistent.

Also updated the call sites to remove redundant error messages about
memory allocation failure.

Note that res_config_ldap sets the category filename to the 'table name'
but that is not read by anything in the core, so I've dropped it.

Change-Id: I3a1fd91e0c807dea1ce3b643b0a6fe5be9002897

addons/res_config_mysql.c
include/asterisk/config.h
res/res_config_curl.c
res/res_config_ldap.c
res/res_config_odbc.c
res/res_config_pgsql.c
res/res_config_sqlite.c
res/res_config_sqlite3.c

index cd72b6c44d6c38e3c21a77e958c397127fd0b41f..259650d4f310312beea84983c35707e75083bd94 100644 (file)
@@ -525,9 +525,8 @@ static struct ast_config *realtime_multi_mysql(const char *database, const char
 
                while ((row = mysql_fetch_row(result))) {
                        var = NULL;
-                       cat = ast_category_new("", "", -1);
+                       cat = ast_category_new_anonymous();
                        if (!cat) {
-                               ast_log(LOG_WARNING, "Out of memory!\n");
                                continue;
                        }
                        for (i = 0; i < numFields; i++) {
@@ -936,8 +935,8 @@ static struct ast_config *config_mysql(const char *database, const char *table,
                        }
 
                        if (strcmp(last, row[0]) || last_cat_metric != atoi(row[3])) {
-                               if (!(cur_cat = ast_category_new(row[0], "", -1))) {
-                                       ast_log(LOG_WARNING, "Out of memory!\n");
+                               cur_cat = ast_category_new_dynamic(row[0]);
+                               if (!cur_cat) {
                                        break;
                                }
                                strcpy(last, row[0]);
index 4944a3af2022bff802fec9fe7b432cdf3275b84e..f57966b0babe7e9c26b212810b94e82f1f9779f7 100644 (file)
@@ -833,6 +833,18 @@ const char *ast_config_option(struct ast_config *cfg, const char *cat, const cha
  */
 struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno);
 
+/*!
+ * \brief Create a category that is not backed by a file
+ *
+ * \param name name of new category
+ */
+#define ast_category_new_dynamic(name) ast_category_new(name, "", -1)
+
+/*!
+ * \brief Create a nameless category that is not backed by a file
+ */
+#define ast_category_new_anonymous() ast_category_new_dynamic("")
+
 /*!
  * \brief Create a category making it a template
  *
index 7b571d549b7eb059109dca0be50f3f12039aaa73..90137e51c8a98eb4ed28b6f5c13d335357a8cc48 100644 (file)
@@ -184,7 +184,8 @@ static struct ast_config *realtime_multi_curl(const char *url, const char *unuse
                        continue;
                }
 
-               if (!(cat = ast_category_new("", "", 99999))) {
+               cat = ast_category_new_anonymous();
+               if (!cat) {
                        continue;
                }
 
@@ -571,8 +572,10 @@ static struct ast_config *config_curl(const char *url, const char *unused, const
                }
 
                if (!cat || strcmp(category, cur_cat) || last_cat_metric != cat_metric) {
-                       if (!(cat = ast_category_new(category, "", 99999)))
+                       cat = ast_category_new_dynamic(category);
+                       if (!cat) {
                                break;
+                       }
                        cur_cat = category;
                        last_cat_metric = cat_metric;
                        ast_category_append(cfg, cat);
index 8454273799a19ab58021a7286ca5d7b8b913602e..99e7736fc21481a0ec5cf9566f356e05934128a2 100644 (file)
@@ -1045,10 +1045,8 @@ static struct ast_config *realtime_multi_ldap(const char *basedn,
                        struct ast_variable **p = vars;
 
                        while (*p) {
-                               struct ast_category *cat = NULL;
-                               cat = ast_category_new("", table_name, -1);
+                               struct ast_category *cat = ast_category_new_anonymous();
                                if (!cat) {
-                                       ast_log(LOG_ERROR, "Unable to create a new category!\n");
                                        break;
                                } else {
                                        struct ast_variable *var = *p;
@@ -1197,7 +1195,7 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
                if (!last_category || strcmp(last_category, categories[i].name) ||
                        last_category_metric != categories[i].metric) {
 
-                       cur_cat = ast_category_new(categories[i].name, table_name, -1);
+                       cur_cat = ast_category_new_dynamic(categories[i].name);
                        if (!cur_cat) {
                                break;
                        }
index 161dc534501e997906c8f99098f91a85f7b437f8..7e2cfcb2c16ba03082198ac8fb22930cd483fe42 100644 (file)
@@ -409,9 +409,8 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                        ast_log(LOG_WARNING, "SQL Fetch error! [%s]\n", ast_str_buffer(sql));
                        continue;
                }
-               cat = ast_category_new("","",99999);
+               cat = ast_category_new_anonymous();
                if (!cat) {
-                       ast_log(LOG_WARNING, "Out of memory!\n");
                        continue;
                }
                for (x=0;x<colcount;x++) {
@@ -1018,9 +1017,8 @@ static struct ast_config *config_odbc(const char *database, const char *table, c
                        continue;
                } 
                if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) {
-                       cur_cat = ast_category_new(q.category, "", 99999);
+                       cur_cat = ast_category_new_dynamic(q.category);
                        if (!cur_cat) {
-                               ast_log(LOG_WARNING, "Out of memory!\n");
                                break;
                        }
                        strcpy(last, q.category);
index d585c66eb5a3b941d905b4a07ec4440120847f61..325dca8dc8ae7219232a136a7cf210f8dcdac7e2 100644 (file)
@@ -650,8 +650,10 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
 
                for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
                        var = NULL;
-                       if (!(cat = ast_category_new("","",99999)))
+                       cat = ast_category_new_anonymous();
+                       if (!cat) {
                                continue;
+                       }
                        for (i = 0; i < numFields; i++) {
                                stringp = PQgetvalue(result, rowIndex, i);
                                while (stringp) {
@@ -1131,9 +1133,10 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                        }
 
                        if (strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
-                               cur_cat = ast_category_new(field_category, "", 99999);
-                               if (!cur_cat)
+                               cur_cat = ast_category_new_dynamic(field_category);
+                               if (!cur_cat) {
                                        break;
+                               }
                                ast_copy_string(last, field_category, sizeof(last));
                                last_cat_metric = atoi(field_cat_metric);
                                ast_category_append(cfg, cur_cat);
index 4375c3960ea8ea4b54cbc4cc5be6cd9eccc8dea0..d1169478058815ee6069661704df3b88ca6213c9 100644 (file)
@@ -875,10 +875,8 @@ static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
        }
 
        if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
-               args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
-
+               args->cat = ast_category_new_dynamic(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
                if (!args->cat) {
-                       ast_log(LOG_WARNING, "Unable to allocate category\n");
                        return 1;
                }
 
@@ -1087,8 +1085,8 @@ static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **colum
                return 1;
        }
 
-       if (!(cat = ast_category_new(cat_name, "", 99999))) {
-               ast_log(LOG_WARNING, "Unable to allocate category\n");
+       cat = ast_category_new_dynamic(cat_name);
+       if (!cat) {
                return 1;
        }
 
index cb1a290738b620d128e41df0f1a2d803b69e3c76..0d553b8444477049caa12639a358ebb56d4c3321 100644 (file)
@@ -505,7 +505,8 @@ static int append_row_to_cfg(void *arg, int num_columns, char **values, char **c
        struct ast_category *cat;
        int i;
 
-       if (!(cat = ast_category_new("", "", 99999))) {
+       cat = ast_category_new_anonymous();
+       if (!cat) {
                return SQLITE_ABORT;
        }
 
@@ -725,8 +726,8 @@ static int static_realtime_cb(void *arg, int num_columns, char **values, char **
        }
 
        if (!args->cat_name || strcmp(args->cat_name, values[COL_CATEGORY])) {
-               if (!(args->cat = ast_category_new(values[COL_CATEGORY], "", 99999))) {
-                       ast_log(LOG_WARNING, "Unable to allocate category\n");
+               args->cat = ast_category_new_dynamic(values[COL_CATEGORY]);
+               if (!args->cat) {
                        return SQLITE_ABORT;
                }