12#include <boost/date_time/posix_time/posix_time.hpp>
13#include <boost/lexical_cast.hpp>
21using namespace boost::posix_time;
42 values_.push_back(value.c_str());
48 if (index && index >=
values_.size()) {
50 <<
", is larger than the array size: " <<
values_.size());
59 if (index && index >=
values_.size()) {
61 <<
", is larger than the array size: " <<
values_.size());
66 values_.insert(
values_.begin() + index, bound_strs_.back()->c_str());
82 values_.push_back(
reinterpret_cast<const char*
>(&(data[0])));
89 reinterpret_cast<const char*
>(data.data()), data.size())));
91 values_.push_back(
reinterpret_cast<const char*
>(bound_strs_.back()->data()));
101 values_.push_back(
reinterpret_cast<const char*
>(&(data[0])));
108 isc_throw(
BadValue,
"PsqlBindArray::addTempBuffer - uint8_t data cannot be NULL");
112 reinterpret_cast<const char*
>(data), len)));
114 values_.push_back(bound_strs_.back()->data());
127 (
static_cast<unsigned int>(
byte)));
196 << value.
toText() <<
"' is not an IPv4 address");
217 << value.
toText() <<
"' is not an IPv6 address");
241 ptime epoch(boost::gregorian::date(1970, 1, 1));
242 if (timestamp < epoch) {
246 time_duration::sec_type since_epoch = (timestamp - epoch).total_seconds();
247 time_t input_time(since_epoch);
248 if (timestamp > max_db_time) {
251 static_cast<int64_t
>(
static_cast<uint32_t
>(input_time)) :
273 std::ostringstream ss;
285 std::ostringstream ss;
292 std::ostringstream stream;
295 return (
"bindarray is empty");
298 for (
int i = 0; i <
values_.size(); ++i) {
299 stream << i <<
" : ";
302 stream <<
"empty" << std::endl;
307 stream <<
"\"" <<
values_[i] <<
"\"" << std::endl;
311 for (
int x = 0; x <
lengths_[i]; ++x) {
312 stream << std::setfill(
'0') << std::setw(2)
314 <<
static_cast<unsigned int>(data[x]);
317 stream << std::endl << std::setbase(10);
321 return (stream.str());
326 if (
values_.size() < index + 1) {
328 " array size " <<
values_.size());
341 localtime_r(&input_time, &tinfo);
345 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", &tinfo);
346 return (std::string(buffer));
355 gmtime_r(&input_time, &tinfo);
359 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", &tinfo);
360 return (std::string(buffer));
366 const uint32_t valid_lifetime) {
369 int64_t expire_time_64 =
static_cast<int64_t
>(cltt)
370 +
static_cast<int64_t
>(valid_lifetime);
389 new_time = (boost::lexical_cast<time_t>(db_time_val));
390 }
catch (
const std::exception& ex) {
399 boost::posix_time::ptime& conv_time) {
401 conv_time = boost::posix_time::from_time_t(tmp_time);
408 const char* value = PQgetvalue(r, row, col);
420 return (PQgetisnull(r, row, col));
425 const size_t col, std::string& value) {
431 const size_t col,
bool &value) {
433 if (!strlen(data) || *data ==
'f') {
435 }
else if (*data ==
't') {
440 <<
" : must be 't' or 'f'");
446 const size_t col, uint8_t &value) {
451 value = boost::lexical_cast<uint16_t>(data);
452 }
catch (
const std::exception& ex) {
455 <<
" : " << ex.what());
461 const size_t col, boost::posix_time::ptime& value) {
462 std::string db_time_val;
473 }
catch (
const std::exception& ex) {
476 <<
" : " << ex.what());
491 }
catch (
const std::exception& ex) {
494 <<
" : " << ex.what());
509 }
catch (
const std::exception& ex) {
512 <<
" : " << ex.what());
522 }
catch (
const std::exception& ex) {
525 <<
" : " << ex.what());
531 const size_t col, uint8_t* buffer,
532 const size_t buffer_size,
533 size_t &bytes_converted) {
536 unsigned char* bytes = PQunescapeBytea((
const unsigned char*)
547 if (bytes_converted > buffer_size) {
551 << bytes_converted <<
" is too large for: "
557 memcpy(buffer, bytes, bytes_converted);
563 std::vector<uint8_t>& value) {
566 size_t bytes_converted = 0;
567 unsigned char* bytes = PQunescapeBytea((
const unsigned char*)
579 if (bytes_converted) {
580 value.assign(bytes, bytes + bytes_converted);
603 const size_t def_col,
const size_t min_col,
604 const size_t max_col) {
612 uint32_t min_value = value;
617 uint32_t max_value = value;
633 std::ostringstream stream;
635 for (
int col = 0; col < columns; ++col) {
638 int format = PQfformat(r, col);
640 stream << col <<
" " << name <<
" : " ;
642 stream <<
"\"" << val <<
"\"" << std::endl;
644 const char *data = val;
645 int length = PQfsize(r, col);
647 stream <<
"empty" << std::endl;
650 for (
int i = 0; i < length; ++i) {
651 stream << std::setfill(
'0') << std::setw(2)
653 <<
static_cast<unsigned int>(data[i]);
660 return (stream.str());
736boost::posix_time::ptime
738 boost::posix_time::ptime value;
757 const size_t max_col) {
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
The IOAddress class represents an IP addresses (version agnostic)
std::string toText() const
Convert the address to a string.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
bool isV6() const
Convenience function to check for an IPv6 address.
bool isV4() const
Convenience function to check for an IPv4 address.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
Exception thrown on failure to execute a database function.
static isc::util::Triplet< uint32_t > getTripletValue(const PgSqlResult &r, const int row, const size_t col)
Fetches a uint32_t value into a Triplet using a single column value.
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.
static std::string convertToDatabaseTime(const time_t input_time)
Converts UTC time_t value to a text representation in local time.
static std::string dumpRow(const PgSqlResult &r, int row)
Diagnostic tool which dumps the Result row contents as a string.
static isc::asiolink::IOAddress getInetValue4(const PgSqlResult &r, const int row, const size_t col)
Converts a column in a row in a result set into IPv4 address.
static std::string getColumnLabel(const PgSqlResult &r, const size_t col)
Fetches the name of the column in a result set.
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.
static std::string convertLocalToDatabaseTime(const time_t input_time)
Converts local time_t value to a text representation in local time.
static bool isColumnNull(const PgSqlResult &r, const int row, const size_t col)
Returns true if a column within a row is null.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
static time_t convertFromDatabaseTime(const std::string &db_time_val)
Converts time stamp from the database to a time_t.
static isc::asiolink::IOAddress getInetValue6(const PgSqlResult &r, const int row, const size_t col)
Converts a column in a row in a result set into IPv6 address.
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.
void getBytes(const size_t col, std::vector< uint8_t > &value)
Fetches binary data at the given column into a vector.
std::string getString(const size_t col)
Fetches the column value as a string.
void getColumnValue(const size_t col, T &value)
Fetches a text column as the given value type.
boost::posix_time::ptime getTimestamp(const size_t col)
Fetches a timestamp column as a ptime.
bool getBool(const size_t col)
Fetches the boolean value at the given column.
PgSqlResultRowWorker(const PgSqlResult &r, const int row)
Constructor.
const char * getRawColumnValue(const size_t col)
Gets a pointer to the raw column value in a result set row.
data::ElementPtr getJSON(const size_t col)
Fetches a JSON column as an ElementPtr.
isc::util::Triplet< uint32_t > getTriplet(const size_t col)
Fetches a uint32_t value into a Triplet using a single column value.
uint16_t getSmallInt(const size_t col)
Fetches the uint16_t value at the given column.
uint32_t getInt(const size_t col)
Fetches the uint32_t value at the given column.
double getDouble(const size_t col)
Fetches the floating point value at the given column.
uint64_t getBigInt(const size_t col)
Fetches the uint64_t value at the given column.
isc::asiolink::IOAddress getInet4(const size_t col)
Fetches the v4 IP address at the given column.
std::string dumpRow()
Diagnostic tool which dumps the Result row contents as a string.
isc::asiolink::IOAddress getInet6(const size_t col)
Fetches the v6 IP address at the given column.
bool isColumnNull(const size_t col)
Indicates whether or not the given column value is null.
RAII wrapper for PostgreSQL Result sets.
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 getCols() const
Returns the number of columns in the result set.
A template representing an optional value.
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
This template specifies a parameter value.
T get(T hint) const
Returns value with a hint.
T getMax() const
Returns a maximum allowed value.
T getMin() const
Returns a minimum allowed value.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
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-lfc.
void insert(const char *value, size_t index)
Inserts a string value to the bind array before the given index.
void addOptionalInet4(const util::Optional< isc::asiolink::IOAddress > &value)
Adds an Optional IPv4 address to the bind array.
void addTempString(const std::string &str)
Binds the given string to the bind array.
void addInet4(const isc::asiolink::IOAddress &value)
Adds an IPv4 address to the bind array.
void addInet6(const isc::asiolink::IOAddress &value)
Adds an IPv6 address to the bind array.
std::vector< const char * > values_
Vector of pointers to the data values.
void addTempBuffer(const uint8_t *data, const size_t len)
Adds a temporary buffer of binary data to the bind array.
void addOptional(const util::Optional< std::string > &value)
Adds an Optional string to the bind array.
std::vector< int > formats_
Vector of "format" for each value.
void add(const char *value)
Adds a char array to bind array based.
void addNull(const int format=PsqlBindArray::TEXT_FMT)
Adds a NULL value to the bind array.
void addMax(const isc::util::Triplet< uint32_t > &triplet)
Adds an integer Triplet's maximum value to the bind array.
static const char * TRUE_STR
Constant string passed to DB for boolean true values.
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.
static const int BINARY_FMT
Format value for binary data.
void addTempBinary(const std::vector< uint8_t > &data)
Adds a vector of binary data to the bind array.
bool amNull(size_t index=0) const
Determines if specified value is null.
static const int TEXT_FMT
Format value for text data.
void addTimestamp()
Adds a timestamp of the current time to the bind array.
void popBack()
Removes the last entry in the bind array.
void addOptionalInet6(const util::Optional< isc::asiolink::IOAddress > &value)
Adds an Optional IPv6 address to the bind array.
std::vector< int > lengths_
Vector of data lengths for each value.
void addMin(const isc::util::Triplet< uint32_t > &triplet)
Adds an integer Triplet's minimum value to the bind array.