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
3637struct 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