Kea 2.5.8
isc::db::PgSqlResult Class Reference

RAII wrapper for PostgreSQL Result sets. More...

#include <pgsql_exchange.h>

+ Inheritance diagram for isc::db::PgSqlResult:

Public Member Functions

 PgSqlResult (PGresult *result)
 Constructor.
 
 ~PgSqlResult ()
 Destructor.
 
void colCheck (int col) const
 Determines if a column index is valid.
 
int getCols () const
 Returns the number of columns in the result set.
 
std::string getColumnLabel (const int col) const
 Fetches the name of the column in a result set.
 
int getRows () const
 Returns the number of rows in the result set.
 
 operator bool () const
 Boolean Operator.
 
 operator PGresult * () const
 Conversion Operator.
 
void rowCheck (int row) const
 Determines if a row index is valid.
 
void rowColCheck (int row, int col) const
 Determines if both a row and column index are valid.
 

Detailed Description

RAII wrapper for PostgreSQL Result sets.

When a Postgresql statement is executed, the results are returned in a pointer allocated structure, PGresult*. Data and status information are accessed via calls to functions such as PQgetvalue() which require the results pointer. In order to ensure this structure is freed, any invocation of Psql function which returns a PGresult* (e.g. PQexec, PQExecPrepared, ...) must save the result to an instance of this class. Example: {{{ PgSqlResult r(PQexec(conn_, "ROLLBACK")); }}}

This eliminates the need for an explicit release via, PQclear() and guarantees that the resources are released even if the an exception is thrown.

Definition at line 46 of file pgsql_exchange.h.

Constructor & Destructor Documentation

◆ PgSqlResult()

isc::db::PgSqlResult::PgSqlResult ( PGresult *  result)

Constructor.

Store the pointer to the fetched result set. Set row and column counts for convenience.

Parameters
resultpointer to the Postgresql client layer result If the value of is NULL, row and col values will be set to -1. This allows PgSqlResult to be passed into statement error checking.

Definition at line 56 of file pgsql_connection.cc.

◆ ~PgSqlResult()

isc::db::PgSqlResult::~PgSqlResult ( )

Destructor.

Frees the result set

Definition at line 79 of file pgsql_connection.cc.

Member Function Documentation

◆ colCheck()

void isc::db::PgSqlResult::colCheck ( int  col) const

Determines if a column index is valid.

Parameters
colindex to range check
Exceptions
DbOperationErrorif the column index is out of range

Definition at line 86 of file pgsql_connection.cc.

References isc_throw.

Referenced by getColumnLabel(), and rowColCheck().

◆ getCols()

int isc::db::PgSqlResult::getCols ( ) const
inline

Returns the number of columns in the result set.

Definition at line 70 of file pgsql_exchange.h.

Referenced by isc::db::PgSqlExchange::dumpRow().

◆ getColumnLabel()

std::string isc::db::PgSqlResult::getColumnLabel ( const int  col) const

Fetches the name of the column in a result set.

Returns the column name of the column from the result set. If the column index is out of range it will return the string "Unknown column:<index>"

Parameters
colindex of the column name to fetch
Returns
string containing the name of the column This method is exception safe.

Definition at line 100 of file pgsql_connection.cc.

References colCheck().

Referenced by isc::db::PgSqlExchange::dumpRow(), and isc::db::PgSqlExchange::getColumnLabel().

+ Here is the call graph for this function:

◆ getRows()

int isc::db::PgSqlResult::getRows ( ) const
inline

Returns the number of rows in the result set.

Definition at line 65 of file pgsql_exchange.h.

Referenced by isc::dhcp::PgSqlHostDataSourceImpl::getHostCollection().

◆ operator bool()

isc::db::PgSqlResult::operator bool ( ) const
inline

Boolean Operator.

Allows testing the PgSqlResult object for emptiness: "if (result)"

Definition at line 119 of file pgsql_exchange.h.

◆ operator PGresult *()

isc::db::PgSqlResult::operator PGresult * ( ) const
inline

Conversion Operator.

Allows the PgSqlResult object to be passed as the result set argument to PQxxxx functions.

Definition at line 112 of file pgsql_exchange.h.

◆ rowCheck()

void isc::db::PgSqlResult::rowCheck ( int  row) const

Determines if a row index is valid.

Parameters
rowindex to range check
Exceptions
DbOperationErrorif the row index is out of range

Definition at line 72 of file pgsql_connection.cc.

References isc_throw.

Referenced by isc::db::PgSqlResultRowWorker::PgSqlResultRowWorker(), isc::db::PgSqlExchange::dumpRow(), and rowColCheck().

◆ rowColCheck()

void isc::db::PgSqlResult::rowColCheck ( int  row,
int  col 
) const

Determines if both a row and column index are valid.

Parameters
rowindex to range check
colindex to range check
Exceptions
DbOperationErrorif either the row or column index is out of range

Definition at line 94 of file pgsql_connection.cc.

References colCheck(), and rowCheck().

Referenced by isc::db::PgSqlExchange::getRawColumnValue(), and isc::db::PgSqlExchange::isColumnNull().

+ Here is the call graph for this function:

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