[asterisk-commits] twilson: branch twilson/sqlite3_playground r342014 - in /team/twilson/sqlite3...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Oct 23 19:00:13 CDT 2011


Author: twilson
Date: Sun Oct 23 19:00:09 2011
New Revision: 342014

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=342014
Log:
Add realtime_replace function

devolves into a call to update possibly followed by a call to store
if a native replace function isn't available.

Modified:
    team/twilson/sqlite3_playground/include/asterisk/config.h
    team/twilson/sqlite3_playground/main/config.c
    team/twilson/sqlite3_playground/main/db.c
    team/twilson/sqlite3_playground/main/realtime_sqlite3.c

Modified: team/twilson/sqlite3_playground/include/asterisk/config.h
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/include/asterisk/config.h?view=diff&rev=342014&r1=342013&r2=342014
==============================================================================
--- team/twilson/sqlite3_playground/include/asterisk/config.h (original)
+++ team/twilson/sqlite3_playground/include/asterisk/config.h Sun Oct 23 19:00:09 2011
@@ -106,6 +106,7 @@
 typedef int realtime_update(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
 typedef int realtime_update2(const char *database, const char *table, va_list ap);
 typedef int realtime_store(const char *database, const char *table, va_list ap);
+typedef int realtime_replace(const char *database, const char *table, va_list ap);
 typedef int realtime_destroy(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
 
 /*!
@@ -129,6 +130,7 @@
 	realtime_update *update_func;
 	realtime_update2 *update2_func;
 	realtime_store *store_func;
+	realtime_replace *replace_func;
 	realtime_destroy *destroy_func;
 	realtime_require *require_func;
 	realtime_unload *unload_func;
@@ -440,6 +442,23 @@
  * order to preserve cross-platform compatibility.
  */
 int ast_store_realtime(const char *family, ...) attribute_sentinel;
+
+/*!
+ * \brief Replace a realtime configuration
+ * \since 11
+ *
+ * \param family which family/config to be replaced
+ *
+ * \deatils
+ * This function will update an existing configuration if it exists and create
+ * a new one if it doesn't. If a native replace funciton is not provided, this
+ * will do the equivalent of an update, followed by a store if no records were
+ * updated. The first two fields passed must define the key to match off of.
+ *
+ * \note You should use the constant SENTINEL to terminate arguments, in
+ * order to preserve cross-platform compatibility.
+ */
+int ast_replace_realtime(const char *family,...) attribute_sentinel;
 
 /*!
  * \brief Destroy realtime configuration

Modified: team/twilson/sqlite3_playground/main/config.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/config.c?view=diff&rev=342014&r1=342013&r2=342014
==============================================================================
--- team/twilson/sqlite3_playground/main/config.c (original)
+++ team/twilson/sqlite3_playground/main/config.c Sun Oct 23 19:00:09 2011
@@ -2532,8 +2532,8 @@
 	va_start(ap, lookup);
 	for (i = 1; ; i++) {
 		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
-			/* If the update succeeds, it returns 0. */
-			if (eng->update_func && !(res = eng->update_func(db, table, keyfield, lookup, ap))) {
+			/* If the update succeeds, it returns >= 0. */
+			if (eng->update_func && (res = eng->update_func(db, table, keyfield, lookup, ap)) >= 0) {
 				break;
 			}
 		} else {
@@ -2556,7 +2556,7 @@
 	va_start(ap, family);
 	for (i = 1; ; i++) {
 		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
-			if (eng->update2_func && !(res = eng->update2_func(db, table, ap))) {
+			if (eng->update2_func && (res = eng->update2_func(db, table, ap)) >= 0) {
 				break;
 			}
 		} else {
@@ -2582,6 +2582,47 @@
 			/* If the store succeeds, it returns 0. */
 			if (eng->store_func && !(res = eng->store_func(db, table, ap))) {
 				break;
+			}
+		} else {
+			break;
+		}
+	}
+	va_end(ap);
+
+	return res;
+}
+
+int ast_replace_realtime(const char *family, ...)
+{
+	struct ast_config_engine *eng;
+	int res = -1, i;
+	char db[256];
+	char table[256];
+	va_list ap;
+
+	va_start(ap, family);
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			if (eng->replace_func) {
+				if ((res = eng->replace_func(db, table, ap)) >= 0) {
+					break;
+				}
+			} else {
+				va_list ap_copy;
+				const char *key, *value;
+				va_copy(ap_copy, ap);
+				if (eng->update_func && (key = va_arg(ap_copy, typeof(key))) && (value = va_arg(ap_copy, typeof(value)))) {
+					/* No update configs, try a store */
+					if (!(res = eng->update_func(db, table, key, value, ap))) {
+						if (eng->store_func && (res = eng->store_func(db, table, ap)) >= 0) {
+							break;
+						}
+					} else if (res >= 0) {
+						/* Successful update, don't go to next matchign engine */
+						break;
+					}
+				}
+				va_end(ap_copy);
 			}
 		} else {
 			break;

Modified: team/twilson/sqlite3_playground/main/db.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/db.c?view=diff&rev=342014&r1=342013&r2=342014
==============================================================================
--- team/twilson/sqlite3_playground/main/db.c (original)
+++ team/twilson/sqlite3_playground/main/db.c Sun Oct 23 19:00:09 2011
@@ -110,10 +110,7 @@
 		return -1;
 	}
 
-	if (!ast_update_realtime(ast_db_family, ast_db_key, ast_str_buffer(fullkey), ast_db_value, value, SENTINEL)) {
-		/* Updated 0 records so we must store a new value */
-		ast_store_realtime(ast_db_family, ast_db_key, ast_str_buffer(fullkey), ast_db_value, value, SENTINEL);
-	}
+	ast_replace_realtime(ast_db_family, ast_db_key, ast_str_buffer(fullkey), ast_db_value, value, SENTINEL);
 
 	ast_free(fullkey);
 

Modified: team/twilson/sqlite3_playground/main/realtime_sqlite3.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/realtime_sqlite3.c?view=diff&rev=342014&r1=342013&r2=342014
==============================================================================
--- team/twilson/sqlite3_playground/main/realtime_sqlite3.c (original)
+++ team/twilson/sqlite3_playground/main/realtime_sqlite3.c Sun Oct 23 19:00:09 2011
@@ -58,6 +58,7 @@
 static int realtime_sqlite3_update(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
 static int realtime_sqlite3_update2(const char *database, const char *table, va_list ap);
 static int realtime_sqlite3_store(const char *database, const char *table, va_list ap);
+static int realtime_sqlite3_replace(const char *database, const char *table, va_list ap);
 static int realtime_sqlite3_destroy(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
 static int realtime_sqlite3_require(const char *database, const char *table, va_list ap);
 static int realtime_sqlite3_unload(const char *database, const char *table);
@@ -70,6 +71,7 @@
 	.update_func = realtime_sqlite3_update,
 	.update2_func = realtime_sqlite3_update2,
 	.store_func = realtime_sqlite3_store,
+	.replace_func = realtime_sqlite3_replace,
 	.destroy_func = realtime_sqlite3_destroy,
 	.require_func = realtime_sqlite3_require,
 	.unload_func = realtime_sqlite3_unload,
@@ -815,7 +817,7 @@
 /*! \brief Realtime callback for inserting a row
  * \return Number of rows affected or -1 on error
  */
-static int realtime_sqlite3_store(const char *database, const char *table, va_list ap)
+static int realtime_sqlite3_store_helper(const char *database, const char *table, int replace, va_list ap)
 {
 	struct ast_str *sql, *values;
 	const char *column, *value;
@@ -837,7 +839,7 @@
 
 	while ((column = va_arg(ap, const char *)) && (value = va_arg(ap, const char *))) {
 		if (first) {
-			ast_str_set(&sql, 0, "INSERT INTO %s (%s", sqlite3_escape_table(table), sqlite3_escape_column(column));
+			ast_str_set(&sql, 0, "INSERT %sINTO %s (%s", replace ? "OR REPLACE " : "", sqlite3_escape_table(table), sqlite3_escape_column(column));
 			ast_str_set(&values, 0, ") VALUES (%s", sqlite3_escape_value(value));
 			first = 0;
 		} else {
@@ -854,6 +856,16 @@
 	ast_free(values);
 
 	return res;
+}
+
+static int realtime_sqlite3_store(const char *database, const char *table, va_list ap)
+{
+	return realtime_sqlite3_store_helper(database, table, 0, ap);
+}
+
+static int realtime_sqlite3_replace(const char *database, const char *table, va_list ap)
+{
+	return realtime_sqlite3_store_helper(database, table, 1, ap);
 }
 
 /*! \brief Realtime callback for deleting a row




More information about the asterisk-commits mailing list