RAII wrapper for sqlite transactions, better transaction handling in key value repo.

This commit is contained in:
Christian Speckner 2019-05-15 10:32:01 +02:00
parent 884d46a45f
commit c75fcfae13
4 changed files with 105 additions and 4 deletions

View File

@ -18,6 +18,7 @@
#include "KeyValueRepositorySqlite.hxx"
#include "Logger.hxx"
#include "SqliteError.hxx"
#include "SqliteTransaction.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
KeyValueRepositorySqlite::KeyValueRepositorySqlite(
@ -51,9 +52,9 @@ std::map<string, Variant> KeyValueRepositorySqlite::load()
void KeyValueRepositorySqlite::save(const std::map<string, Variant>& values)
{
try {
myStmtInsert->reset();
SqliteTransaction tx(myDb);
myDb.exec("BEGIN TRANSACTION");
myStmtInsert->reset();
for (const auto& pair: values) {
(*myStmtInsert)
@ -64,7 +65,7 @@ void KeyValueRepositorySqlite::save(const std::map<string, Variant>& values)
myStmtInsert->reset();
}
myDb.exec("COMMIT");
tx.commit();
}
catch (SqliteError err) {
Logger::log(err.message, 1);

View File

@ -0,0 +1,51 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include "SqliteTransaction.hxx"
#include "SqliteDatabase.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteTransaction::SqliteTransaction(SqliteDatabase& db)
: myDb(db),
myTransactionClosed(false)
{
myDb.exec("BEGIN TRANSACTION");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteTransaction::~SqliteTransaction()
{
if (!myTransactionClosed) rollback();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SqliteTransaction::commit()
{
if (myTransactionClosed) return;
myTransactionClosed = true;
myDb.exec("COMMIT TRANSACTION");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SqliteTransaction::rollback()
{
if (myTransactionClosed) return;
myTransactionClosed = true;
myDb.exec("ROLLBACK TRANSACTION");
}

View File

@ -0,0 +1,48 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#ifndef SQLITE_TRANSACTION_HXX
#define SQLITE_TRANSACTION_HXX
class SqliteDatabase;
class SqliteTransaction {
public:
SqliteTransaction(SqliteDatabase& db);
~SqliteTransaction();
void commit();
void rollback();
private:
SqliteDatabase& myDb;
bool myTransactionClosed;
private:
SqliteTransaction(const SqliteTransaction&) = delete;
SqliteTransaction(SqliteTransaction&&) = delete;
SqliteTransaction& operator=(const SqliteTransaction&) = delete;
SqliteTransaction& operator=(SqliteTransaction&&) = delete;
};
#endif // SQLITE_TRANSACTION_HXX

View File

@ -4,7 +4,8 @@ MODULE_OBJS := \
src/common/repository/sqlite/KeyValueRepositorySqlite.o \
src/common/repository/sqlite/SettingsDb.o \
src/common/repository/sqlite/SqliteDatabase.o \
src/common/repository/sqlite/SqliteStatement.o
src/common/repository/sqlite/SqliteStatement.o \
src/common/repository/sqlite/SqliteTransaction.o
MODULE_DIRS += \
src/common/repository/sqlite