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

SQL result representation. More...

#include <resultSet.hpp>

Inheritance diagram for anch::sql::ResultSet:
Collaboration diagram for anch::sql::ResultSet:

Public Member Functions

 ResultSet () noexcept
virtual ~ResultSet () noexcept
template<typename T>
bool get (std::size_t idx, T &out)
template<anch::sql::SystemTimePoint T>
bool get (std::size_t idx, T &out)
template<typename T>
bool get (const std::string field, T &out)
template<typename T>
std::optional< T > get (std::size_t idx)
template<anch::sql::SystemTimePoint T>
std::optional< T > get (std::size_t idx)
template<typename T>
const std::optional< T > get (const std::string field)
std::vector< std::string > columns () const
std::optional< std::size_t > columnIndex (const std::string &name) const
bool next ()
template<>
bool get (std::size_t idx, int64_t &out)
template<>
bool get (std::size_t idx, int32_t &out)
template<>
bool get (std::size_t idx, int16_t &out)
template<>
bool get (std::size_t idx, uint64_t &out)
template<>
bool get (std::size_t idx, uint32_t &out)
template<>
bool get (std::size_t idx, uint16_t &out)
template<>
bool get (std::size_t idx, double &out)
template<>
bool get (std::size_t idx, float &out)
template<>
bool get (std::size_t idx, bool &out)
template<>
std::optional< int64_t > get (std::size_t idx)
template<>
std::optional< int32_t > get (std::size_t idx)
template<>
std::optional< int16_t > get (std::size_t idx)
template<>
std::optional< uint64_t > get (std::size_t idx)
template<>
std::optional< uint32_t > get (std::size_t idx)
template<>
std::optional< uint16_t > get (std::size_t idx)
template<>
std::optional< double > get (std::size_t idx)
template<>
std::optional< float > get (std::size_t idx)
template<>
std::optional< bool > get (std::size_t idx)

Protected Member Functions

virtual bool fetchNext ()=0
void invalidateStatement () noexcept
virtual bool getValue (std::size_t idx, std::string &out)=0
virtual std::optional< std::string > getValue (std::size_t idx)=0
virtual const anch::date::DateFormatter & getTimestampFormatter ()=0
const anch::date::DateFormatter & getDefaultTimestampFormatter ()

Protected Attributes

std::map< std::string, std::size_t > _fields

Friends

class Connection
class PreparedStatement

Detailed Description

SQL result representation.

The ResultSet has to be used with while(res.next()) { [...] }.
Errors raised by next are reported to the connection which has executed the query (a fatal error invalidates it).
Field getter is templated. Specializations are included in library. If you need other conversions, you can implement them in your own program.

Author
Vincent Lachenal
Since
0.1

Constructor & Destructor Documentation

◆ ResultSet()

anch::sql::ResultSet::ResultSet ( )
noexcept

ResultSet default constructor

◆ ~ResultSet()

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

ResultSet destrcutor

Here is the call graph for this function:

Member Function Documentation

◆ columnIndex()

std::optional< std::size_t > anch::sql::ResultSet::columnIndex ( const std::string & name) const

Column index

Parameters
namethe column name (exact)
Returns
the index, std::nullopt when there is no column of this name
Since
0.3

◆ columns()

std::vector< std::string > anch::sql::ResultSet::columns ( ) const

Columns names, in order of their indices (aliases when they are given ; PostgreSQL gives unquoted names in lowercase). A name given to several columns is the name of the last one only. SQLite and PostgreSQL know the columns once the first row has been fetched ( next ).

Returns
the names
Since
0.3

◆ fetchNext()

virtual bool anch::sql::ResultSet::fetchNext ( )
protectedpure virtual

Retrieve next row (driver implementation)

Returns
true if next row exists, false otherwise
Exceptions
SqlExceptionany error (with connection status)

Implemented in anch::sql::MySQLPreparedStatementResultSet, anch::sql::MySQLResultSet, anch::sql::PostgreSQLResultSet, and anch::sql::SQLite3ResultSet.

◆ get() [1/24]

template<typename T>
const std::optional< T > anch::sql::ResultSet::get ( const std::string field)
inline

Get field value by field name.

Template Parameters
Texpected type
Parameters
fieldthe field name
Returns
the result
Exceptions
SqlExceptionany error
Here is the call graph for this function:

◆ get() [2/24]

template<typename T>
bool anch::sql::ResultSet::get ( const std::string field,
T & out )
inline

Get field value by field name.
The output parameter will be set only if SQL result is not NULL.

Template Parameters
Texpected type
Parameters
fieldthe field name
outthe result
Returns
true if result is NULL, false otherwise
Here is the call graph for this function:

◆ get() [3/24]

template<>
std::optional< int64_t > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as 64 bits signed integer.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ get() [4/24]

template<>
std::optional< int32_t > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as 32 bits signed integer.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error

◆ get() [5/24]

template<>
std::optional< int16_t > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as 16 bits signed integer.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error

◆ get() [6/24]

template<>
std::optional< uint64_t > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as 64 bits unsigned integer.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error

◆ get() [7/24]

template<>
std::optional< uint32_t > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as 32 bits unsigned integer.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error

◆ get() [8/24]

template<>
std::optional< uint16_t > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as 16 bits unsigned integer.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error

◆ get() [9/24]

template<>
std::optional< double > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as double precision floating point number (PostgreSQL NaN , Infinity included).

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not a number (non fatal), database error
Since
0.3

◆ get() [10/24]

template<>
std::optional< float > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as single precision floating point number.

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not a number (non fatal), database error
Since
0.3

◆ get() [11/24]

template<>
std::optional< bool > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as boolean ( 1 / 0 , t / f , true / false ).

Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or not a boolean (non fatal), database error
Since
0.3

