mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-09-01 06:25:34 +00:00
[#1848] Added support for storing Triplets to PsqlBindArray
src/lib/pgsql/pgsql_exchange.* PsqlBindArray::add(const Triplet<uint32_t>& triplet) PsqlBindArray::addMin(const Triplet<uint32_t>& triplet) PsqlBindArray::addMax(const Triplet<uint32_t>& triplet) - new functions for storing Triplets src/lib/pgsql/tests/pgsql_exchange_unittest.cc TEST(PsqlBindArray, addTriplet) - new test
This commit is contained in:
committed by
Tomek Mrugalski
parent
fe31eee4ed
commit
4cd9f584b7
@@ -16,6 +16,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace isc::util;
|
||||||
|
|
||||||
namespace isc {
|
namespace isc {
|
||||||
namespace db {
|
namespace db {
|
||||||
|
|
||||||
@@ -82,6 +84,33 @@ void PsqlBindArray::addNull(const int format) {
|
|||||||
formats_.push_back(format);
|
formats_.push_back(format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
PsqlBindArray::add(const Triplet<uint32_t>& triplet) {
|
||||||
|
if (triplet.unspecified()) {
|
||||||
|
addNull();
|
||||||
|
} else {
|
||||||
|
add<uint32_t>(triplet.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
PsqlBindArray::addMin(const Triplet<uint32_t>& triplet) {
|
||||||
|
if (triplet.unspecified() || (triplet.getMin() == triplet.get())) {
|
||||||
|
addNull();
|
||||||
|
} else {
|
||||||
|
add<uint32_t>(triplet.getMin());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
PsqlBindArray::addMax(const Triplet<uint32_t>& triplet) {
|
||||||
|
if (triplet.unspecified() || (triplet.getMax() == triplet.get())) {
|
||||||
|
addNull();
|
||||||
|
} else {
|
||||||
|
add<uint32_t>(triplet.getMax());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// @todo Eventually this could replace add(std::string&)? This would mean
|
/// @todo Eventually this could replace add(std::string&)? This would mean
|
||||||
/// all bound strings would be internally copies rather than perhaps belonging
|
/// all bound strings would be internally copies rather than perhaps belonging
|
||||||
/// to the originating object such as Host::hostname_. One the one hand it
|
/// to the originating object such as Host::hostname_. One the one hand it
|
||||||
@@ -96,8 +125,9 @@ std::string PsqlBindArray::toText() const {
|
|||||||
std::ostringstream stream;
|
std::ostringstream stream;
|
||||||
for (int i = 0; i < values_.size(); ++i) {
|
for (int i = 0; i < values_.size(); ++i) {
|
||||||
stream << i << " : ";
|
stream << i << " : ";
|
||||||
|
#if 0
|
||||||
if (formats_[i] == TEXT_FMT) {
|
if (formats_[i] == TEXT_FMT) {
|
||||||
stream << "\"" << values_[i] << "\"" << std::endl;
|
stream << "\"" << values_[i] << "\"" << std::endl;
|
||||||
} else {
|
} else {
|
||||||
const char *data = values_[i];
|
const char *data = values_[i];
|
||||||
if (lengths_[i] == 0) {
|
if (lengths_[i] == 0) {
|
||||||
@@ -113,6 +143,26 @@ std::string PsqlBindArray::toText() const {
|
|||||||
stream << std::setbase(10);
|
stream << std::setbase(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
if (lengths_[i] == 0) {
|
||||||
|
stream << "empty" << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formats_[i] == TEXT_FMT) {
|
||||||
|
stream << "\"" << values_[i] << "\"" << std::endl;
|
||||||
|
} else {
|
||||||
|
const char *data = values_[i];
|
||||||
|
stream << "0x";
|
||||||
|
for (int x = 0; x < lengths_[i]; ++x) {
|
||||||
|
stream << std::setfill('0') << std::setw(2)
|
||||||
|
<< std::setbase(16)
|
||||||
|
<< static_cast<unsigned int>(data[x]);
|
||||||
|
}
|
||||||
|
stream << std::endl;
|
||||||
|
stream << std::setbase(10);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return (stream.str());
|
return (stream.str());
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <asiolink/io_address.h>
|
#include <asiolink/io_address.h>
|
||||||
#include <pgsql/pgsql_connection.h>
|
#include <pgsql/pgsql_connection.h>
|
||||||
|
#include <util/triplet.h>
|
||||||
#include <exceptions/exceptions.h>
|
#include <exceptions/exceptions.h>
|
||||||
|
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
@@ -175,10 +176,29 @@ struct PsqlBindArray {
|
|||||||
/// in the SQL statement should be NULL.
|
/// in the SQL statement should be NULL.
|
||||||
void addNull(const int format = PsqlBindArray::TEXT_FMT);
|
void addNull(const int format = PsqlBindArray::TEXT_FMT);
|
||||||
|
|
||||||
//std::vector<const std::string> getBoundStrs() {
|
/// @brief Adds an integer Triplet's value to the bind array
|
||||||
std::vector<ConstStringPtr> getBoundStrs() {
|
///
|
||||||
return (bound_strs_);
|
/// Stores the current value of a triplet to the bind array.
|
||||||
}
|
/// If it is unspecified it stores a NULL.
|
||||||
|
///
|
||||||
|
/// @param triple Triplet instance from which to get the value.
|
||||||
|
void add(const isc::util::Triplet<uint32_t>& triplet);
|
||||||
|
|
||||||
|
/// @brief Adds an integer Triplet's minimum value to the bind array
|
||||||
|
///
|
||||||
|
/// Stores the minimum value of a triplet to the bind array.
|
||||||
|
/// If it is unspecified it stores a NULL.
|
||||||
|
///
|
||||||
|
/// @param triple Triplet instance from which to get the value.
|
||||||
|
void addMin(const isc::util::Triplet<uint32_t>& triplet);
|
||||||
|
|
||||||
|
/// @brief Adds an integer Triplet's maximum value to the bind array
|
||||||
|
///
|
||||||
|
/// Stores the maximum value of a triplet to the bind array.
|
||||||
|
/// If it is unspecified it stores a NULL.
|
||||||
|
///
|
||||||
|
/// @param triple Triplet instance from which to get the value.
|
||||||
|
void addMax(const isc::util::Triplet<uint32_t>& triplet);
|
||||||
|
|
||||||
/// @brief Dumps the contents of the array to a string.
|
/// @brief Dumps the contents of the array to a string.
|
||||||
/// @return std::string containing the dump
|
/// @return std::string containing the dump
|
||||||
|
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
using namespace isc;
|
using namespace isc;
|
||||||
using namespace isc::db;
|
using namespace isc::db;
|
||||||
|
using namespace isc::util;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -72,6 +73,9 @@ TEST(PsqlBindArray, addDataTest) {
|
|||||||
|
|
||||||
// Add the vector
|
// Add the vector
|
||||||
b.add(bytes);
|
b.add(bytes);
|
||||||
|
|
||||||
|
// Add an empty string.
|
||||||
|
b.add(std::string(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
// We've left bind scope, everything should be intact.
|
// We've left bind scope, everything should be intact.
|
||||||
@@ -86,7 +90,42 @@ TEST(PsqlBindArray, addDataTest) {
|
|||||||
"7 : \"FALSE\"\n"
|
"7 : \"FALSE\"\n"
|
||||||
"8 : \"3221360418\"\n"
|
"8 : \"3221360418\"\n"
|
||||||
"9 : \"3001::1\"\n"
|
"9 : \"3001::1\"\n"
|
||||||
"10 : 0x0102030405060708090a\n";
|
"10 : 0x0102030405060708090a\n"
|
||||||
|
"11 : empty\n";
|
||||||
|
|
||||||
|
EXPECT_EQ(expected, b.toText());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Verifies the ability to add Triplets to
|
||||||
|
/// the bind array.
|
||||||
|
TEST(PsqlBindArray, addTriplet) {
|
||||||
|
|
||||||
|
PsqlBindArray b;
|
||||||
|
|
||||||
|
// Add all the items within a different scope. Everything should
|
||||||
|
// still be valid once we exit this scope.
|
||||||
|
{
|
||||||
|
Triplet<uint32_t> empty;
|
||||||
|
Triplet<uint32_t> not_empty(1,2,3);
|
||||||
|
|
||||||
|
// Add an unspecified triplet value.
|
||||||
|
b.add(empty);
|
||||||
|
b.add(not_empty);
|
||||||
|
b.addMin(empty);
|
||||||
|
b.addMin(not_empty);
|
||||||
|
b.addMax(empty);
|
||||||
|
b.addMax(not_empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
// We've left bind scope, everything should be intact.
|
||||||
|
EXPECT_EQ(6, b.size());
|
||||||
|
std::string expected =
|
||||||
|
"0 : empty\n"
|
||||||
|
"1 : \"2\"\n"
|
||||||
|
"2 : empty\n"
|
||||||
|
"3 : \"1\"\n"
|
||||||
|
"4 : empty\n"
|
||||||
|
"5 : \"3\"\n";
|
||||||
|
|
||||||
EXPECT_EQ(expected, b.toText());
|
EXPECT_EQ(expected, b.toText());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user