]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2166] Added pgsql session value fetch functions
authortmark <tmark@isc.org>
Tue, 2 Nov 2021 19:02:01 +0000 (15:02 -0400)
committerTomek Mrugalski <tomek@isc.org>
Tue, 9 Nov 2021 19:26:35 +0000 (19:26 +0000)
src/share/database/scripts/pgsql/dhcpdb_create.pgsql
src/share/database/scripts/pgsql/upgrade_6.2_to_7.0.sh.in
src/share/database/scripts/pgsql/dhcpdb_drop.pgsql
    Added functions for getting config session
    values that handle unset and NULL values

src/share/database/scripts/pgsql/dhcpdb_create.pgsql
src/share/database/scripts/pgsql/dhcpdb_drop.pgsql
src/share/database/scripts/pgsql/upgrade_6.2_to_7.0.sh.in

index d832b1f77e7bd807c50f70947ecf1c2ae2ae39c4..f46e5c4d86e589c0ccf4fe6e884f4662e42fb119 100644 (file)
@@ -1715,6 +1715,79 @@ CREATE TRIGGER dhcp4_audit_modification_ts_update
 CREATE INDEX dhcp4_audit_idx1 ON dhcp4_audit (modification_type);
 CREATE INDEX dhcp4_audit_idx2 ON dhcp4_audit (revision_id);
 
+-- Fetches a text value from the session configuration.
+-- param name name of the session variable to fetch
+-- If the name is not found it returns NULL.
+-- Postgresql allows you to store custom session values
+-- but throws an exception if they have not first been
+-- set.  This allows us to be a bit more graceful.
+CREATE OR REPLACE FUNCTION get_session_value(name text)
+RETURNS TEXT
+AS $$
+DECLARE
+    text_value TEXT := '';
+BEGIN
+    text_value = current_setting(name);
+    RETURN(text_value);
+
+    EXCEPTION
+    WHEN OTHERS THEN
+        RAISE NOTICE 'get_session_value(%) failed, sqlstate: %', name, sqlstate;
+        RETURN NULL;
+END;$$
+LANGUAGE plpgsql;
+
+-- Fetches an BIGINT value from the session configuration.
+-- param name name of the session variable to fetch
+-- If the name is not found it returns zero.
+CREATE OR REPLACE FUNCTION get_session_big_int(name text)
+RETURNS BIGINT
+AS $$
+DECLARE
+    int_value BIGINT := 0;
+    text_value TEXT := '';
+BEGIN
+    text_value = get_session_value(name);
+    IF text_value is NULL or text_value = '' THEN
+        RETURN(0);
+    END IF;
+   
+    int_value = cast(text_value as BIGINT); 
+    RETURN(int_value);
+
+    EXCEPTION
+    WHEN OTHERS THEN
+        RAISE EXCEPTION 'get_session_big_int(%) failed - text:[%] , sqlstate: %', name, text_value, sqlstate;
+
+END;$$
+LANGUAGE plpgsql;
+
+
+-- Fetches an SMALLINT value from the session configuration.
+-- param name name of the session variable to fetch
+-- If the name is not found it returns zero.
+CREATE OR REPLACE FUNCTION get_session_small_int(name text)
+RETURNS SMALLINT
+AS $$
+DECLARE
+    int_value SMALLINT := 0;
+    text_value TEXT := '';
+BEGIN
+    text_value = get_session_value(name);
+    IF text_value is NULL or text_value = '' THEN
+        RETURN(0);
+    END IF;
+   
+    int_value = cast(text_value as SMALLINT); 
+    RETURN(int_value);
+
+    EXCEPTION
+    WHEN OTHERS THEN
+        RAISE EXCEPTION 'get_session_small_int(%) failed - text:[%] , sqlstate: %', name, text_value, sqlstate;
+
+END;$$
+LANGUAGE plpgsql;
+
 -- -----------------------------------------------------
 -- Stored procedure which creates a new entry in the
 -- dhcp4_audit_revision table and sets appropriate session
