From: Thomas Markwalder Date: Wed, 8 Dec 2021 18:48:20 +0000 (-0500) Subject: [#2183] Added convenience method for fetching ptimes X-Git-Tag: Kea-2.1.2~174 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad2ecab51d53c84557c8b9ee8e4c18efe5d4b28d;p=thirdparty%2Fkea.git [#2183] Added convenience method for fetching ptimes 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 --- diff --git a/src/lib/pgsql/pgsql_exchange.cc b/src/lib/pgsql/pgsql_exchange.cc index 939cf0fd92..37438a59bc 100644 --- a/src/lib/pgsql/pgsql_exchange.cc +++ b/src/lib/pgsql/pgsql_exchange.cc @@ -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) { diff --git a/src/lib/pgsql/pgsql_exchange.h b/src/lib/pgsql/pgsql_exchange.h index b44f8a49f6..d67260a359 100644 --- a/src/lib/pgsql/pgsql_exchange.h +++ b/src/lib/pgsql/pgsql_exchange.h @@ -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 diff --git a/src/lib/pgsql/tests/pgsql_exchange_unittest.cc b/src/lib/pgsql/tests/pgsql_exchange_unittest.cc index 80d694323c..2dd79d6b7e 100644 --- a/src/lib/pgsql/tests/pgsql_exchange_unittest.cc +++ b/src/lib/pgsql/tests/pgsql_exchange_unittest.cc @@ -240,7 +240,7 @@ TEST(PsqlBindArray, addOptionalIPv4Address) { // Verify we cannot add a v6 address. Optional 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); }