ulib/3party/sqlpp11/docs/Prepared-Statements.md
tqcq 99c258107b
All checks were successful
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m24s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m30s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m43s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 1m47s
linux-x64-gcc / linux-gcc (push) Successful in 2m11s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 3m19s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 3m39s
feat/support_orm (#2)
Co-authored-by: tqcq <99722391+tqcq@users.noreply.github.com>
Reviewed-on: #2
2024-01-06 13:56:45 +00:00

1.6 KiB

Introduction

Executing a statement in a database is typically done in two phases: First, the statement is prepared (parsed, compiled, optimized). Then, it is run against the database. Since statements often differ only in parameters, not in structure, many databases offer to store prepared statements. These prepared statements can then be executed repeatedly, typically with some parameters.

sqlpp11 supports prepared statements.

Parameters

Currently there are two overloads to specify a parameter:

parameter(const ValueType&, const AliasProvider&);

parameter(const NamedExpression&)

Value types are sqlpp::bigint, sqlpp::text, etc., Alias providers can be generated by using the SQLPP_ALIAS_PROVIDER macro, and named expressions are combinations of the former, e.g. columns of a table.

For instance, you could use:

SQLPP_ALIAS_PROVIDER(cheese);
parameter(sqlpp::bigint(), cheese);

parameter(tab.id);

Prepare and execute statements

insert, update, remove and select statements can be prepared by calling the prepare() method of a database connection object.

auto prepared_statement = db.prepare(some_statement);

You can now set the parameters and execute the prepared statement multiple times, e.g.

auto prepared_insert = db.prepare(
    insert_into(tab).set(
                tab.alpha = parameter(tab.alpha), 
                tab.beta = parameter(sqlpp::text(), cheese)
    ));
for (const auto& input : input_values)
{
   prepared_insert.params.alpha = input.first;
   prepared_insert.params.cheese = input.second;
   db(prepared_insert);
}

Note: need nicer examples...