AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
anch::sql::Connection Class Referenceabstract

SQL connection virtual class. More...

#include <connection.hpp>

+ Inheritance diagram for anch::sql::Connection:
+ Collaboration diagram for anch::sql::Connection:

Public Member Functions

 Connection ()
 
 Connection (const Connection &)=delete
 
virtual ~Connection () noexcept
 
void startTransaction ()
 
void begin ()
 
void commit ()
 
void rollback ()
 
void release () noexcept
 
PreparedStatementprepareStatement (const std::string &query)
 
ResultSetquery (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)
 
template<typename T, typename... Q>
ResultSetquery (const std::string &query, const T &value, const Q &... values)
 
template<typename T, typename... Q>
void queryMapRow (const std::string &query, std::function< void(ResultSet &)> rowMapper, const T &value, const Q &... values)
 
template<typename T, typename... Q>
void queryExtract (const std::string &query, std::function< void(ResultSet &)> resExtractor, const T &value, const Q &... values)
 
uint64_t update (const std::string &query)
 
template<typename T, typename... Q>
uint64_t update (const std::string &query, const T &value, const Q &... values)
 
template<typename T, typename Iterable>
uint64_t batchUpdate (const std::string &query, std::function< void(PreparedStatement &, const T &)> mapper, const Iterable &values)
 
bool isValid () const noexcept
 

Protected Member Functions

