Lightweight 0.20250904.0
Loading...
Searching...
No Matches
Data Mapper

The data mapper is a high level API for mapping records to and from the database using high level C++ syntax. More...

Concepts

concept  Lightweight::DataMapperRecord
 Represents a record type that can be used with the DataMapper.
 
concept  Lightweight::FieldWithStorage
 

Classes

class  Lightweight::BelongsTo< TheReferencedField, ColumnNameOverrideString, Nullable >
 Represents a one-to-one relationship. More...
 
class  Lightweight::DataMapper
 Main API for mapping records to and from the database using high level C++ syntax. More...
 
class  Lightweight::SqlRequireLoadedError
 Represents an error when a record is required to be loaded but is not. More...
 
struct  Lightweight::Field< T, P1, P2 >
 Represents a single column in a table. More...
 
class  Lightweight::HasMany< OtherRecord >
 This HasMany<OtherRecord> represents a simple one-to-many relationship between two records. More...
 
class  Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT >
 This API represents a many-to-many relationship between two records through a third record. More...
 
class  Lightweight::HasOneThrough< OtherTable, ThroughTable >
 Represents a one-to-one relationship through a join table. More...
 
class  Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived >
 
class  Lightweight::SqlAllFieldsQueryBuilder< Record >
 Represents a query builder that retrieves all fields of a record. More...
 

Typedefs

template<auto Field>
using Lightweight::ReferencedFieldTypeOf = typename std::remove_cvref_t< decltype(std::declval< MemberClassType< decltype(Field)> >().*Field)>::ValueType
 Retrieves the type of a member field in a record.
 
template<size_t... Ints>
using Lightweight::SqlElements = std::integer_sequence< size_t, Ints... >
 Represents a sequence of indexes that can be used alongside Query() to retrieve only part of the record.
 

Functions

template<typename Record >
LIGHTWEIGHT_FORCE_INLINE RecordPrimaryKeyType< Record > Lightweight::GetPrimaryKeyField (Record const &record) noexcept
 

Variables

template<typename Record >
constexpr size_t Lightweight::RecordStorageFieldCount
 
template<typename T >
constexpr bool Lightweight::HasPrimaryKey = detail::CheckFieldProperty<[]<typename Field>() { return IsPrimaryKey<Field>; }, T>
 Tests if the given record type does contain a primary key.
 
template<typename T >
constexpr bool Lightweight::HasAutoIncrementPrimaryKey
 Tests if the given record type does contain an auto increment primary key.
 
template<std::size_t I, typename Record >
constexpr std::string_view Lightweight::FieldNameAt = detail::FieldNameAt<I, Record>()
 Returns the SQL field name of the given field index in the record.
 
template<typename Record >
constexpr std::string_view Lightweight::RecordTableName = detail::RecordTableNameImpl<Record>::Value
 Holds the SQL tabl ename for the given record type.
 
template<auto ReferencedField>
constexpr auto Lightweight::FullyQualifiedNameOf
 Holds the quoted fully qualified field name (including table name) of the given field.
 
template<auto... ReferencedFields>
constexpr auto Lightweight::FullyQualifiedNamesOf
 Holds the quoted fully qualified field names of the given fields.
 

Detailed Description

The data mapper is a high level API for mapping records to and from the database using high level C++ syntax.

Typedef Documentation

◆ ReferencedFieldTypeOf

template<auto Field>
using Lightweight::ReferencedFieldTypeOf = typedef typename std::remove_cvref_t<decltype(std::declval<MemberClassType<decltype(Field)> >().*Field)>::ValueType

Retrieves the type of a member field in a record.

Field must be a member of the record type, and it must be a field type, e.g. Field<int> or BelongsTo<OtherRecord>.

using MyRecord = Record {
Field<int> value;
Field<std::optional<char>> optionalValue;
};
using MyFieldType = ReferencedFieldTypeOf<&MyRecord::value>; // Retrieves `int`
using MyOptionalFieldType = ReferencedFieldTypeOf<&MyRecord::optionalValue>; // Retrieves `std::optional<char>`
Represents a single column in a table.
Definition Field.hpp:84

