SQT
A C++ ORM framework for SQLite
Loading...
Searching...
No Matches
sqt::DataContext< ENTITY > Class Template Reference

#include <sqt/orm/data_context.h>

Description

template<EntityValueType ENTITY>
class sqt::DataContext< ENTITY >

Provides a set of operations for performing CRUD (Create, Read, Update, Delete) operations on a database table corresponding to a specified entity type.

Template Parameters
ENTITYThe 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 entity type, assuming its table type has been defined and registered.
struct MyEntity { };
// Open the database.
auto db = sqt::Database::Open("MyDatabase.db");
// Make the database shared.
auto shared_db = std::make_shared<sqt::Database>(std::move(db));
// Create the data context with the shared database.
sqt::DataContext<MyEntity> data_context{ shared_db };
Provides a set of operations for performing CRUD (Create, Read, Update, Delete) operations on a datab...
Definition data_context.h:96
static Database Open(const std::filesystem::path &path)
Opens a SQLite database at the specified file path.
Definition database.cpp:7

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:

  • new columns are added to the table;
  • new indexes are added to the table.
Warning
Only column and index additions are supported. Any other structural changes to the table (e.g., modifying or removing columns) are unsupported and will cause undefined behavior.

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.

See also
SQT_REGISTER
SQT_TABLE_BEGIN

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
DataContextoperator= (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