diff --git a/src/checks/metadata_schema_version.cc b/src/checks/metadata_schema_version.cc index 34577f3..d6ec06f 100644 --- a/src/checks/metadata_schema_version.cc +++ b/src/checks/metadata_schema_version.cc @@ -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( diff --git a/src/checks/object_integrity.cc b/src/checks/object_integrity.cc index 24177cd..bfbabd4 100644 --- a/src/checks/object_integrity.cc +++ b/src/checks/object_integrity.cc @@ -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{ @@ -80,7 +80,6 @@ bool ObjectIntegrityCheck::do_check() { rc = sqlite3_step(stm); } - sqlite3_finalize(stm); return fail_count == 0; } diff --git a/src/checks/orphaned_metadata.cc b/src/checks/orphaned_metadata.cc index a48f26c..1c17490 100644 --- a/src/checks/orphaned_metadata.cc +++ b/src/checks/orphaned_metadata.cc @@ -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) { @@ -60,7 +60,6 @@ bool OrphanedMetadataCheck::do_check() { } rc = sqlite3_step(stm); } - sqlite3_finalize(stm); return orphan_count == 0; } diff --git a/src/checks/orphaned_objects.cc b/src/checks/orphaned_objects.cc index e45f849..11b1be5 100644 --- a/src/checks/orphaned_objects.cc +++ b/src/checks/orphaned_objects.cc @@ -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); @@ -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( diff --git a/src/sqlite.cc b/src/sqlite.cc index b3f36f5..730866e 100644 --- a/src/sqlite.cc +++ b/src/sqlite.cc @@ -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: * @@ -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 Database::select_from_table( const std::string& table, const std::string& column ) const { @@ -89,3 +73,4 @@ std::vector Database::select_from_table( sqlite3_finalize(stm); return result; } +*/ diff --git a/src/sqlite.h b/src/sqlite.h index 6785e22..0d8a70d 100644 --- a/src/sqlite.h +++ b/src/sqlite.h @@ -20,6 +20,24 @@ #include #include +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; @@ -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 select_from_table( - const std::string& table, const std::string& column - ) const; + //std::vector select_from_table( + // const std::string& table, const std::string& column + //) const; }; #endif // FSCK_SFS_SRC_SQLITE_H__