forked from teksi/wastewater
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
90 changed files
with
900 additions
and
1,331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
------ LAST MODIFIED ----- | ||
CREATE FUNCTION tww_app.modification_last_modified() RETURNS trigger AS $$ | ||
BEGIN | ||
NEW.last_modification := TIMEOFDAY(); | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE PLPGSQL; | ||
|
||
CREATE FUNCTION tww_app.modification_last_modified_parent() RETURNS trigger AS $$ | ||
DECLARE | ||
table_name TEXT; | ||
BEGIN | ||
table_name = TG_ARGV[0]; | ||
|
||
EXECUTE ' | ||
UPDATE ' || table_name || ' | ||
SET last_modification = TIMEOFDAY()::timestamp | ||
WHERE obj_id = ''' || NEW.obj_id || ''' | ||
'; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE PLPGSQL; | ||
|
||
CREATE OR REPLACE FUNCTION tww_app.alter_modification_triggers(action_name text) RETURNS VOID AS | ||
$DO$ | ||
DECLARE | ||
schdf text; | ||
tbldf text; | ||
trig text; | ||
BEGIN | ||
IF NOT (action_name ILIKE ANY(ARRAY['ENABLE','DISABLE'])) THEN | ||
RAISE NOTICE '% not a valid input',action_name; | ||
RETURN; | ||
ELSE | ||
FOR schdf,tbldf, trig IN | ||
SELECT | ||
c.relnamespace ::regnamespace::text, | ||
c.relname, | ||
t.tgname | ||
FROM pg_trigger t | ||
INNER JOIN pg_class c on t.tgrelid=c.oid | ||
INNER JOIN pg_proc p on t.tgfoid=p.oid | ||
WHERE p.proname LIKE 'modification_%' | ||
AND p.pronamespace::regnamespace::text = 'tww_app' | ||
LOOP | ||
EXECUTE FORMAT('ALTER TABLE %I.%I %s TRIGGER %I',schdf,tbldf,upper(action_name),trig); | ||
END LOOP; | ||
|
||
RETURN; | ||
END IF; | ||
END; | ||
$DO$ | ||
LANGUAGE plpgsql SECURITY DEFINER; | ||
|
||
CREATE OR REPLACE FUNCTION tww_app.check_modification_enabled() RETURNS BOOL AS | ||
$DO$ | ||
DECLARE _disabled_count numeric; | ||
BEGIN | ||
SELECT count(*) into _disabled_count | ||
FROM pg_trigger t | ||
INNER JOIN pg_class c on t.tgrelid=c.oid | ||
INNER JOIN pg_proc p on t.tgfoid=p.oid | ||
WHERE | ||
p.proname LIKE 'modification_%' | ||
AND p.pronamespace::regnamespace::text = 'tww_app' | ||
|
||
AND t.tgenabled = 'D'; | ||
RETURN _disabled_count=0; | ||
END; | ||
$DO$ | ||
LANGUAGE plpgsql SECURITY DEFINER; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-- function for generating StandardOIDs | ||
|
||
CREATE OR REPLACE FUNCTION tww_app.generate_oid(schema_name text, table_name text) | ||
RETURNS text AS | ||
$BODY$ | ||
DECLARE | ||
myrec_prefix record; | ||
myrec_shortcut record; | ||
myrec_seq record; | ||
BEGIN | ||
-- first we have to get the OID prefix | ||
BEGIN | ||
SELECT prefix::text INTO myrec_prefix FROM tww_sys.oid_prefixes WHERE active = TRUE; | ||
EXCEPTION | ||
WHEN NO_DATA_FOUND THEN | ||
RAISE EXCEPTION 'no active record found in table tww_sys.oid_prefixes'; | ||
WHEN TOO_MANY_ROWS THEN | ||
RAISE EXCEPTION 'more than one active records found in table tww_sys.oid_prefixes'; | ||
END; | ||
-- test if prefix is of correct length | ||
IF char_length(myrec_prefix.prefix) != 8 THEN | ||
RAISE EXCEPTION 'character length of prefix must be 8'; | ||
END IF; | ||
--get table 2char shortcut | ||
BEGIN | ||
SELECT shortcut_en INTO STRICT myrec_shortcut FROM tww_sys.dictionary_od_table WHERE tablename = table_name; | ||
EXCEPTION | ||
WHEN NO_DATA_FOUND THEN | ||
RAISE EXCEPTION 'dictionary entry for table % not found', table_name; | ||
WHEN TOO_MANY_ROWS THEN | ||
RAISE EXCEPTION 'dictonary entry for table % not unique', table_name; | ||
END; | ||
--get sequence for table | ||
EXECUTE format('SELECT nextval(''%1$I.seq_%2$I_oid'') AS seqval', schema_name, table_name) INTO myrec_seq; | ||
IF NOT FOUND THEN | ||
RAISE EXCEPTION 'sequence for table % not found', table_name; | ||
END IF; | ||
RETURN myrec_prefix.prefix || myrec_shortcut.shortcut_en || to_char(myrec_seq.seqval,'FM000000'); | ||
END; | ||
$BODY$ | ||
LANGUAGE plpgsql STABLE | ||
COST 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
CREATE OR REPLACE FUNCTION tww_app.set_organisations_active(_undo_existing boolean default false) | ||
RETURNS void AS | ||
$BODY$ | ||
DECLARE | ||
schm text; | ||
tbl text; | ||
col text; | ||
BEGIN | ||
IF _undo_existing THEN | ||
UPDATE tww_od.organisation | ||
SET tww_active=FALSE; | ||
ELSE NULL; | ||
END IF; | ||
FOR schm,tbl,col IN | ||
SELECT tc.table_schema, | ||
tc.table_name, | ||
kcu.column_name | ||
FROM (SELECT constraint_name FROM information_schema.constraint_column_usage | ||
WHERE table_schema='tww_od' | ||
AND table_name='organisation' | ||
AND column_name='obj_id' | ||
AND constraint_name NOT LIKE'pkey%')ccu | ||
INNER JOIN information_schema.key_column_usage AS kcu | ||
ON ccu.constraint_name = kcu.constraint_name | ||
INNER JOIN information_schema.table_constraints AS tc | ||
ON tc.constraint_name = kcu.constraint_name | ||
LOOP | ||
EXECUTE format($$ UPDATE tww_od.organisation | ||
SET tww_active=TRUE | ||
WHERE obj_id IN (SELECT DISTINCT %I FROM %I.%I)$$, col,schm,tbl); | ||
END LOOP; | ||
END; | ||
$BODY$ | ||
LANGUAGE plpgsql VOLATILE; | ||
|
||
-- function for retrieving default obj_id | ||
CREATE OR REPLACE FUNCTION tww_app.get_default_values(field_name text) | ||
RETURNS varchar(16) AS | ||
$BODY$ | ||
DECLARE | ||
myrec record; | ||
BEGIN | ||
BEGIN | ||
SELECT value_obj_id::varchar(16) INTO myrec FROM tww_od.default_values WHERE fieldname = field_name; | ||
EXCEPTION | ||
WHEN NO_DATA_FOUND THEN | ||
RAISE WARNING 'Field name % not found in table tww_od.default_values. Returning NULL',field_name; | ||
RETURN NULL; | ||
END; | ||
RETURN myrec.value_obj_id; | ||
END; | ||
$BODY$ | ||
LANGUAGE plpgsql STABLE SECURITY DEFINER | ||
COST 100; | ||
|
||
-- Set defaults on all fk_provider,fk_dataowner,fk_owner | ||
CREATE OR REPLACE FUNCTION tww_app.ft_set_default_values() | ||
RETURNS TRIGGER AS | ||
$BODY$ | ||
DECLARE | ||
sch text; | ||
tbl text; | ||
col text; | ||
ttp text; | ||
BEGIN | ||
FOR sch,tbl,col,ttp IN | ||
SELECT | ||
t.table_schema, | ||
c.table_name, | ||
c.column_name, | ||
CASE WHEN t.table_type = 'BASE TABLE' then 'TABLE' ELSE t.table_type END | ||
|
||
FROM information_schema.columns c | ||
LEFT JOIN information_schema.tables t | ||
ON c.table_name = t.table_name | ||
and c.table_schema = t.table_schema | ||
WHERE c.column_name = NEW.fieldname | ||
and ((c.table_schema ='tww_od' | ||
and t.table_type = 'BASE TABLE') | ||
OR (c.table_schema ='tww_app' | ||
and t.table_type = 'VIEW')) | ||
LOOP | ||
EXECUTE format($$ ALTER %4$s %1$I.%2$I ALTER COLUMN %3$I SET DEFAULT tww_app.get_default_values('%2$s') $$, sch,tbl,col,ttp); | ||
END LOOP; | ||
RETURN NEW; | ||
END; | ||
$BODY$ | ||
LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION tww_app.ft_unset_default_values() | ||
RETURNS TRIGGER AS | ||
$BODY$ | ||
DECLARE | ||
sch text; | ||
tbl text; | ||
col text; | ||
ttp text; | ||
BEGIN | ||
FOR sch,tbl,col,ttp IN | ||
SELECT | ||
t.table_schema, | ||
c.table_name, | ||
c.column_name, | ||
CASE WHEN t.table_type = 'BASE TABLE' then 'TABLE' ELSE t.table_type END | ||
|
||
FROM information_schema.columns c | ||
LEFT JOIN information_schema.tables t | ||
ON c.table_name = t.table_name | ||
and c.table_schema = t.table_schema | ||
WHERE c.column_name = OLD.fieldname | ||
and ((c.table_schema ='tww_od' | ||
and t.table_type = 'BASE TABLE') | ||
OR (c.table_schema ='tww_app' | ||
and t.table_type = 'VIEW')) | ||
LOOP | ||
EXECUTE format($$ ALTER %4$s %1$I.%2$I ALTER COLUMN %3$I DROP DEFAULT $$, sch,tbl,col,ttp); | ||
END LOOP; | ||
RETURN NULL; | ||
END; | ||
$BODY$ | ||
LANGUAGE plpgsql; | ||
|
||
|
||
DROP TRIGGER IF EXISTS set_default_values ON tww_od.default_values; | ||
|
||
CREATE TRIGGER set_default_values | ||
BEFORE INSERT OR UPDATE | ||
ON tww_od.default_values | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE tww_app.ft_set_default_values(); | ||
|
||
DROP TRIGGER IF EXISTS unset_default_values ON tww_od.default_values; | ||
|
||
CREATE TRIGGER unset_default_values | ||
BEFORE DELETE | ||
ON tww_od.default_values | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE tww_app.ft_unset_default_values(); |
File renamed without changes.
Oops, something went wrong.