Kea  1.5.0
isc::db::PgSqlConnection Class Reference

Common PgSql Connector Pool. More...

#include <pgsql_connection.h>

+ Inheritance diagram for isc::db::PgSqlConnection:

Public Member Functions

 PgSqlConnection (const ParameterMap &parameters)
 Constructor. More...
 
virtual ~PgSqlConnection ()
 Destructor. More...
 
void checkStatementError (const PgSqlResult &r, PgSqlTaggedStatement &statement) const
 Checks result of the r object. More...
 
void commit ()
 Commit Transactions. More...
 
bool compareError (const PgSqlResult &r, const char *error_state)
 Checks a result set's SQL state against an error state. More...
 
void openDatabase ()
 Open Database. More...
 
 operator bool () const
 Boolean Operator. More...
 
 operator PGconn * () const
 Conversion Operator. More...
 
void prepareStatement (const PgSqlTaggedStatement &statement)
 Prepare Single Statement. More...
 
void prepareStatements (const PgSqlTaggedStatement *start_statement, const PgSqlTaggedStatement *end_statement)
 Prepare statements. More...
 
void rollback ()
 Rollback Transactions. More...
 
void startTransaction ()
 Start a transaction. More...
 
- Public Member Functions inherited from isc::db::DatabaseConnection
 DatabaseConnection (const ParameterMap &parameters)
 Constructor. More...
 
virtual ~DatabaseConnection ()
 Destructor. More...
 
bool configuredReadOnly () const
 Convenience method checking if database should be opened with read only access. More...
 
std::string getParameter (const std::string &name) const
 Returns value of a connection parameter. More...
 
bool invokeDbLostCallback () const
 Invokes the connection's lost connectivity callback. More...
 
virtual ReconnectCtlPtr makeReconnectCtl () const
 Instantiates a ReconnectCtl based on the connection's reconnect parameters. More...
 

Public Attributes

PgSqlHolder conn_
 PgSql connection handle. More...
 

Static Public Attributes

static const char DUPLICATE_KEY [] = ERRCODE_UNIQUE_VIOLATION
 Define the PgSql error state for a duplicate key error. More...
 
- Static Public Attributes inherited from isc::db::DatabaseConnection
static DbLostCallback db_lost_callback = 0
 Optional call back function to invoke if a successfully open connection subsequently fails. More...
 
static const time_t MAX_DB_TIME = 2147483647
 Defines maximum value for time that can be reliably stored. More...
 

Additional Inherited Members

- Public Types inherited from isc::db::DatabaseConnection
typedef boost::function< bool(ReconnectCtlPtr db_retry)> DbLostCallback
 Defines a callback prototype for propogating events upward. More...
 
typedef std::map< std::string, std::string > ParameterMap
 Database configuration parameter map. More...
 
- Static Public Member Functions inherited from isc::db::DatabaseConnection
static ParameterMap parse (const std::string &dbaccess)
 Parse database access string. More...
 
static std::string redactedAccessString (const ParameterMap &parameters)
 Redact database access string. More...
 
static isc::data::ElementPtr toElement (const ParameterMap &params)
 Unparse a parameter map. More...
 
static isc::data::ElementPtr toElementDbAccessString (const std::string &dbaccess)
 Unparse an access string. More...
 

Detailed Description

Common PgSql Connector Pool.

This class provides common operations for PgSql database connection used by both PgSqlLeaseMgr and PgSqlHostDataSource. It manages connecting to the database and preparing compiled statements. Its fields are public, because they are used (both set and retrieved) in classes that use instances of PgSqlConnection.

Definition at line 299 of file pgsql_connection.h.

Constructor & Destructor Documentation

◆ PgSqlConnection()

isc::db::PgSqlConnection::PgSqlConnection ( const ParameterMap parameters)
inline

Constructor.

Initialize PgSqlConnection object with parameters needed for connection.

Definition at line 307 of file pgsql_connection.h.

◆ ~PgSqlConnection()

isc::db::PgSqlConnection::~PgSqlConnection ( )
virtual

Destructor.

Definition at line 116 of file pgsql_connection.cc.

References conn_, DB_LOG_ERROR, and isc::db::PGSQL_DEALLOC_ERROR.

Member Function Documentation

◆ checkStatementError()

void isc::db::PgSqlConnection::checkStatementError ( const PgSqlResult r,
PgSqlTaggedStatement statement 
) const

Checks result of the r object.

This function is used to determine whether or not the SQL statement execution succeeded, and in the event of failures, decide whether or not the failures are recoverable.

If the error is recoverable, the function will throw a DbOperationError. If the error is deemed unrecoverable, such as a loss of connectivity with the server, the function will call invokeDbLostCallback(). If the invocation returns false then either there is no callback registered or the callback has elected not to attempt to reconnect, and exit(-1) is called;

If the invocation returns true, this indicates the calling layer will attempt recovery, and the function throws a DbOperationError to allow the caller to error handle the failed db access attempt.

Parameters
rresult of the last PostgreSQL operation
statement- tagged statement that was executed
Exceptions
isc::dhcp::DbOperationErrorDetailed PostgreSQL failure

Definition at line 291 of file pgsql_connection.cc.

References conn_, DB_LOG_ERROR, isc::db::DatabaseConnection::invokeDbLostCallback(), isc_throw, isc::db::PgSqlTaggedStatement::name, and isc::db::PGSQL_FATAL_ERROR.

Referenced by isc::dhcp::PgSqlHostDataSourceImpl::addStatement(), isc::dhcp::PgSqlHostDataSourceImpl::delStatement(), and isc::dhcp::PgSqlHostDataSourceImpl::getHostCollection().

+ Here is the call graph for this function:

◆ commit()

void isc::db::PgSqlConnection::commit ( )

Commit Transactions.

Commits all pending database operations.

Exceptions
DbOperationErrorIf the commit failed.

Definition at line 344 of file pgsql_connection.cc.

References conn_, isc::db::DB_DBG_TRACE_DETAIL, DB_LOG_DEBUG, isc_throw, and isc::db::PGSQL_COMMIT.

Referenced by isc::dhcp::PgSqlHostDataSource::commit(), isc::db::PgSqlTransaction::commit(), and isc::dhcp::PgSqlLeaseMgr::commit().

◆ compareError()

bool isc::db::PgSqlConnection::compareError ( const PgSqlResult r,
const char *  error_state 
)

Checks a result set's SQL state against an error state.

Parameters
rresult set to check
error_stateerror state to compare against
Returns
True if the result set's SQL state equals the error_state, false otherwise.

Definition at line 283 of file pgsql_connection.cc.

References PGSQL_STATECODE_LEN.

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

◆ openDatabase()

void isc::db::PgSqlConnection::openDatabase ( )

Open Database.

Opens the database using the information supplied in the parameters passed to the constructor.

Exceptions
NoDatabaseNameMandatory database name not given
DbOpenErrorError opening the database

Definition at line 152 of file pgsql_connection.cc.

References conn_, isc::db::DatabaseConnection::getParameter(), isc_throw, isc::db::PGSQL_DEFAULT_CONNECTION_TIMEOUT, and isc::db::PgSqlHolder::setConnection().

Referenced by isc::dhcp::PgSqlHostDataSourceImpl::PgSqlHostDataSourceImpl(), and isc::dhcp::PgSqlLeaseMgr::PgSqlLeaseMgr().

+ Here is the call graph for this function:

◆ operator bool()

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

Boolean Operator.

Allows testing the PgConnection for initialized connection

Definition at line 420 of file pgsql_connection.h.

References conn_.

◆ operator PGconn *()

isc::db::PgSqlConnection::operator PGconn * ( ) const
inline

Conversion Operator.

Allows the PgConnection object to be passed as the context argument to PQxxxx functions.

Definition at line 413 of file pgsql_connection.h.

References conn_.

◆ prepareStatement()

void isc::db::PgSqlConnection::prepareStatement ( const PgSqlTaggedStatement statement)

Prepare Single Statement.

Creates a prepared statement from the text given and adds it to the statements_ vector at the given index.

Parameters
statementSQL statement to be prepared.
Exceptions
isc::dhcp::DbOperationErrorAn operation on the open database has failed.

Definition at line 131 of file pgsql_connection.cc.

References conn_, isc_throw, isc::db::PgSqlTaggedStatement::name, isc::db::PgSqlTaggedStatement::nbparams, isc::db::PgSqlTaggedStatement::text, and isc::db::PgSqlTaggedStatement::types.

Referenced by isc::dhcp::PgSqlLeaseMgr::PgSqlLeaseMgr(), and prepareStatements().

◆ prepareStatements()

void isc::db::PgSqlConnection::prepareStatements ( const PgSqlTaggedStatement start_statement,
const PgSqlTaggedStatement end_statement 
)

Prepare statements.

Creates the prepared statements for all of the SQL statements used by the PostgreSQL backend.

Parameters
start_statementPointer to the first statement in range of the statements to be compiled.
end_statementPointer to the statement marking end of the range of statements to be compiled. This last statement is not compiled.
Exceptions
isc::dhcp::DbOperationErrorAn operation on the open database has failed.

Definition at line 142 of file pgsql_connection.cc.

References prepareStatement().

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

+ Here is the call graph for this function:

◆ rollback()

void isc::db::PgSqlConnection::rollback ( )

Rollback Transactions.

Rolls back all pending database operations.

Exceptions
DbOperationErrorIf the rollback failed.

Definition at line 354 of file pgsql_connection.cc.

References conn_, isc::db::DB_DBG_TRACE_DETAIL, DB_LOG_DEBUG, isc_throw, and isc::db::PGSQL_ROLLBACK.

Referenced by isc::dhcp::PgSqlHostDataSource::rollback(), isc::dhcp::PgSqlLeaseMgr::rollback(), and isc::db::PgSqlTransaction::~PgSqlTransaction().

◆ startTransaction()

void isc::db::PgSqlConnection::startTransaction ( )

Start a transaction.

Starts a transaction.

Exceptions
DbOperationErrorIf the transaction start failed.

Definition at line 333 of file pgsql_connection.cc.

References conn_, isc::db::DB_DBG_TRACE_DETAIL, DB_LOG_DEBUG, isc_throw, and isc::db::PGSQL_START_TRANSACTION.

Referenced by isc::db::PgSqlTransaction::PgSqlTransaction().

Member Data Documentation

◆ conn_

PgSqlHolder isc::db::PgSqlConnection::conn_

PgSql connection handle.

This field is public, because it is used heavily from PgSqlLeaseMgr and from PgSqlHostDataSource.

Definition at line 407 of file pgsql_connection.h.

Referenced by checkStatementError(), commit(), openDatabase(), operator bool(), operator PGconn *(), prepareStatement(), rollback(), startTransaction(), and ~PgSqlConnection().

◆ DUPLICATE_KEY

const char isc::db::PgSqlConnection::DUPLICATE_KEY = ERRCODE_UNIQUE_VIOLATION
static

Define the PgSql error state for a duplicate key error.

Definition at line 302 of file pgsql_connection.h.


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