Kea  2.3.5-git
pgsql_connection.h
Go to the documentation of this file.
1 // Copyright (C) 2016-2023 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>
10 #include <pgsql/pgsql_exchange.h>
11 
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 PGSQL_SCHEMA_VERSION_MAJOR = 13;
22 const uint32_t PGSQL_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.
28 const size_t PGSQL_MAX_PARAMETERS_IN_QUERY = 128;
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 
79 class PgSqlHolder : public boost::noncopyable {
80 public:
81 
86  PgSqlHolder() : pgconn_(NULL) {
87  }
88 
93  if (pgconn_ != NULL) {
94  PQfinish(pgconn_);
95  }
96  }
97 
101  void setConnection(PGconn* connection) {
102  if (pgconn_ != NULL) {
103  // Already set? Release the current connection first.
104  // Maybe this should be an error instead?
105  PQfinish(pgconn_);
106  }
107 
108  pgconn_ = connection;
109  }
110 
115  operator PGconn*() const {
116  return (pgconn_);
117  }
118 
122  operator bool() const {
123  return (pgconn_);
124  }
125 
126 private:
127  PGconn* pgconn_;
128 };
129 
131 class PgSqlConnection;
132 
150 class PgSqlTransaction : public boost::noncopyable {
151 public:
152 
162 
169  ~PgSqlTransaction();
170 
177  void commit();
178 
179 private:
180 
182  PgSqlConnection& conn_;
183 
188  bool committed_;
189 };
190 
199 public:
201  static const char DUPLICATE_KEY[];
203  static const char NULL_KEY[];
204 
206  typedef std::function<void(PgSqlResult&, int)> ConsumeResultRowFun;
207 
209  static bool warned_about_tls;
210 
218  PgSqlConnection(const ParameterMap& parameters,
220  DbCallback callback = DbCallback())
221  : DatabaseConnection(parameters, callback),
222  io_service_accessor_(io_accessor), io_service_(),
223  transaction_ref_count_(0) {
224  }
225 
227  virtual ~PgSqlConnection();
228 
239  static std::pair<uint32_t, uint32_t>
240  getVersion(const ParameterMap& parameters);
241 
251  void prepareStatement(const PgSqlTaggedStatement& statement);
252 
265  void prepareStatements(const PgSqlTaggedStatement* start_statement,
266  const PgSqlTaggedStatement* end_statement);
267 
276  std::string getConnParameters();
277 
285  void openDatabase();
286 
305  void startTransaction();
306 
310  bool isTransactionStarted() const;
311 
322  void commit();
323 
334  void rollback();
335 
344  void createSavepoint(const std::string& name);
345 
355  void rollbackToSavepoint(const std::string& name);
356 
368  void executeSQL(const std::string& sql);
369 
377  bool compareError(const PgSqlResult& r, const char* error_state);
378 
398  void checkStatementError(const PgSqlResult& r,
399  PgSqlTaggedStatement& statement);
400 
407  if (callback_) {
408  if (!io_service_ && io_service_accessor_) {
409  io_service_ = (*io_service_accessor_)();
410  io_service_accessor_.reset();
411  }
412 
413  if (io_service_) {
414  io_service_->post(std::bind(callback_, reconnectCtl()));
415  }
416  }
417  }
418 
436  PgSqlResultPtr executePreparedStatement(PgSqlTaggedStatement& statement,
437  const PsqlBindArray& in_bindings
438  = PsqlBindArray());
439 
457  void selectQuery(PgSqlTaggedStatement& statement,
458  const PsqlBindArray& in_bindings,
459  ConsumeResultRowFun process_result_row);
460 
473  void insertQuery(PgSqlTaggedStatement& statement,
474  const PsqlBindArray& in_bindings);
475 
476 
489  uint64_t updateDeleteQuery(PgSqlTaggedStatement& statement,
490  const PsqlBindArray& in_bindings);
491 
497 
502  operator PGconn*() const {
503  return (conn_);
504  }
505 
509  operator bool() const {
510  return (conn_);
511  }
512 
513 private:
514 
529  template<typename T>
530  void setIntParameterValue(const std::string& name, int64_t min, int64_t max, T& value);
531 
532 public:
533 
542 
545 
553 };
554 
556 typedef boost::shared_ptr<PgSqlConnection> PgSqlConnectionPtr;
557 
558 } // end of isc::db namespace
559 } // end of isc namespace
560 
561 #endif // PGSQL_CONNECTION_H
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
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.
const size_t OID_BOOL
const size_t OID_INT2
Postgresql connection handle Holder.
const size_t OID_INT8
boost::shared_ptr< PgSqlResult > PgSqlResultPtr
int transaction_ref_count_
Reference counter for transactions.
IOServiceAccessorPtr io_service_accessor_
Accessor function which returns the IOService that can be used to recover the connection.
Common database connection class.
const size_t OID_TEXT
boost::shared_ptr< PgSqlConnection > PgSqlConnectionPtr
Defines a pointer to a PgSqlConnection.
void startRecoverDbConnection()
The recover connection.
const size_t PGSQL_MAX_PARAMETERS_IN_QUERY
static bool warned_about_tls
Emit the TLS support warning only once.
Common PgSql Connector Pool.
Defines the logger used by the top-level component of kea-lfc.
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.
const size_t OID_VARCHAR
const char * name
Short name of the query.
const uint32_t PGSQL_SCHEMA_VERSION_MAJOR
Define the PostgreSQL backend version.
const size_t OID_NONE
Constants for PostgreSQL data types These are defined by PostgreSQL in <catalog/pg_type.h>, but including this file is extraordinarily convoluted, so we&#39;ll use these to fill-in.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
PgSqlHolder conn_
PgSql connection handle.
const size_t OID_TIMESTAMP
std::function< void(PgSqlResult &, int)> ConsumeResultRowFun
Function invoked to process fetched row.
int nbparams
Number of parameters for a given query.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
const uint32_t PGSQL_SCHEMA_VERSION_MINOR
RAII object representing a PostgreSQL transaction.