Kea  1.9.9-git
pgsql_exchange.h
Go to the documentation of this file.
1 // Copyright (C) 2016-2018,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 
7 #ifndef PGSQL_EXCHANGE_H
8 #define PGSQL_EXCHANGE_H
9 
10 #include <asiolink/io_address.h>
11 #include <pgsql/pgsql_connection.h>
12 
13 #include <boost/lexical_cast.hpp>
14 #include <boost/noncopyable.hpp>
15 #include <boost/shared_ptr.hpp>
16 
17 #include <stdint.h>
18 #include <vector>
19 #include <iostream>
20 
21 namespace isc {
22 namespace db {
23 
42 typedef boost::shared_ptr<const std::string> ConstStringPtr;
43 
44 struct PsqlBindArray {
46  std::vector<const char *> values_;
48  std::vector<int> lengths_;
51  std::vector<int> formats_;
52 
54  static const int TEXT_FMT;
56  static const int BINARY_FMT;
57 
59  static const char* TRUE_STR;
61  static const char* FALSE_STR;
62 
65  size_t size() const {
66  return (values_.size());
67  }
68 
72  bool empty() const {
73 
74  return (values_.empty());
75  }
76 
86  void add(const char* value);
87 
96  void add(const std::string& value);
97 
106  void add(const std::vector<uint8_t>& data);
107 
118  void add(const uint8_t* data, const size_t len);
119 
127  void add(const bool& value);
128 
136  void add(const uint8_t& byte);
137 
146  void add(const isc::asiolink::IOAddress& addr);
147 
156  template<typename T>
157  void add(const T& value) {
158  addTempString(boost::lexical_cast<std::string>(value));
159  }
160 
169  void addTempString(const std::string& str);
170 
175  void addNull(const int format = PsqlBindArray::TEXT_FMT);
176 
177  //std::vector<const std::string> getBoundStrs() {
178  std::vector<ConstStringPtr> getBoundStrs() {
179  return (bound_strs_);
180  }
181 
184  std::string toText() const;
185 
186 private:
188  std::vector<ConstStringPtr> bound_strs_;
189 
190 };
191 
193 typedef boost::shared_ptr<PsqlBindArray> PsqlBindArrayPtr;
194 
201 public:
203  PgSqlExchange(const size_t num_columns = 0) : columns_(num_columns) {}
204 
206  virtual ~PgSqlExchange(){}
207 
212  static std::string convertToDatabaseTime(const time_t input_time);
213 
236  static std::string convertToDatabaseTime(const time_t cltt,
237  const uint32_t valid_lifetime);
238 
248  static time_t convertFromDatabaseTime(const std::string& db_time_val);
249 
264  static const char* getRawColumnValue(const PgSqlResult& r, const int row,
265  const size_t col);
266 
278  static std::string getColumnLabel(const PgSqlResult& r, const size_t col);
279 
289  static void getColumnValue(const PgSqlResult& r, const int row,
290  const size_t col, std::string& value);
291 
301  static void getColumnValue(const PgSqlResult& r, const int row,
302  const size_t col, bool &value);
303 
313  static void getColumnValue(const PgSqlResult& r, const int row,
314  const size_t col, uint8_t &value);
315 
326  const int row,
327  const size_t col);
328 
336  static bool isColumnNull(const PgSqlResult& r, const int row,
337  const size_t col);
338 
351  template<typename T>
352  static void getColumnValue(const PgSqlResult& r, const int row,
353  const size_t col, T& value) {
354  const char* data = getRawColumnValue(r, row, col);
355  try {
356  value = boost::lexical_cast<T>(data);
357  } catch (const std::exception& ex) {
358  isc_throw(db::DbOperationError, "Invalid data:[" << data
359  << "] for row: " << row << " col: " << col << ","
360  << getColumnLabel(r, col) << " : " << ex.what());
361  }
362  }
363 
380  static void convertFromBytea(const PgSqlResult& r, const int row,
381  const size_t col, uint8_t* buffer,
382  const size_t buffer_size,
383  size_t &bytes_converted);
384 
391  static std::string dumpRow(const PgSqlResult& r, int row);
392 
393 protected:
396  std::vector<std::string>columns_;
397 };
398 
399 }; // end of isc::db namespace
400 }; // end of isc namespace
401 
402 #endif // PGSQL_EXCHANGE_H
static time_t convertFromDatabaseTime(const std::string &db_time_val)
Converts time stamp from the database to a time_t.
RAII wrapper for PostgreSQL Result sets.
static std::string convertToDatabaseTime(const time_t input_time)
Converts time_t value to a text representation in local time.
std::vector< ConstStringPtr > getBoundStrs()
std::vector< int > formats_
Vector of "format" for each value.
static const int BINARY_FMT
Format value for binary data.
static std::string dumpRow(const PgSqlResult &r, int row)
Diagnostic tool which dumps the Result row contents as a string.
std::string toText() const
Dumps the contents of the array to a string.
static const char * FALSE_STR
Constant string passed to DB for boolean false values.
std::vector< int > lengths_
Vector of data lengths for each value.
bool empty() const
Indicates it the array is empty.
PgSqlExchange(const size_t num_columns=0)
Constructor.
static isc::asiolink::IOAddress getIPv6Value(const PgSqlResult &r, const int row, const size_t col)
Converts a column in a row in a result set into IPv6 address.
Base class for marshalling data to and from PostgreSQL.
std::vector< const char * > values_
Vector of pointers to the data values.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void addTempString(const std::string &str)
Binds the given string to the bind array.
void add(const char *value)
Adds a char array to bind array based.
boost::shared_ptr< PsqlBindArray > PsqlBindArrayPtr
Defines a smart pointer to PsqlBindArray.
virtual ~PgSqlExchange()
Destructor.
static const int TEXT_FMT
Format value for text data.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, T &value)
Fetches a text column as the given value type.
static bool isColumnNull(const PgSqlResult &r, const int row, const size_t col)
Returns true if a column within a row is null.
size_t size() const
Fetches the number of entries in the array.
static std::string getColumnLabel(const PgSqlResult &r, const size_t col)
Fetches the name of the column in a result set.
void addNull(const int format=PsqlBindArray::TEXT_FMT)
Adds a NULL value to the bind array.
static void convertFromBytea(const PgSqlResult &r, const int row, const size_t col, uint8_t *buffer, const size_t buffer_size, size_t &bytes_converted)
Converts a column in a row in a result set to a binary bytes.
boost::shared_ptr< const std::string > ConstStringPtr
Structure used to bind C++ input values to dynamic SQL parameters The structure contains three vector...
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::vector< std::string > columns_
Stores text labels for columns, currently only used for logging and errors.
void add(const T &value)
Adds the given value to the bind array.
static const char * getRawColumnValue(const PgSqlResult &r, const int row, const size_t col)
Gets a pointer to the raw column value in a result set row.
Exception thrown on failure to execute a database function.
static const char * TRUE_STR
Constant string passed to DB for boolean true values.
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157