[asterisk-commits] tilghman: branch tilghman/realtime_update2 r147678 - /team/tilghman/realtime_...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Oct 8 15:58:53 CDT 2008
Author: tilghman
Date: Wed Oct 8 15:58:52 2008
New Revision: 147678
URL: http://svn.digium.com/view/asterisk?view=rev&rev=147678
Log:
Fixup sqlite
Modified:
team/tilghman/realtime_update2/res/res_config_odbc.c
team/tilghman/realtime_update2/res/res_config_pgsql.c
team/tilghman/realtime_update2/res/res_config_sqlite.c
Modified: team/tilghman/realtime_update2/res/res_config_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/realtime_update2/res/res_config_odbc.c?view=diff&rev=147678&r1=147677&r2=147678
==============================================================================
--- team/tilghman/realtime_update2/res/res_config_odbc.c (original)
+++ team/tilghman/realtime_update2/res/res_config_odbc.c Wed Oct 8 15:58:52 2008
@@ -524,7 +524,7 @@
while ((newparam = va_arg(ap, const char *))) {
newval = va_arg(ap, const char *);
if ((column = ast_odbc_find_column(tableptr, newparam))) {
- ast_str_append(&sql, 0, "%s%s ? ", first ? "" : ", ", newparam);
+ ast_str_append(&sql, 0, "%s%s=? ", first ? "" : ", ", newparam);
SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
first = 0;
} else {
@@ -535,7 +535,7 @@
/* Restart search, because we need to add the search parameters */
va_copy(ap, ups->ap);
- ast_str_append(&sql, 0, "WHERE ");
+ ast_str_append(&sql, 0, "WHERE");
first = 1;
while ((newparam = va_arg(ap, const char *))) {
@@ -546,7 +546,7 @@
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
return NULL;
}
- ast_str_append(&sql, 0, "%s%s ? ", first ? "" : ", ", newparam);
+ ast_str_append(&sql, 0, "%s %s=?", first ? "" : " AND", newparam);
SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
first = 0;
}
Modified: team/tilghman/realtime_update2/res/res_config_pgsql.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/realtime_update2/res/res_config_pgsql.c?view=diff&rev=147678&r1=147677&r2=147678
==============================================================================
--- team/tilghman/realtime_update2/res/res_config_pgsql.c (original)
+++ team/tilghman/realtime_update2/res/res_config_pgsql.c Wed Oct 8 15:58:52 2008
@@ -696,7 +696,7 @@
ast_free(sql);
return -1;
}
- ast_str_append(&where, 0, "%s %s='%s'", first ? "" : ",", newparam, escapebuf->str);
+ ast_str_append(&where, 0, "%s %s='%s'", first ? "" : " AND", newparam, escapebuf->str);
first = 0;
}
Modified: team/tilghman/realtime_update2/res/res_config_sqlite.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/realtime_update2/res/res_config_sqlite.c?view=diff&rev=147678&r1=147677&r2=147678
==============================================================================
--- team/tilghman/realtime_update2/res/res_config_sqlite.c (original)
+++ team/tilghman/realtime_update2/res/res_config_sqlite.c Wed Oct 8 15:58:52 2008
@@ -123,6 +123,7 @@
MACRO_END
AST_THREADSTORAGE(sql_buf);
+AST_THREADSTORAGE(where_buf);
/*!
* Maximum number of loops before giving up executing a query. Calls to
@@ -1362,76 +1363,67 @@
static int realtime_update2_handler(const char *database, const char *table,
va_list ap)
{
- char *query, *errormsg = NULL, *tmp_str;
- const char **params, **vals;
- size_t params_count;
- int error, rows_num;
+ char *errormsg = NULL, *tmp1, *tmp2;
+ int error, rows_num, first = 1;
+ struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
+ struct ast_str *where = ast_str_thread_get(&where_buf, 100);
+ const char *param, *value;
if (!table) {
ast_log(LOG_WARNING, "Table name unspecified\n");
return -1;
}
- if (!(params_count = get_params(ap, ¶ms, &vals)))
+ if (!sql) {
return -1;
-
-/* \cond DOXYGEN_CAN_PARSE_THIS */
-#undef QUERY
-#define QUERY "UPDATE '%q' SET %q = '%q'"
-/* \endcond */
-
- if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
- ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
- ast_free(params);
- ast_free(vals);
+ }
+
+ ast_str_set(&sql, 0, "UPDATE %s SET", table);
+ ast_str_set(&where, 0, " WHERE");
+
+ while ((param = va_arg(ap, const char *))) {
+ value = va_arg(ap, const char *);
+ ast_str_append(&where, 0, "%s %s = %s",
+ first ? "" : " AND",
+ tmp1 = sqlite_mprintf("%q", param),
+ tmp2 = sqlite_mprintf("%Q", value));
+ sqlite_freemem(tmp1);
+ sqlite_freemem(tmp2);
+ first = 0;
+ }
+
+ if (first) {
+ ast_log(LOG_ERROR, "No criteria specified on update to '%s@%s'!\n", table, database);
return -1;
}
- if (params_count > 1) {
- size_t i;
-
- for (i = 1; i < params_count; i++) {
- tmp_str = sqlite_mprintf("%s, %q = '%q'", query, params[i], vals[i]);
- sqlite_freemem(query);
-
- if (!tmp_str) {
- ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
- ast_free(params);
- ast_free(vals);
- return -1;
- }
-
- query = tmp_str;
- }
- }
-
- 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");
- sqlite_freemem(query);
- return -1;
- }
-
- sqlite_freemem(query);
- query = tmp_str;
- ast_debug(1, "SQL query: %s\n", query);
+ while ((param = va_arg(ap, const char *))) {
+ value = va_arg(ap, const char *);
+ ast_str_append(&sql, 0, "%s %s = %s",
+ first ? "" : ",",
+ tmp1 = sqlite_mprintf("%q", param),
+ tmp2 = sqlite_mprintf("%Q", value));
+ sqlite_freemem(tmp1);
+ sqlite_freemem(tmp2);
+ first = 0;
+ }
+
+ ast_str_append(&sql, 0, " %s", where->str);
+ ast_debug(1, "SQL query: %s\n", sql->str);
ast_mutex_lock(&mutex);
RES_CONFIG_SQLITE_BEGIN
- error = sqlite_exec(db, query, NULL, NULL, &errormsg);
+ error = sqlite_exec(db, sql->str, NULL, NULL, &errormsg);
RES_CONFIG_SQLITE_END(error)
- if (!error)
+ if (!error) {
rows_num = sqlite_changes(db);
- else
+ } else {
rows_num = -1;
+ }
ast_mutex_unlock(&mutex);
-
- sqlite_freemem(query);
if (error) {
ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
More information about the asterisk-commits
mailing list