Browse Source

lib/srdb1: Converted db_(begin|end|rollback) to proper DB API functions

Peter Dunkley 13 years ago
parent
commit
aca1c20d91
3 changed files with 34 additions and 87 deletions
  1. 6 0
      lib/srdb1/db.c
  2. 28 18
      lib/srdb1/db.h
  3. 0 69
      lib/srdb1/db_query.c

+ 6 - 0
lib/srdb1/db.c

@@ -242,6 +242,12 @@ int db_bind_mod(const str* mod, db_func_t* mydbf)
 			"db_insert_update", 2, 0);
 			"db_insert_update", 2, 0);
 		dbf.insert_delayed = (db_insert_delayed_f)find_mod_export(tmp,
 		dbf.insert_delayed = (db_insert_delayed_f)find_mod_export(tmp,
 			"db_insert_delayed", 2, 0);
 			"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)
 	if(db_check_api(&dbf, tmp)!=0)
 		goto error;
 		goto error;

+ 28 - 18
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);
 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
  * \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_update_f insert_update; /* Insert into table, update on duplicate key */ 
 	db_insert_delayed_f insert_delayed;           /* Insert delayed into table */
 	db_insert_delayed_f insert_delayed;           /* Insert delayed into table */
 	db_affected_rows_f affected_rows; /* Numer of affected rows for last query */
 	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;
 } 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,
 int db_fetch_next(db_func_t *dbf, int frows, db1_con_t* _h,
 		db1_res_t** _r);
 		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 */
 #endif /* DB1_H */

+ 0 - 69
lib/srdb1/db_query.c

@@ -456,72 +456,3 @@ error:
 	}
 	}
 	return -1;
 	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;
-}