SQL connection virtual class. More...
#include <connection.hpp>
Classes | |
| struct | CachedStatement |
Public Member Functions | |
| Connection (std::size_t statementCacheSize=DEFAULT_STATEMENT_CACHE_SIZE) | |
| Connection (const Connection &)=delete | |
| virtual | ~Connection () noexcept |
| void | startTransaction () |
| void | begin () |
| void | commit () |
| void | rollback () |
| void | release () noexcept |
| PreparedStatement & | prepareStatement (const std::string &query) |
| ResultSet * | query (const std::string &query) |
| void | queryMapRow (const std::string &sqlQuery, std::function< void(ResultSet &)> rowMapper) |
| void | queryExtract (const std::string &sqlQuery, std::function< void(ResultSet &)> resExtractor) |
| ResultSet * | query (const std::string &query, const anch::sql::Parameters ¶meters) |
| void | queryMapRow (const std::string &query, std::function< void(ResultSet &)> rowMapper, const anch::sql::Parameters ¶meters) |
| void | queryExtract (const std::string &query, std::function< void(ResultSet &)> resExtractor, const anch::sql::Parameters ¶meters) |
| uint64_t | update (const std::string &query) |
| uint64_t | update (const std::string &query, const anch::sql::Parameters ¶meters) |
| template<typename T, typename Iterable> | |
| uint64_t | batchUpdate (const std::string &query, std::function< void(anch::sql::Parameters &, const T &)> mapper, const Iterable &values) |
| template<std::ranges::input_range Iterable> requires anch::Described<std::ranges::range_value_t<Iterable>> | |
| uint64_t | batchUpdate (const std::string &query, const Iterable &values) |
| template<anch::Described T> | |
| ResultSet * | query (const std::string &query, const anch::sql::FieldsOf< T > &fields) |
| template<anch::Described T> | |
| void | queryMapRow (const std::string &query, std::function< void(ResultSet &)> rowMapper, const anch::sql::FieldsOf< T > &fields) |
| template<anch::Described T> | |
| void | queryExtract (const std::string &query, std::function< void(ResultSet &)> resExtractor, const anch::sql::FieldsOf< T > &fields) |
| template<anch::Described T> | |
| uint64_t | update (const std::string &query, const anch::sql::FieldsOf< T > &fields) |
| template<anch::sql::Row T> | |
| std::vector< T > | queryList (const std::string &query) |
| template<anch::sql::Row T> | |
| std::optional< T > | queryOne (const std::string &query) |
| template<anch::sql::Row T> | |
| void | queryEach (const std::string &query, std::function< void(T &&)> rowHandler) |
| template<anch::sql::Row T> | |
| std::vector< T > | queryList (const std::string &query, const anch::sql::Parameters ¶meters) |
| template<anch::sql::Row T> | |
| std::optional< T > | queryOne (const std::string &query, const anch::sql::Parameters ¶meters) |
| template<anch::sql::Row T> | |
| void | queryEach (const std::string &query, std::function< void(T &&)> rowHandler, const anch::sql::Parameters ¶meters) |
| template<anch::sql::Row T, anch::Described U> | |
| std::vector< T > | queryList (const std::string &query, const anch::sql::FieldsOf< U > &fields) |
| template<anch::sql::Row T, anch::Described U> | |
| std::optional< T > | queryOne (const std::string &query, const anch::sql::FieldsOf< U > &fields) |
| template<anch::sql::Row T, anch::Described U> | |
| void | queryEach (const std::string &query, std::function< void(T &&)> rowHandler, const anch::sql::FieldsOf< U > &fields) |
| std::size_t | getStatementCacheSize () const noexcept |
| void | setStatementCacheSize (std::size_t size) |
| std::size_t | getCachedStatementCount () const noexcept |
| bool | isValid () const noexcept |
Protected Member Functions | |
| virtual ResultSet * | executeQuery (const std::string &query)=0 |
| virtual uint64_t | executeUpdate (const std::string &query)=0 |
| virtual void | sendStartTransaction ()=0 |
| virtual void | sendCommit ()=0 |
| virtual void | sendRollback ()=0 |
| virtual PreparedStatement * | makePrepared (const std::string &query)=0 |
| template<typename F> | |
| auto | guard (F &&func) -> decltype(func()) |
| void | onError (const SqlException &error) noexcept |
| void | clearStatements () noexcept |
| virtual bool | checkConnection () const noexcept |
| void | setValid (bool valid) noexcept |
Protected Attributes | |
| bool | _valid |
| bool | _transaction |
| std::list< CachedStatement > | _stmts |
| std::unordered_map< std::string_view, std::list< CachedStatement >::iterator > | _stmtsIndex |
| std::size_t | _stmtsMax |
Friends | |
| class | PreparedStatement |
| class | ResultSet |
SQL connection virtual class.
Abstract layer for SQL connections management.
Errors: every SqlException raised by the connection, its prepared statements and its result sets is reported to the connection. A fatal error (SqlException::isOK is false : connection lost, out of memory, protocol error, ...) invalidates the connection: isValid returns false and connection pools destroy it instead of reusing it. Other errors (syntax, constraint, ...) keep the connection valid.
Transactions: without startTransaction , each query is committed by the database (auto-commit mode). A transaction started with startTransaction is committed only by commit : a transaction which is not ended when the connection is released (returned to its pool or destroyed) is rolled back, with or without error.
Prepared statements: prepareStatement keeps the prepared statements in a bounded cache (least recently used statement first evicted, see setStatementCacheSize ). A statement is never evicted while one of its result sets is alive: the cache can then temporarily exceed its maximum size. An evicted statement is deallocated on the server side. A statement is not executed again while one of its result sets is alive (nested queries with the same SQL text): an other statement is prepared for the same query. A statement invalidated by the database (PostgreSQL: result type changed by a schema change) is prepared again.
|
explicit |
Connection constructor
| statementCacheSize | the maximum number of cached prepared statements (at least 1 ) |
| SqlException | invalid cache size |
|
delete |
|
virtualnoexcept |
Connection destrcutor
| uint64_t anch::sql::Connection::batchUpdate | ( | const std::string & | query, |
| const Iterable & | values ) |
Execute batch prepared statement with described objects: the parameters of each object are its fields ( fieldsOf )
| query | the SQL query (with named parameters: :name ) |
| values | the described objects |
| SqlException | any error (a parameter which is not a field is not bound: non fatal) |
| uint64_t anch::sql::Connection::batchUpdate | ( | const std::string & | query, |
| std::function< void(anch::sql::Parameters &, const T &)> | mapper, | ||
| const Iterable & | values ) |
Execute batch prepared statement
| query | the SQL query (with named parameters: :name ) |
| mapper | the function which gives the parameters of a value (every parameter of the query has to be given for each value: parameters of the previous value are not reused) |
| values | the values as structure or class |
| SqlException | any error |
| void anch::sql::Connection::begin | ( | ) |
Alias for startTransaction
| SqlException | any error |
|
protectedvirtualnoexcept |
Check driver connection status (called by isValid ; default to true )
true when the driver connection is usable, false otherwiseReimplemented in anch::sql::PostgreSQLConnection.
|
protectednoexcept |
Delete every cached prepared statement without deallocating it on the server side.
Drivers which have to release statements before closing their connection call it in their destructor.
| void anch::sql::Connection::commit | ( | ) |
Commit current SQL transaction.
If there is not transaction, this method does nothing.
| SqlException | any error |
|
protectedpure virtual |
Execute SQL select query
| query | the SQL query to execute |
| SqlException | any error |
Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.
|
protectedpure virtual |
Execute SQL update query
| query | the SQL query to execute |
| SqlException | any error |
Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.
|
noexcept |
Number of cached prepared statements getter
|
noexcept |
Maximum number of cached prepared statements getter
|
protected |
Call function and report its SqlException to the connection (see onError )
| F | the function type |
| func | the function to call |
| SqlException | the function error (rethrown) |
|
noexcept |
Valid status getter: false once a fatal error has occurred, or when the driver reports a broken connection. Connection pools destroy invalid connections instead of reusing them.
|
protectedpure virtual |
Send SQL query to prepare SQL statement
| query | the SQL query |
| SqlException | any error |
Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.
|
protectednoexcept |
Report error: a fatal error invalidates the connection
| error | the error |
| PreparedStatement & anch::sql::Connection::prepareStatement | ( | const std::string & | query | ) |
Prepare SQL statement if not already done.
The statement is cached by the connection (see setStatementCacheSize ): the returned reference remains valid until the statement is evicted, that is to say at least until statementCacheSize - 1 other queries have been prepared on this connection (and as long as one of its result sets is alive).
| query | the SQL query |
| SqlException | any error |
| ResultSet * anch::sql::Connection::query | ( | const std::string & | query | ) |
Execute SQL query
| query | the SQL query to execute |
| SqlException | any error |
| ResultSet * anch::sql::Connection::query | ( | const std::string & | query, |
| const anch::sql::FieldsOf< T > & | fields ) |
Execute SQL prepared statement query with the fields of an object as parameters ( fieldsOf )
| query | the SQL query to execute (with named parameters: :name ) |
| fields | the fields of the object |
| SqlException | any error (a parameter which is not a field is not bound: non fatal) |
| ResultSet * anch::sql::Connection::query | ( | const std::string & | query, |
| const anch::sql::Parameters & | parameters ) |
Execute SQL prepared statement query
| query | the SQL query to execute (with named parameters: :name ) |
| parameters | the parameters (every parameter of the query has to be given) |
| SqlException | any error (missing or unknown parameter: non fatal) |
| void anch::sql::Connection::queryEach | ( | const std::string & | query, |
| std::function< void(T &&)> | rowHandler ) |
Execute query and treat each row of its result, read one at a time ( RowReader )
| T | the row type |
| query | the SQL query to execute |
| rowHandler | the row handler function |
| SqlException | any error (invalid value, NULL value which is not optional: non fatal) |
| void anch::sql::Connection::queryEach | ( | const std::string & | query, |
| std::function< void(T &&)> | rowHandler, | ||
| const anch::sql::FieldsOf< U > & | fields ) |
Execute query and treat each row of its result, read one at a time ( RowReader )
| T | the row type |
| U | the type of the object whose fields are parameters |
| query | the SQL query to execute (with named parameters: :name ) |
| rowHandler | the row handler function |
| fields | the fields of an object as parameters ( fieldsOf ) |
| SqlException | any error (invalid value, NULL value which is not optional: non fatal) |
| void anch::sql::Connection::queryEach | ( | const std::string & | query, |
| std::function< void(T &&)> | rowHandler, | ||
| const anch::sql::Parameters & | parameters ) |
Execute query and treat each row of its result, read one at a time ( RowReader )
| T | the row type |
| query | the SQL query to execute (with named parameters: :name ) |
| rowHandler | the row handler function |
| parameters | the parameters |
| SqlException | any error (invalid value, NULL value which is not optional: non fatal) |
| void anch::sql::Connection::queryExtract | ( | const std::string & | query, |
| std::function< void(ResultSet &)> | resExtractor, | ||
| const anch::sql::FieldsOf< T > & | fields ) |
Execute query with the fields of an object as parameters and extract result set
| query | the SQL query to execute (with named parameters: :name ) |
| resExtractor | the result set extractor function |
| fields | the fields of the object ( fieldsOf ) |
| SqlException | any error |
| void anch::sql::Connection::queryExtract | ( | const std::string & | query, |
| std::function< void(ResultSet &)> | resExtractor, | ||
| const anch::sql::Parameters & | parameters ) |
Execute query and extract result set
| query | the SQL query to execute (with named parameters: :name ) |
| resExtractor | the result set extractor function |
| parameters | the parameters (every parameter of the query has to be given) |
| SqlException | any error |
| void anch::sql::Connection::queryExtract | ( | const std::string & | sqlQuery, |
| std::function< void(ResultSet &)> | resExtractor ) |
Execute query and extract result set
| sqlQuery | the SQL query to execute |
| resExtractor | the result set extractor function |
| SqlException | any error |
| std::vector< T > anch::sql::Connection::queryList | ( | const std::string & | query | ) |
Execute query and read all the rows of its result ( RowReader : columns are associated with the fields of a described type by name, a value is read from the first column):
| T | the row type |
| query | the SQL query to execute |
| SqlException | any error (invalid value, NULL value which is not optional: non fatal) |
| std::vector< T > anch::sql::Connection::queryList | ( | const std::string & | query, |
| const anch::sql::FieldsOf< U > & | fields ) |
Execute query and read all the rows of its result ( RowReader : columns are associated with the fields of a described type by name, a value is read from the first column):
| T | the row type |
| U | the type of the object whose fields are parameters |
| query | the SQL query to execute (with named parameters: :name ) |
| fields | the fields of an object as parameters ( fieldsOf ) |
| SqlException | any error (invalid value, NULL value which is not optional: non fatal) |
| std::vector< T > anch::sql::Connection::queryList | ( | const std::string & | query, |
| const anch::sql::Parameters & | parameters ) |
Execute query and read all the rows of its result ( RowReader : columns are associated with the fields of a described type by name, a value is read from the first column):
| T | the row type |
| query | the SQL query to execute (with named parameters: :name ) |
| parameters | the parameters |
| SqlException | any error (invalid value, NULL value which is not optional: non fatal) |
| void anch::sql::Connection::queryMapRow | ( | const std::string & | query, |
| std::function< void(ResultSet &)> | rowMapper, | ||
| const anch::sql::FieldsOf< T > & | fields ) |
Execute query with the fields of an object as parameters and treat each row of result set
| query | the SQL query to execute (with named parameters: :name ) |
| rowMapper | the row mapper function |
| fields | the fields of the object ( fieldsOf ) |
| SqlException | any error |
| void anch::sql::Connection::queryMapRow | ( | const std::string & | query, |
| std::function< void(ResultSet &)> | rowMapper, | ||
| const anch::sql::Parameters & | parameters ) |
Execute query and treat each row of result set
| query | the SQL query to execute (with named parameters: :name ) |
| rowMapper | the row mapper function |
| parameters | the parameters (every parameter of the query has to be given) |
| SqlException | any error |
| void anch::sql::Connection::queryMapRow | ( | const std::string & | sqlQuery, |
| std::function< void(ResultSet &)> | rowMapper ) |
Execute query and treat each row of result set
| sqlQuery | the SQL query to execute |
| rowMapper | the row mapper function |
| SqlException | any error |
| std::optional< T > anch::sql::Connection::queryOne | ( | const std::string & | query | ) |
Execute query and read its only row ( RowReader )
| T | the row type |
| query | the SQL query to execute |
std::nullopt when there is no row| SqlException | any error (more than one row, invalid value, NULL value which is not optional: non fatal) |
| std::optional< T > anch::sql::Connection::queryOne | ( | const std::string & | query, |
| const anch::sql::FieldsOf< U > & | fields ) |
Execute query and read its only row ( RowReader )
| T | the row type |
| U | the type of the object whose fields are parameters |
| query | the SQL query to execute (with named parameters: :name ) |
| fields | the fields of an object as parameters ( fieldsOf ) |
std::nullopt when there is no row| SqlException | any error (more than one row, invalid value, NULL value which is not optional: non fatal) |
| std::optional< T > anch::sql::Connection::queryOne | ( | const std::string & | query, |
| const anch::sql::Parameters & | parameters ) |
Execute query and read its only row ( RowReader )
| T | the row type |
| query | the SQL query to execute (with named parameters: :name ) |
| parameters | the parameters |
std::nullopt when there is no row| SqlException | any error (more than one row, invalid value, NULL value which is not optional: non fatal) |
|
noexcept |
Release connection: roll back the current transaction, if any (a transaction is committed only by commit ).
It is called when the connection is returned to its pool and when it is destroyed. If the transaction can be neither committed nor rolled back, the connection is invalidated (its transaction state is unknown).
| void anch::sql::Connection::rollback | ( | ) |
Rollback current SQL transaction.
If there is not transaction, this method does nothing.
| SqlException | any error |
|
protectedpure virtual |
Send commit to database server
| SqlException | any error |
Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.
|
protectedpure virtual |
Send rollback to database server
| SqlException | any error |
Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.
|
protectedpure virtual |
Send start transaction to database server
| SqlException | any error |
Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.
| void anch::sql::Connection::setStatementCacheSize | ( | std::size_t | size | ) |
Maximum number of cached prepared statements setter: least recently used statements are evicted when the cache exceeds the new size
| size | the maximum number of cached prepared statements (at least 1 ) |
| SqlException | invalid size |
|
protectednoexcept |
Valid status setter
| valid | the status to set |
| void anch::sql::Connection::startTransaction | ( | ) |
Start SQL transaction.
If there is a transaction on this database connection, this method does nothing.
The transaction has to be ended with commit or rollback : it is rolled back by release otherwise.
| SqlException | any error |
| uint64_t anch::sql::Connection::update | ( | const std::string & | query | ) |
Execute update (INSERT, UPDATE or DELETE) SQL query.
| query | the SQL query |
| SqlException | any error |
| uint64_t anch::sql::Connection::update | ( | const std::string & | query, |
| const anch::sql::FieldsOf< T > & | fields ) |
Execute update (INSERT, UPDATE or DELETE) SQL prepared statement with the fields of an object as parameters
| query | the SQL query (with named parameters: :name ) |
| fields | the fields of the object ( fieldsOf ) |
| SqlException | any error |
| uint64_t anch::sql::Connection::update | ( | const std::string & | query, |
| const anch::sql::Parameters & | parameters ) |
Execute update (INSERT, UPDATE or DELETE) SQL prepared statement.
| query | the SQL query (with named parameters: :name ) |
| parameters | the parameters (every parameter of the query has to be given) |
| SqlException | any error |
|
protected |
Prepared statements (the most recently used first)
|
protected |
Prepared statements index (keys are views of CachedStatement::query )
|
protected |
Maximum number of cached prepared statements
|
protected |
Is in SQL transaction
|
protected |
Is SQL connection valid