Skip to content

Commit 7dec249

Browse files
committed
Return fcpp::vector from the public Database query API
FetchAll, Fetch(predicate) and the private FetchRecords hydration loop now return fcpp::vector<T> instead of std::vector<T>, so callers can chain functional operations (filter/map/all_of/...) directly on a query result. The hydration loop builds the result with insert_back(std::move(model)), keeping the no-extra-copy fetch path. The batch Save/SaveAutoIncrement/Update methods now take fcpp::vector<T>, with thin std::vector overloads kept for source compatibility: they copy into an fcpp::vector and forward, and the SaveAutoIncrement wrapper writes the generated ids back into the caller's std::vector. Internal plumbing (bindings, query classes, reflection metadata) stays on std::vector; only the public template API changes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YLXHcZjrnrdhRZAgqDh6kd
1 parent cea5f9f commit 7dec249

1 file changed

Lines changed: 38 additions & 10 deletions

File tree

‎include/database.h‎

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "queries.h"
3333
#include "query_predicates.h"
3434
#include "reflection.h"
35+
#include "vector.h" // fcpp::vector, the functional container returned by the query API
3536

3637
struct sqlite3;
3738

@@ -66,19 +67,23 @@ class REFLECTION_EXPORT Database {
6667
Database& operator=(Database&&) = delete;
6768

6869
/// Retrieves all entries of a given record from the database.
69-
/// This corresponds to a SELECT query in the SQL syntax
70+
/// This corresponds to a SELECT query in the SQL syntax.
71+
/// The result is an fcpp::vector, so functional operations (filter/map/...) can be chained
72+
/// directly on it; range-for, indexing and size() work as with a std::vector.
7073
template <typename T>
71-
std::vector<T> FetchAll() const {
74+
fcpp::vector<T> FetchAll() const {
7275
const auto type_id = typeid(T).name();
7376
const auto& record = GetRecord(type_id);
7477
EmptyPredicate empty;
7578
return FetchRecords<T>(record, &empty);
7679
}
7780

7881
/// Retrieves all entries of a given record from the database, which match a given predicate.
79-
/// This corresponds to a SELECT query in the SQL syntax
82+
/// This corresponds to a SELECT query in the SQL syntax.
83+
/// The result is an fcpp::vector, so functional operations (filter/map/...) can be chained
84+
/// directly on it; range-for, indexing and size() work as with a std::vector.
8085
template <typename T>
81-
std::vector<T> Fetch(const QueryPredicateBase* predicate) const {
86+
fcpp::vector<T> Fetch(const QueryPredicateBase* predicate) const {
8287
const auto type_id = typeid(T).name();
8388
const auto& record = GetRecord(type_id);
8489
return FetchRecords<T>(record, predicate);
@@ -120,26 +125,43 @@ class REFLECTION_EXPORT Database {
120125
/// Saves multiple records iteratively in the database.
121126
/// This corresponds to an INSERT query in the SQL syntax
122127
template <typename T>
123-
void Save(const std::vector<T>& models) {
128+
void Save(const fcpp::vector<T>& models) {
124129
const auto type_id = typeid(T).name();
125130
const auto& record = GetRecord(type_id);
126131
for (const auto& model : models) {
127132
Save((void*)&model, record);
128133
}
129134
}
130135

136+
/// std::vector compatibility overload: copies into an fcpp::vector and forwards.
137+
template <typename T>
138+
void Save(const std::vector<T>& models) {
139+
Save(fcpp::vector<T>(models));
140+
}
141+
131142
/// Saves multiple records iteratively in the database, letting the database assign their ids.
132143
/// The newly generated ids are written back into the passed-in models.
133144
/// This corresponds to an INSERT query in the SQL syntax
134145
template <typename T>
135-
void SaveAutoIncrement(std::vector<T>& models) {
146+
void SaveAutoIncrement(fcpp::vector<T>& models) {
136147
const auto type_id = typeid(T).name();
137148
const auto& record = GetRecord(type_id);
138149
for (auto& model : models) {
139150
model.id = SaveAutoIncrement((void*)&model, record);
140151
}
141152
}
142153

154+
/// std::vector compatibility overload: copies into an fcpp::vector, forwards, and writes the
155+
/// generated ids back into the caller's std::vector.
156+
template <typename T>
157+
void SaveAutoIncrement(std::vector<T>& models) {
158+
fcpp::vector<T> functional_models(models);
159+
SaveAutoIncrement(functional_models);
160+
for (size_t i = 0; i < models.size(); ++i) {
161+
models[i].id = functional_models[i].id;
162+
}
163+
}
164+
143165
/// Updates a given record in the database.
144166
/// This corresponds to an UPDATE query in the SQL syntax
145167
template <typename T>
@@ -152,14 +174,20 @@ class REFLECTION_EXPORT Database {
152174
/// Updates multiple records iteratively in the database.
153175
/// This corresponds to an UPDATE query in the SQL syntax
154176
template <typename T>
155-
void Update(const std::vector<T>& models) {
177+
void Update(const fcpp::vector<T>& models) {
156178
const auto type_id = typeid(T).name();
157179
const auto& record = GetRecord(type_id);
158180
for (const auto& model : models) {
159181
Update((void*)&model, record);
160182
}
161183
}
162184

185+
/// std::vector compatibility overload: copies into an fcpp::vector and forwards.
186+
template <typename T>
187+
void Update(const std::vector<T>& models) {
188+
Update(fcpp::vector<T>(models));
189+
}
190+
163191
/// Deletes a given record from the database.
164192
/// This corresponds to an DELETE query in the SQL syntax
165193
template <typename T>
@@ -203,14 +231,14 @@ class REFLECTION_EXPORT Database {
203231
/// each matching row directly from the prepared statement into a newly constructed T -
204232
/// no intermediate string materialization of the result set
205233
template <typename T>
206-
std::vector<T> FetchRecords(const Reflection& record, const QueryPredicateBase* predicate) const {
234+
fcpp::vector<T> FetchRecords(const Reflection& record, const QueryPredicateBase* predicate) const {
207235
std::lock_guard<std::mutex> lock(db_mutex_);
208236
FetchRecordsQuery query(db_, record, predicate);
209-
std::vector<T> models;
237+
fcpp::vector<T> models;
210238
while (query.StepRow()) {
211239
T model;
212240
query.HydrateCurrentRow((void*)&model, record);
213-
models.emplace_back(std::move(model));
241+
models.insert_back(std::move(model));
214242
}
215243
return models;
216244
}

0 commit comments

Comments
 (0)