|
Lightweight 0.20250904.0
|
Main API for mapping records to and from the database using high level C++ syntax. More...
#include <DataMapper.hpp>
Inherits std::enable_shared_from_this< DataMapper >.
Public Member Functions | |
| DataMapper (PrivateTag _) | |
| Constructs a new data mapper, using the default connection. | |
| DataMapper (SqlConnection &&connection, PrivateTag _) | |
| Constructs a new data mapper, using the given connection. | |
| DataMapper (SqlConnectionString connectionString, PrivateTag _) | |
| Constructs a new data mapper, using the given connection string. | |
| DataMapper (DataMapper const &)=delete | |
| DataMapper (DataMapper &&) noexcept=default | |
| DataMapper & | operator= (DataMapper const &)=delete |
| DataMapper & | operator= (DataMapper &&) noexcept=default |
| SqlConnection const & | Connection () const noexcept |
| Returns the connection reference used by this data mapper. | |
| SqlConnection & | Connection () noexcept |
| Returns the mutable connection reference used by this data mapper. | |
| template<typename Record > | |
| std::vector< std::string > | CreateTableString (SqlServerType serverType) |
| Constructs a string list of SQL queries to create the table for the given record type. | |
| template<typename FirstRecord , typename... MoreRecords> | |
| std::vector< std::string > | CreateTablesString (SqlServerType serverType) |
| Constructs a string list of SQL queries to create the tables for the given record types. | |
| template<typename Record > | |
| void | CreateTable () |
| Creates the table for the given record type. | |
| template<typename FirstRecord , typename... MoreRecords> | |
| void | CreateTables () |
| Creates the tables for the given record types. | |
| template<typename Record > | |
| RecordPrimaryKeyType< Record > | Create (Record &record) |
| Creates a new record in the database. | |
| template<typename Record > | |
| RecordPrimaryKeyType< Record > | CreateExplicit (Record const &record) |
| Creates a new record in the database. | |
| template<typename Record , typename... PrimaryKeyTypes> | |
| std::optional< Record > | QuerySingle (PrimaryKeyTypes &&... primaryKeys) |
| Queries a single record (based on primary key) from the database. | |
| template<typename Record , typename... PrimaryKeyTypes> | |
| std::optional< Record > | QuerySingleWithoutRelationAutoLoading (PrimaryKeyTypes &&... primaryKeys) |
| Queries a single record (based on primary key) from the database without auto-loading relations. | |
| template<typename Record , typename... InputParameters> | |
| std::vector< Record > | Query (SqlSelectQueryBuilder::ComposedQuery const &selectQuery, InputParameters &&... inputParameters) |
| Queries multiple records from the database, based on the given query. | |
| template<typename... Records> requires DataMapperRecords<Records...> | |
| std::vector< std::tuple< Records... > > | QueryToTuple (SqlSelectQueryBuilder::ComposedQuery const &selectQuery) |
| template<typename Record , typename... InputParameters> | |
| std::vector< Record > | Query (std::string_view sqlQueryString, InputParameters &&... inputParameters) |
| template<typename ElementMask , typename Record , typename... InputParameters> | |
| std::vector< Record > | Query (SqlSelectQueryBuilder::ComposedQuery const &selectQuery, InputParameters &&... inputParameters) |
| template<typename Record > | |
| SqlAllFieldsQueryBuilder< Record > | Query () |
| template<typename Record > | |
| void | Update (Record &record) |
| Updates the record in the database. | |
| template<typename Record > | |
| std::size_t | Delete (Record const &record) |
| Deletes the record from the database. | |
| SqlQueryBuilder | FromTable (std::string_view tableName) |
| Constructs an SQL query builder for the given table name. | |
| template<typename Record > | |
| bool | IsModified (Record const &record) const noexcept |
| Checks if the record has any modified fields. | |
| template<typename Record > | |
| void | ClearModifiedState (Record &record) noexcept |
| Clears the modified state of the record. | |
| template<typename Record > | |
| void | LoadRelations (Record &record) |
| Loads all direct relations to this record. | |
| template<typename Record > | |
| void | ConfigureRelationAutoLoading (Record &record) |
| template<typename Record , typename... InputParameters> | |
| LIGHTWEIGHT_FORCE_INLINE std::vector< Record > | Query (SqlSelectQueryBuilder::ComposedQuery const &selectQuery, InputParameters &&... inputParameters) |
| template<typename FirstRecord , typename SecondRecord , typename... InputParameters> requires DataMapperRecord<FirstRecord> && DataMapperRecord<SecondRecord> | |
| std::vector< std::tuple< FirstRecord, SecondRecord > > | Query (SqlSelectQueryBuilder::ComposedQuery const &selectQuery, InputParameters &&... inputParameters) |
| template<typename Record , typename ValueType > | |
| LIGHTWEIGHT_FORCE_INLINE void | SetId (Record &record, ValueType &&id) |
| template<typename Record , size_t InitialOffset> | |
| LIGHTWEIGHT_FORCE_INLINE Record & | BindOutputColumns (Record &record) |
| template<typename ElementMask , typename Record , size_t InitialOffset> | |
| LIGHTWEIGHT_FORCE_INLINE Record & | BindOutputColumns (Record &record) |
Static Public Member Functions | |
| static std::shared_ptr< DataMapper > | Create () |
| Factory method to create a new data mapper with default connection. | |
| static std::shared_ptr< DataMapper > | Create (SqlConnection &&connection) |
| Factory method to create a new data mapper with given connection. | |
| static std::shared_ptr< DataMapper > | Create (SqlConnectionString connectionString) |
| Factory method to create a new data mapper with connection string. | |
| template<typename Record > | |
| static std::string | Inspect (Record const &record) |
| Constructs a human readable string representation of the given record. | |
Main API for mapping records to and from the database using high level C++ syntax.
A DataMapper instances operates on a single SQL connection and provides methods to create, read, update and delete records in the database.
Definition at line 88 of file DataMapper.hpp.
|
inline |
Constructs a new data mapper, using the default connection.
Definition at line 97 of file DataMapper.hpp.
|
inlineexplicit |
Constructs a new data mapper, using the given connection.
Definition at line 104 of file DataMapper.hpp.
|
inlineexplicit |
Constructs a new data mapper, using the given connection string.
Definition at line 111 of file DataMapper.hpp.
|
inlinestatic |
Factory method to create a new data mapper with default connection.
Definition at line 118 of file DataMapper.hpp.
|
inlinestatic |
Factory method to create a new data mapper with given connection.
Definition at line 124 of file DataMapper.hpp.
|
inlinestatic |
Factory method to create a new data mapper with connection string.
Definition at line 130 of file DataMapper.hpp.
|
inlinenoexcept |
Returns the connection reference used by this data mapper.
Definition at line 142 of file DataMapper.hpp.
|
inlinenoexcept |
Returns the mutable connection reference used by this data mapper.
Definition at line 148 of file DataMapper.hpp.
|
static |
Constructs a human readable string representation of the given record.
Definition at line 467 of file DataMapper.hpp.
| std::vector< std::string > Lightweight::DataMapper::CreateTableString | ( | SqlServerType | serverType | ) |
Constructs a string list of SQL queries to create the table for the given record type.
Definition at line 508 of file DataMapper.hpp.
References Lightweight::SqlMigrationQueryBuilder::CreateTable(), Lightweight::SqlQueryFormatter::Get(), Lightweight::SqlQueryBuilder::Migration(), and Lightweight::SqlForeignKeyReferenceDefinition::tableName.
| std::vector< std::string > Lightweight::DataMapper::CreateTablesString | ( | SqlServerType | serverType | ) |
Constructs a string list of SQL queries to create the tables for the given record types.
Definition at line 597 of file DataMapper.hpp.
| void Lightweight::DataMapper::CreateTable | ( | ) |
Creates the table for the given record type.
Definition at line 609 of file DataMapper.hpp.
References Lightweight::SqlStatement::ExecuteDirect(), and Lightweight::SqlConnection::ServerType().
| void Lightweight::DataMapper::CreateTables | ( | ) |
Creates the tables for the given record types.
Definition at line 619 of file DataMapper.hpp.
| RecordPrimaryKeyType< Record > Lightweight::DataMapper::Create | ( | Record & | record | ) |
Creates a new record in the database.
The record is inserted into the database and the primary key is set on this record.
Definition at line 700 of file DataMapper.hpp.
References ClearModifiedState(), ConfigureRelationAutoLoading(), Lightweight::SqlGuid::Create(), CreateExplicit(), Lightweight::GetPrimaryKeyField(), and Lightweight::SqlStatement::LastInsertId().
| RecordPrimaryKeyType< Record > Lightweight::DataMapper::CreateExplicit | ( | Record const & | record | ) |
Creates a new record in the database.
Definition at line 626 of file DataMapper.hpp.
References Lightweight::SqlStatement::Execute(), Lightweight::SqlQueryBuilder::Insert(), Lightweight::SqlStatement::LastInsertId(), Lightweight::SqlStatement::Prepare(), and Lightweight::SqlConnection::Query().
Referenced by Create().
| std::optional< Record > Lightweight::DataMapper::QuerySingle | ( | PrimaryKeyTypes &&... | primaryKeys | ) |
Queries a single record (based on primary key) from the database.
The primary key(s) are used to identify the record to load. If the record is not found, std::nullopt is returned.
Definition at line 947 of file DataMapper.hpp.
References ConfigureRelationAutoLoading().
| std::optional< Record > Lightweight::DataMapper::QuerySingleWithoutRelationAutoLoading | ( | PrimaryKeyTypes &&... | primaryKeys | ) |
Queries a single record (based on primary key) from the database without auto-loading relations.
The primary key(s) are used to identify the record to load.
Main goal of this function is to load record without relationships to decrease compilation time and work around some limitations of template instantiation depth on MSVC compiler.
Definition at line 919 of file DataMapper.hpp.
References Lightweight::SqlStatement::Connection(), Lightweight::SqlStatement::Execute(), Lightweight::SqlStatement::GetResultCursor(), Lightweight::SqlStatement::Prepare(), Lightweight::SqlConnection::Query(), Lightweight::SqlQueryBuilder::Select(), and Lightweight::SqlConnection::ServerType().
| std::vector< std::tuple< Records... > > Lightweight::DataMapper::QueryToTuple | ( | SqlSelectQueryBuilder::ComposedQuery const & | selectQuery | ) |
Queries multiple records and returns them as a vector of std::tuple of the given record types. This can be used to query multiple record types in a single query.
example:
Definition at line 1039 of file DataMapper.hpp.
References ConfigureRelationAutoLoading(), Lightweight::SqlStatement::Connection(), Lightweight::SqlStatement::Execute(), Lightweight::SqlStatement::GetResultCursor(), Lightweight::SqlStatement::Prepare(), and Lightweight::SqlConnection::ServerType().
| std::vector< Record > Lightweight::DataMapper::Query | ( | std::string_view | sqlQueryString, |
| InputParameters &&... | inputParameters | ||
| ) |
Queries multiple records from the database, based on the given query.
| sqlQueryString | The SQL query string to execute. |
| inputParameters | The input parameters for the query to be bound before executing. |
example:
Definition at line 986 of file DataMapper.hpp.
References ConfigureRelationAutoLoading(), Lightweight::SqlStatement::Connection(), Lightweight::SqlStatement::Execute(), Lightweight::SqlStatement::FetchRow(), Lightweight::SqlStatement::GetColumn(), Lightweight::SqlStatement::GetResultCursor(), Lightweight::SqlStatement::NumColumnsAffected(), Lightweight::SqlStatement::Prepare(), and Lightweight::SqlConnection::ServerType().
| std::vector< Record > Lightweight::DataMapper::Query | ( | SqlSelectQueryBuilder::ComposedQuery const & | selectQuery, |
| InputParameters &&... | inputParameters | ||
| ) |
Queries records from the database, based on the given query and can be used to retrieve only part of the record by specifying the ElementMask.
example:
Definition at line 1147 of file DataMapper.hpp.
References ConfigureRelationAutoLoading(), Lightweight::SqlStatement::Connection(), Lightweight::SqlStatement::Execute(), Lightweight::SqlStatement::GetResultCursor(), Lightweight::SqlStatement::Prepare(), and Lightweight::SqlConnection::ServerType().
|
inline |
Queries records of different types from the database, based on the given query. User can constructed query that selects columns from the multiple tables this function is uset to get result of the
example:
Definition at line 369 of file DataMapper.hpp.
| void Lightweight::DataMapper::Update | ( | Record & | record | ) |
Updates the record in the database.
Definition at line 797 of file DataMapper.hpp.
References ClearModifiedState(), Lightweight::SqlStatement::Execute(), Lightweight::SqlStatement::Prepare(), Lightweight::SqlConnection::Query(), and Lightweight::SqlQueryBuilder::Update().
| std::size_t Lightweight::DataMapper::Delete | ( | Record const & | record | ) |
Deletes the record from the database.
Definition at line 869 of file DataMapper.hpp.
References Lightweight::SqlQueryBuilder::Delete(), Lightweight::SqlStatement::Execute(), Lightweight::SqlStatement::NumRowsAffected(), Lightweight::SqlStatement::Prepare(), and Lightweight::SqlConnection::Query().
|
inline |
Constructs an SQL query builder for the given table name.
Definition at line 393 of file DataMapper.hpp.
References Lightweight::SqlConnection::Query().
|
noexcept |
Checks if the record has any modified fields.
Definition at line 769 of file DataMapper.hpp.
|
noexcept |
Clears the modified state of the record.
Definition at line 1186 of file DataMapper.hpp.
| void Lightweight::DataMapper::LoadRelations | ( | Record & | record | ) |
Loads all direct relations to this record.
Definition at line 1432 of file DataMapper.hpp.
| void Lightweight::DataMapper::ConfigureRelationAutoLoading | ( | Record & | record | ) |
Configures the auto loading of relations for the given record.
This means, that no explicit loading of relations is required. The relations are automatically loaded when accessed.
Definition at line 1578 of file DataMapper.hpp.
References Lightweight::SqlStatement::Prepare(), Lightweight::HasOneThrough< OtherTable, ThroughTable >::SetAutoLoader(), Lightweight::HasMany< OtherRecord >::SetAutoLoader(), and Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT >::SetAutoLoader().
Referenced by Create(), Query(), Query(), QuerySingle(), and QueryToTuple().
|
inline |
Definition at line 977 of file DataMapper.hpp.
| std::vector< std::tuple< FirstRecord, SecondRecord > > Lightweight::DataMapper::Query | ( | SqlSelectQueryBuilder::ComposedQuery const & | selectQuery, |
| InputParameters &&... | inputParameters | ||
| ) |
Definition at line 1116 of file DataMapper.hpp.
|
inline |
Definition at line 1483 of file DataMapper.hpp.
|
inline |
Definition at line 1516 of file DataMapper.hpp.
|
inline |
Definition at line 1531 of file DataMapper.hpp.