7 #ifndef PGSQL_EXCHANGE_H 8 #define PGSQL_EXCHANGE_H 17 #include <boost/lexical_cast.hpp> 18 #include <boost/noncopyable.hpp> 19 #include <boost/shared_ptr.hpp> 112 operator PGresult*()
const {
119 operator bool()
const {
180 return (values_.size());
187 return (values_.empty());
199 void add(
const char* value);
209 void add(
const std::string& value);
224 void insert(
const char* value,
size_t index);
238 void insert(
const std::string& value,
size_t index);
253 void add(
const std::vector<uint8_t>& data);
262 void addTempBinary(
const std::vector<uint8_t>& data);
274 void add(
const uint8_t* data,
const size_t len);
288 void addTempBuffer(
const uint8_t* data,
const size_t len);
297 void add(
const bool& value);
306 void add(
const uint8_t& byte);
327 void add(
const T& value) {
328 addTempString(boost::lexical_cast<std::string>(value));
339 void addTempString(
const std::string& str);
434 void addTimestamp(
const boost::posix_time::ptime& timestamp);
461 std::string toText()
const;
469 bool amNull(
size_t index = 0)
const;
480 if (values_.size() < index + 1) {
482 << values_.size() <<
" element(s)");
484 auto x = values_.at(index);
488 return (boost::lexical_cast<T>(x));
497 if (formats_.size() < index + 1) {
499 << formats_.size() <<
" element(s)");
501 return (formats_.at(index));
506 std::vector<ConstStringPtr> bound_strs_;
529 static std::string convertToDatabaseTime(
const time_t input_time);
535 static std::string convertLocalToDatabaseTime(
const time_t input_time);
559 static std::string convertToDatabaseTime(
const time_t cltt,
560 const uint32_t valid_lifetime);
571 static time_t convertFromDatabaseTime(
const std::string& db_time_val);
582 static void convertFromDatabaseTime(
const std::string& db_time_val,
583 boost::posix_time::ptime& conv_time);
599 static const char* getRawColumnValue(
const PgSqlResult& r,
const int row,
624 static void getColumnValue(
const PgSqlResult& r,
const int row,
625 const size_t col, std::string& value);
636 static void getColumnValue(
const PgSqlResult& r,
const int row,
637 const size_t col,
bool &value);
648 static void getColumnValue(
const PgSqlResult& r,
const int row,
649 const size_t col, uint8_t &value);
703 static bool isColumnNull(
const PgSqlResult& r,
const int row,
720 const size_t col, T& value) {
721 const char* data = getRawColumnValue(r, row, col);
723 value = boost::lexical_cast<T>(data);
724 }
catch (
const std::exception& ex) {
726 <<
"] for row: " << row <<
" col: " << col <<
"," 740 static void getColumnValue(
const PgSqlResult& r,
const int row,
741 const size_t col, boost::posix_time::ptime& value);
752 static void getColumnValue(
const PgSqlResult& r,
const int row,
770 static void convertFromBytea(
const PgSqlResult& r,
const int row,
771 const size_t col, uint8_t* buffer,
772 const size_t buffer_size,
773 size_t &bytes_converted);
788 static void convertFromBytea(
const PgSqlResult& r,
const int row,
789 const size_t col, std::vector<uint8_t>& value);
824 const size_t def_col,
825 const size_t min_col,
826 const size_t max_col);
834 static std::string dumpRow(
const PgSqlResult& r,
int row);
859 bool isColumnNull(
const size_t col);
866 std::string getString(
const size_t col);
873 bool getBool(
const size_t col);
880 double getDouble(
const size_t col);
893 const char* getRawColumnValue(
const size_t col);
900 uint64_t getBigInt(
const size_t col);
907 uint32_t getInt(
const size_t col);
914 uint16_t getSmallInt(
const size_t col);
920 void getBytes(
const size_t col, std::vector<uint8_t>& value);
954 boost::posix_time::ptime getTimestamp(
const size_t col);
982 const size_t min_col,
983 const size_t max_col);
988 std::string dumpRow();
1004 #endif // PGSQL_EXCHANGE_H RAII wrapper for PostgreSQL Result sets.
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
std::vector< int > formats_
Vector of "format" for each value.
void addOptional(const util::Optional< T > &value)
Adds an Optional<type> value to the bind array.
static const int BINARY_FMT
Format value for binary data.
static const char * FALSE_STR
Constant string passed to DB for boolean false values.
boost::shared_ptr< PgSqlResult > PgSqlResultPtr
std::vector< int > lengths_
Vector of data lengths for each value.
PgSqlResult(PGresult *result)
Constructor.
PgSqlExchange(const size_t num_columns=0)
Constructor.
boost::shared_ptr< Element > ElementPtr
Base class for marshalling data to and from PostgreSQL.
int getRows() const
Returns the number of rows in the result set.
void rowCheck(int row) const
Determines if a row index is valid.
bool empty() const
Indicates it the array is empty.
std::string getColumnLabel(const int col) const
Fetches the name of the column in a result set.
int getType(size_t index=0)
Returns the column type.
std::vector< const char * > values_
Vector of pointers to the data values.
Convenience class which facilitates fetching column values from a result set row. ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::shared_ptr< PsqlBindArray > PsqlBindArrayPtr
Defines a smart pointer to PsqlBindArray.
virtual ~PgSqlExchange()
Destructor.
static const int TEXT_FMT
Format value for text data.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< PgSqlResultRowWorker > PgSqlResultRowWorkerPtr
Pointer to a result row worker.
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.
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.
size_t size() const
Fetches the number of entries in the array.
std::vector< std::string > columns_
Stores text labels for columns, currently only used for logging and errors.
T getInteger(size_t index=0)
Returns the value as an integer.
void rowColCheck(int row, int col) const
Determines if both a row and column index are valid.
PsqlBindArray()
Constructor.
void colCheck(int col) const
Determines if a column index is valid.
~PgSqlResult()
Destructor.
int getCols() const
Returns the number of columns in the result set.
void add(const T &value)
Adds the given value to the bind array.
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)
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.
void getColumnValue(const size_t col, T &value)
Fetches a text column as the given value type.