SQL prepared statement.
More...
#include <preparedStatement.hpp>
|
|
class | Connection |
|
class | ResultSet |
SQL prepared statement.
Prepared statements are created and cached by Connection::prepareStatement . Parameters are named ( :name , see NamedQuery ): a name used several times in the query is bound once. Every parameter has to be bound before execution (values are kept between executions): the missing ones are reported together. Positional parameters ( ? ) are refused.
Errors are reported to the connection: a fatal error invalidates it (see Connection::isValid ).
- Author
- Vincent Lachenal
- Since
- 0.1
◆ PreparedStatement() [1/2]
| anch::sql::PreparedStatement::PreparedStatement |
( |
| ) |
|
|
noexcept |
◆ PreparedStatement() [2/2]
| anch::sql::PreparedStatement::PreparedStatement |
( |
const PreparedStatement & | | ) |
|
|
delete |
◆ ~PreparedStatement()
| virtual anch::sql::PreparedStatement::~PreparedStatement |
( |
| ) |
|
|
virtualnoexcept |
◆ clearParameters()
| void anch::sql::PreparedStatement::clearParameters |
( |
| ) |
|
|
noexcept |
Unbind every parameter
- Since
- 0.2
◆ deallocate()
| virtual void anch::sql::PreparedStatement::deallocate |
( |
| ) |
|
|
protectedvirtualnoexcept |
Deallocate statement on the server side (called when the statement is evicted from the connection cache, while the connection is open ; default: nothing to do).
Drivers which deallocate statements in their destructor (MySQL, SQLite3) do not have to implement it.
- Since
- 0.2
Reimplemented in anch::sql::PostgreSQLPreparedStatement.
◆ doExecuteQuery()
| virtual ResultSet * anch::sql::PreparedStatement::doExecuteQuery |
( |
| ) |
|
|
protectedpure virtual |
◆ doExecuteUpdate()
| virtual uint64_t anch::sql::PreparedStatement::doExecuteUpdate |
( |
| ) |
|
|
protectedpure virtual |
◆ doPrepare()
| virtual void anch::sql::PreparedStatement::doPrepare |
( |
const std::string & | query | ) |
|
|
protectedpure virtual |
◆ executeQuery()
| ResultSet * anch::sql::PreparedStatement::executeQuery |
( |
| ) |
|
Execute prepared statement for SELECT statement
- Returns
- the result set (to delete)
- Exceptions
-
◆ executeUpdate()
| uint64_t anch::sql::PreparedStatement::executeUpdate |
( |
| ) |
|
Execute prepared statement for update database (INSERT, UPDATE, DELETE)
- Returns
- the number of affected rows
- Exceptions
-
◆ getBindingCount()
| std::size_t anch::sql::PreparedStatement::getBindingCount |
( |
| ) |
const |
|
noexcept |
Number of parameters of the query prepared by the driver ( $1 , ?1 or each ? )
- Returns
- the number of parameters
◆ getBindingValue()
Value of a parameter of the query prepared by the driver
- Parameters
-
| idx | the parameter index (from 0 : $1 , ?1 or the first ? ; a name used several times by MySQL gives the same value) |
- Returns
- the value (
std::nullopt for NULL )
◆ getParameterCount()
| std::size_t anch::sql::PreparedStatement::getParameterCount |
( |
| ) |
const |
|
noexcept |
Number of parameters getter
- Returns
- the number of parameters names
- Since
- 0.2
◆ getParameterNames()
| const std::vector< std::string > & anch::sql::PreparedStatement::getParameterNames |
( |
| ) |
const |
|
noexcept |
Parameters names getter
- Returns
- the parameters names, in order of first use
- Since
- 0.3
◆ getQuery()
| const std::string & anch::sql::PreparedStatement::getQuery |
( |
| ) |
const |
|
noexcept |
Query getter
- Returns
- the query (with named parameters)
- Since
- 0.3
◆ invalidate()
| void anch::sql::PreparedStatement::invalidate |
( |
| ) |
|
|
protectednoexcept |
Invalidate statement: the database can not execute it anymore (PostgreSQL: result type changed by a schema change, statement deallocated by DISCARD ALL , ...). Connection::prepareStatement prepares it again.
- Since
- 0.3
◆ parse()
Parse query and render it for the driver (called by drivers before preparing the statement)
- Parameters
-
| query | the query (with named parameters) |
| syntax | the SQL dialect lexical rules |
| style | the parameters style of the driver |
- Returns
- the query to prepare
- Exceptions
-
◆ set() [1/3]
Bind the fields of an object to the parameters of the same names (the other fields are ignored, the parameters which are not fields are unbound)
- Template Parameters
-
- Parameters
-
| fields | the fields of the object ( fieldsOf ) |
- Since
- 0.3
◆ set() [2/3]
Bind parameters (the parameters of the query which are not in parameters are unbound)
- Parameters
-
- Exceptions
-
- Since
- 0.3
◆ set() [3/3]
| void anch::sql::PreparedStatement::set |
( |
std::string_view | name, |
|
|
const T & | value ) |
Bind value to a parameter ( toParameter : integers, floating point numbers, strings, dates (anch::date::Date , anch::date::DateTime , anch::date::Time ), std::optional of them ; std::nullopt and NULL character arrays bind SQL NULL ), or a list of values ( BindableList : PostgreSQL array, expanded IN (:name) list for other databases ; the query is prepared again when the list sizes change its text)
- Template Parameters
-
- Parameters
-
| name | the parameter name (without ':' ) |
| value | the value to bind |
- Exceptions
-
| SqlException | unknown parameter (non fatal, with the parameters of the query) |
- Since
- 0.3
◆ setNull()
| void anch::sql::PreparedStatement::setNull |
( |
std::string_view | name | ) |
|
Bind SQL NULL to a parameter
- Parameters
-
| name | the parameter name (without ':' ) |
- Exceptions
-
- Since
- 0.3
◆ _bindings
Name of each parameter of the query prepared by the driver ( $1 , ?1 or each ? )
◆ _bound
| std::vector<bool> anch::sql::PreparedStatement::_bound |
|
protected |
◆ _lists
Values of each name bound to a list
◆ _named
◆ _names
| std::vector<std::string> anch::sql::PreparedStatement::_names |
|
protected |
◆ _prepared
| std::string anch::sql::PreparedStatement::_prepared |
|
protected |
The query prepared by the driver
◆ _query
| std::string anch::sql::PreparedStatement::_query |
|
protected |
The query (with named parameters)
◆ _shape
| std::vector<std::size_t> anch::sql::PreparedStatement::_shape |
|
protected |
List size of each name of the prepared query ( NamedQuery::SCALAR for a value which is not a list)
◆ _style
◆ _values
Value of each name ( std::nullopt for NULL ; PostgreSQL array literal of a list)
The documentation for this class was generated from the following file: