浏览代码

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().
Peter Dunkley 13 年之前
父节点
当前提交
5da9a4e773
共有 2 个文件被更改,包括 86 次插入0 次删除
  1. 17 0
      lib/srdb1/db.h
  2. 69 0
      lib/srdb1/db_query.c

+ 17 - 0
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 */

+ 69 - 0
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;
+}