Kea 1.5.0
pgsql_connection.h
Go to the documentation of this file.
1// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6#ifndef PGSQL_CONNECTION_H
7#define PGSQL_CONNECTION_H
8
10
11#include <libpq-fe.h>
12#include <boost/scoped_ptr.hpp>
13
14#include <vector>
15#include <stdint.h>
16
17namespace isc {
18namespace db {
19
21const uint32_t PG_SCHEMA_VERSION_MAJOR = 5;
22const uint32_t PG_SCHEMA_VERSION_MINOR = 0;
23
24// Maximum number of parameters that can be used a statement
25// @todo This allows us to use an initializer list (since we can't
26// require C++11). It's unlikely we'd go past this many a single
27// statement.
29
37
44
46 const char* name;
47
49 const char* text;
50};
51
57const size_t OID_NONE = 0; // PostgreSQL infers proper type
58const size_t OID_BOOL = 16;
59const size_t OID_BYTEA = 17;
60const size_t OID_INT8 = 20; // 8 byte int
61const size_t OID_INT2 = 21; // 2 byte int
62const size_t OID_INT4 = 23; // 4 byte int
63const size_t OID_TEXT = 25;
64const size_t OID_VARCHAR = 1043;
65const size_t OID_TIMESTAMP = 1114;
67
75
84
85class PgSqlResult : public boost::noncopyable {
86public:
96 PgSqlResult(PGresult *result);
97
101 ~PgSqlResult();
102
104 int getRows() const {
105 return (rows_);
106 }
107
109 int getCols() const {
110 return (cols_);
111 }
112
118 void rowCheck(int row) const;
119
125 void colCheck(int col) const;
126
134 void rowColCheck(int row, int col) const;
135
145 std::string getColumnLabel(const int col) const;
146
151 operator PGresult*() const {
152 return (result_);
153 }
154
158 operator bool() const {
159 return (result_);
160 }
161
162private:
163 PGresult* result_;
164 int rows_;
165 int cols_;
166};
167
168
180class PgSqlHolder : public boost::noncopyable {
181public:
182
187 PgSqlHolder() : pgconn_(NULL) {
188 }
189
194 if (pgconn_ != NULL) {
195 PQfinish(pgconn_);
196 }
197 }
198
202 void setConnection(PGconn* connection) {
203 if (pgconn_ != NULL) {
204 // Already set? Release the current connection first.
205 // Maybe this should be an error instead?
206 PQfinish(pgconn_);
207 }
208
209 pgconn_ = connection;
210 }
211
216 operator PGconn*() const {
217 return (pgconn_);
218 }
219
223 operator bool() const {
224 return (pgconn_);
225 }
226
227private:
228 PGconn* pgconn_;
229};
230
232class PgSqlConnection;
233
251class PgSqlTransaction : public boost::noncopyable {
252public:
253
263
271
278 void commit();
279
280private:
281
283 PgSqlConnection& conn_;
284
289 bool committed_;
290};
291
300public:
302 static const char DUPLICATE_KEY[];
303
307 PgSqlConnection(const ParameterMap& parameters)
308 : DatabaseConnection(parameters) {
309 }
310
312 virtual ~PgSqlConnection();
313
323 void prepareStatement(const PgSqlTaggedStatement& statement);
324
337 void prepareStatements(const PgSqlTaggedStatement* start_statement,
338 const PgSqlTaggedStatement* end_statement);
339
347 void openDatabase();
348
354 void startTransaction();
355
361 void commit();
362
368 void rollback();
369
377 bool compareError(const PgSqlResult& r, const char* error_state);
378
400 void checkStatementError(const PgSqlResult& r,
401 PgSqlTaggedStatement& statement) const;
402
408
413 operator PGconn*() const {
414 return (conn_);
415 }
416
420 operator bool() const {
421 return (conn_);
422 }
423
424};
425
426}; // end of isc::db namespace
427}; // end of isc namespace
428
429#endif // PGSQL_CONNECTION_H
Common database connection class.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Common PgSql Connector Pool.
void startTransaction()
Start a transaction.
void rollback()
Rollback Transactions.
PgSqlConnection(const ParameterMap &parameters)
Constructor.
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.
PgSqlHolder conn_
PgSql connection handle.
void commit()
Commit Transactions.
virtual ~PgSqlConnection()
Destructor.
void prepareStatements(const PgSqlTaggedStatement *start_statement, const PgSqlTaggedStatement *end_statement)
Prepare statements.
void openDatabase()
Open Database.
void checkStatementError(const PgSqlResult &r, PgSqlTaggedStatement &statement) const
Checks result of the r object.
Postgresql connection handle Holder.
void setConnection(PGconn *connection)
Sets the connection to the value given.
PgSqlHolder()
Constructor.
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.
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.
int getCols() const
Returns the number of columns in the result set.
RAII object representing a PostgreSQL transaction.
void commit()
Commits transaction.
const size_t OID_INT4
const size_t OID_INT2
const uint32_t PG_SCHEMA_VERSION_MAJOR
Define PostgreSQL backend version: 5.0.
const uint32_t PG_SCHEMA_VERSION_MINOR
const size_t PGSQL_MAX_PARAMETERS_IN_QUERY
const size_t OID_VARCHAR
const size_t OID_NONE
Constants for PostgreSQL data types These are defined by PostgreSQL in <catalog/pg_type....
const size_t OID_TIMESTAMP
const size_t OID_TEXT
const size_t OID_BOOL
const size_t OID_INT8
const size_t OID_BYTEA
Defines the logger used by the top-level component of kea-dhcp-ddns.
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.