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

◆ MakeInserter() [1/2]

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

Creates an inserter for inserting an entire entity into the database table.

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

The returned inserter corresponds to an INSERT SQL statement that inserts all columns of the entity into the database table. The following alternatives provide more control over the inserted columns:

  • MakeAutoIncInserter() for inserting non-primary key columns while auto-generating the primary key.
  • MakeInserter(ASSIGNMENTS&&...) for inserting specific columns.

The MakeReplacer() method is a shorthand for MakeInserter<sqt::ConflictAction::Replace>().

A placeholder for the entity is implicitly added to the returned inserter. To execute the inserter, an entity instance must be bound. The following code demonstrates how to use the inserter:

// The entity type, assuming its table type has been defined and registered.
struct MyEntity {
int id{};
std::string name;
};
// Create the inserter.
constexpr auto inserter = sqt::DataContext<MyEntity>::MakeInserter();
// Create a data context, assuming the shared_db is an opened database instance.
sqt::DataContext<MyEntity> data_context{ shared_db };
// Prepare the inserter to create a corresponding executor.
auto executor = data_context.Prepare(inserter);
// Bind an entity to the executor.
MyEntity entity{ 1, "The First" };
executor.BeginBindings().Bind(entity);
// Execute the statement.
executor.Execute();
Executor< QUERIER > Prepare(const QUERIER &querier)
Creates an executor for executing the specified querier.
Definition data_context.h:639
static constexpr auto MakeInserter() noexcept
Creates an inserter for inserting an entire entity into the database table.
Definition data_context.h:156
Provides a set of operations for performing CRUD (Create, Read, Update, Delete) operations on a datab...
Definition data_context.h:96

For an easier-to-use method, use the Insert() method from the easy style interface.

See also
sqt::DataContext<>::Insert()
sqt::DataContext<>::MakeAutoIncInserter()
sqt::DataContext<>::MakeInserter(ASSIGNMENTS&&... assignments)
sqt::DataContext<>::MakeReplacer()