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>
41 const char PgSqlConnection::DUPLICATE_KEY[] = ERRCODE_UNIQUE_VIOLATION;
43 PgSqlResult::PgSqlResult(PGresult *result)
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) {
133 std::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_);
We want to reuse the database backend connection and exchange code for other uses, in particular for hook libraries.
RAII wrapper for PostgreSQL Result sets.
~PgSqlTransaction()
Destructor.
const Oid types[PGSQL_MAX_PARAMETERS_IN_QUERY]
OID types.
void startTransaction()
Start a transaction.
void commit()
Commits transaction.
const int PGSQL_DEFAULT_CONNECTION_TIMEOUT
DB_LOG & arg(T first, Args...args)
Pass parameters to replace logger placeholders.
void colCheck(int col) const
Determines if a column index is valid.
bool compareError(const PgSqlResult &r, const char *error_state)
Checks a result set's SQL state against an error state.
void rowColCheck(int row, int col) const
Determines if both a row and column index are valid.
void rowCheck(int row) const
Determines if a row index is valid.
void checkUnusable()
Throws an exception if the connection is not usable.
Exception thrown on failure to open database.
void commit()
Commit Transactions.
void startRecoverDbConnection()
The recover connection.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void prepareStatements(const PgSqlTaggedStatement *start_statement, const PgSqlTaggedStatement *end_statement)
Prepare statements.
Exception thrown if name of database is not specified.
void checkStatementError(const PgSqlResult &r, PgSqlTaggedStatement &statement)
Checks result of the r object.
void rollback()
Rollback Transactions.
PgSqlTransaction(PgSqlConnection &conn)
Constructor.
int version()
returns Kea hooks version.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
Common PgSql Connector Pool.
virtual ~PgSqlConnection()
Destructor.
void prepareStatement(const PgSqlTaggedStatement &statement)
Prepare Single Statement.
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void setConnection(PGconn *connection)
Sets the connection to the value given.
Define a PostgreSQL statement.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
#define PGSQL_STATECODE_LEN
const char * text
Text representation of the actual query.
~PgSqlResult()
Destructor.
void markUnusable()
Sets the unusable flag to true.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
std::string getColumnLabel(const int col) const
Fetches the name of the column in a result set.
void openDatabase()
Open Database.
const char * name
Short name of the query.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
PgSqlHolder conn_
PgSql connection handle.
Exception thrown on failure to execute a database function.
int nbparams
Number of parameters for a given query.