2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 21:45:37 +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:
Thomas Markwalder
2021-11-10 11:17:11 -05:00
committed by Tomek Mrugalski
parent fe31eee4ed
commit 4cd9f584b7
3 changed files with 115 additions and 6 deletions

View File

@@ -16,6 +16,8 @@
#include <sstream>
#include <vector>
using namespace isc::util;
namespace isc {
namespace db {
@@ -82,6 +84,33 @@ void PsqlBindArray::addNull(const int 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
/// all bound strings would be internally copies rather than perhaps belonging
/// 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;
for (int i = 0; i < values_.size(); ++i) {
stream << i << " : ";
#if 0
if (formats_[i] == TEXT_FMT) {
stream << "\"" << values_[i] << "\"" << std::endl;
stream << "\"" << values_[i] << "\"" << std::endl;
} else {
const char *data = values_[i];
if (lengths_[i] == 0) {
@@ -113,6 +143,26 @@ std::string PsqlBindArray::toText() const {
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());

View File

@@ -9,6 +9,7 @@
#include <asiolink/io_address.h>
#include <pgsql/pgsql_connection.h>
#include <util/triplet.h>
#include <exceptions/exceptions.h>
#include <boost/lexical_cast.hpp>
@@ -175,10 +176,29 @@ struct PsqlBindArray {
/// in the SQL statement should be NULL.
void addNull(const int format = PsqlBindArray::TEXT_FMT);
//std::vector<const std::string> getBoundStrs() {
std::vector<ConstStringPtr> getBoundStrs() {
return (bound_strs_);
}
/// @brief Adds an integer Triplet's value to the bind array
///
/// 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.
/// @return std::string containing the dump

View File

@@ -19,6 +19,7 @@
using namespace isc;
using namespace isc::db;
using namespace isc::util;
namespace {
@@ -72,6 +73,9 @@ TEST(PsqlBindArray, addDataTest) {
// Add the vector
b.add(bytes);
// Add an empty string.
b.add(std::string(""));
}
// We've left bind scope, everything should be intact.
@@ -86,7 +90,42 @@ TEST(PsqlBindArray, addDataTest) {
"7 : \"FALSE\"\n"
"8 : \"3221360418\"\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());
}