Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions > Class Template Reference

#include <QueryBuilders.hpp>

Inheritance diagram for Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >:
Lightweight::SqlAllFieldsQueryBuilder< Record, QueryOptions, Execution >

Public Member Functions

auto Exist ()
 
auto Count ()
 
auto All ()
 Executes a SELECT query and returns all records found.
 
template<auto... RelationPath>
requires DataMapperRecord<Record> && (sizeof...(RelationPath) >= 1)
Derived & With ()
 Eagerly loads the given relation for every record the query returns, in a bounded number of queries instead of one per record.
 
auto Delete ()
 Executes a DELETE query.
 
template<auto Field>
requires std::is_member_object_pointer_v<decltype(Field)>
auto All ()
 Executes a SELECT query and returns all records found for the specified field.
 
template<auto... ReferencedFields>
requires (sizeof...(ReferencedFields) >= 2)
auto All ()
 Executes a SELECT query and returns all records found for the specified field, only having the specified fields queried and populated.
 
auto First ()
 Executes a SELECT query for the first record found and returns it.
 
template<auto Field>
requires std::is_member_object_pointer_v<decltype(Field)>
auto First ()
 Executes the query to get a single scalar value from the first record found.
 
template<auto... ReferencedFields>
requires (sizeof...(ReferencedFields) >= 2)
auto First ()
 Executes a SELECT query for the first record found and returns it with only the specified fields populated.
 
auto First (size_t n)
 Executes a SELECT query for the first n records found and returns them.
 
template<auto... ReferencedFields>
auto First (size_t n)
 Executes a SELECT query for the first n records with only the specified fields populated.
 
auto Range (size_t offset, size_t limit)
 Executes a SELECT query for a range of records and returns them.
 
template<auto... ReferencedFields>
auto Range (size_t offset, size_t limit)
 Executes a SELECT query for a range of records with only the specified fields populated.
 

Protected Member Functions

LIGHTWEIGHT_FORCE_INLINE SqlCoreDataMapperQueryBuilder (DataMapper &dm, std::string fields) noexcept
 Constructs a query builder with the given data mapper and field list.
 

Detailed Description

template<typename Record, typename Derived, DataMapperOptions QueryOptions = {}>
class Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >

Main API for mapping records to C++ from the database using high level C++ syntax.

Definition at line 69 of file QueryBuilders.hpp.

Constructor & Destructor Documentation

◆ SqlCoreDataMapperQueryBuilder()

template<typename Record , typename Derived , DataMapperOptions QueryOptions>
Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::SqlCoreDataMapperQueryBuilder ( DataMapper &  dm,
std::string  fields 
)
inlineexplicitprotectednoexcept

Constructs a query builder with the given data mapper and field list.

Definition at line 1211 of file DataMapper.hpp.

Member Function Documentation

◆ Exist()

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::Exist ( )
inline

Executes a SELECT 1 ... query and returns true if a record exists We do not provide db specific syntax to check this but reuse the First() implementation

Definition at line 112 of file QueryBuilders.hpp.

◆ Count()

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::Count ( )
inline

Executes a SELECT COUNT query and returns the number of records found.

A preceding GroupBy is honored, making the query count per group. Since only a single value is returned, the caller receives the count of one arbitrary group: the emitted SELECT COUNT query carries no ORDER BY (a preceding OrderBy does not apply to it), so which group's count is read back is unspecified and may differ between database systems and between runs.

Definition at line 124 of file QueryBuilders.hpp.

◆ All() [1/3]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::All ( )
inline

Executes a SELECT query and returns all records found.

Definition at line 130 of file QueryBuilders.hpp.

◆ With()

template<typename Record , typename Derived , DataMapperOptions QueryOptions>
requires DataMapperRecord<Record> && (sizeof...(RelationPath) >= 1)
template<auto... RelationPath>
requires DataMapperRecord<Record> && (sizeof...(RelationPath) >= 1)
Derived & Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::With ( )

Eagerly loads the given relation for every record the query returns, in a bounded number of queries instead of one per record.

Touching a relation on a query result normally loads it on demand, one query per record - the N+1 problem. With<>() instead resolves the relation for the whole result set after it has been materialized: the keys are collected, the related rows are fetched with WHERE ... IN (...) (chunked, see SqlQueryFormatter::MaxInPredicateValues), and the rows are distributed to the records in memory.

Call it once per relation to load; the calls chain.

Naming several relations forms a path: the first is resolved for the result set, the records it loaded are then gathered and the next relation resolved for all of them at once. A path of any length still costs a constant number of queries per level, never one per record - which is what a nested BelongsTo needs, since each record holds its own copy of the target and touching that copy's own relation would otherwise be an N+1 one level down.