@@ -1760,9 +1833,8 @@ DECLARE
     srv_id BIGINT;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
-
-    IF disable_audit IS NULL OR disable_audit = 0 THEN
+    disable_audit := get_session_small_int('kea.disable_audit');
+    IF disable_audit = 0 THEN
         SELECT id INTO STRICT srv_id FROM dhcp4_server WHERE tag = server_tag;
         INSERT INTO dhcp4_audit_revision (modification_ts, server_id, log_message)
             VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
@@ -1805,11 +1877,11 @@ DECLARE
     disable_audit SMALLINT := 0;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
+    disable_audit := get_session_small_int('kea.disable_audit');
 
     IF disable_audit IS NULL OR disable_audit = 0 THEN
         -- Fetch session value most recently created audit_revision_id.
-        audit_revision_id := current_setting('kea.audit_revision_id');
+        audit_revision_id := get_session_big_int('kea.audit_revision_id');
         INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id)
             VALUES (object_type_val, object_id_val,
                 (SELECT id FROM modification WHERE modification_type = modification_type_val),
@@ -2272,7 +2344,7 @@ DECLARE
 BEGIN
 
     -- Session variables are set upon a client class update.
-    client_class_id := current_setting('kea.client_class_id');
+    client_class_id := get_session_big_int('kea.client_class_id');
     IF client_class_id IS NOT NULL THEN
         -- Check if any of the classes depend on this class. If not,
         -- it is ok to change the dependency on KNOWN/UNKNOWN.
@@ -2282,8 +2354,8 @@ BEGIN
         ) THEN
             -- Using the session variables, determine whether the client class
             -- depended on KNOWN/UNKNOWN before the update.
-            depend_on_known_directly := current_setting('kea.depend_on_known_directly');
-            depend_on_known_indirectly := current_setting('kea.depend_on_known_indirectly');
+            depend_on_known_directly := get_session_small_int('kea.depend_on_known_directly');
+            depend_on_known_indirectly := get_session_small_int('kea.depend_on_known_indirectly');
             IF depend_on_known_directly <> 0 OR depend_on_known_indirectly <> 0 THEN
                 SET depended = 1;
             END IF;
@@ -2370,9 +2442,8 @@ DECLARE
     srv_id BIGINT;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
-
-    IF disable_audit IS NULL OR disable_audit = 0 THEN
+    disable_audit := get_session_small_int('kea.disable_audit');
+    IF disable_audit = 0 THEN
         SELECT id INTO STRICT srv_id FROM dhcp6_server WHERE tag = server_tag;
         INSERT INTO dhcp6_audit_revision (modification_ts, server_id, log_message)
             VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
@@ -2415,11 +2486,10 @@ DECLARE
     disable_audit SMALLINT := 0;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
-
-    IF disable_audit IS NULL OR disable_audit = 0 THEN
+    disable_audit := get_session_small_int('kea.disable_audit');
+    IF disable_audit = 0 THEN
         -- Fetch session value most recently created audit_revision_id.
-        audit_revision_id := current_setting('kea.audit_revision_id');
+        audit_revision_id := get_session_big_int('kea.audit_revision_id');
         INSERT INTO dhcp6_audit (object_type, object_id, modification_type, revision_id)
             VALUES (object_type_val, object_id_val,
                 (SELECT id FROM modification WHERE modification_type = modification_type_val),
@@ -2877,7 +2947,7 @@ DECLARE
 BEGIN
 
     -- Session variables are set upon a client class update.
-    client_class_id := current_setting('kea.client_class_id');
+    client_class_id := get_session_big_int('kea.client_class_id');
     IF client_class_id IS NOT NULL THEN
         -- Check if any of the classes depend on this class. If not,
         -- it is ok to change the dependency on KNOWN/UNKNOWN.
@@ -2887,8 +2957,8 @@ BEGIN
         ) THEN
             -- Using the session variables, determine whether the client class
             -- depended on KNOWN/UNKNOWN before the update.
