Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Fix constness and references in Database class
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Serong <[email protected]>
  • Loading branch information
tserong committed Nov 14, 2023
1 parent 6136130 commit b94dfd9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
#include <filesystem>
#include <iostream>

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
}
}

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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<std::string> 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;
Expand All @@ -81,6 +84,7 @@ std::vector<std::string> Database::select_from_table(
std::vector<std::string> 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;
Expand Down
13 changes: 8 additions & 5 deletions src/sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> select_from_table(std::string, std::string);
int count_in_table(const std::string& table, const std::string& condition)
const;
std::vector<std::string> select_from_table(
const std::string& table, const std::string& column
) const;
};

#endif // FSCK_SFS_SRC_SQLITE_H__

0 comments on commit b94dfd9

Please sign in to comment.