Skip to content

Commit 218ca04

Browse files
jkaliasclaude
andauthored
Remove the now-unused GetMaxId API (#10) (#29)
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 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3dc4de4 commit 218ca04

5 files changed

Lines changed: 0 additions & 81 deletions

File tree

include/database.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,6 @@ class REFLECTION_EXPORT Database {
9999
return Hydrate<T>(query_result, record)[0];
100100
}
101101

102-
/// Retrieves the max id of a given record from the database
103-
/// This corresponds to SELECT MAX(id) FROM TABLE in the SQL syntax
104-
template <typename T>
105-
int64_t GetMaxId() const {
106-
const auto type_id = typeid(T).name();
107-
const auto& record = GetRecord(type_id);
108-
return GetMaxId(record);
109-
}
110-
111102
/// Saves a given record in the database.
112103
/// This corresponds to an INSERT query in the SQL syntax
113104
template <typename T>
@@ -213,9 +204,6 @@ class REFLECTION_EXPORT Database {
213204
/// Returns a record type from its type information, retrieved from typeid(...).name()
214205
static const Reflection& GetRecord(const std::string& type_id);
215206

216-
/// Returns the max id currently stored for a given record (SELECT MAX(id) FROM table)
217-
int64_t GetMaxId(const Reflection& record) const;
218-
219207
/// Creates concrete record types with initialized members,
220208
/// based on the textual representation of results from a fetch query
221209
template <typename T>

include/queries.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,6 @@ class REFLECTION_EXPORT UpdateQuery final : public ExecutionQuery {
139139
void* p_;
140140
};
141141

142-
/// A query for retrieving the max id of a given record from the database
143-
class REFLECTION_EXPORT FetchMaxIdQuery final : public Query {
144-
public:
145-
explicit FetchMaxIdQuery(sqlite3* db, const Reflection& record);
146-
~FetchMaxIdQuery() override;
147-
148-
/// Retrieve the max id currently used for the given record type
149-
int64_t GetMaxId();
150-
151-
protected:
152-
std::string PrepareSql() const override;
153-
sqlite3_stmt* stmt_;
154-
};
155-
156142
struct FetchQueryResults;
157143

158144
/// A query for retrieving all records from the database, which match a given predicate condition

src/database.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ const Reflection& Database::GetRecord(const std::string& type_id) {
111111
return GetReflectionRegister().records.at(type_id);
112112
}
113113

114-
int64_t Database::GetMaxId(const Reflection& record) const {
115-
std::lock_guard<std::mutex> lock(db_mutex_);
116-
FetchMaxIdQuery query(db_, record);
117-
return query.GetMaxId();
118-
}
119-
120114
void Database::Save(void* p, const Reflection& record) const {
121115
std::lock_guard<std::mutex> lock(db_mutex_);
122116
InsertQuery query(db_, record, p);

src/queries.cc

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -277,38 +277,6 @@ std::vector<SqlValue> UpdateQuery::Bindings() const {
277277
return bindings;
278278
}
279279

280-
FetchMaxIdQuery::FetchMaxIdQuery(sqlite3* db, const Reflection& record) : Query(db, record), stmt_(nullptr) {}
281-
282-
FetchMaxIdQuery::~FetchMaxIdQuery() {
283-
if (stmt_) {
284-
sqlite3_finalize(stmt_);
285-
}
286-
}
287-
288-
std::string FetchMaxIdQuery::PrepareSql() const {
289-
return "SELECT MAX(id) FROM " + record_.name + ";";
290-
}
291-
292-
int64_t FetchMaxIdQuery::GetMaxId() {
293-
const auto sql = PrepareSql();
294-
295-
if (sqlite3_prepare_v2(db_, sql.data(), -1, &stmt_, nullptr)) {
296-
throw std::runtime_error("Could not retrieve max id for table " + record_.name);
297-
}
298-
299-
const auto column_count = sqlite3_column_count(stmt_);
300-
if (column_count != 1) {
301-
throw std::runtime_error("Number of columns for max id is wrong for table " + record_.name);
302-
}
303-
304-
if (sqlite3_step(stmt_) != SQLITE_ROW) {
305-
throw std::runtime_error("Row result could not be read for max id of table " + record_.name);
306-
}
307-
308-
const auto max_id = sqlite3_column_int(stmt_, 0);
309-
return max_id;
310-
}
311-
312280
FetchRecordsQuery::FetchRecordsQuery(sqlite3* db, const Reflection& record, const QueryPredicateBase* predicate)
313281
: Query(db, record), stmt_(nullptr), predicate_(predicate) {}
314282

tests/database_test.cc

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -571,23 +571,6 @@ TEST_F(DatabaseTest, FetchWithPredicateChaining) {
571571
EXPECT_EQ(37, fetched_persons[1].age);
572572
}
573573

574-
TEST_F(DatabaseTest, ReadMaxId) {
575-
const auto db = Database::Instance();
576-
577-
std::vector<Person> persons;
578-
579-
persons.push_back({L"john", L"appleseed", 28, false, 54});
580-
persons.push_back({L"mary", L"poppins", 20, false, 156});
581-
582-
db->Save(persons);
583-
584-
const auto max_id_person = db->GetMaxId<Person>();
585-
EXPECT_EQ(156, max_id_person);
586-
587-
const auto max_id_pet = db->GetMaxId<Pet>();
588-
EXPECT_EQ(0, max_id_pet);
589-
}
590-
591574
TEST_F(DatabaseTest, RawSqlQueryForPersistedRecord) {
592575
const auto db = Database::Instance();
593576

0 commit comments

Comments
 (0)