|
@@ -160,7 +160,8 @@ static int db_unixodbc_submit_query(const db1_con_t* _h, const str* _s)
|
|
|
SQLFreeHandle(SQL_HANDLE_STMT, CON_RESULT(_h));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ /* Store the ODBC query result */
|
|
|
+ CON_QUERY_RESULT(_h) = ret;
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -485,6 +486,53 @@ int db_unixodbc_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t*
|
|
|
db_unixodbc_submit_query);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+/*
|
|
|
+ * Just like insert, but update the row if it exists otherwise insert it.
|
|
|
+ * For DB not supporting the replace query.
|
|
|
+ */
|
|
|
+int db_unixodbc_update_or_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
|
|
|
+ const int _n, const int _un, const int _m)
|
|
|
+{
|
|
|
+ if(_un > _n)
|
|
|
+ {
|
|
|
+ LM_ERR("number of columns for unique key is too high\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(_un > 0)
|
|
|
+ {
|
|
|
+ /* Query error */
|
|
|
+ if(db_unixodbc_update(_h, _k, 0, _v, _k + _un,
|
|
|
+ _v + _un, _un, _n -_un)< 0)
|
|
|
+ {
|
|
|
+ LM_ERR("update failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ /* No row updated ? */
|
|
|
+ else if (CON_QUERY_RESULT(_h) == SQL_NO_DATA_FOUND)
|
|
|
+ {
|
|
|
+ /* Do an insert then */
|
|
|
+ if(db_unixodbc_insert(_h, _k, _v, _n)< 0)
|
|
|
+ {
|
|
|
+ LM_ERR("insert failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ LM_DBG("inserted new record in database table\n");
|
|
|
+ } else {
|
|
|
+ LM_DBG("updated record in database table\n");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if(db_unixodbc_insert(_h, _k, _v, _n)< 0)
|
|
|
+ {
|
|
|
+ LM_ERR("direct insert failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ LM_DBG("directly inserted new record in database table\n");
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
* Store name of table that will be used by
|
|
|
* subsequent database functions
|