#include <sstream>
#include <vector>
+using namespace isc::util;
+
namespace isc {
namespace db {
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
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) {
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());
#include <asiolink/io_address.h>
#include <pgsql/pgsql_connection.h>
+#include <util/triplet.h>
#include <exceptions/exceptions.h>
#include <boost/lexical_cast.hpp>
/// 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
using namespace isc;
using namespace isc::db;
+using namespace isc::util;
namespace {
// Add the vector
b.add(bytes);
+
+ // Add an empty string.
+ b.add(std::string(""));
}
// We've left bind scope, everything should be intact.
"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());
}