◆ get() [12/24]

std::optional< T > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index as std::chrono::system_clock point in time: read as anch::date::DateTime , rounded down to T precision.

Template Parameters
Texpected type ( std::chrono::sys_seconds , std::chrono::sys_time<std::chrono::milliseconds> , ...)
Parameters
idxthe index
Returns
the value, std::nullopt if result is NULL
Exceptions
SqlExceptionindex out of range or invalid timestamp (non fatal), database error
Since
0.3

◆ get() [13/24]

template<typename T>
std::optional< T > anch::sql::ResultSet::get ( std::size_t idx)

Get field value by index.

Template Parameters
Texpected type: std::string , integers, anch::date::Date , anch::date::Time , anch::date::DateTime , anch::UUID (with ANCH_UUID ), std::chrono::sys_time , double , float , bool
Parameters
idxthe index
Returns
the result
Exceptions
SqlExceptionany error

◆ get() [14/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
bool & out )

Get field value by index as boolean ( 1 / 0 , t / f , true / false ).
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not a boolean (non fatal), database error
Since
0.3
Here is the call graph for this function:

◆ get() [15/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
double & out )

Get field value by index as double precision floating point number (PostgreSQL NaN , Infinity included).
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not a number (non fatal), database error
Since
0.3
Here is the call graph for this function:

◆ get() [16/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
float & out )

Get field value by index as single precision floating point number.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not a number (non fatal), database error
Since
0.3
Here is the call graph for this function:

◆ get() [17/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
int16_t & out )

Get field value by index as 16 bits signed integer.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ get() [18/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
int32_t & out )

Get field value by index as 32 bits signed integer.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ get() [19/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
int64_t & out )

Get field value by index as 64 bits signed integer.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ get() [20/24]

bool anch::sql::ResultSet::get ( std::size_t idx,
T & out )

Get field value by index as std::chrono::system_clock point in time: read as anch::date::DateTime , rounded down to T precision.
The output parameter will be set only if SQL result is not NULL.

Template Parameters
Texpected type ( std::chrono::sys_seconds , std::chrono::sys_time<std::chrono::milliseconds> , ...)
Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or invalid timestamp (non fatal), database error
Since
0.3
Here is the call graph for this function:

◆ get() [21/24]

template<typename T>
bool anch::sql::ResultSet::get ( std::size_t idx,
T & out )

Get field value by index.
The output parameter will be set only if SQL result is not NULL.

Template Parameters
Texpected type: std::string , integers, anch::date::Date , anch::date::Time , anch::date::DateTime , anch::UUID (with ANCH_UUID ), std::chrono::sys_time , double , float , bool
Parameters
idxthe index
outthe result
Returns
true if result is NULL, false otherwise
Exceptions
SqlExceptionany error
Here is the call graph for this function:

◆ get() [22/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
uint16_t & out )

Get field value by index as 16 bits unsigned integer.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ get() [23/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
uint32_t & out )

Get field value by index as 32 bits unsigned integer.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ get() [24/24]

template<>
bool anch::sql::ResultSet::get ( std::size_t idx,
uint64_t & out )

Get field value by index as 64 bits unsigned integer.
The output parameter is set only if SQL result is not NULL.

Parameters
idxthe index
outthe result
Returns
true if result is NULL , false otherwise
Exceptions
SqlExceptionindex out of range or not an integer of this type (non fatal), database error
Here is the call graph for this function:

◆ getDefaultTimestampFormatter()

const anch::date::DateFormatter & anch::sql::ResultSet::getDefaultTimestampFormatter ( )
protected

Retrieve SQL timestamp formatter

Returns
the SQL timestamp formatter
Here is the call graph for this function:

◆ getTimestampFormatter()

virtual const anch::date::DateFormatter & anch::sql::ResultSet::getTimestampFormatter ( )
protectedpure virtual

Retrieve SQL timestamp formatter

Returns
the SQL timestamp formatter

Implemented in anch::sql::MySQLPreparedStatementResultSet, anch::sql::MySQLResultSet, anch::sql::PostgreSQLResultSet, and anch::sql::SQLite3ResultSet.

Here is the call graph for this function:

◆ getValue() [1/2]

virtual std::optional< std::string > anch::sql::ResultSet::getValue ( std::size_t idx)
protectedpure virtual

Retrieve string value from result set according to SQL database engine.

Parameters
idxthe field index
Returns
the result as optional character string
Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLPreparedStatementResultSet, anch::sql::MySQLResultSet, anch::sql::PostgreSQLResultSet, and anch::sql::SQLite3ResultSet.

Here is the call graph for this function:

◆ getValue() [2/2]

virtual bool anch::sql::ResultSet::getValue ( std::size_t idx,
std::string & out )
protectedpure virtual

Retrieve string value from result set according to SQL database engine.

Parameters
idxthe field index
outthe result
Exceptions
SqlExceptionany error

Implemented in anch::sql::MySQLPreparedStatementResultSet, anch::sql::MySQLResultSet, anch::sql::PostgreSQLResultSet, and anch::sql::SQLite3ResultSet.

Here is the call graph for this function:

◆ invalidateStatement()

void anch::sql::ResultSet::invalidateStatement ( )
protectednoexcept

Invalidate the prepared statement which has executed the query, if any (see PreparedStatement::invalidate )

Since
0.3

◆ next()

bool anch::sql::ResultSet::next ( )

Retrieve next row

Returns
true if next row exists, false otherwise
Exceptions
SqlExceptionany error

Member Data Documentation

◆ _fields

std::map<std::string, std::size_t> anch::sql::ResultSet::_fields
protected

Result set fields


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