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
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;
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),
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.
) 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;
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;
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),
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.
) 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;
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
-- 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
-- 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
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);
+
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
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;
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),
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.
) 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;
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;
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),
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.
) 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;
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
-- 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
-- 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