rename DB methods to match standard API v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2019 17:36:57 +0100
branchv_0
changeset 25 ec793cb3e686
parent 24 884ece10575d
child 26 49919a60c747
rename DB methods to match standard API
src/SqlHandler.h
--- a/src/SqlHandler.h	Sat Dec 14 14:00:36 2019 +0100
+++ b/src/SqlHandler.h	Tue Dec 24 17:36:57 2019 +0100
@@ -135,6 +135,11 @@
 class Connection {
 private:
 	sqlite3* db;
+
+	void begin() {
+		sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
+	}
+
 public:
 
 	Connection(const char* filename) {
@@ -143,6 +148,7 @@
 			sqlite3_close(db);
 			throw SqlException(L"Unable to open SQLite database.");
 		}
+		begin();
 	}
 
 	virtual ~Connection() {
@@ -157,15 +163,21 @@
 		else throw SqlException(L"Unable to prepare SQLite statement.");
 	}
 
-	void transactionBegin() {
-		sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
+	bool getAutoCommit() {
+		return sqlite3_get_autocommit(db);
 	}
 
-	void transactionCommit() {
+	void setAutoCommit(bool autoCommit) {
+		bool autoCommitOld = getAutoCommit();
+		if (autoCommit && !autoCommitOld) commit();
+		else if (!autoCommit && autoCommitOld) begin();
+	}
+
+	void commit() {
 		sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
 	}
 
-	void transactionRollback() {
+	void rollback() {
 		sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr);
 	}
 
@@ -287,7 +299,7 @@
 		}
 
 		connection.reset(new Connection(file.c_str()));
-		connection->transactionBegin();
+		connection->setAutoCommit(false);
 	}
 
 	virtual ~SqlHandler() {
@@ -376,7 +388,7 @@
 		// pass-through some relations:
 		for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy);
 
-		connection->transactionCommit();
+		connection->commit();
 
 		// delete or keep the file:
 		if (configuration.file.size()) {