Skip to content

Commit 5c5106a

Browse files
committed
[DataMapper] Add DataMapperOptions in all query functions
1 parent c03e057 commit 5c5106a

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

src/Lightweight/DataMapper/DataMapper.hpp

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class DataMapper
214214
std::optional<Record> QuerySingle(PrimaryKeyTypes&&... primaryKeys);
215215

216216
/// Queries multiple records from the database, based on the given query.
217-
template <typename Record, typename... InputParameters>
217+
template <typename Record, DataMapperOptions QueryOptions = {}, typename... InputParameters>
218218
std::vector<Record> Query(SqlSelectQueryBuilder::ComposedQuery const& selectQuery, InputParameters&&... inputParameters);
219219

220220
/// Queries multiple records from the database, based on the given query.
@@ -246,7 +246,7 @@ class DataMapper
246246
/// }
247247
/// }
248248
/// @endcode
249-
template <typename Record, typename... InputParameters>
249+
template <typename Record, DataMapperOptions QueryOptions = {}, typename... InputParameters>
250250
std::vector<Record> Query(std::string_view sqlQueryString, InputParameters&&... inputParameters);
251251

252252
/// Queries records from the database, based on the given query and can be used to retrieve only part of the record
@@ -273,7 +273,7 @@ class DataMapper
273273
/// // only info.name and info.city are loaded
274274
/// }
275275
/// @endcode
276-
template <typename ElementMask, typename Record, typename... InputParameters>
276+
template <typename ElementMask, typename Record, DataMapperOptions QueryOptions = {}, typename... InputParameters>
277277
std::vector<Record> Query(SqlSelectQueryBuilder::ComposedQuery const& selectQuery, InputParameters&&... inputParameters);
278278

279279
/// Queries records of different types from the database, based on the given query.
@@ -305,27 +305,6 @@ class DataMapper
305305
requires DataMapperRecord<First> && DataMapperRecord<Second> && DataMapperRecords<Rest...>
306306
std::vector<std::tuple<First, Second, Rest...>> Query(SqlSelectQueryBuilder::ComposedQuery const& selectQuery);
307307

308-
/// Queries records of different types from the database, based on the given query.
309-
template <typename FirstRecord, typename NextRecord, DataMapperOptions QueryOptions = {}>
310-
requires DataMapperRecord<FirstRecord> && DataMapperRecord<NextRecord>
311-
SqlAllFieldsQueryBuilder<std::tuple<FirstRecord, NextRecord>, QueryOptions> Query()
312-
{
313-
std::string fields;
314-
315-
auto const emplaceRecordsFrom = [&fields]<typename Record>() {
316-
Reflection::EnumerateMembers<Record>([&fields]<size_t I, typename Field>() {
317-
if (!fields.empty())
318-
fields += ", ";
319-
fields += std::format(R"("{}"."{}")", RecordTableName<Record>, FieldNameAt<I, Record>);
320-
});
321-
};
322-
323-
emplaceRecordsFrom.template operator()<FirstRecord>();
324-
emplaceRecordsFrom.template operator()<NextRecord>();
325-
326-
return SqlAllFieldsQueryBuilder<std::tuple<FirstRecord, NextRecord>, QueryOptions>(*this, std::move(fields));
327-
}
328-
329308
/// Queries records of given Record type.
330309
///
331310
/// The query builder can be used to further refine the query.
@@ -1567,16 +1546,16 @@ std::optional<Record> DataMapper::QuerySingle(SqlSelectQueryBuilder selectQuery,
15671546
// TODO: Provide Query(QueryBuilder, ...) method variant
15681547

15691548
/// Queries multiple records from the database using a composed query and optional input parameters.
1570-
template <typename Record, typename... InputParameters>
1549+
template <typename Record, DataMapperOptions QueryOptions, typename... InputParameters>
15711550
inline LIGHTWEIGHT_FORCE_INLINE std::vector<Record> DataMapper::Query(
15721551
SqlSelectQueryBuilder::ComposedQuery const& selectQuery, InputParameters&&... inputParameters)
15731552
{
15741553
static_assert(DataMapperRecord<Record> || std::same_as<Record, SqlVariantRow>, "Record must satisfy DataMapperRecord");
15751554

1576-
return Query<Record>(selectQuery.ToSql(), std::forward<InputParameters>(inputParameters)...);
1555+
return Query<Record, QueryOptions>(selectQuery.ToSql(), std::forward<InputParameters>(inputParameters)...);
15771556
}
15781557

1579-
template <typename Record, typename... InputParameters>
1558+
template <typename Record, DataMapperOptions QueryOptions, typename... InputParameters>
15801559
std::vector<Record> DataMapper::Query(std::string_view sqlQueryString, InputParameters&&... inputParameters)
15811560
{
15821561
auto result = std::vector<Record> {};
@@ -1622,7 +1601,8 @@ std::vector<Record> DataMapper::Query(std::string_view sqlQueryString, InputPara
16221601
for (auto& record: result)
16231602
{
16241603
SetModifiedState<ModifiedState::NotModified>(record);
1625-
ConfigureRelationAutoLoading(record);
1604+
if constexpr (QueryOptions.loadRelations)
1605+
ConfigureRelationAutoLoading(record);
16261606
}
16271607
}
16281608

@@ -1709,7 +1689,7 @@ std::vector<std::tuple<First, Second, Rest...>> DataMapper::Query(SqlSelectQuery
17091689
return result;
17101690
}
17111691

1712-
template <typename ElementMask, typename Record, typename... InputParameters>
1692+
template <typename ElementMask, typename Record, DataMapperOptions QueryOptions, typename... InputParameters>
17131693
std::vector<Record> DataMapper::Query(SqlSelectQueryBuilder::ComposedQuery const& selectQuery,
17141694
InputParameters&&... inputParameters)
17151695
{
@@ -1744,7 +1724,8 @@ std::vector<Record> DataMapper::Query(SqlSelectQueryBuilder::ComposedQuery const
17441724
for (auto& record: records)
17451725
{
17461726
SetModifiedState<ModifiedState::NotModified>(record);
1747-
ConfigureRelationAutoLoading(record);
1727+
if constexpr (QueryOptions.loadRelations)
1728+
ConfigureRelationAutoLoading(record);
17481729
}
17491730

17501731
return records;

src/tests/DataMapper/ReadTests.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,12 @@ TEST_CASE_METHOD(SqlTestFixture, "MapForJointStatement", "[DataMapper]")
531531
dm.Create(c);
532532
}
533533

534-
auto const records = dm.Query<JoinA, JoinC>()
535-
.InnerJoin<Member(JoinB::a_id), Member(JoinA::id)>()
536-
.InnerJoin<Member(JoinC::id), Member(JoinB::c_id)>()
537-
.All();
534+
auto const records = dm.Query<JoinA, JoinC>(dm.FromTable(RecordTableName<JoinA>)
535+
.Select()
536+
.Fields<JoinA, JoinC>()
537+
.InnerJoin<Member(JoinB::a_id), Member(JoinA::id)>()
538+
.InnerJoin<Member(JoinC::id), Member(JoinB::c_id)>()
539+
.All());
538540

539541
CHECK(records.size() == 50);
540542
int i = 1;

0 commit comments

Comments
 (0)