-            depend_on_known_directly := current_setting('kea.depend_on_known_directly');
-            depend_on_known_indirectly := current_setting('kea.depend_on_known_indirectly');
+            depend_on_known_directly := get_session_small_int('kea.depend_on_known_directly');
+            depend_on_known_indirectly := get_session_small_int('kea.depend_on_known_indirectly');
             IF depend_on_known_directly <> 0 OR depend_on_known_indirectly <> 0 THEN
                 SET depended = 1;
             END IF;
@@ -3143,6 +3213,7 @@ DECLARE
     snid VARCHAR(128);
     sid BIGINT;
     cascade_transaction SMALLINT := 0;
+    ct TEXT;
 BEGIN
     -- Cascade transaction flag is set to 1 to prevent creation of
     -- the audit entries for the options when the options are
@@ -3156,8 +3227,9 @@ BEGIN
     -- entire subnet. The only case when the object_type will be
     -- set to 'dhcp4_options' is when a global option is added.
     -- Global options do not have the owner.
-    cascade_transaction := current_setting('kea.cascade_transaction');
-    IF cascade_transaction IS NULL OR cascade_transaction = 0 THEN
+
+    cascade_transaction := get_session_small_int('kea.cascade_transaction');
+    IF cascade_transaction = 0 THEN
         -- todo: host manager hasn't been updated to use audit
         -- mechanisms so ignore host specific options for now.
         IF scope_id = 0 THEN
@@ -3419,8 +3491,8 @@ BEGIN
     -- entire subnet. The only case when the object_type will be
     -- set to 'dhcp6_options' is when a global option is added.
     -- Global options do not have the owner.
-    cascade_transaction := current_setting('kea.cascade_transaction');
-    IF cascade_transaction IS NULL OR cascade_transaction = 0 THEN
+    cascade_transaction := get_session_small_int('kea.cascade_transaction');
+    IF cascade_transaction = 0 THEN
         -- todo: host manager hasn't been updated to use audit
         -- mechanisms so ignore host specific options for now.
         IF scope_id = 0 THEN
index d15b458e5ebf7881b9eacd8ab52695558d219897..474299671e2ef278d990b898a9be1cad4678d490 100644 (file)
@@ -162,4 +162,8 @@ DROP TABLE IF EXISTS modification CASCADE;
 DROP TABLE IF EXISTS parameter_data_type CASCADE;
 DROP TABLE IF EXISTS ddns_replace_client_name_types CASCADE;
 DROP FUNCTION IF EXISTS modification_ts_update();
+DROP FUNCTION IF EXISTS get_session_small_int(name text);
+DROP FUNCTION IF EXISTS get_session_big_int(name text);
+DROP FUNCTION IF EXISTS get_session_value(name text);
+
 
index c49a317aa8d72b272bba47980c0d5be47e40328d..d1eaa4ef1df44d4cf723c3c7830504f3235356cc 100644 (file)
@@ -721,6 +721,79 @@ CREATE TRIGGER dhcp4_audit_modification_ts_update
 CREATE INDEX dhcp4_audit_idx1 ON dhcp4_audit (modification_type);
 CREATE INDEX dhcp4_audit_idx2 ON dhcp4_audit (revision_id);
 
