src/SqlHandler.h
branchv_0
changeset 25 ec793cb3e686
parent 24 884ece10575d
child 26 49919a60c747
equal deleted inserted replaced
24:884ece10575d 25:ec793cb3e686
   133 };
   133 };
   134 
   134 
   135 class Connection {
   135 class Connection {
   136 private:
   136 private:
   137 	sqlite3* db;
   137 	sqlite3* db;
       
   138 
       
   139 	void begin() {
       
   140 		sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
       
   141 	}
       
   142 
   138 public:
   143 public:
   139 
   144 
   140 	Connection(const char* filename) {
   145 	Connection(const char* filename) {
   141 		int result = sqlite3_open(filename, &db);
   146 		int result = sqlite3_open(filename, &db);
   142 		if (result != SQLITE_OK) {
   147 		if (result != SQLITE_OK) {
   143 			sqlite3_close(db);
   148 			sqlite3_close(db);
   144 			throw SqlException(L"Unable to open SQLite database.");
   149 			throw SqlException(L"Unable to open SQLite database.");
   145 		}
   150 		}
       
   151 		begin();
   146 	}
   152 	}
   147 
   153 
   148 	virtual ~Connection() {
   154 	virtual ~Connection() {
   149 		sqlite3_close(db);
   155 		sqlite3_close(db);
   150 	}
   156 	}
   155 		int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining);
   161 		int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining);
   156 		if (result == SQLITE_OK) return new PreparedStatement(stmt);
   162 		if (result == SQLITE_OK) return new PreparedStatement(stmt);
   157 		else throw SqlException(L"Unable to prepare SQLite statement.");
   163 		else throw SqlException(L"Unable to prepare SQLite statement.");
   158 	}
   164 	}
   159 
   165 
   160 	void transactionBegin() {
   166 	bool getAutoCommit() {
   161 		sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
   167 		return sqlite3_get_autocommit(db);
   162 	}
   168 	}
   163 
   169 
   164 	void transactionCommit() {
   170 	void setAutoCommit(bool autoCommit) {
       
   171 		bool autoCommitOld = getAutoCommit();
       
   172 		if (autoCommit && !autoCommitOld) commit();
       
   173 		else if (!autoCommit && autoCommitOld) begin();
       
   174 	}
       
   175 
       
   176 	void commit() {
   165 		sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
   177 		sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
   166 	}
   178 	}
   167 
   179 
   168 	void transactionRollback() {
   180 	void rollback() {
   169 		sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr);
   181 		sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr);
   170 	}
   182 	}
   171 
   183 
   172 };
   184 };
   173 
   185 
   285 		} else {
   297 		} else {
   286 			file = ":memory:";
   298 			file = ":memory:";
   287 		}
   299 		}
   288 
   300 
   289 		connection.reset(new Connection(file.c_str()));
   301 		connection.reset(new Connection(file.c_str()));
   290 		connection->transactionBegin();
   302 		connection->setAutoCommit(false);
   291 	}
   303 	}
   292 
   304 
   293 	virtual ~SqlHandler() {
   305 	virtual ~SqlHandler() {
   294 	}
   306 	}
   295 
   307 
   374 		processSqlInput(configuration.sqlAfterRelational);
   386 		processSqlInput(configuration.sqlAfterRelational);
   375 
   387 
   376 		// pass-through some relations:
   388 		// pass-through some relations:
   377 		for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy);
   389 		for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy);
   378 
   390 
   379 		connection->transactionCommit();
   391 		connection->commit();
   380 
   392 
   381 		// delete or keep the file:
   393 		// delete or keep the file:
   382 		if (configuration.file.size()) {
   394 		if (configuration.file.size()) {
   383 			if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) {
   395 			if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) {
   384 				std::wcerr << L"will unlink file" << std::endl;
   396 				std::wcerr << L"will unlink file" << std::endl;