ulib/3party/sqlpp11/docs/Prepared-Statements.md
tqcq 9ec8eb952e
All checks were successful
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m11s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m22s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m27s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m29s
linux-x64-gcc / linux-gcc (push) Successful in 1m55s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 3m17s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (pull_request) Successful in 1m9s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (pull_request) Successful in 1m14s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (pull_request) Successful in 1m20s
linux-x64-gcc / linux-gcc (pull_request) Successful in 1m24s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 4m26s
linux-mips64-gcc / linux-gcc-mips64el (pull_request) Successful in 1m43s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (pull_request) Successful in 1m55s
linux-hisiv500-gcc / linux-gcc-hisiv500 (pull_request) Successful in 4m8s
fix use mirror repository of date
2024-01-06 21:51:10 +08: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...