Say hello to composite key value repo.

This commit is contained in:
Christian Speckner 2020-12-29 21:50:30 +01:00
parent c4b93c346d
commit b63e2a8fe0
11 changed files with 354 additions and 60 deletions

View File

@ -98,6 +98,7 @@
"forward_list": "cpp",
"regex": "cpp",
"valarray": "cpp",
"ranges": "cpp"
"ranges": "cpp",
"stop_token": "cpp"
}
}

View File

@ -0,0 +1,35 @@
//============================================================================
//
// 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-2020 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 COMPOSITE_KEY_VALUE_REPOSITORY_HXX
#define COMPOSITE_KEY_VALUE_REPOSITORY_HXX
#include "KeyValueRepository.hxx"
#include "bspf.hxx"
class CompositeKeyValueRepository
{
public:
virtual ~CompositeKeyValueRepository() = default;
virtual shared_ptr<KeyValueRepository> get(const string& key) = 0;
virtual bool has(const string& key) = 0;
};
#endif // COMPOSITE_KEY_VALUE_REPOSITORY_HXX

View File

@ -0,0 +1,80 @@
//============================================================================
//
// 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-2020 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 "AbstractKeyValueRepositorySqlite.hxx"
#include "Logger.hxx"
#include "SqliteError.hxx"
#include "SqliteTransaction.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::map<string, Variant> AbstractKeyValueRepositorySqlite::load()
{
std::map<string, Variant> values;
try {
SqliteStatement& stmt{stmtSelect()};
while (stmt.step())
values[stmt.columnText(0)] = stmt.columnText(1);
stmt.reset();
}
catch (const SqliteError& err) {
Logger::info(err.what());
}
return values;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void AbstractKeyValueRepositorySqlite::save(const std::map<string, Variant>& values)
{
try {
SqliteTransaction tx{database()};
for (const auto& pair: values) {
SqliteStatement& stmt{stmtInsert(pair.first, pair.second.toString())};
stmt.step();
stmt.reset();
}
tx.commit();
}
catch (const SqliteError& err) {
Logger::info(err.what());
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void AbstractKeyValueRepositorySqlite::save(const string& key, const Variant& value)
{
try {
SqliteStatement& stmt{stmtInsert(key, value.toString())};
stmt
.bind(1, key.c_str())
.bind(2, value.toCString())
.step();
stmt.reset();
}
catch (const SqliteError& err) {
Logger::info(err.what());
}
}

View File

@ -0,0 +1,26 @@
#ifndef ABSTRACT_KEY_VALUE_REPOSITORY_SQLITE_HXX
#define ABSTRACT_KEY_VALUE_REPOSITORY_SQLITE_HXX
#include "bspf.hxx"
#include "repository/KeyValueRepository.hxx"
#include "SqliteDatabase.hxx"
#include "SqliteStatement.hxx"
class AbstractKeyValueRepositorySqlite : public KeyValueRepository
{
public:
std::map<string, Variant> load() override;
void save(const std::map<string, Variant>& values) override;
void save(const string& key, const Variant& value) override;
protected:
virtual SqliteStatement& stmtInsert(const string& key, const string& value) = 0;
virtual SqliteStatement& stmtSelect() = 0;
virtual SqliteDatabase& database() = 0;
};
#endif // ABSTRACT_KEY_VALUE_REPOSITORY_SQLITE_HXX

View File

@ -0,0 +1,100 @@
//============================================================================
//
// 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-2020 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 "CompositeKeyValueRepositorySqlite.hxx"
#include "Logger.hxx"
#include "SqliteError.hxx"
#include "bspf.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CompositeKeyValueRepositorySqlite::CompositeKeyValueRepositorySqlite(SqliteDatabase& db, const string& tableName)
: myTableName{tableName}, myDb{db}
{}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
shared_ptr<KeyValueRepository> CompositeKeyValueRepositorySqlite::get(const string& key)
{
return make_shared<ProxyRepository>(*this, key);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CompositeKeyValueRepositorySqlite::has(const string& key)
{
try {
myStmtCount->reset();
myStmtCount->bind(1, key.c_str());
if (!myStmtCount->step())
throw new SqliteError("count failed");
Int32 rowCount = myStmtCount->columnInt(0);
myStmtCount->reset();
return rowCount > 0;
} catch (const SqliteError& err) {
Logger::info(err.what());
return false;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CompositeKeyValueRepositorySqlite::initialize()
{
myDb.exec(
"CREATE TABLE IF NOT EXISTS `" + myTableName + "` (`key1` TEXT, `key2` TEXT, `value` TEXT, PRIMARY KEY (`key1`, `key2`)) WITHOUT ROWID"
);
myStmtInsert = make_unique<SqliteStatement>(myDb, "INSERT OR REPLACE INTO `" + myTableName + "` VALUES (?, ?, ?)");
myStmtSelect = make_unique<SqliteStatement>(myDb, "SELECT `key2`, `VALUE` FROM `" + myTableName + "` WHERE `key1` = ?");
myStmtCount = make_unique<SqliteStatement>(myDb, "SELECT COUNT(*) FROM `" + myTableName + "` WHERE `key1` = ?");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CompositeKeyValueRepositorySqlite::ProxyRepository::ProxyRepository(
const CompositeKeyValueRepositorySqlite& repo,
const string& key
) : myRepo(repo), myKey(key)
{}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtInsert(
const string& key, const string& value
) {
myRepo.myStmtInsert->reset();
return (*myRepo.myStmtInsert)
.bind(1, myKey.c_str())
.bind(2, key.c_str())
.bind(3, value.c_str());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtSelect()
{
myRepo.myStmtSelect->reset();
return (*myRepo.myStmtSelect)
.bind(1, myKey.c_str());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteDatabase& CompositeKeyValueRepositorySqlite::ProxyRepository::database()
{
return myRepo.myDb;
}

View File

@ -0,0 +1,81 @@
//============================================================================
//
// 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-2020 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 COMPOSITE_KEY_VALUE_REPOSITORY_SQLITE_HXX
#define COMPOSITE_KEY_VALUE_REPOSITORY_SQLITE_HXX
#include "repository/CompositeKeyValueRepository.hxx"
#include "SqliteDatabase.hxx"
#include "SqliteStatement.hxx"
#include "AbstractKeyValueRepositorySqlite.hxx"
class CompositeKeyValueRepositorySqlite : public CompositeKeyValueRepository {
public:
CompositeKeyValueRepositorySqlite(SqliteDatabase& db, const string& tableName);
shared_ptr<KeyValueRepository> get(const string& key) override;
bool has(const string& key) override;
void initialize();
private:
class ProxyRepository : public AbstractKeyValueRepositorySqlite {
public:
ProxyRepository(const CompositeKeyValueRepositorySqlite& repo, const string& key);
protected:
SqliteStatement& stmtInsert(const string& key, const string& value) override;
SqliteStatement& stmtSelect() override;
SqliteDatabase& database() override;
private:
const CompositeKeyValueRepositorySqlite& myRepo;
const string myKey;
private:
ProxyRepository(const ProxyRepository&) = delete;
ProxyRepository(ProxyRepository&&) = delete;
ProxyRepository& operator=(const ProxyRepository&) = delete;
ProxyRepository& operator=(ProxyRepository&&) = delete;
};
private:
string myTableName;
SqliteDatabase& myDb;
unique_ptr<SqliteStatement> myStmtInsert;
unique_ptr<SqliteStatement> myStmtSelect;
unique_ptr<SqliteStatement> myStmtCount;
private:
CompositeKeyValueRepositorySqlite(const CompositeKeyValueRepositorySqlite&) = delete;
CompositeKeyValueRepositorySqlite(CompositeKeyValueRepositorySqlite&&) = delete;
CompositeKeyValueRepositorySqlite& operator=(const CompositeKeyValueRepositorySqlite&) = delete;
CompositeKeyValueRepositorySqlite& operator=(CompositeKeyValueRepositorySqlite&&) = delete;
};
#endif // COMPOSITE_KEY_VALUE_REPOSITORY_SQLITE_HXX

View File

@ -17,8 +17,6 @@
#include "KeyValueRepositorySqlite.hxx"
#include "Logger.hxx"
#include "SqliteError.hxx"
#include "SqliteTransaction.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
KeyValueRepositorySqlite::KeyValueRepositorySqlite(
@ -29,65 +27,27 @@ KeyValueRepositorySqlite::KeyValueRepositorySqlite(
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::map<string, Variant> KeyValueRepositorySqlite::load()
SqliteStatement& KeyValueRepositorySqlite::stmtInsert(const string& key, const string& value)
{
std::map<string, Variant> values;
myStmtInsert->reset();
try {
myStmtSelect->reset();
while (myStmtSelect->step())
values[myStmtSelect->columnText(0)] = myStmtSelect->columnText(1);
myStmtSelect->reset();
}
catch (const SqliteError& err) {
Logger::info(err.what());
}
return values;
return (*myStmtInsert)
.bind(1, key.c_str())
.bind(2, value.c_str());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyValueRepositorySqlite::save(const std::map<string, Variant>& values)
SqliteStatement& KeyValueRepositorySqlite::stmtSelect()
{
try {
SqliteTransaction tx(myDb);
myStmtInsert->reset();
myStmtInsert->reset();
for (const auto& pair: values) {
(*myStmtInsert)
.bind(1, pair.first.c_str())
.bind(2, pair.second.toCString())
.step();
myStmtInsert->reset();
}
tx.commit();
}
catch (const SqliteError& err) {
Logger::info(err.what());
}
return *myStmtSelect;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyValueRepositorySqlite::save(const string& key, const Variant& value)
SqliteDatabase& KeyValueRepositorySqlite::database()
{
try {
myStmtInsert->reset();
(*myStmtInsert)
.bind(1, key.c_str())
.bind(2, value.toCString())
.step();
myStmtInsert->reset();
}
catch (const SqliteError& err) {
Logger::info(err.what());
}
return myDb;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -19,24 +19,24 @@
#define KEY_VALUE_REPOSITORY_SQLITE_HXX
#include "bspf.hxx"
#include "repository/KeyValueRepository.hxx"
#include "AbstractKeyValueRepositorySqlite.hxx"
#include "SqliteDatabase.hxx"
#include "SqliteStatement.hxx"
class KeyValueRepositorySqlite : public KeyValueRepository
class KeyValueRepositorySqlite : public AbstractKeyValueRepositorySqlite
{
public:
KeyValueRepositorySqlite(SqliteDatabase& db, const string& tableName);
std::map<string, Variant> load() override;
void save(const std::map<string, Variant>& values) override;
void save(const string& key, const Variant& value) override;
void initialize();
protected:
SqliteStatement& stmtInsert(const string& key, const string& value) override;
SqliteStatement& stmtSelect() override;
SqliteDatabase& database() override;
private:
string myTableName;
@ -50,7 +50,7 @@ class KeyValueRepositorySqlite : public KeyValueRepository
KeyValueRepositorySqlite(const KeyValueRepositorySqlite&) = delete;
KeyValueRepositorySqlite(KeyValueRepositorySqlite&&) = delete;
KeyValueRepositorySqlite& operator=(const KeyValueRepositorySqlite&) = delete;
KeyValueRepositorySqlite operator=(KeyValueRepositorySqlite&&) = delete;
KeyValueRepositorySqlite& operator=(KeyValueRepositorySqlite&&) = delete;
};
#endif // KEY_VALUE_REPOSITORY_SQLITE_HXX

View File

@ -61,3 +61,10 @@ string SqliteStatement::columnText(int index) const
{
return reinterpret_cast<const char*>(sqlite3_column_text(myStmt, index));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int SqliteStatement::columnInt(int index) const
{
return sqlite3_column_int(myStmt, index);
}

View File

@ -39,6 +39,8 @@ class SqliteStatement {
string columnText(int index) const;
Int32 columnInt(int index) const;
private:
sqlite3_stmt* myStmt{nullptr};

View File

@ -1,7 +1,9 @@
MODULE := src/common/repository/sqlite
MODULE_OBJS := \
src/common/repository/sqlite/AbstractKeyValueRepositorySqlite.o \
src/common/repository/sqlite/KeyValueRepositorySqlite.o \
src/common/repository/sqlite/CompositeKeyValueRepositorySqlite.o \
src/common/repository/sqlite/SettingsDb.o \
src/common/repository/sqlite/SqliteDatabase.o \
src/common/repository/sqlite/SqliteStatement.o \