--
--


SELECT pg_catalog.set_config('search_path', '', false);

--
-- Name: gnumed_v22; Type: DATABASE; Schema: -; Owner: gm-dbo
--

CREATE DATABASE gnumed_v22 WITH TEMPLATE = template0 ENCODING = 'UTF8' LOCALE_PROVIDER = libc LOCALE = 'en_US.UTF-8';


ALTER DATABASE gnumed_v22 OWNER TO "gm-dbo";

\connect gnumed_v22

SELECT pg_catalog.set_config('search_path', '', false);

--
-- Name: gnumed_v22; Type: DATABASE PROPERTIES; Schema: -; Owner: gm-dbo
--

ALTER DATABASE gnumed_v22 SET lc_messages TO 'C';
ALTER DATABASE gnumed_v22 SET default_transaction_read_only TO 'on';
ALTER DATABASE gnumed_v22 SET check_function_bodies TO 'on';
ALTER DATABASE gnumed_v22 SET ignore_checksum_failure TO 'off';


\connect gnumed_v22

SELECT pg_catalog.set_config('search_path', '', false);

--
-- Name: au; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA au;


ALTER SCHEMA au OWNER TO "gm-dbo";

--
-- Name: audit; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA audit;


ALTER SCHEMA audit OWNER TO "gm-dbo";

--
-- Name: bill; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA bill;


ALTER SCHEMA bill OWNER TO "gm-dbo";

--
-- Name: SCHEMA bill; Type: COMMENT; Schema: -; Owner: gm-dbo
--

COMMENT ON SCHEMA bill IS 'Holds everything related to billing.';


--
-- Name: blobs; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA blobs;


ALTER SCHEMA blobs OWNER TO "gm-dbo";

--
-- Name: cfg; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA cfg;


ALTER SCHEMA cfg OWNER TO "gm-dbo";

--
-- Name: SCHEMA cfg; Type: COMMENT; Schema: -; Owner: gm-dbo
--

COMMENT ON SCHEMA cfg IS 'This schema holds all the configuration data.';


--
-- Name: clin; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA clin;


ALTER SCHEMA clin OWNER TO "gm-dbo";

--
-- Name: de_de; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA de_de;


ALTER SCHEMA de_de OWNER TO "gm-dbo";

--
-- Name: dem; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA dem;


ALTER SCHEMA dem OWNER TO "gm-dbo";

--
-- Name: gm; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA gm;


ALTER SCHEMA gm OWNER TO "gm-dbo";

--
-- Name: i18n; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA i18n;


ALTER SCHEMA i18n OWNER TO "gm-dbo";

--
-- Name: pgtrgm; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA pgtrgm;


ALTER SCHEMA pgtrgm OWNER TO postgres;

--
-- Name: ref; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA ref;


ALTER SCHEMA ref OWNER TO "gm-dbo";

--
-- Name: SCHEMA ref; Type: COMMENT; Schema: -; Owner: gm-dbo
--

COMMENT ON SCHEMA ref IS 'This schema holds data that is "reference material" which comes
	 pre-installed with a GNUmed database. Examples are:
	 - document types
	 - ICD codes
	 - form templates';


--
-- Name: staging; Type: SCHEMA; Schema: -; Owner: gm-dbo
--

CREATE SCHEMA staging;


ALTER SCHEMA staging OWNER TO "gm-dbo";

--
-- Name: SCHEMA staging; Type: COMMENT; Schema: -; Owner: gm-dbo
--

COMMENT ON SCHEMA staging IS 'A schema used for staging data imports.';


--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA pgtrgm;


--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';


--
-- Name: add_table_for_audit(name); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.add_table_for_audit(name) RETURNS boolean
    LANGUAGE sql SECURITY DEFINER
    AS $_$
select audit.add_table_for_audit('public', $1);$_$;


ALTER FUNCTION audit.add_table_for_audit(name) OWNER TO "gm-dbo";

--
-- Name: FUNCTION add_table_for_audit(name); Type: COMMENT; Schema: audit; Owner: gm-dbo
--

COMMENT ON FUNCTION audit.add_table_for_audit(name) IS 'sanity-checking convenience function for marking tables
	 for auditing, schema is always "public"';


--
-- Name: add_table_for_audit(name, name); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.add_table_for_audit(name, name) RETURNS boolean
    LANGUAGE plpgsql SECURITY DEFINER
    AS $_$
DECLARE
	_relnamespace alias for $1;
	_relname ALIAS FOR $2;
	dummy RECORD;
	tmp text;
BEGIN
	-- does table exist ?
	select relname into dummy from pg_class where
		relname = _relname and
		relnamespace = (select oid from pg_namespace where nspname = _relnamespace)
	;
	if not found then
		tmp := _relnamespace || '.' || _relname;
		raise exception 'audit.add_table_for_audit: Table [%] does not exist.', tmp;
		return false;
	end if;
	-- already queued for auditing ?
	select 1 into dummy from audit.audited_tables where table_name = _relname and schema = _relnamespace;
	if found then
		return true;
	end if;
	-- add definition
	insert into audit.audited_tables (
		schema, table_name
	) values (
		_relnamespace, _relname
	);
	return true;
END;$_$;


ALTER FUNCTION audit.add_table_for_audit(name, name) OWNER TO "gm-dbo";

--
-- Name: FUNCTION add_table_for_audit(name, name); Type: COMMENT; Schema: audit; Owner: gm-dbo
--

COMMENT ON FUNCTION audit.add_table_for_audit(name, name) IS 'sanity-checking convenience function for marking tables for auditing';