+-- Fetches a text value from the session configuration.
+-- param name name of the session variable to fetch
+-- If the name is not found it returns NULL.
+-- Postgresql allows you to store custom session values
+-- but throws an exception if they have not first been
+-- set.  This allows us to be a bit more graceful.
+CREATE OR REPLACE FUNCTION get_session_value(name text)
+RETURNS TEXT
+AS \$\$
+DECLARE
+    text_value TEXT := '';
+BEGIN
+    text_value = current_setting(name);
+    RETURN(text_value);
+
+    EXCEPTION
+    WHEN OTHERS THEN
+        RAISE NOTICE 'get_session_value(%) failed, sqlstate: %', name, sqlstate;
+        RETURN NULL;
+END;\$\$
+LANGUAGE plpgsql;
+
+-- Fetches an BIGINT value from the session configuration.
+-- param name name of the session variable to fetch
+-- If the name is not found it returns zero.
+CREATE OR REPLACE FUNCTION get_session_big_int(name text)
+RETURNS BIGINT
+AS \$\$
+DECLARE
+    int_value BIGINT := 0;
+    text_value TEXT := '';
+BEGIN
+    text_value = get_session_value(name);
+    IF text_value is NULL or text_value = '' THEN
+        RETURN(0);
+    END IF;
+   
+    int_value = cast(text_value as BIGINT); 
+    RETURN(int_value);
+
+    EXCEPTION
+    WHEN OTHERS THEN
+        RAISE EXCEPTION 'get_session_big_int(%) failed - text:[%] , sqlstate: %', name, text_value, sqlstate;
+
+END;\$\$
+LANGUAGE plpgsql;
+
+
+-- Fetches an SMALLINT value from the session configuration.
+-- param name name of the session variable to fetch
+-- If the name is not found it returns zero.
+CREATE OR REPLACE FUNCTION get_session_small_int(name text)
+RETURNS SMALLINT
+AS \$\$
+DECLARE
+    int_value SMALLINT := 0;
+    text_value TEXT := '';
+BEGIN
+    text_value = get_session_value(name);
+    IF text_value is NULL or text_value = '' THEN
+        RETURN(0);
+    END IF;
+   
+    int_value = cast(text_value as SMALLINT); 
+    RETURN(int_value);
+
+    EXCEPTION
+    WHEN OTHERS THEN
+        RAISE EXCEPTION 'get_session_small_int(%) failed - text:[%] , sqlstate: %', name, text_value, sqlstate;
+
+END;\$\$
+LANGUAGE plpgsql;
+
 -- -----------------------------------------------------
 -- Stored procedure which creates a new entry in the
 -- dhcp4_audit_revision table and sets appropriate session
@@ -766,9 +839,8 @@ DECLARE
     srv_id BIGINT;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
-
-    IF disable_audit IS NULL OR disable_audit = 0 THEN
+    disable_audit := get_session_small_int('kea.disable_audit');
+    IF disable_audit = 0 THEN
         SELECT id INTO STRICT srv_id FROM dhcp4_server WHERE tag = server_tag;
         INSERT INTO dhcp4_audit_revision (modification_ts, server_id, log_message)
             VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
@@ -811,11 +883,11 @@ DECLARE
     disable_audit SMALLINT := 0;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
+    disable_audit := get_session_small_int('kea.disable_audit');
 
     IF disable_audit IS NULL OR disable_audit = 0 THEN
         -- Fetch session value most recently created audit_revision_id.
-        audit_revision_id := current_setting('kea.audit_revision_id');
+        audit_revision_id := get_session_big_int('kea.audit_revision_id');
         INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id)
             VALUES (object_type_val, object_id_val,
                 (SELECT id FROM modification WHERE modification_type = modification_type_val),
@@ -1278,7 +1350,7 @@ DECLARE
 BEGIN
 
     -- Session variables are set upon a client class update.
-    client_class_id := current_setting('kea.client_class_id');
+    client_class_id := get_session_big_int('kea.client_class_id');
     IF client_class_id IS NOT NULL THEN
         -- Check if any of the classes depend on this class. If not,
         -- it is ok to change the dependency on KNOWN/UNKNOWN.
@@ -1288,8 +1360,8 @@ BEGIN
         ) THEN
             -- Using the session variables, determine whether the client class
             -- depended on KNOWN/UNKNOWN before the update.
-            depend_on_known_directly := current_setting('kea.depend_on_known_directly');
-            depend_on_known_indirectly := current_setting('kea.depend_on_known_indirectly');
+            depend_on_known_directly := get_session_small_int('kea.depend_on_known_directly');
+            depend_on_known_indirectly := get_session_small_int('kea.depend_on_known_indirectly');
             IF depend_on_known_directly <> 0 OR depend_on_known_indirectly <> 0 THEN
                 SET depended = 1;
             END IF;
