generated from teksi/template
-
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.
Add modification_last_modified functions
Add modification_last_modified functions
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
------ LAST MODIFIED ----- | ||
CREATE FUNCTION tdh_app.modification_last_modified() RETURNS trigger AS $$ | ||
BEGIN | ||
NEW.last_modification := TIMEOFDAY(); | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE PLPGSQL; | ||
|
||
CREATE FUNCTION tdh_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 tdh_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 = 'tdh_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 tdh_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 = 'tdh_app' | ||
|
||
AND t.tgenabled = 'D'; | ||
RETURN _disabled_count=0; | ||
END; | ||
$DO$ | ||
LANGUAGE plpgsql SECURITY DEFINER; |