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

Commit

Permalink
Replace Database::prepare with Statement wrapper class
Browse files Browse the repository at this point in the history
This means we can get rid of all those annoying calls to
sqlite3_finalize().

Signed-off-by: Tim Serong <[email protected]>
  • Loading branch information
tserong committed Nov 16, 2023
1 parent fce9b91 commit 669840d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 39 deletions.
7 changes: 2 additions & 5 deletions src/checks/metadata_schema_version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ std::string MetadataSchemaVersionFix::to_string() const {

bool MetadataSchemaVersionCheck::do_check() {
int version = 0;
sqlite3_stmt* stm = metadata->prepare("PRAGMA user_version;");
Statement stm(metadata->handle, "PRAGMA user_version;");
if (sqlite3_step(stm) == SQLITE_ROW && sqlite3_column_count(stm) > 0) {
version = sqlite3_column_int(stm, 0);
} else {
const char* err = sqlite3_errmsg(metadata->handle);
sqlite3_finalize(stm);
throw std::runtime_error(err);
throw std::runtime_error(sqlite3_errmsg(metadata->handle));
}
sqlite3_finalize(stm);
log_verbose("Got schema version " + std::to_string(version));
if (version != EXPECTED_METADATA_SCHEMA_VERSION) {
fixes.emplace_back(
Expand Down
3 changes: 1 addition & 2 deletions src/checks/object_integrity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool ObjectIntegrityCheck::do_check() {
"SELECT object_id, id, checksum, size FROM versioned_objects "
"WHERE object_id IS NOT NULL;";

sqlite3_stmt* stm = metadata->prepare(query);
Statement stm(metadata->handle, query);
int rc = sqlite3_step(stm);
while (rc == SQLITE_ROW && sqlite3_column_count(stm) > 0) {
std::string uuid{
Expand Down Expand Up @@ -80,7 +80,6 @@ bool ObjectIntegrityCheck::do_check() {

rc = sqlite3_step(stm);
}
sqlite3_finalize(stm);

return fail_count == 0;
}
3 changes: 1 addition & 2 deletions src/checks/orphaned_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool OrphanedMetadataCheck::do_check() {
std::string query =
"SELECT object_id, id FROM versioned_objects WHERE object_id IS NOT "
"NULL;";
sqlite3_stmt* stm = metadata->prepare(query);
Statement stm(metadata->handle, query);

int rc = sqlite3_step(stm);
while (rc == SQLITE_ROW && sqlite3_column_count(stm) > 0) {
Expand All @@ -60,7 +60,6 @@ bool OrphanedMetadataCheck::do_check() {
}
rc = sqlite3_step(stm);
}
sqlite3_finalize(stm);

return orphan_count == 0;
}
7 changes: 2 additions & 5 deletions src/checks/orphaned_objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool OrphanedObjectsCheck::do_check() {
" AND "
" path_uuid = '" +
uuid + "'";
sqlite3_stmt* stm = metadata->prepare(query);
Statement stm(metadata->handle, query);
if (sqlite3_step(stm) == SQLITE_ROW &&
sqlite3_column_count(stm) > 0) {
int count = sqlite3_column_int(stm, 0);
Expand All @@ -143,11 +143,8 @@ bool OrphanedObjectsCheck::do_check() {
} else {
// This can't happen ("SELECT COUNT(...)" is _always_ going
// to give us one row with one column...)
const char* err = sqlite3_errmsg(metadata->handle);
sqlite3_finalize(stm);
throw std::runtime_error(err);
throw std::runtime_error(sqlite3_errmsg(metadata->handle));
}
sqlite3_finalize(stm);
} else {
// This is something else
fixes.emplace_back(
Expand Down
25 changes: 5 additions & 20 deletions src/sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ Database::~Database() {
sqlite3_close(handle);
}

sqlite3_stmt* Database::prepare(const std::string& query) const {
sqlite3_stmt* stm;
const char* unused = 0;
if (sqlite3_prepare(handle, query.c_str(), query.length(), &stm, &unused) !=
SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(handle));
}
return stm;
}

/* Count in Table - Count number of rows in named table where the condition
* is true. Translates directly into:
*
Expand All @@ -48,27 +38,21 @@ int Database::count_in_table(
) const {
std::string query =
"SELECT COUNT(*) FROM " + table + " WHERE " + condition + ";";
Statement stm(handle, query);
int count = 0;
int rc = 0;
sqlite3_stmt* stm;
const char* unused = 0;
rc = sqlite3_prepare(handle, query.c_str(), query.length(), &stm, &unused);
if (rc != SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(handle));
}
rc = sqlite3_step(stm);
if (rc == SQLITE_ROW && sqlite3_column_count(stm) > 0) {
if (sqlite3_step(stm) == SQLITE_ROW && sqlite3_column_count(stm) > 0) {
count = sqlite3_column_int(stm, 0);
}
sqlite3_finalize(stm);
return count;
}

// TODO: delete this, it's not used anywhere
/* Select from Table - Get all non-null entries of one column from a table.
* Translates into:
*
* SELECT column FROM table WHERE column IS NOT NULL ;
*/
/*
std::vector<std::string> Database::select_from_table(
const std::string& table, const std::string& column
) const {
Expand All @@ -89,3 +73,4 @@ std::vector<std::string> Database::select_from_table(
sqlite3_finalize(stm);
return result;
}
*/
26 changes: 21 additions & 5 deletions src/sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
#include <string>
#include <vector>

class Statement {
private:
sqlite3_stmt* stmt;

public:
Statement() = delete;
Statement(const Statement&) = delete;
Statement& operator=(const Statement&) = delete;
Statement(sqlite3* db, const std::string& query) : stmt(nullptr) {
if (sqlite3_prepare_v2(db, query.c_str(), query.length(), &stmt, nullptr) !=
SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(db));
}
};
virtual ~Statement() { sqlite3_finalize(stmt); };
operator sqlite3_stmt*() { return stmt; }
};

class Database {
private:
const std::filesystem::path& db;
Expand All @@ -29,13 +47,11 @@ class Database {
Database(const std::filesystem::path& _db);
~Database();

sqlite3_stmt* prepare(const std::string& query) const;

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;
//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 669840d

Please sign in to comment.