[sr-dev] git:master: lib/srdb1: Added db_begin()/db_commit()/db_rollback() wrapper functions

Peter Dunkley peter.dunkley at crocodile-rcs.com
Fri Apr 20 15:02:30 CEST 2012


Module: sip-router
Branch: master
Commit: 5da9a4e773d2c3c625a870dd4639408d99676d69
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=5da9a4e773d2c3c625a870dd4639408d99676d69

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Fri Apr 20 13:56:37 2012 +0100

lib/srdb1: Added db_begin()/db_commit()/db_rollback() wrapper functions

- These are helper functions to enable blocks of DB queries/updates in
  modules to be handled as a single transaction.
- These functions wrap db_raw_query() and only perform the BEGIN/COMMIT/
  ROLLBACK when the db module supports raw_query().

---

 lib/srdb1/db.h       |   17 ++++++++++++
 lib/srdb1/db_query.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/lib/srdb1/db.h b/lib/srdb1/db.h
index 6d3a45d..5258e2c 100644
--- a/lib/srdb1/db.h
+++ b/lib/srdb1/db.h
@@ -473,5 +473,22 @@ 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 004d10c..1305f46 100644
--- a/lib/srdb1/db_query.c
+++ b/lib/srdb1/db_query.c
@@ -456,3 +456,72 @@ 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;
+}




More information about the sr-dev mailing list