diff --git a/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx b/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx index c57293ff8..c6155e258 100644 --- a/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx +++ b/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx @@ -18,6 +18,7 @@ #include "KeyValueRepositorySqlite.hxx" #include "Logger.hxx" #include "SqliteError.hxx" +#include "SqliteTransaction.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KeyValueRepositorySqlite::KeyValueRepositorySqlite( @@ -51,9 +52,9 @@ std::map KeyValueRepositorySqlite::load() void KeyValueRepositorySqlite::save(const std::map& 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& values) myStmtInsert->reset(); } - myDb.exec("COMMIT"); + tx.commit(); } catch (SqliteError err) { Logger::log(err.message, 1); diff --git a/src/common/repository/sqlite/SqliteTransaction.cxx b/src/common/repository/sqlite/SqliteTransaction.cxx new file mode 100644 index 000000000..34882e9dd --- /dev/null +++ b/src/common/repository/sqlite/SqliteTransaction.cxx @@ -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"); +} diff --git a/src/common/repository/sqlite/SqliteTransaction.hxx b/src/common/repository/sqlite/SqliteTransaction.hxx new file mode 100644 index 000000000..f4e0df342 --- /dev/null +++ b/src/common/repository/sqlite/SqliteTransaction.hxx @@ -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 diff --git a/src/common/repository/sqlite/module.mk b/src/common/repository/sqlite/module.mk index ef6f5b207..0c2192960 100644 --- a/src/common/repository/sqlite/module.mk +++ b/src/common/repository/sqlite/module.mk @@ -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