--
-- Name: ft_del_access_log(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_access_log() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_access_log (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, user_action
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.user_action
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_access_log() OWNER TO "gm-dbo";

--
-- Name: ft_del_active_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_active_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_active_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, description, atc_code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.description, OLD.atc_code
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_active_substance() OWNER TO "gm-dbo";

--
-- Name: ft_del_address(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_address() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_address (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_street, aux_street, number, subunit, addendum, lat_lon
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_street, OLD.aux_street, OLD.number, OLD.subunit, OLD.addendum, OLD.lat_lon
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_address() OWNER TO "gm-dbo";

--
-- Name: ft_del_allergy(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_allergy() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_allergy (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, substance, substance_code, generics, allergene, atc_code, fk_type, generic_specific, definite
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.substance, OLD.substance_code, OLD.generics, OLD.allergene, OLD.atc_code, OLD.fk_type, OLD.generic_specific, OLD.definite
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_allergy() OWNER TO "gm-dbo";

--
-- Name: ft_del_allergy_state(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_allergy_state() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_allergy_state (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, has_allergy, comment, last_confirmed, fk_encounter
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.has_allergy, OLD.comment, OLD.last_confirmed, OLD.fk_encounter
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_allergy_state() OWNER TO "gm-dbo";

--
-- Name: ft_del_atc_group(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_atc_group() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_atc_group (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, code, description
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.code, OLD.description
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_atc_group() OWNER TO "gm-dbo";

--
-- Name: ft_del_atc_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_atc_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_atc_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, code, name, ddd_amount, fk_ddd_unit, route, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.code, OLD.name, OLD.ddd_amount, OLD.fk_ddd_unit, OLD.route, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_atc_substance() OWNER TO "gm-dbo";

--
-- Name: ft_del_auto_hint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_auto_hint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_auto_hint (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, query, title, hint, url, is_active, source, lang, recommendation_query, popup_type, highlight_as_priority
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.query, OLD.title, OLD.hint, OLD.url, OLD.is_active, OLD.source, OLD.lang, OLD.recommendation_query, OLD.popup_type, OLD.highlight_as_priority
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_auto_hint() OWNER TO "gm-dbo";

--
-- Name: ft_del_beh_fall_typ(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_beh_fall_typ() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_beh_fall_typ (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, code, kurzform, name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.code, OLD.kurzform, OLD.name
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_beh_fall_typ() OWNER TO "gm-dbo";

--
-- Name: ft_del_behandlungsfall(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_behandlungsfall() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_behandlungsfall (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient, fk_falltyp, started, must_pay_prax_geb
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient, OLD.fk_falltyp, OLD.started, OLD.must_pay_prax_geb
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_behandlungsfall() OWNER TO "gm-dbo";

--
-- Name: ft_del_bill(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_bill() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_bill (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, invoice_id, close_date, apply_vat, fk_receiver_identity, fk_receiver_address, fk_doc, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.invoice_id, OLD.close_date, OLD.apply_vat, OLD.fk_receiver_identity, OLD.fk_receiver_address, OLD.fk_doc, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_bill() OWNER TO "gm-dbo";

--
-- Name: ft_del_bill_item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_bill_item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_bill_item (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_provider, fk_encounter, date_to_bill, description, net_amount_per_unit, currency, status, fk_billable, fk_bill, unit_count, amount_multiplier
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_provider, OLD.fk_encounter, OLD.date_to_bill, OLD.description, OLD.net_amount_per_unit, OLD.currency, OLD.status, OLD.fk_billable, OLD.fk_bill, OLD.unit_count, OLD.amount_multiplier
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_bill_item() OWNER TO "gm-dbo";

--
-- Name: ft_del_clin_aux_note(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_clin_aux_note() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_clin_aux_note (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_clin_aux_note() OWNER TO "gm-dbo";

--
-- Name: ft_del_clin_diag(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_clin_diag() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_clin_diag (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_narrative, laterality, is_chronic, is_active, is_definite, clinically_relevant
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_narrative, OLD.laterality, OLD.is_chronic, OLD.is_active, OLD.is_definite, OLD.clinically_relevant
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_clin_diag() OWNER TO "gm-dbo";

--
-- Name: ft_del_clin_item_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_clin_item_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_clin_item_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, type, code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.type, OLD.code
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_clin_item_type() OWNER TO "gm-dbo";

--
-- Name: ft_del_clin_medication(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_clin_medication() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_clin_medication (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, last_prescribed, fk_last_script, discontinued, brandname, generic, adjuvant, dosage_form, ufk_drug, drug_db, atc_code, is_cr, dosage, period, dosage_unit, directions, is_prn
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.last_prescribed, OLD.fk_last_script, OLD.discontinued, OLD.brandname, OLD.generic, OLD.adjuvant, OLD.dosage_form, OLD.ufk_drug, OLD.drug_db, OLD.atc_code, OLD.is_cr, OLD.dosage, OLD.period, OLD.dosage_unit, OLD.directions, OLD.is_prn
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_clin_medication() OWNER TO "gm-dbo";

--
-- Name: ft_del_clin_narrative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_clin_narrative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_clin_narrative (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_clin_narrative() OWNER TO "gm-dbo";

--
-- Name: ft_del_coded_phrase(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_coded_phrase() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_coded_phrase (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, term, code, xfk_coding_system
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.term, OLD.code, OLD.xfk_coding_system
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_coded_phrase() OWNER TO "gm-dbo";

--
-- Name: ft_del_consumable_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_consumable_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_consumable_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, atc_code, amount, unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.atc_code, OLD.amount, OLD.unit
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_consumable_substance() OWNER TO "gm-dbo";

--
-- Name: ft_del_consumed_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_consumed_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_consumed_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, description, atc_code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.description, OLD.atc_code
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_consumed_substance() OWNER TO "gm-dbo";

--
-- Name: ft_del_data_source(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_data_source() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_data_source (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, name_long, name_short, version, description, source, lang
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.name_long, OLD.name_short, OLD.version, OLD.description, OLD.source, OLD.lang
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_data_source() OWNER TO "gm-dbo";

--
-- Name: ft_del_doc_desc(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_doc_desc() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_doc_desc (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_doc, text
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_doc, OLD.text
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_doc_desc() OWNER TO "gm-dbo";

--
-- Name: ft_del_doc_med(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_doc_med() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_doc_med (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_encounter, fk_episode, fk_type, comment, clin_when, ext_ref, fk_org_unit, unit_is_receiver, fk_hospital_stay
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_encounter, OLD.fk_episode, OLD.fk_type, OLD.comment, OLD.clin_when, OLD.ext_ref, OLD.fk_org_unit, OLD.unit_is_receiver, OLD.fk_hospital_stay
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_doc_med() OWNER TO "gm-dbo";

--
-- Name: ft_del_dose(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_dose() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_dose (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_substance, amount, unit, dose_unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_substance, OLD.amount, OLD.unit, OLD.dose_unit
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_dose() OWNER TO "gm-dbo";

--
-- Name: ft_del_drug_product(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_drug_product() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_drug_product (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, preparation, atc_code, is_fake, fk_data_source, external_code, external_code_type
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.preparation, OLD.atc_code, OLD.is_fake, OLD.fk_data_source, OLD.external_code, OLD.external_code_type
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_drug_product() OWNER TO "gm-dbo";

--
-- Name: ft_del_encounter(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_encounter() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_encounter (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient, fk_type, fk_location, source_time_zone, reason_for_encounter, assessment_of_encounter, started, last_affirmed
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient, OLD.fk_type, OLD.fk_location, OLD.source_time_zone, OLD.reason_for_encounter, OLD.assessment_of_encounter, OLD.started, OLD.last_affirmed
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_encounter() OWNER TO "gm-dbo";

--
-- Name: ft_del_episode(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_episode() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_episode (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_health_issue, description, is_open, fk_encounter, diagnostic_certainty_classification, summary
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_health_issue, OLD.description, OLD.is_open, OLD.fk_encounter, OLD.diagnostic_certainty_classification, OLD.summary
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_episode() OWNER TO "gm-dbo";

--
-- Name: ft_del_external_care(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_external_care() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_external_care (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_encounter, fk_health_issue, issue, fk_org_unit, provider, comment, inactive
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_encounter, OLD.fk_health_issue, OLD.issue, OLD.fk_org_unit, OLD.provider, OLD.comment, OLD.inactive
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_external_care() OWNER TO "gm-dbo";

--
-- Name: ft_del_family_history(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_family_history() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_family_history (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_relation_type, age_noted, age_of_death, contributed_to_death, name_relative, dob_relative, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_relation_type, OLD.age_noted, OLD.age_of_death, OLD.contributed_to_death, OLD.name_relative, OLD.dob_relative, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_family_history() OWNER TO "gm-dbo";

--
-- Name: ft_del_fhx_relation_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_fhx_relation_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_fhx_relation_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, is_genetic
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.is_genetic
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_fhx_relation_type() OWNER TO "gm-dbo";

--
-- Name: ft_del_form_data(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_form_data() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_form_data (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_instance, fk_form_field, value
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_instance, OLD.fk_form_field, OLD.value
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_form_data() OWNER TO "gm-dbo";

--
-- Name: ft_del_form_defs(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_form_defs() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_form_defs (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, fk_type, country, locale, soap_cat, name_short, name_long, revision, template, engine, in_use, url, is_user
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.fk_type, OLD.country, OLD.locale, OLD.soap_cat, OLD.name_short, OLD.name_long, OLD.revision, OLD.template, OLD.engine, OLD.in_use, OLD.url, OLD.is_user
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_form_defs() OWNER TO "gm-dbo";

--
-- Name: ft_del_form_instances(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_form_instances() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_form_instances (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_form_def, form_name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_form_def, OLD.form_name
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_form_instances() OWNER TO "gm-dbo";

--
-- Name: ft_del_gender_label(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_gender_label() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_gender_label (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, tag, label, sort_weight, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.tag, OLD.label, OLD.sort_weight, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_gender_label() OWNER TO "gm-dbo";

--
-- Name: ft_del_health_issue(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_health_issue() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_health_issue (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, laterality, age_noted, is_active, clinically_relevant, is_confidential, is_cause_of_death, fk_encounter, grouping, diagnostic_certainty_classification, summary
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.laterality, OLD.age_noted, OLD.is_active, OLD.clinically_relevant, OLD.is_confidential, OLD.is_cause_of_death, OLD.fk_encounter, OLD.grouping, OLD.diagnostic_certainty_classification, OLD.summary
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_health_issue() OWNER TO "gm-dbo";

--
-- Name: ft_del_hospital_stay(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_hospital_stay() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_hospital_stay (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, discharge, fk_org_unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.discharge, OLD.fk_org_unit
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_hospital_stay() OWNER TO "gm-dbo";

--
-- Name: ft_del_hx_family_item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_hx_family_item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_hx_family_item (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, fk_narrative_condition, fk_relative, name_relative, dob_relative, condition, age_noted, age_of_death, is_cause_of_death
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.fk_narrative_condition, OLD.fk_relative, OLD.name_relative, OLD.dob_relative, OLD.condition, OLD.age_noted, OLD.age_of_death, OLD.is_cause_of_death
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_hx_family_item() OWNER TO "gm-dbo";

--
-- Name: ft_del_identity(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_identity() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_identity (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, deleted, gender, dob, fk_marital_status, cob, deceased, title, tob, emergency_contact, fk_emergency_contact, comment, fk_primary_provider, dob_is_estimated
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.deleted, OLD.gender, OLD.dob, OLD.fk_marital_status, OLD.cob, OLD.deceased, OLD.title, OLD.tob, OLD.emergency_contact, OLD.fk_emergency_contact, OLD.comment, OLD.fk_primary_provider, OLD.dob_is_estimated
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_identity() OWNER TO "gm-dbo";

--
-- Name: ft_del_identity_tag(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_identity_tag() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_identity_tag (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_identity, fk_tag, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_identity, OLD.fk_tag, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_identity_tag() OWNER TO "gm-dbo";

--
-- Name: ft_del_inbox_item_category(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_inbox_item_category() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_inbox_item_category (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, is_user
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.is_user
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_inbox_item_category() OWNER TO "gm-dbo";

--
-- Name: ft_del_inbox_item_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_inbox_item_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_inbox_item_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_inbox_item_category, description, is_user
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_inbox_item_category, OLD.description, OLD.is_user
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_inbox_item_type() OWNER TO "gm-dbo";

--
-- Name: ft_del_incoming_data_unmatchable(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_incoming_data_unmatchable() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_incoming_data_unmatchable (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient_candidates, request_id, firstnames, lastnames, dob, postcode, other_info, type, data, gender, requestor, external_data_id, fk_identity_disambiguated
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient_candidates, OLD.request_id, OLD.firstnames, OLD.lastnames, OLD.dob, OLD.postcode, OLD.other_info, OLD.type, OLD.data, OLD.gender, OLD.requestor, OLD.external_data_id, OLD.fk_identity_disambiguated
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_incoming_data_unmatchable() OWNER TO "gm-dbo";

--
-- Name: ft_del_incoming_data_unmatched(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_incoming_data_unmatched() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_incoming_data_unmatched (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient_candidates, request_id, firstnames, lastnames, dob, postcode, other_info, type, data, gender, requestor, external_data_id, fk_identity_disambiguated, comment, fk_provider_disambiguated
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient_candidates, OLD.request_id, OLD.firstnames, OLD.lastnames, OLD.dob, OLD.postcode, OLD.other_info, OLD.type, OLD.data, OLD.gender, OLD.requestor, OLD.external_data_id, OLD.fk_identity_disambiguated, OLD.comment, OLD.fk_provider_disambiguated
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_incoming_data_unmatched() OWNER TO "gm-dbo";

--
-- Name: ft_del_lab_request(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lab_request() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lab_request (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_test_org, request_id, fk_requestor, lab_request_id, lab_rxd_when, results_reported_when, request_status, is_pending, diagnostic_service_section, ordered_service, note_test_org
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_test_org, OLD.request_id, OLD.fk_requestor, OLD.lab_request_id, OLD.lab_rxd_when, OLD.results_reported_when, OLD.request_status, OLD.is_pending, OLD.diagnostic_service_section, OLD.ordered_service, OLD.note_test_org
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lab_request() OWNER TO "gm-dbo";

--
-- Name: ft_del_lab_test_gnr(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lab_test_gnr() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lab_test_gnr (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_test
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_test
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lab_test_gnr() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2aoe(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2aoe() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2aoe (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2aoe() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2episode(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2episode() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2episode (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2episode() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2fhx(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2fhx() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2fhx (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2fhx() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2h_issue(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2h_issue() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2h_issue (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2h_issue() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2narrative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2narrative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2narrative (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2narrative() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2procedure(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2procedure() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2procedure (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2procedure() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2rfe(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2rfe() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2rfe (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2rfe() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_code2tst_pnl(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_code2tst_pnl() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_code2tst_pnl (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_code2tst_pnl() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_constraint2vacc_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_constraint2vacc_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_constraint2vacc_course (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_vaccination_course, fk_constraint
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_vaccination_course, OLD.fk_constraint
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_constraint2vacc_course() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_dose2drug(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_dose2drug() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_dose2drug (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_drug_product, fk_dose
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_drug_product, OLD.fk_dose
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_dose2drug() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_identity2ext_id(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_identity2ext_id() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_identity2ext_id (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_identity, external_id, fk_origin, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_identity, OLD.external_id, OLD.fk_origin, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_identity2ext_id() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_identity2primary_doc(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_identity2primary_doc() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_lnk_identity2primary_doc (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_identity, fk_primary_doc
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_identity, OLD.fk_primary_doc
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_identity2primary_doc() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_job2person(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_job2person() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_job2person (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_identity, fk_occupation, activities
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_identity, OLD.fk_occupation, OLD.activities
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_job2person() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_loinc2substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_loinc2substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_loinc2substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_substance, loinc, max_age, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_substance, OLD.loinc, OLD.max_age, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_loinc2substance() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_loinc2test_panel(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_loinc2test_panel() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_loinc2test_panel (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_test_panel, loinc
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_test_panel, OLD.loinc
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_loinc2test_panel() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_org_unit2comm(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_org_unit2comm() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_org_unit2comm (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_org_unit, url, fk_type, is_confidential, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_org_unit, OLD.url, OLD.fk_type, OLD.is_confidential, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_org_unit2comm() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_org_unit2ext_id(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_org_unit2ext_id() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_org_unit2ext_id (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_org_unit, external_id, fk_type, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_org_unit, OLD.external_id, OLD.fk_type, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_org_unit2ext_id() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_pat2vaccination_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_pat2vaccination_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_pat2vaccination_course (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient, fk_course
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient, OLD.fk_course
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_pat2vaccination_course() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_person2relative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_person2relative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_person2relative (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_identity, id_relative, id_relation_type, started, ended
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_identity, OLD.id_relative, OLD.id_relation_type, OLD.started, OLD.ended
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_person2relative() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_substance2brand(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_substance2brand() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_substance2brand (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_brand, fk_substance
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_brand, OLD.fk_substance
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_substance2brand() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_tst2norm(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_tst2norm() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_tst2norm (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_test, id_norm
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_test, OLD.id_norm
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_tst2norm() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_type2item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_type2item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_type2item (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_type, fk_item
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_type, OLD.fk_item
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_type2item() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_vaccination_course2schedule(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_vaccination_course2schedule() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_vaccination_course2schedule (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_course, fk_schedule
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_course, OLD.fk_schedule
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_vaccination_course2schedule() OWNER TO "gm-dbo";

--
-- Name: ft_del_lnk_vaccine2inds(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_lnk_vaccine2inds() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_lnk_vaccine2inds (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, fk_vaccine, fk_indication
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.fk_vaccine, OLD.fk_indication
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_lnk_vaccine2inds() OWNER TO "gm-dbo";

--
-- Name: ft_del_message_inbox(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_message_inbox() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_message_inbox (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_staff, fk_inbox_item_type, comment, data, importance, fk_patient, ufk_context, due_date, expiry_date
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_staff, OLD.fk_inbox_item_type, OLD.comment, OLD.data, OLD.importance, OLD.fk_patient, OLD.ufk_context, OLD.due_date, OLD.expiry_date
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_message_inbox() OWNER TO "gm-dbo";

--
-- Name: ft_del_occupation(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_occupation() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_occupation (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.name
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_occupation() OWNER TO "gm-dbo";

--
-- Name: ft_del_operation(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_operation() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_operation (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, fk_health_issue, fk_encounter, clin_where, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.fk_health_issue, OLD.fk_encounter, OLD.clin_where, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_operation() OWNER TO "gm-dbo";

--
-- Name: ft_del_org(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_org() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_org (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, fk_category, fk_data_source
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.fk_category, OLD.fk_data_source
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_org() OWNER TO "gm-dbo";

--
-- Name: ft_del_org_unit(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_org_unit() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_org_unit (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, fk_org, fk_address, fk_category
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.fk_org, OLD.fk_address, OLD.fk_category
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_org_unit() OWNER TO "gm-dbo";

--
-- Name: ft_del_paperwork_templates(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_paperwork_templates() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_paperwork_templates (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_template_type, instance_type, name_short, name_long, external_version, gnumed_revision, engine, in_use, filename, data, edit_after_substitution
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_template_type, OLD.instance_type, OLD.name_short, OLD.name_long, OLD.external_version, OLD.gnumed_revision, OLD.engine, OLD.in_use, OLD.filename, OLD.data, OLD.edit_after_substitution
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_paperwork_templates() OWNER TO "gm-dbo";

--
-- Name: ft_del_patient(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_patient() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_patient (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_identity, edc
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_identity, OLD.edc
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_patient() OWNER TO "gm-dbo";

--
-- Name: ft_del_prax_geb_paid(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_prax_geb_paid() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_prax_geb_paid (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_fall, paid_amount, paid_when, paid_with
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_fall, OLD.paid_amount, OLD.paid_when, OLD.paid_with
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_prax_geb_paid() OWNER TO "gm-dbo";

--
-- Name: ft_del_praxis_branch(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_praxis_branch() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_praxis_branch (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_org_unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_org_unit
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_praxis_branch() OWNER TO "gm-dbo";

--
-- Name: ft_del_procedure(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_procedure() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_procedure (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_hospital_stay, clin_end, is_ongoing, fk_org_unit, fk_doc, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_hospital_stay, OLD.clin_end, OLD.is_ongoing, OLD.fk_org_unit, OLD.fk_doc, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_procedure() OWNER TO "gm-dbo";

--
-- Name: ft_del_ref_source(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_ref_source() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_ref_source (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, name_short, name_long, version, description, source
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.name_short, OLD.name_long, OLD.version, OLD.description, OLD.source
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_ref_source() OWNER TO "gm-dbo";

--
-- Name: ft_del_referral(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_referral() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_referral (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, id, fk_referee, fk_form
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.id, OLD.fk_referee, OLD.fk_form
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_referral() OWNER TO "gm-dbo";

--
-- Name: ft_del_region(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_region() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_region (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, code, country, name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.code, OLD.country, OLD.name
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_region() OWNER TO "gm-dbo";

--
-- Name: ft_del_relation_types(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_relation_types() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_relation_types (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, inverse, biological, biol_verified, description
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.inverse, OLD.biological, OLD.biol_verified, OLD.description
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_relation_types() OWNER TO "gm-dbo";

--
-- Name: ft_del_report_query(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_report_query() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_report_query (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, label, cmd
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.label, OLD.cmd
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_report_query() OWNER TO "gm-dbo";

--
-- Name: ft_del_staff(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_staff() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_staff (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_identity, db_user, short_alias, comment, is_active
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_identity, OLD.db_user, OLD.short_alias, OLD.comment, OLD.is_active
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_staff() OWNER TO "gm-dbo";

--
-- Name: ft_del_staff_role(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_staff_role() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_staff_role (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, name, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.name, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_staff_role() OWNER TO "gm-dbo";

--
-- Name: ft_del_street(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_street() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_street (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_urb, name, postcode, suburb, lat_lon
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_urb, OLD.name, OLD.postcode, OLD.suburb, OLD.lat_lon
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_street() OWNER TO "gm-dbo";

--
-- Name: ft_del_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, atc, intake_instructions
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.atc, OLD.intake_instructions
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_substance() OWNER TO "gm-dbo";

--
-- Name: ft_del_substance_in_brand(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_substance_in_brand() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_substance_in_brand (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, fk_brand, description, atc_code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.fk_brand, OLD.description, OLD.atc_code
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_substance_in_brand() OWNER TO "gm-dbo";

--
-- Name: ft_del_substance_intake(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_substance_intake() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_substance_intake (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, schedule, aim, duration, intake_is_approved_of, is_long_term, discontinued, discontinue_reason, fk_drug_component, comment_on_start, harmful_use_type
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.schedule, OLD.aim, OLD.duration, OLD.intake_is_approved_of, OLD.is_long_term, OLD.discontinued, OLD.discontinue_reason, OLD.fk_drug_component, OLD.comment_on_start, OLD.harmful_use_type
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_substance_intake() OWNER TO "gm-dbo";

--
-- Name: ft_del_suppressed_hint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_suppressed_hint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_suppressed_hint (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_encounter, fk_hint, suppressed_by, suppressed_when, rationale, md5_sum
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_encounter, OLD.fk_hint, OLD.suppressed_by, OLD.suppressed_when, OLD.rationale, OLD.md5_sum
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_suppressed_hint() OWNER TO "gm-dbo";

--
-- Name: ft_del_tag_image(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_tag_image() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_tag_image (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, filename, image
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.filename, OLD.image
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_tag_image() OWNER TO "gm-dbo";

--
-- Name: ft_del_test_org(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_test_org() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_test_org (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_org_unit, fk_adm_contact, fk_med_contact, comment, contact
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_org_unit, OLD.fk_adm_contact, OLD.fk_med_contact, OLD.comment, OLD.contact
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_test_org() OWNER TO "gm-dbo";

--
-- Name: ft_del_test_panel(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_test_panel() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_test_panel (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_test_panel() OWNER TO "gm-dbo";

--
-- Name: ft_del_test_result(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_test_result() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_test_result (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_type, val_num, val_alpha, val_unit, val_normal_min, val_normal_max, val_normal_range, val_target_min, val_target_max, val_target_range, abnormality_indicator, norm_ref_group, note_test_org, material, material_detail, fk_intended_reviewer, fk_request, status, source_data, val_grouping
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_type, OLD.val_num, OLD.val_alpha, OLD.val_unit, OLD.val_normal_min, OLD.val_normal_max, OLD.val_normal_range, OLD.val_target_min, OLD.val_target_max, OLD.val_target_range, OLD.abnormality_indicator, OLD.norm_ref_group, OLD.note_test_org, OLD.material, OLD.material_detail, OLD.fk_intended_reviewer, OLD.fk_request, OLD.status, OLD.source_data, OLD.val_grouping
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_test_result() OWNER TO "gm-dbo";

--
-- Name: ft_del_test_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_test_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_test_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_test_org, name, comment, reference_unit, loinc, abbrev, fk_meta_test_type
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_test_org, OLD.name, OLD.comment, OLD.reference_unit, OLD.loinc, OLD.abbrev, OLD.fk_meta_test_type
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_test_type() OWNER TO "gm-dbo";

--
-- Name: ft_del_urb(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_urb() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_urb (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, fk_region, postcode, lat_lon, name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.fk_region, OLD.postcode, OLD.lat_lon, OLD.name
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_urb() OWNER TO "gm-dbo";

--
-- Name: ft_del_vacc_indication(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vacc_indication() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vacc_indication (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, description, atcs_single_indication, atcs_combi_indication
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.description, OLD.atcs_single_indication, OLD.atcs_combi_indication
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vacc_indication() OWNER TO "gm-dbo";

--
-- Name: ft_del_vacc_route(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vacc_route() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vacc_route (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, abbreviation, description
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.abbreviation, OLD.description
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vacc_route() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccination(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccination() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccination (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_provider, fk_vaccine, site, batch_no, reaction
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_provider, OLD.fk_vaccine, OLD.site, OLD.batch_no, OLD.reaction
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccination() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccination_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccination_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccination_course (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_recommended_by, fk_indication, is_active, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_recommended_by, OLD.fk_indication, OLD.is_active, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccination_course() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccination_course_constraint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccination_course_constraint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccination_course_constraint (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccination_course_constraint() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccination_definition(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccination_definition() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccination_definition (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, fk_course, is_booster, seq_no, min_age_due, max_age_due, min_interval, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.fk_course, OLD.is_booster, OLD.seq_no, OLD.min_age_due, OLD.max_age_due, OLD.min_interval, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccination_definition() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccination_schedule(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccination_schedule() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccination_schedule (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, name, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.name, OLD.comment
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccination_schedule() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccine(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccine() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccine (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, id_route, is_live, min_age, max_age, comment, fk_drug_product
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.id_route, OLD.is_live, OLD.min_age, OLD.max_age, OLD.comment, OLD.fk_drug_product
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccine() OWNER TO "gm-dbo";

--
-- Name: ft_del_vaccine_batches(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_vaccine_batches() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_vaccine_batches (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_vaccine, batch_no
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_vaccine, OLD.batch_no
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_vaccine_batches() OWNER TO "gm-dbo";

--
-- Name: ft_del_waiting_list(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_waiting_list() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_deleter boolean;
BEGIN
	-- is the session user allowed to delete data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_deleter;
	IF _is_allowed_deleter IS FALSE THEN
		RAISE EXCEPTION
			'DELETE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return OLD;
	END IF;
	INSERT INTO audit.log_waiting_list (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient, registered, urgency, list_position, comment, area
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient, OLD.registered, OLD.urgency, OLD.list_position, OLD.comment, OLD.area
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_waiting_list() OWNER TO "gm-dbo";

--
-- Name: ft_del_xlnk_identity(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_del_xlnk_identity() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO audit.log_xlnk_identity (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, xfk_identity, pupic, data
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.xfk_identity, OLD.pupic, OLD.data
	);
	return OLD;
END;$$;


ALTER FUNCTION audit.ft_del_xlnk_identity() OWNER TO "gm-dbo";

--
-- Name: ft_ins_access_log(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_access_log() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_access_log() OWNER TO "gm-dbo";

--
-- Name: ft_ins_active_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_active_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_active_substance() OWNER TO "gm-dbo";

--
-- Name: ft_ins_address(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_address() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_address() OWNER TO "gm-dbo";

--
-- Name: ft_ins_allergy(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_allergy() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_allergy() OWNER TO "gm-dbo";

--
-- Name: ft_ins_allergy_state(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_allergy_state() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_allergy_state() OWNER TO "gm-dbo";

--
-- Name: ft_ins_atc_group(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_atc_group() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_atc_group() OWNER TO "gm-dbo";

--
-- Name: ft_ins_atc_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_atc_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_atc_substance() OWNER TO "gm-dbo";

--
-- Name: ft_ins_auto_hint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_auto_hint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_auto_hint() OWNER TO "gm-dbo";

--
-- Name: ft_ins_beh_fall_typ(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_beh_fall_typ() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_beh_fall_typ() OWNER TO "gm-dbo";

--
-- Name: ft_ins_behandlungsfall(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_behandlungsfall() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_behandlungsfall() OWNER TO "gm-dbo";

--
-- Name: ft_ins_bill(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_bill() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_bill() OWNER TO "gm-dbo";

--
-- Name: ft_ins_bill_item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_bill_item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_bill_item() OWNER TO "gm-dbo";

--
-- Name: ft_ins_clin_aux_note(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_clin_aux_note() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_clin_aux_note() OWNER TO "gm-dbo";

--
-- Name: ft_ins_clin_diag(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_clin_diag() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_clin_diag() OWNER TO "gm-dbo";

--
-- Name: ft_ins_clin_item_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_clin_item_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_clin_item_type() OWNER TO "gm-dbo";

--
-- Name: ft_ins_clin_medication(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_clin_medication() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_clin_medication() OWNER TO "gm-dbo";

--
-- Name: ft_ins_clin_narrative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_clin_narrative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_clin_narrative() OWNER TO "gm-dbo";

--
-- Name: ft_ins_coded_phrase(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_coded_phrase() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_coded_phrase() OWNER TO "gm-dbo";

--
-- Name: ft_ins_consumable_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_consumable_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_consumable_substance() OWNER TO "gm-dbo";

--
-- Name: ft_ins_consumed_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_consumed_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_consumed_substance() OWNER TO "gm-dbo";

--
-- Name: ft_ins_data_source(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_data_source() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_data_source() OWNER TO "gm-dbo";

--
-- Name: ft_ins_doc_desc(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_doc_desc() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_doc_desc() OWNER TO "gm-dbo";

--
-- Name: ft_ins_doc_med(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_doc_med() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_doc_med() OWNER TO "gm-dbo";

--
-- Name: ft_ins_dose(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_dose() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_dose() OWNER TO "gm-dbo";

--
-- Name: ft_ins_drug_product(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_drug_product() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_drug_product() OWNER TO "gm-dbo";

--
-- Name: ft_ins_encounter(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_encounter() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_encounter() OWNER TO "gm-dbo";

--
-- Name: ft_ins_episode(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_episode() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_episode() OWNER TO "gm-dbo";

--
-- Name: ft_ins_external_care(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_external_care() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_external_care() OWNER TO "gm-dbo";

--
-- Name: ft_ins_family_history(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_family_history() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_family_history() OWNER TO "gm-dbo";

--
-- Name: ft_ins_fhx_relation_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_fhx_relation_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_fhx_relation_type() OWNER TO "gm-dbo";

--
-- Name: ft_ins_form_data(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_form_data() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_form_data() OWNER TO "gm-dbo";

--
-- Name: ft_ins_form_defs(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_form_defs() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_form_defs() OWNER TO "gm-dbo";

--
-- Name: ft_ins_form_instances(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_form_instances() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_form_instances() OWNER TO "gm-dbo";

--
-- Name: ft_ins_gender_label(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_gender_label() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_gender_label() OWNER TO "gm-dbo";

--
-- Name: ft_ins_health_issue(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_health_issue() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_health_issue() OWNER TO "gm-dbo";

--
-- Name: ft_ins_hospital_stay(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_hospital_stay() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_hospital_stay() OWNER TO "gm-dbo";

--
-- Name: ft_ins_hx_family_item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_hx_family_item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_hx_family_item() OWNER TO "gm-dbo";

--
-- Name: ft_ins_identity(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_identity() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_identity() OWNER TO "gm-dbo";

--
-- Name: ft_ins_identity_tag(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_identity_tag() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_identity_tag() OWNER TO "gm-dbo";

--
-- Name: ft_ins_inbox_item_category(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_inbox_item_category() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_inbox_item_category() OWNER TO "gm-dbo";

--
-- Name: ft_ins_inbox_item_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_inbox_item_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_inbox_item_type() OWNER TO "gm-dbo";

--
-- Name: ft_ins_incoming_data_unmatchable(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_incoming_data_unmatchable() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_incoming_data_unmatchable() OWNER TO "gm-dbo";

--
-- Name: ft_ins_incoming_data_unmatched(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_incoming_data_unmatched() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_incoming_data_unmatched() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lab_request(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lab_request() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lab_request() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lab_test_gnr(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lab_test_gnr() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lab_test_gnr() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2aoe(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2aoe() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2aoe() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2episode(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2episode() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2episode() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2fhx(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2fhx() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2fhx() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2h_issue(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2h_issue() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2h_issue() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2narrative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2narrative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2narrative() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2procedure(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2procedure() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2procedure() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2rfe(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2rfe() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2rfe() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_code2tst_pnl(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_code2tst_pnl() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_code2tst_pnl() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_constraint2vacc_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_constraint2vacc_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_constraint2vacc_course() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_dose2drug(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_dose2drug() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_dose2drug() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_identity2ext_id(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_identity2ext_id() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_identity2ext_id() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_identity2primary_doc(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_identity2primary_doc() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_identity2primary_doc() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_job2person(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_job2person() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_job2person() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_loinc2substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_loinc2substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_loinc2substance() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_loinc2test_panel(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_loinc2test_panel() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_loinc2test_panel() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_org_unit2comm(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_org_unit2comm() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_org_unit2comm() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_org_unit2ext_id(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_org_unit2ext_id() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_org_unit2ext_id() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_pat2vaccination_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_pat2vaccination_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_pat2vaccination_course() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_person2relative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_person2relative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_person2relative() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_substance2brand(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_substance2brand() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_substance2brand() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_tst2norm(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_tst2norm() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_tst2norm() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_type2item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_type2item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_type2item() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_vaccination_course2schedule(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_vaccination_course2schedule() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_vaccination_course2schedule() OWNER TO "gm-dbo";

--
-- Name: ft_ins_lnk_vaccine2inds(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_lnk_vaccine2inds() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_lnk_vaccine2inds() OWNER TO "gm-dbo";

--
-- Name: ft_ins_message_inbox(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_message_inbox() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_message_inbox() OWNER TO "gm-dbo";

--
-- Name: ft_ins_occupation(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_occupation() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_occupation() OWNER TO "gm-dbo";

--
-- Name: ft_ins_operation(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_operation() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_operation() OWNER TO "gm-dbo";

--
-- Name: ft_ins_org(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_org() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_org() OWNER TO "gm-dbo";

--
-- Name: ft_ins_org_unit(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_org_unit() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_org_unit() OWNER TO "gm-dbo";

--
-- Name: ft_ins_paperwork_templates(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_paperwork_templates() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_paperwork_templates() OWNER TO "gm-dbo";

--
-- Name: ft_ins_patient(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_patient() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_patient() OWNER TO "gm-dbo";

--
-- Name: ft_ins_prax_geb_paid(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_prax_geb_paid() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_prax_geb_paid() OWNER TO "gm-dbo";

--
-- Name: ft_ins_praxis_branch(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_praxis_branch() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_praxis_branch() OWNER TO "gm-dbo";

--
-- Name: ft_ins_procedure(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_procedure() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_procedure() OWNER TO "gm-dbo";

--
-- Name: ft_ins_ref_source(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_ref_source() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_ref_source() OWNER TO "gm-dbo";

--
-- Name: ft_ins_referral(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_referral() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_referral() OWNER TO "gm-dbo";

--
-- Name: ft_ins_region(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_region() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_region() OWNER TO "gm-dbo";

--
-- Name: ft_ins_relation_types(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_relation_types() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_relation_types() OWNER TO "gm-dbo";

--
-- Name: ft_ins_report_query(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_report_query() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_report_query() OWNER TO "gm-dbo";

--
-- Name: ft_ins_staff(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_staff() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_staff() OWNER TO "gm-dbo";

--
-- Name: ft_ins_staff_role(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_staff_role() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_staff_role() OWNER TO "gm-dbo";

--
-- Name: ft_ins_street(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_street() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_street() OWNER TO "gm-dbo";

--
-- Name: ft_ins_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_substance() OWNER TO "gm-dbo";

--
-- Name: ft_ins_substance_in_brand(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_substance_in_brand() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_substance_in_brand() OWNER TO "gm-dbo";

--
-- Name: ft_ins_substance_intake(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_substance_intake() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_substance_intake() OWNER TO "gm-dbo";

--
-- Name: ft_ins_suppressed_hint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_suppressed_hint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_suppressed_hint() OWNER TO "gm-dbo";

--
-- Name: ft_ins_tag_image(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_tag_image() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_tag_image() OWNER TO "gm-dbo";

--
-- Name: ft_ins_test_org(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_test_org() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_test_org() OWNER TO "gm-dbo";

--
-- Name: ft_ins_test_panel(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_test_panel() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_test_panel() OWNER TO "gm-dbo";

--
-- Name: ft_ins_test_result(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_test_result() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_test_result() OWNER TO "gm-dbo";

--
-- Name: ft_ins_test_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_test_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_test_type() OWNER TO "gm-dbo";

--
-- Name: ft_ins_urb(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_urb() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_urb() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vacc_indication(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vacc_indication() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vacc_indication() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vacc_route(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vacc_route() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vacc_route() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccination(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccination() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccination() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccination_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccination_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccination_course() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccination_course_constraint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccination_course_constraint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccination_course_constraint() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccination_definition(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccination_definition() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccination_definition() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccination_schedule(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccination_schedule() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccination_schedule() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccine(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccine() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccine() OWNER TO "gm-dbo";

--
-- Name: ft_ins_vaccine_batches(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_vaccine_batches() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_vaccine_batches() OWNER TO "gm-dbo";

--
-- Name: ft_ins_waiting_list(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_waiting_list() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_inserter boolean;
BEGIN
	-- is the session user allowed to insert data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_inserter;
	IF _is_allowed_inserter IS FALSE THEN
		RAISE EXCEPTION
			'INSERT: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_waiting_list() OWNER TO "gm-dbo";

--
-- Name: ft_ins_xlnk_identity(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_ins_xlnk_identity() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := 0;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_ins_xlnk_identity() OWNER TO "gm-dbo";

--
-- Name: ft_upd_access_log(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_access_log() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_access_log (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, user_action
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.user_action
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_access_log() OWNER TO "gm-dbo";

--
-- Name: ft_upd_active_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_active_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_active_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, description, atc_code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.description, OLD.atc_code
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_active_substance() OWNER TO "gm-dbo";

--
-- Name: ft_upd_address(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_address() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_address (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_street, aux_street, number, subunit, addendum, lat_lon
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_street, OLD.aux_street, OLD.number, OLD.subunit, OLD.addendum, OLD.lat_lon
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_address() OWNER TO "gm-dbo";

--
-- Name: ft_upd_allergy(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_allergy() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_allergy (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, substance, substance_code, generics, allergene, atc_code, fk_type, generic_specific, definite
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.substance, OLD.substance_code, OLD.generics, OLD.allergene, OLD.atc_code, OLD.fk_type, OLD.generic_specific, OLD.definite
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_allergy() OWNER TO "gm-dbo";

--
-- Name: ft_upd_allergy_state(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_allergy_state() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_allergy_state (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, has_allergy, comment, last_confirmed, fk_encounter
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.has_allergy, OLD.comment, OLD.last_confirmed, OLD.fk_encounter
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_allergy_state() OWNER TO "gm-dbo";

--
-- Name: ft_upd_atc_group(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_atc_group() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_atc_group (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, code, description
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.code, OLD.description
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_atc_group() OWNER TO "gm-dbo";

--
-- Name: ft_upd_atc_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_atc_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_atc_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, code, name, ddd_amount, fk_ddd_unit, route, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.code, OLD.name, OLD.ddd_amount, OLD.fk_ddd_unit, OLD.route, OLD.comment
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_atc_substance() OWNER TO "gm-dbo";

--
-- Name: ft_upd_auto_hint(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_auto_hint() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_auto_hint (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, query, title, hint, url, is_active, source, lang, recommendation_query, popup_type, highlight_as_priority
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.query, OLD.title, OLD.hint, OLD.url, OLD.is_active, OLD.source, OLD.lang, OLD.recommendation_query, OLD.popup_type, OLD.highlight_as_priority
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_auto_hint() OWNER TO "gm-dbo";

--
-- Name: ft_upd_beh_fall_typ(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_beh_fall_typ() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_beh_fall_typ (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, code, kurzform, name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.code, OLD.kurzform, OLD.name
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_beh_fall_typ() OWNER TO "gm-dbo";

--
-- Name: ft_upd_behandlungsfall(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_behandlungsfall() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_behandlungsfall (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient, fk_falltyp, started, must_pay_prax_geb
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient, OLD.fk_falltyp, OLD.started, OLD.must_pay_prax_geb
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_behandlungsfall() OWNER TO "gm-dbo";

--
-- Name: ft_upd_bill(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_bill() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_bill (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, invoice_id, close_date, apply_vat, fk_receiver_identity, fk_receiver_address, fk_doc, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.invoice_id, OLD.close_date, OLD.apply_vat, OLD.fk_receiver_identity, OLD.fk_receiver_address, OLD.fk_doc, OLD.comment
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_bill() OWNER TO "gm-dbo";

--
-- Name: ft_upd_bill_item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_bill_item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_bill_item (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_provider, fk_encounter, date_to_bill, description, net_amount_per_unit, currency, status, fk_billable, fk_bill, unit_count, amount_multiplier
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_provider, OLD.fk_encounter, OLD.date_to_bill, OLD.description, OLD.net_amount_per_unit, OLD.currency, OLD.status, OLD.fk_billable, OLD.fk_bill, OLD.unit_count, OLD.amount_multiplier
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_bill_item() OWNER TO "gm-dbo";

--
-- Name: ft_upd_clin_aux_note(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_clin_aux_note() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_clin_aux_note (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_clin_aux_note() OWNER TO "gm-dbo";

--
-- Name: ft_upd_clin_diag(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_clin_diag() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_clin_diag (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_narrative, laterality, is_chronic, is_active, is_definite, clinically_relevant
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_narrative, OLD.laterality, OLD.is_chronic, OLD.is_active, OLD.is_definite, OLD.clinically_relevant
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_clin_diag() OWNER TO "gm-dbo";

--
-- Name: ft_upd_clin_item_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_clin_item_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_clin_item_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, type, code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.type, OLD.code
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_clin_item_type() OWNER TO "gm-dbo";

--
-- Name: ft_upd_clin_medication(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_clin_medication() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_clin_medication (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, last_prescribed, fk_last_script, discontinued, brandname, generic, adjuvant, dosage_form, ufk_drug, drug_db, atc_code, is_cr, dosage, period, dosage_unit, directions, is_prn
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.last_prescribed, OLD.fk_last_script, OLD.discontinued, OLD.brandname, OLD.generic, OLD.adjuvant, OLD.dosage_form, OLD.ufk_drug, OLD.drug_db, OLD.atc_code, OLD.is_cr, OLD.dosage, OLD.period, OLD.dosage_unit, OLD.directions, OLD.is_prn
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_clin_medication() OWNER TO "gm-dbo";

--
-- Name: ft_upd_clin_narrative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_clin_narrative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_clin_narrative (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_clin_narrative() OWNER TO "gm-dbo";

--
-- Name: ft_upd_coded_phrase(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_coded_phrase() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_coded_phrase (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, term, code, xfk_coding_system
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.term, OLD.code, OLD.xfk_coding_system
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_coded_phrase() OWNER TO "gm-dbo";

--
-- Name: ft_upd_consumable_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_consumable_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_consumable_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, atc_code, amount, unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.atc_code, OLD.amount, OLD.unit
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_consumable_substance() OWNER TO "gm-dbo";

--
-- Name: ft_upd_consumed_substance(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_consumed_substance() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_consumed_substance (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, description, atc_code
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.description, OLD.atc_code
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_consumed_substance() OWNER TO "gm-dbo";

--
-- Name: ft_upd_data_source(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_data_source() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_data_source (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, name_long, name_short, version, description, source, lang
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.name_long, OLD.name_short, OLD.version, OLD.description, OLD.source, OLD.lang
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_data_source() OWNER TO "gm-dbo";

--
-- Name: ft_upd_doc_desc(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_doc_desc() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_doc_desc (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_doc, text
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_doc, OLD.text
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_doc_desc() OWNER TO "gm-dbo";

--
-- Name: ft_upd_doc_med(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_doc_med() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_doc_med (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_encounter, fk_episode, fk_type, comment, clin_when, ext_ref, fk_org_unit, unit_is_receiver, fk_hospital_stay
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_encounter, OLD.fk_episode, OLD.fk_type, OLD.comment, OLD.clin_when, OLD.ext_ref, OLD.fk_org_unit, OLD.unit_is_receiver, OLD.fk_hospital_stay
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_doc_med() OWNER TO "gm-dbo";

--
-- Name: ft_upd_dose(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_dose() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_dose (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_substance, amount, unit, dose_unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_substance, OLD.amount, OLD.unit, OLD.dose_unit
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_dose() OWNER TO "gm-dbo";

--
-- Name: ft_upd_drug_product(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_drug_product() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_drug_product (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, preparation, atc_code, is_fake, fk_data_source, external_code, external_code_type
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.preparation, OLD.atc_code, OLD.is_fake, OLD.fk_data_source, OLD.external_code, OLD.external_code_type
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_drug_product() OWNER TO "gm-dbo";

--
-- Name: ft_upd_encounter(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_encounter() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_encounter (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient, fk_type, fk_location, source_time_zone, reason_for_encounter, assessment_of_encounter, started, last_affirmed
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient, OLD.fk_type, OLD.fk_location, OLD.source_time_zone, OLD.reason_for_encounter, OLD.assessment_of_encounter, OLD.started, OLD.last_affirmed
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_encounter() OWNER TO "gm-dbo";

--
-- Name: ft_upd_episode(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_episode() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_episode (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_health_issue, description, is_open, fk_encounter, diagnostic_certainty_classification, summary
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_health_issue, OLD.description, OLD.is_open, OLD.fk_encounter, OLD.diagnostic_certainty_classification, OLD.summary
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_episode() OWNER TO "gm-dbo";

--
-- Name: ft_upd_external_care(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_external_care() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_external_care (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_encounter, fk_health_issue, issue, fk_org_unit, provider, comment, inactive
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_encounter, OLD.fk_health_issue, OLD.issue, OLD.fk_org_unit, OLD.provider, OLD.comment, OLD.inactive
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_external_care() OWNER TO "gm-dbo";

--
-- Name: ft_upd_family_history(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_family_history() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_family_history (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_relation_type, age_noted, age_of_death, contributed_to_death, name_relative, dob_relative, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_relation_type, OLD.age_noted, OLD.age_of_death, OLD.contributed_to_death, OLD.name_relative, OLD.dob_relative, OLD.comment
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_family_history() OWNER TO "gm-dbo";

--
-- Name: ft_upd_fhx_relation_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_fhx_relation_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_fhx_relation_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, is_genetic
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.is_genetic
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_fhx_relation_type() OWNER TO "gm-dbo";

--
-- Name: ft_upd_form_data(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_form_data() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_form_data (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_instance, fk_form_field, value
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_instance, OLD.fk_form_field, OLD.value
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_form_data() OWNER TO "gm-dbo";

--
-- Name: ft_upd_form_defs(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_form_defs() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_form_defs (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, fk_type, country, locale, soap_cat, name_short, name_long, revision, template, engine, in_use, url, is_user
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.fk_type, OLD.country, OLD.locale, OLD.soap_cat, OLD.name_short, OLD.name_long, OLD.revision, OLD.template, OLD.engine, OLD.in_use, OLD.url, OLD.is_user
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_form_defs() OWNER TO "gm-dbo";

--
-- Name: ft_upd_form_instances(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_form_instances() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_form_instances (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_form_def, form_name
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_form_def, OLD.form_name
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_form_instances() OWNER TO "gm-dbo";

--
-- Name: ft_upd_gender_label(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_gender_label() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_gender_label (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, tag, label, sort_weight, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.tag, OLD.label, OLD.sort_weight, OLD.comment
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_gender_label() OWNER TO "gm-dbo";

--
-- Name: ft_upd_health_issue(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_health_issue() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_health_issue (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, laterality, age_noted, is_active, clinically_relevant, is_confidential, is_cause_of_death, fk_encounter, grouping, diagnostic_certainty_classification, summary
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.laterality, OLD.age_noted, OLD.is_active, OLD.clinically_relevant, OLD.is_confidential, OLD.is_cause_of_death, OLD.fk_encounter, OLD.grouping, OLD.diagnostic_certainty_classification, OLD.summary
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_health_issue() OWNER TO "gm-dbo";

--
-- Name: ft_upd_hospital_stay(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_hospital_stay() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_hospital_stay (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, discharge, fk_org_unit
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.discharge, OLD.fk_org_unit
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_hospital_stay() OWNER TO "gm-dbo";

--
-- Name: ft_upd_hx_family_item(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_hx_family_item() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
BEGIN
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_hx_family_item (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_audit, row_version, modified_when, modified_by, pk, fk_narrative_condition, fk_relative, name_relative, dob_relative, condition, age_noted, age_of_death, is_cause_of_death
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_audit, OLD.row_version, OLD.modified_when, OLD.modified_by, OLD.pk, OLD.fk_narrative_condition, OLD.fk_relative, OLD.name_relative, OLD.dob_relative, OLD.condition, OLD.age_noted, OLD.age_of_death, OLD.is_cause_of_death
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_hx_family_item() OWNER TO "gm-dbo";

--
-- Name: ft_upd_identity(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_identity() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_identity (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, deleted, gender, dob, fk_marital_status, cob, deceased, title, tob, emergency_contact, fk_emergency_contact, comment, fk_primary_provider, dob_is_estimated
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.deleted, OLD.gender, OLD.dob, OLD.fk_marital_status, OLD.cob, OLD.deceased, OLD.title, OLD.tob, OLD.emergency_contact, OLD.fk_emergency_contact, OLD.comment, OLD.fk_primary_provider, OLD.dob_is_estimated
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_identity() OWNER TO "gm-dbo";

--
-- Name: ft_upd_identity_tag(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_identity_tag() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_identity_tag (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_identity, fk_tag, comment
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_identity, OLD.fk_tag, OLD.comment
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_identity_tag() OWNER TO "gm-dbo";

--
-- Name: ft_upd_inbox_item_category(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_inbox_item_category() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_inbox_item_category (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, description, is_user
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.description, OLD.is_user
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_inbox_item_category() OWNER TO "gm-dbo";

--
-- Name: ft_upd_inbox_item_type(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_inbox_item_type() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_inbox_item_type (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_inbox_item_category, description, is_user
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_inbox_item_category, OLD.description, OLD.is_user
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_inbox_item_type() OWNER TO "gm-dbo";

--
-- Name: ft_upd_incoming_data_unmatchable(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_incoming_data_unmatchable() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_incoming_data_unmatchable (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient_candidates, request_id, firstnames, lastnames, dob, postcode, other_info, type, data, gender, requestor, external_data_id, fk_identity_disambiguated
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient_candidates, OLD.request_id, OLD.firstnames, OLD.lastnames, OLD.dob, OLD.postcode, OLD.other_info, OLD.type, OLD.data, OLD.gender, OLD.requestor, OLD.external_data_id, OLD.fk_identity_disambiguated
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_incoming_data_unmatchable() OWNER TO "gm-dbo";

--
-- Name: ft_upd_incoming_data_unmatched(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_incoming_data_unmatched() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_incoming_data_unmatched (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_patient_candidates, request_id, firstnames, lastnames, dob, postcode, other_info, type, data, gender, requestor, external_data_id, fk_identity_disambiguated, comment, fk_provider_disambiguated
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_patient_candidates, OLD.request_id, OLD.firstnames, OLD.lastnames, OLD.dob, OLD.postcode, OLD.other_info, OLD.type, OLD.data, OLD.gender, OLD.requestor, OLD.external_data_id, OLD.fk_identity_disambiguated, OLD.comment, OLD.fk_provider_disambiguated
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_incoming_data_unmatched() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lab_request(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lab_request() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lab_request (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_item, clin_when, fk_encounter, fk_episode, narrative, soap_cat, pk, fk_test_org, request_id, fk_requestor, lab_request_id, lab_rxd_when, results_reported_when, request_status, is_pending, diagnostic_service_section, ordered_service, note_test_org
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_item, OLD.clin_when, OLD.fk_encounter, OLD.fk_episode, OLD.narrative, OLD.soap_cat, OLD.pk, OLD.fk_test_org, OLD.request_id, OLD.fk_requestor, OLD.lab_request_id, OLD.lab_rxd_when, OLD.results_reported_when, OLD.request_status, OLD.is_pending, OLD.diagnostic_service_section, OLD.ordered_service, OLD.note_test_org
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lab_request() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lab_test_gnr(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lab_test_gnr() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lab_test_gnr (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		id, id_test
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.id, OLD.id_test
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lab_test_gnr() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2aoe(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2aoe() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2aoe (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2aoe() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2episode(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2episode() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2episode (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2episode() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2fhx(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2fhx() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2fhx (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2fhx() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2h_issue(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2h_issue() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2h_issue (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2h_issue() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2narrative(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2narrative() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2narrative (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2narrative() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2procedure(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2procedure() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2procedure (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2procedure() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2rfe(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2rfe() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2rfe (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2rfe() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_code2tst_pnl(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_code2tst_pnl() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_code2tst_pnl (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk_lnk_code2item, fk_generic_code, fk_item, code_modifier, pk
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk_lnk_code2item, OLD.fk_generic_code, OLD.fk_item, OLD.code_modifier, OLD.pk
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_code2tst_pnl() OWNER TO "gm-dbo";

--
-- Name: ft_upd_lnk_constraint2vacc_course(); Type: FUNCTION; Schema: audit; Owner: gm-dbo
--

CREATE FUNCTION audit.ft_upd_lnk_constraint2vacc_course() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
	_is_allowed_updater boolean;
BEGIN
	-- is the session user allowed to update data ?
	SELECT gm.account_is_dbowner_or_staff(SESSION_USER) INTO STRICT _is_allowed_updater;
	IF _is_allowed_updater IS FALSE THEN
		RAISE EXCEPTION
			'UPDATE: gm.account_is_dbowner_or_staff(NAME): <%> is neither database owner, nor <postgres>, nor on staff', SESSION_USER
			USING ERRCODE = 'integrity_constraint_violation'
		;
		return NEW;
	END IF;
	NEW.row_version := OLD.row_version + 1;
	NEW.modified_when := CURRENT_TIMESTAMP;
	NEW.modified_by := SESSION_USER;
	INSERT INTO audit.log_lnk_constraint2vacc_course (
		orig_version, orig_when, orig_by, orig_tableoid, audit_action,
		pk, fk_vaccination_course, fk_constraint
	) VALUES (
		OLD.row_version, OLD.modified_when, OLD.modified_by, TG_RELID, TG_OP,
		OLD.pk, OLD.fk_vaccination_course, OLD.fk_constraint
	);
	return NEW;
END;$$;


ALTER FUNCTION audit.ft_upd_lnk_constraint2vacc_course() OWNER TO "gm-dbo";

--
