10f7f078afaf1d1ec4262ca823ccc723c0d30603
[xtuple] / foundation-database / public / trigger_functions / pkgmetasql.sql
1 -- NO create trigger statements. the updater will create them.
2
3 SELECT dropIfExists('TRIGGER', 'pkgmetasqlbeforetrigger');
4 CREATE OR REPLACE FUNCTION _pkgmetasqlbeforetrigger() RETURNS "trigger" AS $$
5 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
6 -- See www.xtuple.com/CPAL for the full text of the software license.
7 DECLARE
8   _metasqlid    INTEGER;
9   _isdba        BOOLEAN := false;
10
11 BEGIN
12   SELECT rolsuper INTO _isdba FROM pg_roles WHERE (rolname=getEffectiveXtUser());
13
14   IF (NOT (_isdba OR checkPrivilege('MaintainMetaSQL'))) THEN
15     RAISE EXCEPTION '% does not have privileges to maintain MetaSQL statements in %.% (DBA=%)',
16                 getEffectiveXtUser(), TG_TABLE_SCHEMA, TG_TABLE_NAME, _isdba;
17   END IF;
18
19   IF (TG_OP = 'UPDATE') THEN
20     RAISE DEBUG 'update OLD %-%-%, NEW %-%-%',
21                  OLD.metasql_group, OLD.metasql_name, OLD.metasql_grade,
22                  NEW.metasql_group, NEW.metasql_name, NEW.metasql_grade;
23
24     IF (NEW.metasql_name != OLD.metasql_name OR NEW.metasql_group != OLD.metasql_group OR NEW.metasql_grade != OLD.metasql_grade) THEN
25       SELECT metasql_id INTO _metasqlid
26       FROM metasql
27       WHERE metasql_name=NEW.metasql_name AND metasql_group=NEW.metasql_group AND metasql_grade=NEW.metasql_grade;
28       IF (FOUND) THEN
29         RAISE EXCEPTION 'Cannot change the MetaSQL statement named %-%-% because another MetaSQL statement with that group, name and grade already exists.', NEW.metasql_group, NEW.metasql_name, NEW.metasql_grade;
30       END IF;
31     END IF;
32
33   ELSIF (TG_OP = 'INSERT') THEN
34     RAISE DEBUG 'insert NEW %-% %',
35                  NEW.metasql_group, NEW.metasql_name, NEW.metasql_grade;
36     SELECT metasql_id INTO _metasqlid
37     FROM metasql
38     WHERE metasql_name=NEW.metasql_name AND metasql_group=NEW.metasql_group AND metasql_grade=NEW.metasql_grade;
39     IF (FOUND) THEN
40       RAISE EXCEPTION 'The new MetaSQL statement %-% % conflicts with an existing statement.',
41                       NEW.metasql_group, NEW.metasql_name, NEW.metasql_grade;
42     END IF;
43
44   ELSIF (TG_OP = 'DELETE') THEN
45     RETURN OLD;
46   END IF;
47
48   RETURN NEW;
49 END;
50 $$ LANGUAGE 'plpgsql';
51
52 CREATE OR REPLACE FUNCTION _pkgmetasqlalterTrigger() RETURNS TRIGGER AS $$
53 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
54 -- See www.xtuple.com/CPAL for the full text of the software license.
55 DECLARE
56   _isdba        BOOLEAN := false;
57
58 BEGIN
59   SELECT rolsuper INTO _isdba FROM pg_roles WHERE (rolname=getEffectiveXtUser());
60
61   IF (pkgMayBeModified(TG_TABLE_SCHEMA)) THEN
62     IF (TG_OP = 'DELETE') THEN
63       RETURN OLD;
64     ELSE
65       RETURN NEW;
66     END IF;
67   END IF;
68
69   -- cannot combine IF's because plpgsql does not always evaluate left-to-right
70   IF (TG_OP = 'INSERT') THEN
71     IF (NEW.metasql_grade <= 0 AND NOT _isdba) THEN
72       RAISE EXCEPTION 'You may not create grade 0 MetaSQL statements in packages except using the xTuple Updater utility';
73     END IF;
74
75   ELSIF (TG_OP = 'UPDATE') THEN
76     IF (NEW.metasql_grade <= 0 AND NOT _isdba) THEN
77       RAISE EXCEPTION 'You may not alter grade 0 MetaSQL statements in packages except using the xTuple Updater utility';
78     END IF;
79
80   ELSIF (TG_OP = 'DELETE') THEN
81     IF (OLD.metasql_grade <= 0 AND NOT _isdba) THEN
82       RAISE EXCEPTION 'You may not delete grade 0 MetaSQL statements from packages. Try deleting or disabling the package.';
83     ELSE
84       RETURN OLD;
85     END IF;
86
87   END IF;
88
89   RETURN NEW;
90 END;
91 $$ LANGUAGE 'plpgsql';
92
93 CREATE OR REPLACE FUNCTION _pkgmetasqlaftertrigger() RETURNS TRIGGER AS $$
94 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
95 -- See www.xtuple.com/CPAL for the full text of the software license.
96 BEGIN
97   IF (TG_OP = 'DELETE') THEN
98     RETURN OLD;
99   END IF;
100
101   RETURN NEW;
102 END;
103 $$ LANGUAGE 'plpgsql';