]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2183] Added convenience method for fetching ptimes
authorThomas Markwalder <tmark@isc.org>
Wed, 8 Dec 2021 18:48:20 +0000 (13:48 -0500)
committerThomas Markwalder <tmark@isc.org>
Tue, 21 Dec 2021 18:30:14 +0000 (13:30 -0500)
src/lib/pgsql/pgsql_exchange.*
    PgSqlExchange::getColumnValue(const PgSqlResult& r, const int row,
                                  const size_t col, boost::posix_time::ptime& value);

src/lib/pgsql/tests/pgsql_exchange_unittest.cc
    TEST_F(PgSqlBasicsTest, ptimeTimestamp) - modified to use new method

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

index 939cf0fd92f23befe5bfdf0fe9b7c9154854683c..37438a59bcb47410ace5b2a412cf9a38fb526b37 100644 (file)
@@ -318,6 +318,14 @@ PgSqlExchange::getColumnValue(const PgSqlResult& r, const int row,
     }
 }
 
+void
+PgSqlExchange::getColumnValue(const PgSqlResult& r, const int row,
+                              const size_t col, boost::posix_time::ptime& value) {
+    std::string db_time_val;
+    PgSqlExchange::getColumnValue(r, row, col, db_time_val );
+    PgSqlExchange::convertFromDatabaseTime(db_time_val, value);
+}
+
 isc::asiolink::IOAddress
 PgSqlExchange::getIPv6Value(const PgSqlResult& r, const int row,
                             const size_t col) {
index b44f8a49f6cc0d1aef91c9df88d1496247fc419c..d67260a359326a53159310557a5a067cd853dfaa 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016-2018,2021 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2021 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -583,6 +583,18 @@ public:
         }
     }
 
+    /// @brief Fetches a timestamp column as a ptime.
+    ///
+    /// @param r the result set containing the query results
+    /// @param row the row number within the result set
+    /// @param col the column number within the row
+    /// @param[out] value ptime parameter to receive the converted timestamp
+    ///
+    /// @throw  DbOperationError if the value cannot be fetched or is
+    /// invalid.
+    static void getColumnValue(const PgSqlResult& r, const int row,
+                               const size_t col, boost::posix_time::ptime& value);
+
     /// @brief Converts a column in a row in a result set to a binary bytes
     ///
     /// Method is used to convert columns stored as BYTEA into a buffer of
index 80d694323c4c5ea49fcd571901c3a48e4dab6165..2dd79d6b7e5125c9e5d8201adf9856f3b47844c7 100644 (file)
@@ -240,7 +240,7 @@ TEST(PsqlBindArray, addOptionalIPv4Address) {
 
         // Verify we cannot add a v6 address.
         Optional<asiolink::IOAddress> not_v4(asiolink::IOAddress("3001::1"));
-        ASSERT_THROW_MSG(b.addOptionalIPv4Address(not_v4), BadValue, 
+        ASSERT_THROW_MSG(b.addOptionalIPv4Address(not_v4), BadValue,
                          "unable to add address to PsqlBindAray"
                          " '3001::1' is not an IPv4 address");
 
@@ -931,15 +931,12 @@ TEST_F(PgSqlBasicsTest, ptimeTimestamp) {
     // Fetch the newly inserted row.
     FETCH_ROWS(r, 1);
 
-    // Fetch the timestamp column
+    // Timestamp column should not be null.
     ASSERT_FALSE(PgSqlExchange::isColumnNull(*r, 0, TIMESTAMP_COL));
-    std::string timestamp_str = "";
-    ASSERT_NO_THROW(PgSqlExchange::getColumnValue(*r, 0, TIMESTAMP_COL,
-                                                  timestamp_str));
 
-    // Convert fetched values into a ptime.
+    // Convert fetched value into a ptime.
     ptime fetched_time;
-    ASSERT_NO_THROW(PgSqlExchange::convertFromDatabaseTime(timestamp_str, fetched_time));
+    ASSERT_NO_THROW_LOG(PgSqlExchange::getColumnValue(*r, 0, TIMESTAMP_COL,
 
     ASSERT_EQ(fetched_time, nice_day);
 }