]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1848] Added support for storing Triplets to PsqlBindArray
authorThomas Markwalder <tmark@isc.org>
Wed, 10 Nov 2021 16:17:11 +0000 (11:17 -0500)
committerTomek Mrugalski <tomek@isc.org>
Wed, 17 Nov 2021 14:35:19 +0000 (15:35 +0100)
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

src/lib/pgsql/pgsql_exchange.cc
src/lib/pgsql/pgsql_exchange.h
src/lib/pgsql/tests/pgsql_exchange_unittest.cc

index 2c8d8158d5b56d593829556e147a10ccc8b2c729..75d5fc310f41c6378d7d40294b5e3695dad0d9ff 100644 (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());
index 0535dce605a901aec1862be6abe2a3caa9fd62dd..0df66af5db292dc50d938cf738af435f7b02dab7 100644 (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
index ee59ac7dd43ca9171994d516e9fa7870d518ef10..525d9d982a6fec5f5043d6f47230faebc8686897 100644 (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());
 }