SQL query with named parameters ( :name ).
More...
#include <placeholders.hpp>
Classes | |
| struct | Binding |
| Value bound to a parameter of the rendered query. More... | |
| struct | Rendered |
| Query rendered for a driver. More... | |
Public Member Functions | |
| NamedQuery (std::string_view query, const anch::sql::SqlSyntax &syntax) | |
| const std::string & | query () const noexcept |
| const std::vector< std::string > & | names () const noexcept |
| std::optional< std::size_t > | slot (std::string_view name) const noexcept |
| Rendered | render (anch::sql::ParameterStyle style, std::span< const std::size_t > sizes={}) const |
| std::string | rename (std::span< const std::string > names) const |
Static Public Attributes | |
| static constexpr std::size_t | SCALAR = static_cast<std::size_t>(-1) |
SQL query with named parameters ( :name ).
The query is parsed once (SqlSyntax : parameters in string literals, quoted identifiers and comments are ignored) and rendered in the parameters style of a driver ( render ). A parameter is ':' followed by a name ( [A-Za-z_][A-Za-z0-9_]* ), which is not preceded by an identifier character or by ':' ( x::int is a cast, := an assignment) and which is not an array slice bound (PostgreSQL arr[1:n] ; a parameter in brackets is written in parentheses: arr[(:i)] ). A name can be used several times: its value is bound once.
A parameter which is the only item of an IN list ( IN (:ids) , NOT IN (:ids) ) can be bound to a list of values: PostgreSQL binds it as an array ( = ANY($1) , <> ALL($1) ), other databases expand the list ( IN (?, ?, ?) ; an empty list is an empty subquery: IN is false and NOT IN true, as ANY and ALL of an empty array).
Positional parameters ( ? , PostgreSQL $1 , SQLite ?1 , @name and $name ) are refused.
| anch::sql::NamedQuery::NamedQuery | ( | std::string_view | query, |
| const anch::sql::SqlSyntax & | syntax ) |
NamedQuery constructor: parse query
| query | the SQL query |
| syntax | the SQL dialect lexical rules |
| SqlException | positional parameter in query (non fatal) |
|
noexcept |
Names getter
|
noexcept |
Query getter
| std::string anch::sql::NamedQuery::rename | ( | std::span< const std::string > | names | ) | const |
Render query with other parameters names (queries built from parts whose names collide)
| names | the new name of each name (see names ) |
| SqlException | names does not have a name for each name (non fatal) |
| Rendered anch::sql::NamedQuery::render | ( | anch::sql::ParameterStyle | style, |
| std::span< const std::size_t > | sizes = {} ) const |
Render query in the parameters style of a driver
| style | the parameters style |
| sizes | the list size bound to each name ( SCALAR for a value which is not a list), empty when no list is bound |
| SqlException | a list is bound to a parameter which is not the only item of an IN list (it can be used anywhere as a PostgreSQL array), or sizes does not have a size for each name (non fatal) |
|
noexcept |
Find name index
| name | the parameter name |
std::nullopt when the query has no such parameter
|
staticconstexpr |
Element of a value which is not a list