[svn-commits] twilson: branch twilson/res_config_sqlite3 r334114 - in /team/twilson/res_con...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Aug 31 12:28:25 CDT 2011


Author: twilson
Date: Wed Aug 31 12:28:22 2011
New Revision: 334114

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=334114
Log:
Add static realtime support

Added:
    team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample   (with props)
Modified:
    team/twilson/res_config_sqlite3/res/res_config_sqlite3.c

Added: team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample
URL: http://svnview.digium.com/svn/asterisk/team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample?view=auto&rev=334114
==============================================================================
--- team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample (added)
+++ team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample Wed Aug 31 12:28:22 2011
@@ -1,0 +1,4 @@
+[general]
+
+; The database file. Defaults to the astdb database file: astdb.sqlite3
+;dbfile => /var/lib/asterisk/sqlite.db

Propchange: team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/twilson/res_config_sqlite3/configs/res_config_sqlite3.conf.sample
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/twilson/res_config_sqlite3/res/res_config_sqlite3.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/res_config_sqlite3/res/res_config_sqlite3.c?view=diff&rev=334114&r1=334113&r2=334114
==============================================================================
--- team/twilson/res_config_sqlite3/res/res_config_sqlite3.c (original)
+++ team/twilson/res_config_sqlite3/res/res_config_sqlite3.c Wed Aug 31 12:28:22 2011
@@ -74,23 +74,12 @@
 static struct {
 	AST_DECLARE_STRING_FIELDS(
 		AST_STRING_FIELD(filename);
-		AST_STRING_FIELD(config_table);
-		AST_STRING_FIELD(cdr_table);
 	);
 } realtime_sqlite3_config;
 
 AST_MUTEX_DEFINE_STATIC(config_lock);
 AST_MUTEX_DEFINE_STATIC(db_lock);
 static sqlite3 *db;
-
-/*!
- * \return ast_config on success, NULL on failure
- */
-static struct ast_config *realtime_sqlite3_load(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked)
-{
-	/* Currently no handling of realtime pbx stuff */
-	return NULL;
-}
 
 static int row_to_varlist(void *arg, int num_columns, char **values, char **columns)
 {
@@ -129,6 +118,102 @@
 	ast_category_append(cfg, cat);
 
 	return 0;
+}
+
+/*!
+ * Structure sent to the SQLite callback function for static configuration.
+ *
+ * \see static_realtime_cb()
+ */
+struct cfg_entry_args {
+	struct ast_config *cfg;
+	struct ast_category *cat;
+	char *cat_name;
+	struct ast_flags flags;
+	const char *who_asked;
+};
+
+enum {
+	COL_CATEGORY,
+	COL_VAR_NAME,
+	COL_VAR_VAL,
+	COL_COLUMNS,
+};
+
+static int static_realtime_cb(void *arg, int num_columns, char **values, char **columns)
+{
+	struct cfg_entry_args *args = arg;
+	struct ast_variable *var;
+
+	if (!strcmp(values[COL_VAR_NAME], "#include")) {
+		struct ast_config *cfg;
+		char *val;
+
+		val = values[COL_VAR_VAL];
+		if (!(cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked))) {
+			ast_log(LOG_WARNING, "Unable to include %s\n", val);
+			return SQLITE_ABORT;
+		} else {
+			args->cfg = cfg;
+			return 0;
+		}
+	}
+
+	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");
+			return SQLITE_ABORT;
+		}
+
+		ast_free(args->cat_name);
+
+		if (!(args->cat_name = ast_strdup(values[COL_CATEGORY]))) {
+			ast_category_destroy(args->cat);
+			return SQLITE_ABORT;
+		}
+
+		ast_category_append(args->cfg, args->cat);
+	}
+
+	if (!(var = ast_variable_new(values[COL_VAR_NAME], values[COL_VAR_VAL], ""))) {
+		ast_log(LOG_WARNING, "Unable to allocate variable");
+		return SQLITE_ABORT;
+	}
+
+	ast_variable_append(args->cat, var);
+
+	return 0;
+}
+
+/*!
+ * \return ast_config on success, NULL on failure
+ */
+static struct ast_config *realtime_sqlite3_load(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked)
+{
+	char *sql, *errmsg;
+	struct cfg_entry_args args;
+
+	if (!(sql = sqlite3_mprintf("SELECT category, var_name, var_val FROM %Q WHERE filename = %Q AND commented = 0 ORDER BY cat_metric ASC, var_metric ASC", table, configfile))) {
+		ast_log(LOG_WARNING, "Couldn't allocate query\n");
+		return NULL;	
+	};
+
+	args.cfg = config;
+	args.cat = NULL;
+	args.cat_name = NULL;
+	args.flags = flags;
+	args.who_asked = who_asked;
+
+	ast_mutex_lock(&db_lock);
+	if (sqlite3_exec(db, sql, static_realtime_cb, &args, &errmsg)) {
+		ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg);
+		sqlite3_free(errmsg);
+	}
+	ast_mutex_unlock(&db_lock);
+
+	sqlite3_free(sql);
+
+	return config;
 }
 
 static int realtime_sqlite3_helper(const char *table, va_list ap, int is_multi, void *arg)
@@ -428,8 +513,6 @@
 
 	ast_mutex_lock(&config_lock);
 	ast_string_field_set(&realtime_sqlite3_config, filename, "");
-	ast_string_field_set(&realtime_sqlite3_config, config_table, "");
-	ast_string_field_set(&realtime_sqlite3_config, cdr_table, "");
 
 	if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
 		ast_log(LOG_ERROR, "%s config file '%s', using default values\n",
@@ -438,8 +521,6 @@
 		for (var = ast_variable_browse(config, "general"); var; var = var->next) {
 			if (!strcasecmp(var->name, "dbfile")) {
 				ast_string_field_set(&realtime_sqlite3_config, filename, var->value);
-			} else if (!strcasecmp(var->name, "config_table")) {
-				ast_string_field_set(&realtime_sqlite3_config, config_table, var->value);
 			}
 		}
 	}




More information about the svn-commits mailing list