From b94dfd9954e93be4fac0241db22a88f6bfce838c Mon Sep 17 00:00:00 2001 From: Tim Serong Date: Tue, 14 Nov 2023 19:00:19 +1100 Subject: [PATCH] Fix constness and references in Database class Signed-off-by: Tim Serong --- src/sqlite.cc | 18 +++++++++++------- src/sqlite.h | 13 ++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/sqlite.cc b/src/sqlite.cc index 79a6541..36b9ea4 100644 --- a/src/sqlite.cc +++ b/src/sqlite.cc @@ -14,13 +14,12 @@ #include #include -Database::Database(std::filesystem::path dbpath) { - db = dbpath; - +Database::Database(const std::filesystem::path& _db) : db(_db) { int rc = sqlite3_open(db.string().c_str(), &handle); if (rc != SQLITE_OK) { std::cout << "failed to open db" << std::endl; sqlite3_close(handle); + // FIXME: throw exception } } @@ -28,11 +27,12 @@ Database::~Database() { sqlite3_close(handle); } -int Database::prepare(std::string query, sqlite3_stmt** stm) { +int Database::prepare(const std::string& query, sqlite3_stmt** stm) const { int rc = 0; const char* unused = 0; rc = sqlite3_prepare(handle, query.c_str(), query.length(), stm, &unused); if (rc != SQLITE_OK) { + // FIXME: either throw an exception or log to stderr std::cout << "error while prepare: " << rc << std::endl; std::cout << query << std::endl; } @@ -44,7 +44,9 @@ int Database::prepare(std::string query, sqlite3_stmt** stm) { * * SELECT COUNT(*) FROM table WHERE condition ; */ -int Database::count_in_table(std::string table, std::string condition) { +int Database::count_in_table( + const std::string& table, const std::string& condition +) const { std::string query = "SELECT COUNT(*) FROM " + table + " WHERE " + condition + ";"; int count = 0; @@ -53,6 +55,7 @@ int Database::count_in_table(std::string table, std::string condition) { const char* unused = 0; rc = sqlite3_prepare(handle, query.c_str(), query.length(), &stm, &unused); if (rc != SQLITE_OK) { + // FIXME: either throw an exception or log to stderr std::cout << "error while prepare: " << rc << std::endl; std::cout << query << std::endl; return 0; @@ -71,8 +74,8 @@ int Database::count_in_table(std::string table, std::string condition) { * SELECT column FROM table WHERE column IS NOT NULL ; */ std::vector Database::select_from_table( - std::string table, std::string column -) { + const std::string& table, const std::string& column +) const { std::string query = "SELECT " + column + " FROM " + table + " WHERE " + column + " IS NOT NULL;"; int rc = 0; @@ -81,6 +84,7 @@ std::vector Database::select_from_table( std::vector result; rc = sqlite3_prepare(handle, query.c_str(), query.length(), &stm, &unused); if (rc != SQLITE_OK) { + // FIXME: either throw an exception or log to stderr std::cout << "error while prepare: " << rc << std::endl; std::cout << query << std::endl; return result; diff --git a/src/sqlite.h b/src/sqlite.h index 51bebad..2243d31 100644 --- a/src/sqlite.h +++ b/src/sqlite.h @@ -22,17 +22,20 @@ class Database { private: - std::filesystem::path db; + const std::filesystem::path& db; public: sqlite3* handle; - Database(std::filesystem::path); + Database(const std::filesystem::path& _db); ~Database(); - int prepare(std::string, sqlite3_stmt**); + int prepare(const std::string& query, sqlite3_stmt** stm) const; - int count_in_table(std::string, std::string); - std::vector select_from_table(std::string, std::string); + int count_in_table(const std::string& table, const std::string& condition) + const; + std::vector select_from_table( + const std::string& table, const std::string& column + ) const; }; #endif // FSCK_SFS_SRC_SQLITE_H__