23 #define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5) {ch1,ch2,ch3,ch4,ch5} 25 #define PGSQL_STATECODE_LEN 5 26 #include <utils/errcodes.h> 38 const char PgSqlConnection::DUPLICATE_KEY[] = ERRCODE_UNIQUE_VIOLATION;
40 PgSqlResult::PgSqlResult(PGresult *result)
41 : result_(result), rows_(0), cols_(0) {
50 rows_ = PQntuples(result);
51 cols_ = PQnfields(result);
57 if (row < 0 || row >= rows_) {
59 <<
", out of range: 0.." << rows_);
71 if (col < 0 || col >= cols_) {
73 <<
", out of range: 0.." << cols_);
85 const char* label = NULL;
88 label = PQfname(result_, col);
90 std::ostringstream os;
91 os <<
"Unknown column:" << col;
99 : conn_(conn), committed_(false) {
119 if (PQstatus(
conn_) == CONNECTION_OK) {
121 if(PQresultStatus(r) != PGRES_COMMAND_OK) {
124 .arg(PQerrorMessage(
conn_));
135 if(PQresultStatus(r) != PGRES_COMMAND_OK) {
137 << statement.
text <<
", reason: " << PQerrorMessage(
conn_));
146 tagged_statement != end_statement; ++tagged_statement) {
153 string dbconnparameters;
154 string shost =
"localhost";
161 dbconnparameters +=
"host = '" + shost +
"'" ;
171 if (sport.size() > 0) {
172 unsigned int port = 0;
176 port = boost::lexical_cast<unsigned int>(sport);
185 if (port > numeric_limits<uint16_t>::max()) {
191 std::ostringstream oss;
193 dbconnparameters +=
" port = " + oss.str();
200 dbconnparameters +=
" user = '" + suser +
"'";
208 dbconnparameters +=
" password = '" + spassword +
"'";
216 dbconnparameters +=
" dbname = '" + sname +
"'";
231 if (stimeout.size() > 0) {
235 connect_timeout = boost::lexical_cast<unsigned int>(stimeout);
252 if ((connect_timeout == 0) ||
253 (connect_timeout > numeric_limits<int>::max())) {
255 stimeout <<
") must be an integer greater than 0");
259 std::ostringstream oss;
260 oss << connect_timeout;
261 dbconnparameters +=
" connect_timeout = " + oss.str();
265 PGconn* new_conn = PQconnectdb(dbconnparameters.c_str());
270 if (PQstatus(new_conn) != CONNECTION_OK) {
273 std::string error_message = PQerrorMessage(new_conn);
284 const char* sqlstate = PQresultErrorField(r, PG_DIAG_SQLSTATE);
286 return ((sqlstate != NULL) &&
293 int s = PQresultStatus(r);
294 if (s != PGRES_COMMAND_OK && s != PGRES_TUPLES_OK) {
299 const char* sqlstate = PQresultErrorField(r, PG_DIAG_SQLSTATE);
300 if ((sqlstate == NULL) ||
301 ((memcmp(sqlstate,
"08", 2) == 0) ||
302 (memcmp(sqlstate,
"53", 2) == 0) ||
303 (memcmp(sqlstate,
"54", 2) == 0) ||
304 (memcmp(sqlstate,
"57", 2) == 0) ||
305 (memcmp(sqlstate,
"58", 2) == 0))) {
308 .arg(PQerrorMessage(
conn_))
309 .arg(sqlstate ? sqlstate :
"<sqlstate null>");
320 "fatal database errror or connectivity lost");
324 const char* error_message = PQerrorMessage(
conn_);
326 << statement.
name <<
", status: " << s
327 <<
"sqlstate:[ " << (sqlstate ? sqlstate :
"<null>")
328 <<
"], reason: " << error_message);
336 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
337 const char* error_message = PQerrorMessage(
conn_);
347 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
348 const char* error_message = PQerrorMessage(
conn_);
357 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
358 const char* error_message = PQerrorMessage(
conn_);
We want to reuse the database backend connection and exchange code for other uses,...
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
#define DB_LOG_DEBUG(LEVEL, MESSAGE)
Macros.
bool compareError(const PgSqlResult &r, const char *error_state)
Checks a result set's SQL state against an error state.
bool invokeDbLostCallback() const
Invokes the connection's lost connectivity callback.
void rowCheck(int row) const
Determines if a row index is valid.
std::string getColumnLabel(const int col) const
Fetches the name of the column in a result set.
Exception thrown on failure to open database.
void commit()
Commit Transactions.
#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 rollback()
Rollback Transactions.
PgSqlTransaction(PgSqlConnection &conn)
Constructor.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
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.
void checkStatementError(const PgSqlResult &r, PgSqlTaggedStatement &statement) const
Checks result of the r object.
#define PGSQL_STATECODE_LEN
void rowColCheck(int row, int col) const
Determines if both a row and column index are valid.
const char * text
Text representation of the actual query.
void colCheck(int col) const
Determines if a column index is valid.
~PgSqlResult()
Destructor.
#define DB_LOG_ERROR(MESSAGE)
void openDatabase()
Open Database.
const char * name
Short name of the query.
PgSqlHolder conn_
PgSql connection handle.
Exception thrown on failure to execute a database function.
int nbparams
Number of parameters for a given query.