SQT
A C++ ORM framework for SQLite
Loading...
Searching...
No Matches

◆ MakeUpdater() [1/2]

template<EntityValueType ENTITY>
template<ConflictAction CONFLICT_ACTION = ConflictAction::Abort>
constexpr auto sqt::DataContext< ENTITY >::MakeUpdater ( )
staticconstexprnoexcept

Creates an updater for updating all columns in the database table.

Template Parameters
CONFLICT_ACTIONThe conflict action to be used when an unique constraint violation occurs. The default action is sqt::ConflictAction::Abort.
Returns
The new updater instance.

The returned updater corresponds to an UPDATE SQL statement without any conditions, so it will update all rows in the database table. To control which rows to update, use the Where() method of the updater to create a new updater with conditions.

A placeholder for the entity is implicitly added to the returned updater. To execute the updater, an entity instance must be bound.

The following code demonstrates how to use the updater:

// The entity type, assuming its table type has been defined and registered, and its
// columns' names are defined as the same as the entity's field names.
struct MyEntity {
int id{};
std::string name;
};
// Create an updater with condition.
constexpr auto updater = sqt::DataContext<MyEntity>::MakeUpdater().Where(
);
// Create a data context, assuming the shared_db is an opened database instance.
sqt::DataContext<MyEntity> data_context{ shared_db };
// Prepare the updater to create a corresponding executor.
auto executor = data_context.Prepare(updater);
// Bind an entity to the executor.
MyEntity entity{ 1, "The First" };
executor.BeginBindings().Bind(entity);
// Execute the statement.
executor.Execute();
static constexpr auto MakeUpdater() noexcept
Creates an updater for updating all columns in the database table.
Definition data_context.h:349
Executor< QUERIER > Prepare(const QUERIER &querier)
Creates an executor for executing the specified querier.
Definition data_context.h:639
Provides a set of operations for performing CRUD (Create, Read, Update, Delete) operations on a datab...
Definition data_context.h:96
constexpr const auto & Table
A helper template to retrieve the singleton instance of the table type associated with an entity type...
Definition table_mapping.h:64

The returned instance updates all columns of the entity type. The following alternatives provides more control over the updated columns:

  • MakeNoPrimaryKeyUpdater() for updating non-primary key columns. It is typically used when updating the entity by its primary key value, reducing the unnecessary overhead of updating the primary key columns.
  • MakeUpdater(ASSIGNMENTS&&...) for updating specific columns.
See also
sqt::DataContext<>::MakeNoPrimaryKeyUpdater()
sqt::DataContext<>::MakeUpdater(ASSIGNMENTS&&... assignments)