From ba24fe7430ebc4ccb24c964f7751be56e3cbdc41 Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Sat, 2 Jan 2021 17:05:42 +0100 Subject: [PATCH] Refactoring: more fluent interfaces, improve const semantics. --- .../CompositeKeyValueRepositorySqlite.cxx | 22 +++++++++---------- .../sqlite/KeyValueRepositorySqlite.cxx | 17 +++++--------- .../repository/sqlite/SqliteDatabase.cxx | 4 ++-- .../repository/sqlite/SqliteDatabase.hxx | 8 +++---- .../repository/sqlite/SqliteStatement.cxx | 6 +++-- .../repository/sqlite/SqliteStatement.hxx | 4 ++-- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/common/repository/sqlite/CompositeKeyValueRepositorySqlite.cxx b/src/common/repository/sqlite/CompositeKeyValueRepositorySqlite.cxx index c4dab8e33..933e5e893 100644 --- a/src/common/repository/sqlite/CompositeKeyValueRepositorySqlite.cxx +++ b/src/common/repository/sqlite/CompositeKeyValueRepositorySqlite.cxx @@ -41,8 +41,9 @@ shared_ptr 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()); } diff --git a/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx b/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx index c25de5832..c68d84d5d 100644 --- a/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx +++ b/src/common/repository/sqlite/KeyValueRepositorySqlite.cxx @@ -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()); } diff --git a/src/common/repository/sqlite/SqliteDatabase.cxx b/src/common/repository/sqlite/SqliteDatabase.cxx index 3317261dc..6bddb1e79 100644 --- a/src/common/repository/sqlite/SqliteDatabase.cxx +++ b/src/common/repository/sqlite/SqliteDatabase.cxx @@ -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(version)) .step(); diff --git a/src/common/repository/sqlite/SqliteDatabase.hxx b/src/common/repository/sqlite/SqliteDatabase.hxx index 58d4e3e02..c2e5e5b7a 100644 --- a/src/common/repository/sqlite/SqliteDatabase.hxx +++ b/src/common/repository/sqlite/SqliteDatabase.hxx @@ -36,13 +36,13 @@ class SqliteDatabase operator sqlite3*() const { return myHandle; } - void exec(const string &sql) const; + void exec(const string &sql); template - 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 -void SqliteDatabase::exec(const string& sql, T arg1, Ts... args) const +void SqliteDatabase::exec(const string& sql, T arg1, Ts... args) { char buffer[512]; diff --git a/src/common/repository/sqlite/SqliteStatement.cxx b/src/common/repository/sqlite/SqliteStatement.cxx index d807281a2..755608786 100644 --- a/src/common/repository/sqlite/SqliteStatement.cxx +++ b/src/common/repository/sqlite/SqliteStatement.cxx @@ -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; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/repository/sqlite/SqliteStatement.hxx b/src/common/repository/sqlite/SqliteStatement.hxx index 99b8a3f7b..e2ce3294d 100644 --- a/src/common/repository/sqlite/SqliteStatement.hxx +++ b/src/common/repository/sqlite/SqliteStatement.hxx @@ -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;