From: Thomas Markwalder Date: Wed, 10 Nov 2021 16:17:11 +0000 (-0500) Subject: [#1848] Added support for storing Triplets to PsqlBindArray X-Git-Tag: Kea-2.1.1~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4cd9f584b7f320c3ce0dcbb613c78937954bcd92;p=thirdparty%2Fkea.git [#1848] Added support for storing Triplets to PsqlBindArray src/lib/pgsql/pgsql_exchange.* PsqlBindArray::add(const Triplet& triplet) PsqlBindArray::addMin(const Triplet& triplet) PsqlBindArray::addMax(const Triplet& triplet) - new functions for storing Triplets src/lib/pgsql/tests/pgsql_exchange_unittest.cc TEST(PsqlBindArray, addTriplet) - new test --- diff --git a/src/lib/pgsql/pgsql_exchange.cc b/src/lib/pgsql/pgsql_exchange.cc index 2c8d8158d5..75d5fc310f 100644 --- a/src/lib/pgsql/pgsql_exchange.cc +++ b/src/lib/pgsql/pgsql_exchange.cc @@ -16,6 +16,8 @@ #include #include +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& triplet) { + if (triplet.unspecified()) { + addNull(); + } else { + add(triplet.get()); + } +} + +void +PsqlBindArray::addMin(const Triplet& triplet) { + if (triplet.unspecified() || (triplet.getMin() == triplet.get())) { + addNull(); + } else { + add(triplet.getMin()); + } +} + +void +PsqlBindArray::addMax(const Triplet& triplet) { + if (triplet.unspecified() || (triplet.getMax() == triplet.get())) { + addNull(); + } else { + add(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(data[x]); + } + stream << std::endl; + stream << std::setbase(10); + } +#endif } return (stream.str()); diff --git a/src/lib/pgsql/pgsql_exchange.h b/src/lib/pgsql/pgsql_exchange.h index 0535dce605..0df66af5db 100644 --- a/src/lib/pgsql/pgsql_exchange.h +++ b/src/lib/pgsql/pgsql_exchange.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -175,10 +176,29 @@ struct PsqlBindArray { /// in the SQL statement should be NULL. void addNull(const int format = PsqlBindArray::TEXT_FMT); - //std::vector getBoundStrs() { - std::vector 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& 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& 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& triplet); /// @brief Dumps the contents of the array to a string. /// @return std::string containing the dump diff --git a/src/lib/pgsql/tests/pgsql_exchange_unittest.cc b/src/lib/pgsql/tests/pgsql_exchange_unittest.cc index ee59ac7dd4..525d9d982a 100644 --- a/src/lib/pgsql/tests/pgsql_exchange_unittest.cc +++ b/src/lib/pgsql/tests/pgsql_exchange_unittest.cc @@ -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 empty; + Triplet 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()); }