Definition at line 358 of file Field.hpp.

◆ SqlElements

template<size_t... Ints>
using Lightweight::SqlElements = typedef std::integer_sequence<size_t, Ints...>

Represents a sequence of indexes that can be used alongside Query() to retrieve only part of the record.

Definition at line 20 of file Record.hpp.

Function Documentation

◆ GetPrimaryKeyField()

template<typename Record >
LIGHTWEIGHT_FORCE_INLINE RecordPrimaryKeyType< Record > Lightweight::GetPrimaryKeyField ( Record const &  record)
inlinenoexcept

Returns the first primary key field of the record.

Definition at line 178 of file Record.hpp.

Referenced by Lightweight::DataMapper::Create().

Variable Documentation

◆ RecordStorageFieldCount

template<typename Record >
constexpr size_t Lightweight::RecordStorageFieldCount
constexpr
Initial value:
=
Reflection::FoldMembers<Record>(size_t { 0 }, []<size_t I, typename Field>(size_t const accum) constexpr {
if constexpr (FieldWithStorage<Field>)
return accum + 1;
else
return accum;
})

Represents the number of fields with storage in a record.

Definition at line 137 of file Record.hpp.

◆ HasPrimaryKey

template<typename T >
constexpr bool Lightweight::HasPrimaryKey = detail::CheckFieldProperty<[]<typename Field>() { return IsPrimaryKey<Field>; }, T>
constexpr

Tests if the given record type does contain a primary key.

Definition at line 165 of file Record.hpp.

◆ HasAutoIncrementPrimaryKey

template<typename T >
constexpr bool Lightweight::HasAutoIncrementPrimaryKey
constexpr
Initial value:
=
detail::CheckFieldProperty<[]<typename Field>() { return IsAutoIncrementPrimaryKey<Field>; }, T>

Tests if the given record type does contain an auto increment primary key.

Definition at line 171 of file Record.hpp.

◆ FieldNameAt

template<std::size_t I, typename Record >
constexpr std::string_view Lightweight::FieldNameAt = detail::FieldNameAt<I, Record>()
inlineconstexpr

Returns the SQL field name of the given field index in the record.

Definition at line 170 of file Utils.hpp.

Referenced by Lightweight::SqlCoreDataMapperQueryBuilder< Record, Derived >::Range().

◆ RecordTableName

template<typename Record >
constexpr std::string_view Lightweight::RecordTableName = detail::RecordTableNameImpl<Record>::Value
constexpr

Holds the SQL tabl ename for the given record type.

Definition at line 176 of file Utils.hpp.

◆ FullyQualifiedNameOf

template<auto ReferencedField>
constexpr auto Lightweight::FullyQualifiedNameOf
inlineconstexpr
Initial value:
= SqlRawColumnNameView {
.value = detail::FullyQualifiedNameOfImpl<ReferencedField>::value,
}

Holds the quoted fully qualified field name (including table name) of the given field.

Template Parameters
ReferencedField
auto const quotedFieldName = FullyQualifiedNameOf<&Person::id>;
static_assert(quotedFieldName.value == R"sql("Person"."id")sql");

Definition at line 311 of file Utils.hpp.

◆ FullyQualifiedNamesOf

template<auto... ReferencedFields>
constexpr auto Lightweight::FullyQualifiedNamesOf
inlineconstexpr
Initial value:
= SqlRawColumnNameView {
.value = detail::FullyQualifiedNamesOfImpl<ReferencedFields...>::value,
}

Holds the quoted fully qualified field names of the given fields.

auto const quotedFieldNames = FullyQualifiedNamesOf<&Person::id, &Person::name, &Person::age>;
static_assert(quotedFieldNames.value == R"sql("Person"."id", "Person"."name", "Person"."age")sql");

Definition at line 356 of file Utils.hpp.