From 09917807268124068c599c8cb2d56ccfc44dedfe Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 16 Jun 2026 09:05:40 +0000 Subject: [PATCH] Remove the now-unused GetMaxId API (#10) GetMaxId and its backing FetchMaxIdQuery only existed to support the old client-side SaveAutoIncrement (SELECT MAX(id) + 1). Since #8, SaveAutoIncrement relies on SQLite's native AUTOINCREMENT and sqlite3_last_insert_rowid(), so nothing in the library uses GetMaxId anymore and it was the last remnant of the user-space id scheme. Removes: - Database::GetMaxId() (public template) and the private GetMaxId(Reflection&) - FetchMaxIdQuery (declaration and implementation) - the ReadMaxId test This is a minor breaking change to the public API. Full suite green (58 tests). Closes #10 --- include/database.h | 12 ------------ include/queries.h | 14 -------------- src/database.cc | 6 ------ src/queries.cc | 32 -------------------------------- tests/database_test.cc | 17 ----------------- 5 files changed, 81 deletions(-) diff --git a/include/database.h b/include/database.h index 93086c1..611bdb7 100644 --- a/include/database.h +++ b/include/database.h @@ -99,15 +99,6 @@ class REFLECTION_EXPORT Database { return Hydrate(query_result, record)[0]; } - /// Retrieves the max id of a given record from the database - /// This corresponds to SELECT MAX(id) FROM TABLE in the SQL syntax - template - int64_t GetMaxId() const { - const auto type_id = typeid(T).name(); - const auto& record = GetRecord(type_id); - return GetMaxId(record); - } - /// Saves a given record in the database. /// This corresponds to an INSERT query in the SQL syntax template @@ -213,9 +204,6 @@ class REFLECTION_EXPORT Database { /// Returns a record type from its type information, retrieved from typeid(...).name() static const Reflection& GetRecord(const std::string& type_id); - /// Returns the max id currently stored for a given record (SELECT MAX(id) FROM table) - int64_t GetMaxId(const Reflection& record) const; - /// Creates concrete record types with initialized members, /// based on the textual representation of results from a fetch query template diff --git a/include/queries.h b/include/queries.h index 453b1b5..b0929af 100644 --- a/include/queries.h +++ b/include/queries.h @@ -139,20 +139,6 @@ class REFLECTION_EXPORT UpdateQuery final : public ExecutionQuery { void* p_; }; -/// A query for retrieving the max id of a given record from the database -class REFLECTION_EXPORT FetchMaxIdQuery final : public Query { -public: - explicit FetchMaxIdQuery(sqlite3* db, const Reflection& record); - ~FetchMaxIdQuery() override; - - /// Retrieve the max id currently used for the given record type - int64_t GetMaxId(); - -protected: - std::string PrepareSql() const override; - sqlite3_stmt* stmt_; -}; - struct FetchQueryResults; /// A query for retrieving all records from the database, which match a given predicate condition diff --git a/src/database.cc b/src/database.cc index 331f53d..870258b 100644 --- a/src/database.cc +++ b/src/database.cc @@ -111,12 +111,6 @@ const Reflection& Database::GetRecord(const std::string& type_id) { return GetReflectionRegister().records.at(type_id); } -int64_t Database::GetMaxId(const Reflection& record) const { - std::lock_guard lock(db_mutex_); - FetchMaxIdQuery query(db_, record); - return query.GetMaxId(); -} - void Database::Save(void* p, const Reflection& record) const { std::lock_guard lock(db_mutex_); InsertQuery query(db_, record, p); diff --git a/src/queries.cc b/src/queries.cc index 733cb4a..46bd366 100644 --- a/src/queries.cc +++ b/src/queries.cc @@ -277,38 +277,6 @@ std::vector UpdateQuery::Bindings() const { return bindings; } -FetchMaxIdQuery::FetchMaxIdQuery(sqlite3* db, const Reflection& record) : Query(db, record), stmt_(nullptr) {} - -FetchMaxIdQuery::~FetchMaxIdQuery() { - if (stmt_) { - sqlite3_finalize(stmt_); - } -} - -std::string FetchMaxIdQuery::PrepareSql() const { - return "SELECT MAX(id) FROM " + record_.name + ";"; -} - -int64_t FetchMaxIdQuery::GetMaxId() { - const auto sql = PrepareSql(); - - if (sqlite3_prepare_v2(db_, sql.data(), -1, &stmt_, nullptr)) { - throw std::runtime_error("Could not retrieve max id for table " + record_.name); - } - - const auto column_count = sqlite3_column_count(stmt_); - if (column_count != 1) { - throw std::runtime_error("Number of columns for max id is wrong for table " + record_.name); - } - - if (sqlite3_step(stmt_) != SQLITE_ROW) { - throw std::runtime_error("Row result could not be read for max id of table " + record_.name); - } - - const auto max_id = sqlite3_column_int(stmt_, 0); - return max_id; -} - FetchRecordsQuery::FetchRecordsQuery(sqlite3* db, const Reflection& record, const QueryPredicateBase* predicate) : Query(db, record), stmt_(nullptr), predicate_(predicate) {} diff --git a/tests/database_test.cc b/tests/database_test.cc index ca143f1..ed55737 100644 --- a/tests/database_test.cc +++ b/tests/database_test.cc @@ -571,23 +571,6 @@ TEST_F(DatabaseTest, FetchWithPredicateChaining) { EXPECT_EQ(37, fetched_persons[1].age); } -TEST_F(DatabaseTest, ReadMaxId) { - const auto db = Database::Instance(); - - std::vector persons; - - persons.push_back({L"john", L"appleseed", 28, false, 54}); - persons.push_back({L"mary", L"poppins", 20, false, 156}); - - db->Save(persons); - - const auto max_id_person = db->GetMaxId(); - EXPECT_EQ(156, max_id_person); - - const auto max_id_pet = db->GetMaxId(); - EXPECT_EQ(0, max_id_pet); -} - TEST_F(DatabaseTest, RawSqlQueryForPersistedRecord) { const auto db = Database::Instance();