virtual ResultSetexecuteQuery (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 PreparedStatementmakePrepared (const std::string &query)=0
 
void setValid (bool valid) noexcept
 

Protected Attributes

bool _valid
 
bool _transaction
 
std::map< std::string, PreparedStatement * > _stmts
 
bool _errors
 

Detailed Description

SQL connection virtual class.

Abstract layer for SQL connections management.

Author
Vincent Lachenal
Since
0.1

Constructor & Destructor Documentation

◆ Connection() [1/2]

anch::sql::Connection::Connection ( )

Connection default constructor

Exceptions
SqlExceptionany error

◆ Connection() [2/2]

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

Prohibit Connection copy constructor

+ Here is the call graph for this function:

◆ ~Connection()

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

Connection destrcutor

Member Function Documentation

◆ batchUpdate()

template<typename T, typename Iterable>
uint64_t anch::sql::Connection::batchUpdate ( const std::string & query,
std::function< void(PreparedStatement &, const T &)> mapper,
const Iterable & values )

Execute batch prepared statement

Parameters
querythe SQL query
mapperthe single value prepared statement mapper
valuesthe values as structure or class
Returns
the number of affected rows
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ begin()

void anch::sql::Connection::begin ( )

Alias for startTransaction

Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ commit()

void anch::sql::Connection::commit ( )

Commit current SQL transaction.
If there is not transaction, this method does nothing.

Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ executeQuery()

virtual ResultSet * anch::sql::Connection::executeQuery ( const std::string & query)
protectedpure virtual

Execute SQL select query

Parameters
querythe SQL query to execute
Returns
the result set which has to be deleted after use
Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.

+ Here is the call graph for this function:

◆ executeUpdate()

virtual uint64_t anch::sql::Connection::executeUpdate ( const std::string & query)
protectedpure virtual

Execute SQL update query

Parameters
querythe SQL query to execute
Returns
the result set which has to be deleted after use
Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.

+ Here is the call graph for this function:

◆ isValid()

bool anch::sql::Connection::isValid ( ) const
noexcept

Valid status getter

Returns
the status
+ Here is the call graph for this function:

◆ makePrepared()

virtual PreparedStatement * anch::sql::Connection::makePrepared ( const std::string & query)
protectedpure virtual

Send SQL query to prepare SQL statement

Parameters
querythe SQL query
Returns
the prepared statement
Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.

+ Here is the call graph for this function:

◆ prepareStatement()

PreparedStatement & anch::sql::Connection::prepareStatement ( const std::string & query)

Prepare SQL statement if not already done.

Parameters
querythe SQL query
Returns
the prepared statement
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ query() [1/2]

ResultSet * anch::sql::Connection::query ( const std::string & query)

Execute SQL query

Parameters
querythe SQL query to execute
Returns
the result set which has to be deleted after use
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ query() [2/2]

template<typename T, typename... Q>
ResultSet * anch::sql::Connection::query ( const std::string & query,
const T & value,
const Q &... values )

Execute SQL prepared statement query

Parameters
querythe SQL query to execute
valuethe first value to bind
valuesthe other values
Returns
the result set which has to be deleted after use
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ queryExtract() [1/2]

template<typename T, typename... Q>
void anch::sql::Connection::queryExtract ( const std::string & query,
std::function< void(ResultSet &)> resExtractor,
const T & value,
const Q &... values )

Execute query and extract result set

Parameters
querythe SQL query to execute
resExtractorthe result set extractor function
valuethe first value to bind
valuesthe other values
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ queryExtract() [2/2]

void anch::sql::Connection::queryExtract ( const std::string & sqlQuery,
std::function< void(ResultSet &)> resExtractor )

Execute query and extract result set

Parameters
sqlQuerythe SQL query to execute
resExtractorthe result set extractor function
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ queryMapRow() [1/2]

template<typename T, typename... Q>
void anch::sql::Connection::queryMapRow ( const std::string & query,
std::function< void(ResultSet &)> rowMapper,
const T & value,
const Q &... values )

Execute query and treat each row of result set

Parameters
querythe SQL query to execute
rowMapperthe row mapper function
valuethe first value to bind
valuesthe other values
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ queryMapRow() [2/2]

void anch::sql::Connection::queryMapRow ( const std::string & sqlQuery,
std::function< void(ResultSet &)> rowMapper )

Execute query and treat each row of result set

Parameters
sqlQuerythe SQL query to execute
rowMapperthe row mapper function
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ release()

void anch::sql::Connection::release ( )
noexcept

Release connection (used for connections' pool)

+ Here is the call graph for this function:

◆ rollback()

void anch::sql::Connection::rollback ( )

Rollback current SQL transaction.
If there is not transaction, this method does nothing.

Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ sendCommit()

virtual void anch::sql::Connection::sendCommit ( )
protectedpure virtual

Send commit to database server

Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.

+ Here is the call graph for this function:

◆ sendRollback()

virtual void anch::sql::Connection::sendRollback ( )
protectedpure virtual

Send rollback to database server

Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.

+ Here is the call graph for this function:

◆ sendStartTransaction()

virtual void anch::sql::Connection::sendStartTransaction ( )
protectedpure virtual

Send start transaction to database server

Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLConnection, anch::sql::PostgreSQLConnection, and anch::sql::SQLite3Connection.

+ Here is the call graph for this function:

◆ setValid()

void anch::sql::Connection::setValid ( bool valid)
protectednoexcept

Valid status setter

Parameters
validthe status to set
+ Here is the call graph for this function:

◆ startTransaction()

void anch::sql::Connection::startTransaction ( )

Start SQL transaction.
If there is a transaction on this database connection, this method does nothing.

Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ update() [1/2]

uint64_t anch::sql::Connection::update ( const std::string & query)

Execute update (INSERT, UPDATE or DELETE) SQL query.

Parameters
querythe SQL query
Returns
the number of affected rows
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

◆ update() [2/2]

template<typename T, typename... Q>
uint64_t anch::sql::Connection::update ( const std::string & query,
const T & value,
const Q &... values )

Execute update (INSERT, UPDATE or DELETE) SQL prepared statement.

Parameters
querythe SQL query
valuethe first value to bind
valuesthe other values
Returns
the number of affected rows
Exceptions
SqlExceptionany error
+ Here is the call graph for this function:

Member Data Documentation

◆ _errors

bool anch::sql::Connection::_errors
protected

Error on current calls

◆ _stmts

std::map<std::string, PreparedStatement*> anch::sql::Connection::_stmts
protected

Prepared statements

◆ _transaction

bool anch::sql::Connection::_transaction
protected

Is in SQL transaction

◆ _valid

bool anch::sql::Connection::_valid
protected

Is SQL connection valid


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