Refactoring: more fluent interfaces, improve const semantics.

This commit is contained in:
Christian Speckner 2021-01-02 17:05:42 +01:00
parent ae534e271a
commit ba24fe7430
6 changed files with 28 additions and 33 deletions

View File

@ -41,8 +41,9 @@ shared_ptr<KeyValueRepository> CompositeKeyValueRepositorySqlite::get(const stri
bool CompositeKeyValueRepositorySqlite::has(const string& key)
{
try {
myStmtCountSet->reset();
myStmtCountSet->bind(1, key.c_str());
(*myStmtCountSet)
.reset()
.bind(1, key.c_str());
if (!myStmtCountSet->step())
throw new SqliteError("count failed");
@ -63,11 +64,12 @@ bool CompositeKeyValueRepositorySqlite::has(const string& key)
void CompositeKeyValueRepositorySqlite::remove(const string& key)
{
try {
myStmtDeleteSet->reset();
(*myStmtDeleteSet)
.reset()
.bind(1, key.c_str())
.step();
myStmtDelete->reset();
}
catch (const SqliteError& err) {
Logger::error(err.what());
@ -147,9 +149,8 @@ CompositeKeyValueRepositorySqlite::ProxyRepository::ProxyRepository(
SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtInsert(
const string& key, const string& value
) {
myRepo.myStmtInsert->reset();
return (*myRepo.myStmtInsert)
.reset()
.bind(1, myKey.c_str())
.bind(2, key.c_str())
.bind(3, value.c_str());
@ -158,9 +159,8 @@ SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtInsert(
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtSelect()
{
myRepo.myStmtSelect->reset();
return (*myRepo.myStmtSelect)
.reset()
.bind(1, myKey.c_str());
}
@ -177,9 +177,8 @@ SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtDelete(
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtSelectOne(const string& key)
{
myRepo.myStmtSelectOne->reset();
return (*myRepo.myStmtSelectOne)
.reset()
.bind(1, myKey.c_str())
.bind(2, key.c_str());
}
@ -187,9 +186,8 @@ SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtSelectO
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& CompositeKeyValueRepositorySqlite::ProxyRepository::stmtCount(const string& key)
{
myRepo.myStmtCount->reset();
return (*myRepo.myStmtCount)
.reset()
.bind(1, myKey.c_str())
.bind(2, key.c_str());
}

View File

@ -32,9 +32,8 @@ KeyValueRepositorySqlite::KeyValueRepositorySqlite(
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& KeyValueRepositorySqlite::stmtInsert(const string& key, const string& value)
{
myStmtInsert->reset();
return (*myStmtInsert)
.reset()
.bind(1, key.c_str())
.bind(2, value.c_str());
}
@ -42,35 +41,31 @@ SqliteStatement& KeyValueRepositorySqlite::stmtInsert(const string& key, const s
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& KeyValueRepositorySqlite::stmtSelect()
{
myStmtInsert->reset();
return *myStmtSelect;
return (*myStmtSelect)
.reset();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& KeyValueRepositorySqlite::stmtDelete(const string& key)
{
myStmtDelete->reset();
return (*myStmtDelete)
.reset()
.bind(1, key.c_str());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& KeyValueRepositorySqlite::stmtCount(const string& key)
{
myStmtCount->reset();
return (*myStmtCount)
.reset()
.bind(1, key.c_str());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SqliteStatement& KeyValueRepositorySqlite::stmtSelectOne(const string& key)
{
myStmtSelectOne->reset();
return (*myStmtSelectOne)
.reset()
.bind(1, key.c_str());
}

View File

@ -87,7 +87,7 @@ void SqliteDatabase::initialize()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SqliteDatabase::exec(const string& sql) const
void SqliteDatabase::exec(const string& sql)
{
if (sqlite3_exec(myHandle, sql.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK)
throw SqliteError(myHandle);
@ -105,7 +105,7 @@ Int32 SqliteDatabase::getUserVersion() const
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SqliteDatabase::setUserVersion(Int32 version) const
void SqliteDatabase::setUserVersion(Int32 version)
{
SqliteStatement(*this, "PRAGMA user_version = %i", static_cast<int>(version))
.step();

View File

@ -36,13 +36,13 @@ class SqliteDatabase
operator sqlite3*() const { return myHandle; }
void exec(const string &sql) const;
void exec(const string &sql);
template<class T, class ...Ts>
void exec(const string& sql, T arg1, Ts... args) const;
void exec(const string& sql, T arg1, Ts... args);
Int32 getUserVersion() const;
void setUserVersion(Int32 version) const;
void setUserVersion(Int32 version);
private:
@ -68,7 +68,7 @@ class SqliteDatabase
#endif
template <class T, class ...Ts>
void SqliteDatabase::exec(const string& sql, T arg1, Ts... args) const
void SqliteDatabase::exec(const string& sql, T arg1, Ts... args)
{
char buffer[512];

View File

@ -60,7 +60,7 @@ SqliteStatement& SqliteStatement::bind(int index, Int32 value)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool SqliteStatement::step() const
bool SqliteStatement::step()
{
int result = sqlite3_step(myStmt);
@ -70,9 +70,11 @@ bool SqliteStatement::step() const
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SqliteStatement::reset() const
SqliteStatement& SqliteStatement::reset()
{
if (sqlite3_reset(myStmt) != SQLITE_OK) throw SqliteError(myHandle);
return *this;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -37,9 +37,9 @@ class SqliteStatement {
SqliteStatement& bind(int index, const string& value);
SqliteStatement& bind(int index, Int32 value);
bool step() const;
bool step();
void reset() const;
SqliteStatement& reset();
string columnText(int index) const;