AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::sql::NamedQuery Class Reference

SQL query with named parameters ( :name ). More...

#include <placeholders.hpp>

Collaboration diagram for anch::sql::NamedQuery:

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)

Detailed Description

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.

Author
Vincent Lachenal
Since
0.3

Constructor & Destructor Documentation

◆ NamedQuery()

anch::sql::NamedQuery::NamedQuery ( std::string_view query,
const anch::sql::SqlSyntax & syntax )

NamedQuery constructor: parse query

Parameters
querythe SQL query
syntaxthe SQL dialect lexical rules
Exceptions
SqlExceptionpositional parameter in query (non fatal)
Here is the call graph for this function:

Member Function Documentation

◆ names()

const std::vector< std::string > & anch::sql::NamedQuery::names ( ) const
noexcept

Names getter

Returns
the parameters names, in order of first use
Here is the call graph for this function:

◆ query()

const std::string & anch::sql::NamedQuery::query ( ) const
noexcept

Query getter

Returns
the query

◆ rename()

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)

Parameters
namesthe new name of each name (see names )
Returns
the query with the new names
Exceptions
SqlExceptionnames does not have a name for each name (non fatal)
Here is the call graph for this function:

◆ render()

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

Parameters
stylethe parameters style
sizesthe list size bound to each name ( SCALAR for a value which is not a list), empty when no list is bound
Returns
the rendered query and its bindings
Exceptions
SqlExceptiona 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)
Here is the call graph for this function:

◆ slot()

std::optional< std::size_t > anch::sql::NamedQuery::slot ( std::string_view name) const
noexcept

Find name index

Parameters
namethe parameter name
Returns
the name index, std::nullopt when the query has no such parameter
Here is the call graph for this function:

Member Data Documentation

◆ SCALAR

std::size_t anch::sql::NamedQuery::SCALAR = static_cast<std::size_t>(-1)
staticconstexpr

Element of a value which is not a list


The documentation for this class was generated from the following file: