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

SQL prepared statement. More...

#include <preparedStatement.hpp>

Inheritance diagram for anch::sql::PreparedStatement:
Collaboration diagram for anch::sql::PreparedStatement:

Public Member Functions

 PreparedStatement () noexcept
 PreparedStatement (const PreparedStatement &)=delete
virtual ~PreparedStatement () noexcept
ResultSet * executeQuery ()
uint64_t executeUpdate ()
template<anch::sql::Bindable T>
void set (std::string_view name, const T &value)
void setNull (std::string_view name)
void set (const anch::sql::Parameters &parameters)
template<anch::Described T>
void set (const anch::sql::FieldsOf< T > &fields)
void clearParameters () noexcept
std::size_t getParameterCount () const noexcept
const std::vector< std::string > & getParameterNames () const noexcept
const std::string & getQuery () const noexcept
std::size_t getBindingCount () const noexcept
const anch::sql::ParameterValue & getBindingValue (std::size_t idx) const

Protected Member Functions

std::string parse (const std::string &query, const anch::sql::SqlSyntax &syntax, anch::sql::ParameterStyle style)
virtual void doPrepare (const std::string &query)=0
virtual ResultSet * doExecuteQuery ()=0
virtual uint64_t doExecuteUpdate ()=0
virtual void deallocate () noexcept
void invalidate () noexcept

Protected Attributes

std::string _query
std::vector< std::string > _names
std::vector< anch::sql::ParameterValue > _values
std::vector< std::optional< anch::sql::ParameterList > > _lists
std::vector< bool > _bound
std::vector< anch::sql::NamedQuery::Binding > _bindings
std::optional< anch::sql::NamedQuery > _named
anch::sql::ParameterStyle _style
std::string _prepared
std::vector< std::size_t > _shape

Friends

class Connection
class ResultSet

Detailed Description

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

Constructor & Destructor Documentation

◆ PreparedStatement() [1/2]

anch::sql::PreparedStatement::PreparedStatement ( )
noexcept

PreparedStatement constructor

◆ PreparedStatement() [2/2]

anch::sql::PreparedStatement::PreparedStatement ( const PreparedStatement & )
delete

Prohibits PreparedStatement copy constructor

Here is the call graph for this function:

◆ ~PreparedStatement()

virtual anch::sql::PreparedStatement::~PreparedStatement ( )
virtualnoexcept

PreparedStatement destructor

Here is the call graph for this function:

Member Function Documentation

◆ clearParameters()

void anch::sql::PreparedStatement::clearParameters ( )
noexcept

Unbind every parameter

Since
0.2
Here is the call graph for this function:

◆ 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.

Here is the call graph for this function:

◆ doExecuteQuery()

virtual ResultSet * anch::sql::PreparedStatement::doExecuteQuery ( )
protectedpure virtual

Execute prepared statement for SELECT statement (driver implementation: parameters are bound)

Returns
the result set
Exceptions
SqlExceptionany error (with connection status)

Implemented in anch::sql::MySQLPreparedStatement, anch::sql::PostgreSQLPreparedStatement, and anch::sql::SQLite3PreparedStatement.

Here is the call graph for this function:

◆ doExecuteUpdate()

virtual uint64_t anch::sql::PreparedStatement::doExecuteUpdate ( )
protectedpure virtual

Execute prepared statement for update (driver implementation: parameters are bound)

Returns
the number of affected rows
Exceptions
SqlExceptionany error (with connection status)

Implemented in anch::sql::MySQLPreparedStatement, anch::sql::PostgreSQLPreparedStatement, and anch::sql::SQLite3PreparedStatement.

Here is the call graph for this function:

◆ doPrepare()

virtual void anch::sql::PreparedStatement::doPrepare ( const std::string & query)
protectedpure virtual

Prepare the query rendered for the driver (driver implementation: called by its constructor with the query given by parse , and before execution when bound lists change the rendered query: the new query replaces the previous one)

Parameters
querythe query to prepare
Exceptions
SqlExceptionany error (with connection status)

Implemented in anch::sql::MySQLPreparedStatement, anch::sql::PostgreSQLPreparedStatement, and anch::sql::SQLite3PreparedStatement.

