]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2445] implement PostgreSQL lease manager methods for limit checking
authorAndrei Pavel <andrei@isc.org>
Mon, 27 Jun 2022 08:08:33 +0000 (11:08 +0300)
committerAndrei Pavel <andrei@isc.org>
Wed, 29 Jun 2022 12:51:27 +0000 (15:51 +0300)
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

index 368852b740349d783ca6fee3c57ecc59bca8fb97..40ca2cabe12376c6070ca14fde116f06f8b628d4 100644 (file)
@@ -3135,6 +3135,28 @@ MySqlLeaseMgr::checkLimits6(ConstElementPtr const& user_context) const {
     return checkLimits(user_context, CHECK_LEASE6_LIMITS);
 }
 
+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;
+}
+
 LeaseStatsQueryPtr
 MySqlLeaseMgr::startLeaseStatsQuery4() {
     // Get a context
@@ -3231,28 +3253,6 @@ 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 77f384edf2a893112f8d5813f93b99eca3f3874f..cd1db818507154f3418bc894a324a32006d06d13 100644 (file)
@@ -638,12 +638,6 @@ public:
     /// @return number of leases removed.
     virtual size_t wipeLeases6(const SubnetID& subnet_id) override;
 
-    /// @brief Checks if JSON support is enabled in the database.
-    /// MySQL implementation.
-    ///
-    /// @return true if there is JSON support, false otherwise
-    virtual bool isJsonSupported() const override;
-
     /// @brief Return backend type
     ///
     /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
@@ -976,6 +970,12 @@ private:
     virtual std::string
     checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
 
+    /// @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 Check Error and Throw Exception
     ///
     /// This method invokes @ref MySqlConnection::checkError.
index a233db66f0a763577d3552ca997dd5bcb68527b7..045ce7f23811befff0f9d048ff1fc175e48f2da2 100644 (file)
@@ -359,17 +359,20 @@ PgSqlTaggedStatement tagged_statements[] = {
       "  WHERE subnet_id >= $1 and subnet_id <= $2 "
       "  ORDER BY subnet_id, lease_type, state" },
 
-    // TODO: remove single quotes from the following two SELECTs when the functions are implemented
-
     // CHECK_LEASE4_LIMITS
     { 1, { OID_TEXT },
       "check_lease4_limits",
-      "SELECT 'checkLease4Limits($1)'" },
+      "SELECT checkLease4Limits($1)" },
 
     // CHECK_LEASE6_LIMITS
     { 1, { OID_TEXT },
       "check_lease6_limits",
-      "SELECT 'checkLease6Limits($1)'" },
+      "SELECT checkLease6Limits($1)" },
+
+    // IS_JSON_SUPPORTED
+    { 0, { OID_NONE },
+      "is_json_supported",
+      "SELECT isJsonSupported()" },
 
     // End of list sentinel
     { 0,  { 0 }, NULL, NULL}
@@ -2290,31 +2293,29 @@ PgSqlLeaseMgr::deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
 
 string
 PgSqlLeaseMgr::checkLimits(ConstElementPtr const& user_context, StatementIndex const stindex) const {
-    // Bindings
-    PsqlBindArray bind_array;
-    std::string const user_context_str(user_context->str());
-    bind_array.add(user_context_str);
+    // No user context means no limits means allocation allowed means empty string.
+    if (!user_context) {
+        return string();
+    }
 
     // Get a context.
     PgSqlLeaseContextAlloc get_context(*this);
     PgSqlLeaseContextPtr ctx(get_context.ctx_);
 
+    // Create bindings.
+    PsqlBindArray bind_array;
+    std::string const user_context_str(user_context->str());
+    bind_array.add(user_context_str);
+
+    // Execute the select.
     PgSqlResult r(PQexecPrepared(ctx->conn_,
                                  tagged_statements[stindex].name,
                                  tagged_statements[stindex].nbparams,
                                  &bind_array.values_[0],
                                  &bind_array.lengths_[0],
                                  &bind_array.formats_[0], 0));
-
     ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
 
-    int rows = PQntuples(r);
-    if (rows > 1) {
-        isc_throw(MultipleRecords, "multiple records were found in the "
-                      "database where only one was expected for query "
-                      << tagged_statements[stindex].name);
-    }
-
     std::string const limits(PgSqlExchange::getRawColumnValue(r, 0, 0));
     return limits;
 }
@@ -2329,6 +2330,22 @@ PgSqlLeaseMgr::checkLimits6(ConstElementPtr const& user_context) const {
     return checkLimits(user_context, CHECK_LEASE6_LIMITS);
 }
 
+bool
+PgSqlLeaseMgr::isJsonSupported() const {
+    // Get a context.
+    PgSqlLeaseContextAlloc get_context(*this);
+    PgSqlLeaseContextPtr ctx(get_context.ctx_);
+
+    // Execute the select.
+    StatementIndex const stindex(IS_JSON_SUPPORTED);
+    PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
+                                 0, 0, 0, 0, 0));
+    ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
+
+    bool const json_supported(PgSqlExchange::getRawColumnValue(r, 0, 0));
+    return json_supported;
+}
+
 LeaseStatsQueryPtr
 PgSqlLeaseMgr::startLeaseStatsQuery4() {
     // Get a context
@@ -2425,11 +2442,6 @@ 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 66522fa678091bed28aba0f57f72072498562149..81d831cc844e62953ce508d15031745aebeeac02 100644 (file)
@@ -613,12 +613,6 @@ public:
     /// @return number of leases removed.
     virtual size_t wipeLeases6(const SubnetID& subnet_id) override;
 
-    /// @brief Checks if JSON support is enabled in the database.
-    /// PostgreSQL implementation.
-    ///
-    /// @return true if there is JSON support, false otherwise
-    virtual bool isJsonSupported() const override;
-
     /// @brief Return backend type
     ///
     /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
@@ -707,6 +701,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
     };
 
@@ -949,6 +944,12 @@ private:
     virtual std::string
     checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
 
+    /// @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 Context RAII Allocator.
     class PgSqlLeaseContextAlloc {
     public: