#include <sqt/orm/data_context.h>
Provides a set of operations for performing CRUD (Create, Read, Update, Delete) operations on a database table corresponding to a specified entity type.
| ENTITY | The entity type that can be mapped to a database table. A table type for the entity must be defined using the SQT_TABLE_BEGIN macro, and this table type must be registered using the SQT_REGISTER macro. |
One data context instance corresponds to a single table in a database. Multiple data context instances can share the same database instance. The following code demonstrates how to create a data context instance:
The database table will be automatically initialized when the first CRUD operation is executed. Alternatively, the table can be initialized explicitly using the InitializeTable() method. The table will be created if it does not exist, or altered only if:
sqt::DataContext<> provides two styles of interfaces for interacting with the database table:
Easy style
Methods like Insert(), Update(), Delete(), and Select() provide simple, direct interfaces for performing the corresponding operations. These methods are easy to use but not flexible enough for more complex scenarios.
For example, Select() retrieves all columns of the table based on the primary key value, without the option to select a subset of columns or apply custom conditions.
Complex style
Complex interfaces are provided via static methods prefixed with Make, which create queriers for the corresponding operations. Querier objects allow you to build SQL statements and bind parameters, providing greater flexibility for complex queries.
For example, the MakeSelecter() method creates a querier that can be used to select specific columns and add conditions using the Where() method. Queriers also support parameter binding, which improves performance by allowing the same query to be reused with different parameters.
To execute a querier, pass it to the Prepare() method, which returns an executor. The executor can be used to execute the SQL statement and retrieve the results.
Static Public Member Functions | |
| template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort> | |
| static constexpr auto | MakeInserter () noexcept |
| template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort, AssignmentType... ASSIGNMENTS> | |
| static constexpr auto | MakeInserter (ASSIGNMENTS &&... assignments) noexcept |
| static constexpr auto | MakeReplacer () noexcept |
| template<AssignmentType... ASSIGNMENTS> | |
| static constexpr auto | MakeReplacer (ASSIGNMENTS &&... assignments) noexcept |
| template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort> requires AutoIncEntityValueType<ENTITY> | |
| static constexpr auto | MakeAutoIncInserter () noexcept |
| static constexpr auto | MakeAutoIncReplacer () noexcept |
| template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort> | |
| static constexpr auto | MakeUpdater () noexcept |
| template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort> requires PrimaryKeyEntityValueType<ENTITY> | |
| static constexpr auto | MakeNoPrimaryKeyUpdater () noexcept |
| template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort, AssignmentType... ASSIGNMENTS> | |
| static constexpr auto | MakeUpdater (ASSIGNMENTS &&... assignments) noexcept |
| static constexpr auto | MakeDeleter () noexcept |
| static constexpr auto | MakeSelecter () noexcept |
| template<ColumnType... COLUMNS> | |
| static constexpr auto | MakeSelecter (const COLUMNS &... columns) noexcept |
Public Member Functions | |
| DataContext (std::shared_ptr< sqt::Database > database) noexcept | |
| DataContext (const DataContext &)=delete | |
| DataContext & | operator= (const DataContext &)=delete |
| template<QuerierType QUERIER> | |
| Executor< QUERIER > | Prepare (const QUERIER &querier) |
| std::int64_t | Insert (const ENTITY &entity) |
| std::int64_t | AutoIncInsert (const ENTITY &entity) |
| std::int64_t | Replace (const ENTITY &entity) |
| std::int64_t | AutoIncReplace (const ENTITY &entity) |
| bool | Update (const ENTITY &entity) |
| std::size_t | DeleteAll () |
| template<typename E = ENTITY> requires PrimaryKeyEntityValueType<E> | |
| bool | Delete (const typename TableType< E >::PrimaryKeyType::ValueType &primary_key) |
| std::vector< ENTITY > | SelectAll () |
| template<typename E = ENTITY> requires PrimaryKeyEntityValueType<E> | |
| std::optional< E > | Select (const typename TableType< E >::PrimaryKeyType::ValueType &primary_key) |
| void | InitializeTable () |
| const std::shared_ptr< sqt::Database > & | Database () const noexcept |