userver: uPg: Running queries
Loading...
Searching...
No Matches
uPg: Running queries

All queries are executed through a transaction object, event when being executed through singe-query interface, so here only executing queries with transaction will be covered. Single-query interface is basically the same except for additional options.

uPg provides means to execute text queries only. There is no query generation, but can be used by other tools to execute SQL queries.

Warning
A query must contain a single query, multiple statements delimited by ';' are not supported.

All queries are parsed and prepared during the first invocation and are executed as prepared statements afterwards.

Any query execution can throw an exception. Please see uPg: Postgres errors for more information on possible errors.

Queries without parameters

Executing a query without any parameters is rather straightforward.

auto trx = cluster->Begin(/* transaction options */);
auto res = trx.Execute("select foo, bar from foobar");
trx.Commit();

The cluster also provides interface for single queries

auto res = cluster->Execute(/* transaction options */, /* the statement */);
Queries with parameters

uPg supports SQL dollar notation for parameter placeholders. The statement is prepared at first execution and then only arguments for a query is sent to the server.

A parameter can be of any type that is supported by the driver. See uPg: Supported data types for more information.

auto trx = cluster->Begin(/* transaction options */);
auto res = trx.Execute(
"select foo, bar from foobar where foo > $1 and bar = $2", 42, "baz");
trx.Commit();
See also
Transaction
ResultSet