aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_config_odbc.c
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2007-08-17 13:45:44 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2007-08-17 13:45:44 +0000
commit81cd3d2c34bdb8ce420c2d5425d5cf822b0fb730 (patch)
treee07fb49c38779c0bf93a55a1335855b9ed6b13a3 /res/res_config_odbc.c
parent13f1a2e899bacb3b157b9ebd7b4626a4b7329ac1 (diff)
store and destroy implementations for sqlite (closes issue #10446) and odbc (closes issue #10447)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@79860 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'res/res_config_odbc.c')
-rw-r--r--res/res_config_odbc.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c
index 6388ab72b..1faf28dfc 100644
--- a/res/res_config_odbc.c
+++ b/res/res_config_odbc.c
@@ -450,6 +450,145 @@ static int update_odbc(const char *database, const char *table, const char *keyf
return -1;
}
+/*!
+ * \brief Excute an INSERT query
+ * \param database
+ * \param table
+ * \param ap list containing one or more field/value set(s)
+ * Insert a new record into database table, prepare the sql statement.
+ * All values to be changed are stored in ap list.
+ * Sub-in the values to the prepared statement and execute it.
+ *
+ * \retval number of rows affected
+ * \retval -1 on failure
+*/
+static int store_odbc(const char *database, const char *table, va_list ap)
+{
+ struct odbc_obj *obj;
+ SQLHSTMT stmt;
+ char sql[256];
+ char keys[256];
+ char vals[256];
+ SQLLEN rowcount=0;
+ const char *newparam, *newval;
+ int res;
+ va_list aq;
+ struct custom_prepare_struct cps = { .sql = sql, .extra = NULL };
+
+ va_copy(cps.ap, ap);
+ va_copy(aq, ap);
+
+ if (!table)
+ return -1;
+
+ obj = ast_odbc_request_obj(database, 0);
+ if (!obj)
+ return -1;
+
+ newparam = va_arg(aq, const char *);
+ if (!newparam) {
+ ast_odbc_release_obj(obj);
+ return -1;
+ }
+ newval = va_arg(aq, const char *);
+ snprintf(keys, sizeof(keys), "%s", newparam);
+ snprintf(vals, sizeof(vals), "?");
+ while ((newparam = va_arg(aq, const char *))) {
+ snprintf(keys + strlen(keys), sizeof(keys) - strlen(keys), ", %s", newparam);
+ snprintf(vals + strlen(vals), sizeof(vals) - strlen(vals), ", ?");
+ newval = va_arg(aq, const char *);
+ }
+ va_end(aq);
+ snprintf(sql, sizeof(sql), "INSERT INTO %s (%s) VALUES (%s)", table, keys, vals);
+
+ stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
+
+ if (!stmt) {
+ ast_odbc_release_obj(obj);
+ return -1;
+ }
+
+ res = SQLRowCount(stmt, &rowcount);
+ SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+ ast_odbc_release_obj(obj);
+
+ if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+ ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
+ return -1;
+ }
+
+ if (rowcount >= 0)
+ return (int)rowcount;
+
+ return -1;
+}
+
+/*!
+ * \brief Excute an DELETE query
+ * \param database
+ * \param table
+ * \param keyfield where clause field
+ * \param lookup value of field for where clause
+ * \param ap list containing one or more field/value set(s)
+ * Dlete a row from a database table, prepare the sql statement using keyfield and lookup
+ * control the number of records to change. Additional params to match rows are stored in ap list.
+ * Sub-in the values to the prepared statement and execute it.
+ *
+ * \retval number of rows affected
+ * \retval -1 on failure
+*/
+static int destroy_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
+{
+ struct odbc_obj *obj;
+ SQLHSTMT stmt;
+ char sql[256];
+ SQLLEN rowcount=0;
+ const char *newparam, *newval;
+ int res;
+ va_list aq;
+ struct custom_prepare_struct cps = { .sql = sql, .extra = lookup };
+
+ va_copy(cps.ap, ap);
+ va_copy(aq, ap);
+
+ if (!table)
+ return -1;
+
+ obj = ast_odbc_request_obj(database, 0);
+ if (!obj)
+ return -1;
+
+ snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE ", table);
+ while((newparam = va_arg(aq, const char *))) {
+ snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s=? AND ", newparam);
+ newval = va_arg(aq, const char *);
+ }
+ va_end(aq);
+ snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s=?", keyfield);
+
+ stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
+
+ if (!stmt) {
+ ast_odbc_release_obj(obj);
+ return -1;
+ }
+
+ res = SQLRowCount(stmt, &rowcount);
+ SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+ ast_odbc_release_obj(obj);
+
+ if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+ ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
+ return -1;
+ }
+
+ if (rowcount >= 0)
+ return (int)rowcount;
+
+ return -1;
+}
+
+
struct config_odbc_obj {
char *sql;
unsigned long cat_metric;
@@ -575,6 +714,8 @@ static struct ast_config_engine odbc_engine = {
.load_func = config_odbc,
.realtime_func = realtime_odbc,
.realtime_multi_func = realtime_multi_odbc,
+ .store_func = store_odbc,
+ .destroy_func = destroy_odbc,
.update_func = update_odbc
};