]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2438] add MySqlLeaseMgr::isJsonSupported()
authorAndrei Pavel <andrei@isc.org>
Tue, 21 Jun 2022 19:42:46 +0000 (22:42 +0300)
committerAndrei Pavel <andrei@isc.org>
Wed, 22 Jun 2022 12:18:08 +0000 (15:18 +0300)
src/lib/dhcpsrv/lease_mgr.h
src/lib/dhcpsrv/memfile_lease_mgr.cc
src/lib/dhcpsrv/memfile_lease_mgr.h
src/lib/dhcpsrv/mysql_lease_mgr.cc
src/lib/dhcpsrv/mysql_lease_mgr.h
src/lib/dhcpsrv/pgsql_lease_mgr.cc
src/lib/dhcpsrv/pgsql_lease_mgr.h
src/lib/dhcpsrv/tests/lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc
src/lib/mysql/mysql_binding.cc
src/lib/mysql/mysql_binding.h

index 6f19c6348502e695dad2881207815f0ef4976e19..2e959782215ef641e61dbc321ac72b3bf059bba0 100644 (file)
@@ -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.)
index c0e20c127c80fd9e33f2775da57c199013662308..dbe9722a5503ee66bde903909c11f79803527317 100644 (file)
@@ -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
-
index 15be0cb564c06961f51fb4efea999293763fcab9..b566cc5a2a8979ec08a72c245861beab41d0ddbf 100644 (file)
@@ -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
index e71c252c65e21dff954197a4d3097fb8727c0a0a..de24ff81b33064014e626df847e8f26d6f172d0c 100644 (file)
@@ -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
index 562310284177405a4ae74991db3772afc8306069..5acc10a4962820bb72bbbb7b8df35d47a50dcf72 100644 (file)
@@ -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
     };
 
index e702324f0a787acfaeb46415ba51a525843e9ca0..a233db66f0a763577d3552ca997dd5bcb68527b7 100644 (file)
@@ -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
index 3abd14c9d1a76a3669feeae5452267982cb4dbd2..e96cf885ce6db362cc4503ef5198c9491fc1ff09 100644 (file)
@@ -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.)
index 0fce71ec0258e6b8d3a7be0c216ae89bf4c775e7..12cd6b4562ca0a4b30a6160e96f7a9a3e74dcb66 100644 (file)
@@ -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.)
index 31d52cf7b51533523fa78916e02a63da642737ce..add034551064290e55d29c356b4afe3e56c834ce 100644 (file)
@@ -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
index cd8222435e29fffeaab94ed1f28bedad7f298d91..b043225b81c717676b131dd1fb6a29e7bfd9b84f 100644 (file)
@@ -131,6 +131,11 @@ MySqlBinding::createFloat(const float value) {
     return (createInteger<float>(value));
 }
 
+MySqlBindingPtr
+MySqlBinding::createBool() {
+    return (createInteger<uint8_t>(static_cast<uint8_t>(false)));
+}
+
 MySqlBindingPtr
 MySqlBinding::createBool(const bool value) {
     return (createInteger<uint8_t>(static_cast<uint8_t>(value)));
index 3dbca4a1f494609b44e16b5ef7a91d4db8c0a9b1..cafc285825b53fe7b147f4321cfcb8fd77f8dc6f 100644 (file)
@@ -447,6 +447,12 @@ public:
                 createInteger<float> (static_cast<float>(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.