Here is the call graph for this function:

◆ executeQuery()

ResultSet * anch::sql::PreparedStatement::executeQuery ( )

Execute prepared statement for SELECT statement

Returns
the result set (to delete)
Exceptions
SqlExceptionunbound parameter or any database error
Here is the call graph for this function:

◆ executeUpdate()

uint64_t anch::sql::PreparedStatement::executeUpdate ( )

Execute prepared statement for update database (INSERT, UPDATE, DELETE)

Returns
the number of affected rows
Exceptions
SqlExceptionunbound parameter or any database error
Here is the call graph for this function:

◆ 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
Here is the call graph for this function:

◆ getBindingValue()

const anch::sql::ParameterValue & anch::sql::PreparedStatement::getBindingValue ( std::size_t idx) const

Value of a parameter of the query prepared by the driver

Parameters
idxthe 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 )
Here is the call graph for this function:

◆ getParameterCount()

std::size_t anch::sql::PreparedStatement::getParameterCount ( ) const
noexcept

Number of parameters getter

Returns
the number of parameters names
Since
0.2
Here is the call graph for this function:

◆ 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
Here is the call graph for this function:

◆ getQuery()

const std::string & anch::sql::PreparedStatement::getQuery ( ) const
noexcept

Query getter

Returns
the query (with named parameters)
Since
0.3
Here is the call graph for this function:

◆ 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
Here is the call graph for this function:

◆ parse()

std::string anch::sql::PreparedStatement::parse ( const std::string & query,
const anch::sql::SqlSyntax & syntax,
anch::sql::ParameterStyle style )
protected

Parse query and render it for the driver (called by drivers before preparing the statement)

Parameters
querythe query (with named parameters)
syntaxthe SQL dialect lexical rules
stylethe parameters style of the driver
Returns
the query to prepare
Exceptions
SqlExceptionpositional parameter in query (non fatal)
Here is the call graph for this function:

◆ set() [1/3]

template<anch::Described T>
void anch::sql::PreparedStatement::set ( const anch::sql::FieldsOf< T > & fields)

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
Tthe described type
Parameters
fieldsthe fields of the object ( fieldsOf )
Since
0.3
Here is the call graph for this function:

◆ set() [2/3]

void anch::sql::PreparedStatement::set ( const anch::sql::Parameters & parameters)

Bind parameters (the parameters of the query which are not in parameters are unbound)

Parameters
parametersthe parameters
Exceptions
SqlExceptionunknown parameter (non fatal)
Since
0.3
Here is the call graph for this function:

◆ set() [3/3]

template<anch::sql::Bindable T>
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
Tthe value type
Parameters
namethe parameter name (without ':' )
valuethe value to bind
Exceptions
SqlExceptionunknown parameter (non fatal, with the parameters of the query)
Since
0.3
Here is the call graph for this function:

◆ setNull()

void anch::sql::PreparedStatement::setNull ( std::string_view name)

Bind SQL NULL to a parameter

Parameters
namethe parameter name (without ':' )
Exceptions
SqlExceptionunknown parameter (non fatal)
Since
0.3
Here is the call graph for this function:

Member Data Documentation

◆ _bindings

std::vector<anch::sql::NamedQuery::Binding> anch::sql::PreparedStatement::_bindings
protected

Name of each parameter of the query prepared by the driver ( $1 , ?1 or each ? )

◆ _bound

std::vector<bool> anch::sql::PreparedStatement::_bound
protected

Bound names

◆ _lists

std::vector<std::optional<anch::sql::ParameterList> > anch::sql::PreparedStatement::_lists
protected

Values of each name bound to a list

◆ _named

std::optional<anch::sql::NamedQuery> anch::sql::PreparedStatement::_named
protected

The parsed query

◆ _names

std::vector<std::string> anch::sql::PreparedStatement::_names
protected

Parameters names, in order of first use

◆ _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

anch::sql::ParameterStyle anch::sql::PreparedStatement::_style
protected

Parameters style of the driver

◆ _values

std::vector<anch::sql::ParameterValue> anch::sql::PreparedStatement::_values
protected

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: