Module: sip-router
Branch: master
Commit: aca1c20d91878991b0a0bef4ae8bef3da89527d1
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=aca1c20…
Author: Peter Dunkley <peter.dunkley(a)crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley(a)crocodile-rcs.com>
Date: Tue Apr 24 10:48:39 2012 +0100
lib/srdb1: Converted db_(begin|end|rollback) to proper DB API functions
---
lib/srdb1/db.c | 6 ++++
lib/srdb1/db.h | 46 ++++++++++++++++++++-------------
lib/srdb1/db_query.c | 69 --------------------------------------------------
3 files changed, 34 insertions(+), 87 deletions(-)
diff --git a/lib/srdb1/db.c b/lib/srdb1/db.c
index 17c018a..d57efa8 100644
--- a/lib/srdb1/db.c
+++ b/lib/srdb1/db.c
@@ -242,6 +242,12 @@ int db_bind_mod(const str* mod, db_func_t* mydbf)
"db_insert_update", 2, 0);
dbf.insert_delayed = (db_insert_delayed_f)find_mod_export(tmp,
"db_insert_delayed", 2, 0);
+ dbf.start_transaction = (db_start_transaction_f)find_mod_export(tmp,
+ "db_start_transaction", 1, 0);
+ dbf.end_transaction = (db_end_transaction_f)find_mod_export(tmp,
+ "db_end_transaction", 1, 0);
+ dbf.abort_transaction = (db_abort_transaction_f)find_mod_export(tmp,
+ "db_abort_transaction", 1, 0);
}
if(db_check_api(&dbf, tmp)!=0)
goto error;
diff --git a/lib/srdb1/db.h b/lib/srdb1/db.h
index a0927e5..2cbcdf7 100644
--- a/lib/srdb1/db.h
+++ b/lib/srdb1/db.h
@@ -341,6 +341,31 @@ typedef int (*db_insert_delayed_f) (const db1_con_t* _h, const
db_key_t* _k,
*/
typedef int (*db_affected_rows_f) (const db1_con_t* _h);
+/**
+ * \brief Start a single transaction that will consist of one or more queries.
+ *
+ * \param _h structure representing database connection
+ * \return 0 if everything is OK, otherwise returns < 0
+ */
+typedef int (*db_start_transaction_f) (db1_con_t* _h);
+
+/**
+ * \brief End a transaction.
+ *
+ * \param _h structure representing database connection
+ * \return 0 if everything is OK, otherwise returns < 0
+ */
+typedef int (*db_end_transaction_f) (db1_con_t* _h);
+
+/**
+ * \brief Abort a transaction.
+ *
+ * Use this function if you have an error after having started a transaction
+ * and you want to rollback any uncommitted changes before continuing.
+ * \param _h structure representing database connection
+ * \return 1 if there was something to rollbak, 0 if not, negative on failure
+ */
+typedef int (*db_abort_transaction_f) (db1_con_t* _h);
/**
* \brief Database module callbacks
@@ -368,6 +393,9 @@ typedef struct db_func {
db_insert_update_f insert_update; /* Insert into table, update on duplicate key */
db_insert_delayed_f insert_delayed; /* Insert delayed into table */
db_affected_rows_f affected_rows; /* Numer of affected rows for last query */
+ db_start_transaction_f start_transaction; /* Start a single transaction consisting of
multiple queries */
+ db_end_transaction_f end_transaction; /* End a transaction */
+ db_abort_transaction_f abort_transaction; /* Abort a transaction */
} db_func_t;
@@ -518,22 +546,4 @@ int db_fetch_query(db_func_t *dbf, int frows,
int db_fetch_next(db_func_t *dbf, int frows, db1_con_t* _h,
db1_res_t** _r);
-/**
- * \brief wrapper around db raw_query to perform BEGIN
- * \return -1 error; 0 OK with no raw_query capability; 1 OK with raw_query capability
- */
-int db_begin(db_func_t *dbf, db1_con_t* _h);
-
-/**
- * \brief wrapper around db raw_query to perform COMMIT
- * \return -1 error; 0 OK with no raw_query capability; 1 OK with raw_query capability
- */
-int db_commit(db_func_t *dbf, db1_con_t* _h);
-
-/**
- * \wrapper around db raw_query to perform ROLLBACK
- * \return -1 error; 0 OK with no raw_query capability; 1 OK with raw_query capability
- */
-int db_rollback(db_func_t *dbf, db1_con_t* _h);
-
#endif /* DB1_H */
diff --git a/lib/srdb1/db_query.c b/lib/srdb1/db_query.c
index 1305f46..004d10c 100644
--- a/lib/srdb1/db_query.c
+++ b/lib/srdb1/db_query.c
@@ -456,72 +456,3 @@ error:
}
return -1;
}
-
-/**
- * wrapper around db raw_query to perform BEGIN
- * return: -1 error; 0 OK with no raw_query capability; 1 OK with raw_query capability
- */
-int db_begin(db_func_t *dbf, db1_con_t* _h)
-{
- db1_res_t *res = NULL;
- int ret = 0;
- str query_str = str_init("BEGIN");
-
- if (DB_CAPABILITY(*dbf, DB_CAP_RAW_QUERY)) {
- if (dbf->raw_query(_h, &query_str, &res)) {
- LM_ERR("unable to run raw query\n");
- ret = -1;
- goto done;
- }
- ret = 1;
- }
-done:
- if (res) dbf->free_result(_h, res);
- return ret;
-}
-
-/**
- * wrapper around db raw_query to perform COMMIT
- * return: -1 error; 0 OK with no raw_query capability; 1 OK with raw_query capability
- */
-int db_commit(db_func_t *dbf, db1_con_t* _h)
-{
- db1_res_t *res = NULL;
- int ret = 0;
- str query_str = str_init("COMMIT");
-
- if (DB_CAPABILITY(*dbf, DB_CAP_RAW_QUERY)) {
- if (dbf->raw_query(_h, &query_str, &res)) {
- LM_ERR("unable to run raw query\n");
- ret = -1;
- goto done;
- }
- ret = 1;
- }
-done:
- if (res) dbf->free_result(_h, res);
- return ret;
-}
-
-/**
- * wrapper around db raw_query to perform ROLLBACK
- * return: -1 error; 0 OK with no raw_query capability; 1 OK with raw_query capability
- */
-int db_rollback(db_func_t *dbf, db1_con_t* _h)
-{
- db1_res_t *res = NULL;
- int ret = 0;
- str query_str = str_init("COMMIT");
-
- if (DB_CAPABILITY(*dbf, DB_CAP_RAW_QUERY)) {
- if (dbf->raw_query(_h, &query_str, &res)) {
- LM_ERR("unable to run raw query\n");
- ret = -1;
- goto done;
- }
- ret = 1;
- }
-done:
- if (res) dbf->free_result(_h, res);
- return ret;
-}