diff --git a/src/lib/dhcpsrv/lease_mgr.h b/src/lib/dhcpsrv/lease_mgr.h index 6f19c63485..2e95978221 100644 --- a/src/lib/dhcpsrv/lease_mgr.h +++ b/src/lib/dhcpsrv/lease_mgr.h @@ -724,6 +724,12 @@ public: /// string if no limits are exceeded virtual std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const = 0; + /// @brief Checks if JSON support is enabled in the database. + /// Abstract method. + /// + /// @return true if there is JSON support, false otherwise + virtual bool isJsonSupported() const = 0; + /// @brief Return backend type /// /// Returns the type of the backend (e.g. "mysql", "memfile" etc.) diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc index c0e20c127c..dbe9722a55 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.cc +++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc @@ -2069,6 +2069,10 @@ Memfile_LeaseMgr::checkLimits6(ConstElementPtr const& /* user_context */) const isc_throw(NotImplemented, "Memfile_LeaseMgr::checkLimits4() not implemented"); } +bool +Memfile_LeaseMgr::isJsonSupported() const { + return true; +} + } // namespace dhcp } // namespace isc - diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.h b/src/lib/dhcpsrv/memfile_lease_mgr.h index 15be0cb564..b566cc5a2a 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.h +++ b/src/lib/dhcpsrv/memfile_lease_mgr.h @@ -530,6 +530,12 @@ public: /// string if no limits are exceeded std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const override; + /// @brief Checks if JSON support is enabled in the database. + /// Memfile implementation assumes JSON support is always enabled. + /// + /// @return true if there is JSON support, false otherwise + bool isJsonSupported() const override; + private: /// @name Internal methods called while holding the mutex in multi threading diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index e71c252c65..de24ff81b3 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -329,6 +329,7 @@ tagged_statements = { { // TODO: remove single quotes from the following two SELECTs when the functions are implemented {MySqlLeaseMgr::CHECK_LEASE4_LIMITS, "SELECT 'checkLease4Limits(?)'"}, {MySqlLeaseMgr::CHECK_LEASE6_LIMITS, "SELECT 'checkLease6Limits(?)'"}, + {MySqlLeaseMgr::IS_JSON_SUPPORTED, "SELECT isJsonSupported()"}, } }; // tagged_statements } // namespace @@ -3278,6 +3279,28 @@ MySqlLeaseMgr::wipeLeases6(const SubnetID& /*subnet_id*/) { isc_throw(NotImplemented, "wipeLeases6 is not implemented for MySQL backend"); } +bool +MySqlLeaseMgr::isJsonSupported() const { + // Get a context. + MySqlLeaseContextAlloc get_context(*this); + MySqlLeaseContextPtr ctx = get_context.ctx_; + + // Create bindings. + MySqlBindingCollection in_bindings; + MySqlBindingCollection out_bindings({ + MySqlBinding::createBool() + }); + + // Execute the select. + bool json_supported(false); + ctx->conn_.selectQuery(IS_JSON_SUPPORTED, in_bindings, out_bindings, + [&json_supported] (MySqlBindingCollection const& result) { + json_supported = result[0]->getBool(); + }); + + return json_supported; +} + // Miscellaneous database methods. std::string diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 5623102841..5acc10a496 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -638,6 +638,12 @@ public: /// @return number of leases removed. virtual size_t wipeLeases6(const SubnetID& subnet_id); + /// @brief Checks if JSON support is enabled in the database. + /// MySQL implementation. + /// + /// @return true if there is JSON support, false otherwise + bool isJsonSupported() const override; + /// @brief Return backend type /// /// Returns the type of the backend (e.g. "mysql", "memfile" etc.) @@ -726,6 +732,7 @@ public: SUBNET_RANGE_LEASE6_STATS, // Fetched IPv6 lease stats for a subnet range. CHECK_LEASE4_LIMITS, // Check if allocated IPv4 leases are inside the set limits. CHECK_LEASE6_LIMITS, // Check if allocated IPv6 leases are inside the set limits. + IS_JSON_SUPPORTED, // Checks if JSON support is enabled in the database. NUM_STATEMENTS // Number of statements }; diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index e702324f0a..a233db66f0 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -2425,6 +2425,11 @@ PgSqlLeaseMgr::wipeLeases6(const SubnetID& /*subnet_id*/) { isc_throw(NotImplemented, "wipeLeases6 is not implemented for PostgreSQL backend"); } +bool +PgSqlLeaseMgr::isJsonSupported() const { + isc_throw(NotImplemented, "PgSqlLeaseMgr::isJsonSupported() not implemented"); +} + std::string PgSqlLeaseMgr::getName() const { // Get a context diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index 3abd14c9d1..e96cf885ce 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -613,6 +613,12 @@ public: /// @return number of leases removed. virtual size_t wipeLeases6(const SubnetID& subnet_id); + /// @brief Checks if JSON support is enabled in the database. + /// PostgreSQL implementation. + /// + /// @return true if there is JSON support, false otherwise + bool isJsonSupported() const override; + /// @brief Return backend type /// /// Returns the type of the backend (e.g. "mysql", "memfile" etc.) diff --git a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc index 0fce71ec02..12cd6b4562 100644 --- a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc @@ -369,6 +369,13 @@ public: isc_throw(NotImplemented, "ConcreteLeaseMgr::checkLimits6() not implemented"); } + /// @brief Checks if JSON support is enabled in the database. + /// + /// @return true if there is JSON support, false otherwise + bool isJsonSupported() const override { + isc_throw(NotImplemented, "ConcreteLeaseMgr::isJsonSupported() not implemented"); + } + /// @brief Returns backend type. /// /// Returns the type of the backend (e.g. "mysql", "memfile" etc.) diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index 31d52cf7b5..add0345510 100644 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -1072,4 +1072,13 @@ TEST_F(MySqlLeaseMgrTest, leaseStatsQueryAttribution6MultiThreading) { testLeaseStatsQueryAttribution6(); } +/// @brief Checks that no exceptions are thrown when inquiring about JSON +/// support and prints an informative message. +TEST_F(MySqlLeaseMgrTest, isJsonSupported) { + bool json_supported; + ASSERT_NO_THROW(json_supported = LeaseMgrFactory::instance().isJsonSupported()); + std::cout << "JSON support is " << (json_supported ? "" : "not ") << + "enabled in the database." << std::endl; +} + } // namespace diff --git a/src/lib/mysql/mysql_binding.cc b/src/lib/mysql/mysql_binding.cc index cd8222435e..b043225b81 100644 --- a/src/lib/mysql/mysql_binding.cc +++ b/src/lib/mysql/mysql_binding.cc @@ -131,6 +131,11 @@ MySqlBinding::createFloat(const float value) { return (createInteger(value)); } +MySqlBindingPtr +MySqlBinding::createBool() { + return (createInteger(static_cast(false))); +} + MySqlBindingPtr MySqlBinding::createBool(const bool value) { return (createInteger(static_cast(value))); diff --git a/src/lib/mysql/mysql_binding.h b/src/lib/mysql/mysql_binding.h index 3dbca4a1f4..cafc285825 100644 --- a/src/lib/mysql/mysql_binding.h +++ b/src/lib/mysql/mysql_binding.h @@ -447,6 +447,12 @@ public: createInteger (static_cast(value.get()))); } + /// @brief Creates binding having a bool type for receiving data. + /// + /// @return Pointer to the created binding holding an @c uint8_t + /// value representing the boolean value. + static MySqlBindingPtr createBool(); + /// @brief Creates binding having a bool type for sending data. /// /// @param value Boolean value to be sent to the database.