Kea  1.9.9-git
pgsql_connection.h
Go to the documentation of this file.
1 // Copyright (C) 2016-2021 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 
9 #include <asiolink/io_service.h>
11 
12 #include <libpq-fe.h>
13 #include <boost/scoped_ptr.hpp>
14 
15 #include <vector>
16 #include <stdint.h>
17 
18 namespace isc {
19 namespace db {
20 
22 const uint32_t PG_SCHEMA_VERSION_MAJOR = 6;
23 const uint32_t PG_SCHEMA_VERSION_MINOR = 2;
24 
25 // Maximum number of parameters that can be used a statement
26 // @todo This allows us to use an initializer list (since we can't
27 // require C++11). It's unlikely we'd go past this many a single
28 // statement.
30 
37  int nbparams;
38 
45 
47  const char* name;
48 
50  const char* text;
51 };
52 
58 const size_t OID_NONE = 0; // PostgreSQL infers proper type
59 const size_t OID_BOOL = 16;
60 const size_t OID_BYTEA = 17;
61 const size_t OID_INT8 = 20; // 8 byte int
62 const size_t OID_INT2 = 21; // 2 byte int
63 const size_t OID_INT4 = 23; // 4 byte int
64 const size_t OID_TEXT = 25;
65 const size_t OID_VARCHAR = 1043;
66 const size_t OID_TIMESTAMP = 1114;
68 
76 
85 
86 class PgSqlResult : public boost::noncopyable {
87 public:
97  PgSqlResult(PGresult *result);
98 
102  ~PgSqlResult();
103 
105  int getRows() const {
106  return (rows_);
107  }
108 
110  int getCols() const {
111  return (cols_);
112  }
113 
119  void rowCheck(int row) const;
120 
126  void colCheck(int col) const;
127 
135  void rowColCheck(int row, int col) const;
136 
146  std::string getColumnLabel(const int col) const;
147 
152  operator PGresult*() const {
153  return (result_);
154  }
155 
159  operator bool() const {
160  return (result_);
161  }
162 
163 private:
164  PGresult* result_;
165  int rows_;
166  int cols_;
167 };
168 
169 
181 class PgSqlHolder : public boost::noncopyable {
182 public:
183 
188  PgSqlHolder() : pgconn_(NULL) {
189  }
190 
195  if (pgconn_ != NULL) {
196  PQfinish(pgconn_);
197  }
198  }
199 
203  void setConnection(PGconn* connection) {
204  if (pgconn_ != NULL) {
205  // Already set? Release the current connection first.
206  // Maybe this should be an error instead?
207  PQfinish(pgconn_);
208  }
209 
210  pgconn_ = connection;
211  }
212 
217  operator PGconn*() const {
218  return (pgconn_);
219  }
220 
224  operator bool() const {
225  return (pgconn_);
226  }
227 
228 private:
229  PGconn* pgconn_;
230 };
231 
233 class PgSqlConnection;
234 
252 class PgSqlTransaction : public boost::noncopyable {
253 public:
254 
264 
272 
279  void commit();
280 
281 private:
282 
284  PgSqlConnection& conn_;
285 
290  bool committed_;
291 };
292 
301 public:
303  static const char DUPLICATE_KEY[];
304 
312  PgSqlConnection(const ParameterMap& parameters,
314  DbCallback callback = DbCallback())
315  : DatabaseConnection(parameters, callback),
316  io_service_accessor_(io_accessor), io_service_() {
317  }
318 
320  virtual ~PgSqlConnection();
321 
332  static std::pair<uint32_t, uint32_t>
333  getVersion(const ParameterMap& parameters);
334 
344  void prepareStatement(const PgSqlTaggedStatement& statement);
345 
358  void prepareStatements(const PgSqlTaggedStatement* start_statement,
359  const PgSqlTaggedStatement* end_statement);
360 
368  void openDatabase();
369 
375  void startTransaction();
376 
382  void commit();
383 
389  void rollback();
390 
398  bool compareError(const PgSqlResult& r, const char* error_state);
399 
419  void checkStatementError(const PgSqlResult& r,
420  PgSqlTaggedStatement& statement);
421 
428  if (callback_) {
430  io_service_ = (*io_service_accessor_)();
431  io_service_accessor_.reset();
432  }
433 
434  if (io_service_) {
435  io_service_->post(std::bind(callback_, reconnectCtl()));
436  }
437  }
438  }
439 
445 
450  operator PGconn*() const {
451  return (conn_);
452  }
453 
457  operator bool() const {
458  return (conn_);
459  }
460 
469 
472 };
473 
474 } // end of isc::db namespace
475 } // end of isc namespace
476 
477 #endif // PGSQL_CONNECTION_H
isc::asiolink::IOServicePtr io_service_
IOService object, used for all ASIO operations.
PgSqlHolder()
Constructor.
RAII wrapper for PostgreSQL Result sets.
~PgSqlHolder()
Destructor.
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
ReconnectCtlPtr reconnectCtl()
The reconnect settings.
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.
PgSqlResult(PGresult *result)
Constructor.
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
void rowCheck(int row) const
Determines if a row index is valid.
IOServiceAccessorPtr io_service_accessor_
Accessor function which returns the IOService that can be used to recover the connection.
Common database connection class.
int getRows() const
Returns the number of rows in the result set.
const size_t OID_TEXT
void commit()
Commit Transactions.
void startRecoverDbConnection()
The recover connection.
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 checkStatementError(const PgSqlResult &r, PgSqlTaggedStatement &statement)
Checks result of the r object.
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.
int getCols() const
Returns the number of columns in the result set.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
void setConnection(PGconn *connection)
Sets the connection to the value given.
Define a PostgreSQL statement.
const size_t OID_BYTEA
PgSqlConnection(const ParameterMap &parameters, IOServiceAccessorPtr io_accessor=IOServiceAccessorPtr(), DbCallback callback=DbCallback())
Constructor.
const size_t OID_INT4
const char * text
Text representation of the actual query.
~PgSqlResult()
Destructor.
std::string getColumnLabel(const int col) const
Fetches the name of the column in a result set.
void openDatabase()
Open Database.
const size_t OID_VARCHAR
const char * name
Short name of the query.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap &parameters)
Get the schema version.
const size_t OID_NONE
Constants for PostgreSQL data types These are defined by PostgreSQL in , but including this file is extraordinarily convoluted, so we'll use these to fill-in.
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: 6.2.
const size_t OID_TIMESTAMP
int nbparams
Number of parameters for a given query.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
DbCallback callback_
The callback used to recover the connection.
RAII object representing a PostgreSQL transaction.