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 
17 namespace isc {
18 namespace db {
19 
21 const uint32_t PG_SCHEMA_VERSION_MAJOR = 5;
22 const 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 
36  int nbparams;
37 
44 
46  const char* name;
47 
49  const char* text;
50 };
51 
57 const size_t OID_NONE = 0; // PostgreSQL infers proper type
58 const size_t OID_BOOL = 16;
59 const size_t OID_BYTEA = 17;
60 const size_t OID_INT8 = 20; // 8 byte int
61 const size_t OID_INT2 = 21; // 2 byte int
62 const size_t OID_INT4 = 23; // 4 byte int
63 const size_t OID_TEXT = 25;
64 const size_t OID_VARCHAR = 1043;
65 const size_t OID_TIMESTAMP = 1114;
67 
75 
84 
85 class PgSqlResult : public boost::noncopyable {
86 public:
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 
162 private:
163  PGresult* result_;
164  int rows_;
165  int cols_;
166 };
167 
168 
180 class PgSqlHolder : public boost::noncopyable {
181 public:
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 
227 private:
228  PGconn* pgconn_;
229 };
230 
232 class PgSqlConnection;
233 
251 class PgSqlTransaction : public boost::noncopyable {
252 public:
253 
263 
271 
278  void commit();
279 
280 private:
281 
283  PgSqlConnection& conn_;
284 
289  bool committed_;
290 };
291 
300 public:
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
PgSqlHolder()
Constructor.
RAII wrapper for PostgreSQL Result sets.
const Oid types[PGSQL_MAX_PARAMETERS_IN_QUERY]
OID types.
void startTransaction()
Start a transaction.
const size_t OID_BOOL
const size_t OID_INT2
void commit()
Commits transaction.
Postgresql connection handle Holder.
const size_t OID_INT8
bool compareError(const PgSqlResult &r, const char *error_state)
Checks a result set's SQL state against an error state.
PgSqlResult(PGresult *result)
Constructor.
PgSqlConnection(const ParameterMap &parameters)
Constructor.
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
int getRows() const
Returns the number of rows in the result set.
void rowCheck(int row) const
Determines if a row index is valid.
Common database connection class.
std::string getColumnLabel(const int col) const
Fetches the name of the column in a result set.
const size_t OID_TEXT
void commit()
Commit Transactions.
void prepareStatements(const PgSqlTaggedStatement *start_statement, const PgSqlTaggedStatement *end_statement)
Prepare statements.
const uint32_t PG_SCHEMA_VERSION_MINOR
const size_t PGSQL_MAX_PARAMETERS_IN_QUERY
void rollback()
Rollback Transactions.
PgSqlTransaction(PgSqlConnection &conn)
Constructor.
Common PgSql Connector Pool.
virtual ~PgSqlConnection()
Destructor.
void prepareStatement(const PgSqlTaggedStatement &statement)
Prepare Single Statement.
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.
const size_t OID_BYTEA
void rowColCheck(int row, int col) const
Determines if both a row and column index are valid.
const size_t OID_INT4
const char * text
Text representation of the actual query.
void colCheck(int col) const
Determines if a column index is valid.
int getCols() const
Returns the number of columns in the result set.
void openDatabase()
Open Database.
const size_t OID_VARCHAR
const char * name
Short name of the query.
const size_t OID_NONE
Constants for PostgreSQL data types These are defined by PostgreSQL in <catalog/pg_type....
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
PgSqlHolder conn_
PgSql connection handle.
const uint32_t PG_SCHEMA_VERSION_MAJOR
Define PostgreSQL backend version: 5.0.
const size_t OID_TIMESTAMP
int nbparams
Number of parameters for a given query.
RAII object representing a PostgreSQL transaction.