Template Parameters
RelationPathOne or more relations, in the form of &Record::FieldName. The first must be a member of the queried record, each subsequent one a member of the preceding relation's target. Supported for BelongsTo and HasMany; naming any other member is a compile error.
auto tracks = dm.Query<Track>()
.With<&Track::album>() // one extra SELECT ... WHERE id IN (...)
.With<&Track::album, &Album::artist>() // one more, for every album at once
.All();
for (auto& track: tracks)
std::println("{} - {}", track.album.Record().title,
track.album.Record().artist.Record().name); // no queries here
auto All()
Executes a SELECT query and returns all records found.
Note
Relations that were not named keep their usual on-demand behaviour. Combining With<>() with DataMapperOptions { .loadRelations = false } therefore makes any unrequested relation throw SqlRequireLoadedError on access rather than quietly issuing a query. To eagerly load everything instead of naming paths, see DataMapperOptions::eagerLoadDepth.
Returns
This builder, for chaining.

Definition at line 1239 of file DataMapper.hpp.

◆ Delete()

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::Delete ( )
inline

Executes a DELETE query.

Definition at line 180 of file QueryBuilders.hpp.

◆ All() [2/3]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
template<auto Field>
requires std::is_member_object_pointer_v<decltype(Field)>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::All ( )
inline

Executes a SELECT query and returns all records found for the specified field.

Template Parameters
FieldThe field to select from the record, in the form of &Record::FieldName.
Returns
A vector of values of the type of the specified field.
auto dm = DataMapper {};
auto const ages = dm.Query<Person>()
.OrderBy(FieldNameOf<&Person::age>, SqlResultOrdering::ASCENDING)
.All<&Person::age>();
Main API for mapping records to and from the database using high level C++ syntax.
std::vector< Record > Query(SqlSelectQueryBuilder::ComposedQuery const &selectQuery, InputParameters &&... inputParameters)

Definition at line 203 of file QueryBuilders.hpp.

◆ All() [3/3]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
template<auto... ReferencedFields>
requires (sizeof...(ReferencedFields) >= 2)
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::All ( )
inline

Executes a SELECT query and returns all records found for the specified field, only having the specified fields queried and populated.

Template Parameters
ReferencedFieldsThe fields to select from the record, in the form of &Record::FieldName.
Returns
A vector of records with the given fields populated.
auto dm = DataMapper {};
auto const ages = dm.Query<Person>()
.OrderBy(FieldNameOf<&Person::age>, SqlResultOrdering::ASCENDING)
.All<&Person::name, &Person::age>();

Definition at line 223 of file QueryBuilders.hpp.

◆ First() [1/5]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::First ( )
inline

Executes a SELECT query for the first record found and returns it.

Definition at line 229 of file QueryBuilders.hpp.

◆ First() [2/5]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
template<auto Field>
requires std::is_member_object_pointer_v<decltype(Field)>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::First ( )
inline

Executes the query to get a single scalar value from the first record found.

Template Parameters
FieldThe field to select from the record, in the form of &Record::FieldName.
Returns
an optional value of the type of the field, or an empty optional if no record was found.

Definition at line 245 of file QueryBuilders.hpp.

◆ First() [3/5]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
template<auto... ReferencedFields>
requires (sizeof...(ReferencedFields) >= 2)
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::First ( )
inline

Executes a SELECT query for the first record found and returns it with only the specified fields populated.

Template Parameters
ReferencedFieldsThe fields to select from the record, in the form of &Record::FieldName.
Returns
an optional record with only the specified fields populated, or an empty optional if no record was found.

Definition at line 257 of file QueryBuilders.hpp.

◆ First() [4/5]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::First ( size_t  n)
inline

Executes a SELECT query for the first n records found and returns them.

Definition at line 263 of file QueryBuilders.hpp.

◆ First() [5/5]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
template<auto... ReferencedFields>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::First ( size_t  n)
inline

Executes a SELECT query for the first n records with only the specified fields populated.

Definition at line 270 of file QueryBuilders.hpp.

◆ Range() [1/2]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::Range ( size_t  offset,
size_t  limit 
)
inline

Executes a SELECT query for a range of records and returns them.

Definition at line 276 of file QueryBuilders.hpp.

◆ Range() [2/2]

template<typename Record , typename Derived , DataMapperOptions QueryOptions = {}>
template<auto... ReferencedFields>
auto Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived, QueryOptions >::Range ( size_t  offset,
size_t  limit 
)
inline

Executes a SELECT query for a range of records with only the specified fields populated.

Definition at line 283 of file QueryBuilders.hpp.


The documentation for this class was generated from the following files: