24#define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5) {ch1,ch2,ch3,ch4,ch5}
26#define PGSQL_STATECODE_LEN 5
27#include <utils/errcodes.h>
44 : result_(result), rows_(0), cols_(0) {
53 rows_ = PQntuples(result);
54 cols_ = PQnfields(result);
60 if (row < 0 || row >= rows_) {
62 <<
", out of range: 0.." << rows_);
74 if (col < 0 || col >= cols_) {
76 <<
", out of range: 0.." << cols_);
88 const char* label = NULL;
91 label = PQfname(result_, col);
93 std::ostringstream os;
94 os <<
"Unknown column:" << col;
102 : conn_(conn), committed_(false) {
122 if (PQstatus(
conn_) == CONNECTION_OK) {
124 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
133std::pair<uint32_t, uint32_t>
141 const char* version_sql =
"SELECT version, minor FROM schema_version;";
143 if (PQresultStatus(r) != PGRES_TUPLES_OK) {
145 << version_sql <<
", reason: " << PQerrorMessage(conn.
conn_));
154 return (make_pair(
version, minor));
162 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
164 << statement.
text <<
", reason: " << PQerrorMessage(
conn_));
173 tagged_statement != end_statement; ++tagged_statement) {
180 string dbconnparameters;
181 string shost =
"localhost";
188 dbconnparameters +=
"host = '" + shost +
"'" ;
198 if (sport.size() > 0) {
199 unsigned int port = 0;
203 port = boost::lexical_cast<unsigned int>(sport);
212 if (port > numeric_limits<uint16_t>::max()) {
218 std::ostringstream oss;
220 dbconnparameters +=
" port = " + oss.str();
227 dbconnparameters +=
" user = '" + suser +
"'";
235 dbconnparameters +=
" password = '" + spassword +
"'";
243 dbconnparameters +=
" dbname = '" + sname +
"'";
258 if (stimeout.size() > 0) {
262 connect_timeout = boost::lexical_cast<unsigned int>(stimeout);
279 if ((connect_timeout == 0) ||
280 (connect_timeout > numeric_limits<int>::max())) {
282 stimeout <<
") must be an integer greater than 0");
286 std::ostringstream oss;
287 oss << connect_timeout;
288 dbconnparameters +=
" connect_timeout = " + oss.str();
292 PGconn* new_conn = PQconnectdb(dbconnparameters.c_str());
297 if (PQstatus(new_conn) != CONNECTION_OK) {
300 std::string error_message = PQerrorMessage(new_conn);
311 const char* sqlstate = PQresultErrorField(r, PG_DIAG_SQLSTATE);
313 return ((sqlstate != NULL) &&
320 int s = PQresultStatus(r);
321 if (s != PGRES_COMMAND_OK && s != PGRES_TUPLES_OK) {
326 const char* sqlstate = PQresultErrorField(r, PG_DIAG_SQLSTATE);
327 if ((sqlstate == NULL) ||
328 ((memcmp(sqlstate,
"08", 2) == 0) ||
329 (memcmp(sqlstate,
"53", 2) == 0) ||
330 (memcmp(sqlstate,
"54", 2) == 0) ||
331 (memcmp(sqlstate,
"57", 2) == 0) ||
332 (memcmp(sqlstate,
"58", 2) == 0))) {
336 .
arg(sqlstate ? sqlstate :
"<sqlstate null>");
347 "fatal database error or connectivity lost");
351 const char* error_message = PQerrorMessage(
conn_);
353 << statement.
name <<
", status: " << s
354 <<
"sqlstate:[ " << (sqlstate ? sqlstate :
"<null>")
355 <<
" ], reason: " << error_message);
364 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
365 const char* error_message = PQerrorMessage(
conn_);
376 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
377 const char* error_message = PQerrorMessage(
conn_);
387 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
388 const char* error_message = PQerrorMessage(
conn_);
int version()
returns Kea hooks version.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
void markUnusable()
Sets the unusable flag to true.
void checkUnusable()
Throws an exception if the connection is not usable.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
Exception thrown on failure to open database.
Exception thrown on failure to execute a database function.
Exception thrown if name of database is not specified.
Common PgSql Connector Pool.
void startTransaction()
Start a transaction.
void rollback()
Rollback Transactions.
bool compareError(const PgSqlResult &r, const char *error_state)
Checks a result set's SQL state against an error state.
void prepareStatement(const PgSqlTaggedStatement &statement)
Prepare Single Statement.
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
PgSqlHolder conn_
PgSql connection handle.
void startRecoverDbConnection()
The recover connection.
void commit()
Commit Transactions.
virtual ~PgSqlConnection()
Destructor.
void checkStatementError(const PgSqlResult &r, PgSqlTaggedStatement &statement)
Checks result of the r object.
void prepareStatements(const PgSqlTaggedStatement *start_statement, const PgSqlTaggedStatement *end_statement)
Prepare statements.
void openDatabase()
Open Database.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
void setConnection(PGconn *connection)
Sets the connection to the value given.
RAII wrapper for PostgreSQL Result sets.
void colCheck(int col) const
Determines if a column index is valid.
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.
~PgSqlResult()
Destructor.
std::string getColumnLabel(const int col) const
Fetches the name of the column in a result set.
PgSqlResult(PGresult *result)
Constructor.
PgSqlTransaction(PgSqlConnection &conn)
Constructor.
void commit()
Commits transaction.
~PgSqlTransaction()
Destructor.
We want to reuse the database backend connection and exchange code for other uses,...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const int DB_DBG_TRACE_DETAIL
Database logging levels.
const int PGSQL_DEFAULT_CONNECTION_TIMEOUT
@ PGSQL_START_TRANSACTION
Defines the logger used by the top-level component of kea-lfc.
#define PGSQL_STATECODE_LEN
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Define a PostgreSQL statement.
int nbparams
Number of parameters for a given query.
const char * text
Text representation of the actual query.
const char * name
Short name of the query.
const Oid types[PGSQL_MAX_PARAMETERS_IN_QUERY]
OID types.