@@ -1376,9 +1448,8 @@ DECLARE
     srv_id BIGINT;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
-
-    IF disable_audit IS NULL OR disable_audit = 0 THEN
+    disable_audit := get_session_small_int('kea.disable_audit');
+    IF disable_audit = 0 THEN
         SELECT id INTO STRICT srv_id FROM dhcp6_server WHERE tag = server_tag;
         INSERT INTO dhcp6_audit_revision (modification_ts, server_id, log_message)
             VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
@@ -1421,11 +1492,10 @@ DECLARE
     disable_audit SMALLINT := 0;
 BEGIN
     -- Fetch session value for disable_audit.
-    disable_audit := current_setting('kea.disable_audit');
-
-    IF disable_audit IS NULL OR disable_audit = 0 THEN
+    disable_audit := get_session_small_int('kea.disable_audit');
+    IF disable_audit = 0 THEN
         -- Fetch session value most recently created audit_revision_id.
-        audit_revision_id := current_setting('kea.audit_revision_id');
+        audit_revision_id := get_session_big_int('kea.audit_revision_id');
         INSERT INTO dhcp6_audit (object_type, object_id, modification_type, revision_id)
             VALUES (object_type_val, object_id_val,
                 (SELECT id FROM modification WHERE modification_type = modification_type_val),
@@ -1883,7 +1953,7 @@ DECLARE
 BEGIN
 
     -- Session variables are set upon a client class update.
-    client_class_id := current_setting('kea.client_class_id');
+    client_class_id := get_session_big_int('kea.client_class_id');
     IF client_class_id IS NOT NULL THEN
         -- Check if any of the classes depend on this class. If not,
         -- it is ok to change the dependency on KNOWN/UNKNOWN.
@@ -1893,8 +1963,8 @@ BEGIN
         ) THEN
             -- Using the session variables, determine whether the client class
             -- depended on KNOWN/UNKNOWN before the update.
-            depend_on_known_directly := current_setting('kea.depend_on_known_directly');
-            depend_on_known_indirectly := current_setting('kea.depend_on_known_indirectly');
+            depend_on_known_directly := get_session_small_int('kea.depend_on_known_directly');
+            depend_on_known_indirectly := get_session_small_int('kea.depend_on_known_indirectly');
             IF depend_on_known_directly <> 0 OR depend_on_known_indirectly <> 0 THEN
                 SET depended = 1;
             END IF;
@@ -2149,6 +2219,7 @@ DECLARE
     snid VARCHAR(128);
     sid BIGINT;
     cascade_transaction SMALLINT := 0;
+    ct TEXT;
 BEGIN
     -- Cascade transaction flag is set to 1 to prevent creation of
     -- the audit entries for the options when the options are
@@ -2162,8 +2233,9 @@ BEGIN
     -- entire subnet. The only case when the object_type will be
     -- set to 'dhcp4_options' is when a global option is added.
     -- Global options do not have the owner.
-    cascade_transaction := current_setting('kea.cascade_transaction');
-    IF cascade_transaction IS NULL OR cascade_transaction = 0 THEN
+
+    cascade_transaction := get_session_small_int('kea.cascade_transaction');
+    IF cascade_transaction = 0 THEN
         -- todo: host manager hasn't been updated to use audit
         -- mechanisms so ignore host specific options for now.
         IF scope_id = 0 THEN
@@ -2425,8 +2497,8 @@ BEGIN
     -- entire subnet. The only case when the object_type will be
     -- set to 'dhcp6_options' is when a global option is added.
     -- Global options do not have the owner.
-    cascade_transaction := current_setting('kea.cascade_transaction');
-    IF cascade_transaction IS NULL OR cascade_transaction = 0 THEN
+    cascade_transaction := get_session_small_int('kea.cascade_transaction');
+    IF cascade_transaction = 0 THEN
         -- todo: host manager hasn't been updated to use audit
         -- mechanisms so ignore host specific options for now.
         IF scope_id = 0 THEN