let administrators modify package contents even if they are not marked as such to...
[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 BEGIN
56   IF (pkgMayBeModified(TG_TABLE_SCHEMA) OR isDba()) THEN
57     IF (TG_OP = 'DELETE') THEN
58       RETURN OLD;
59     ELSE
60       RETURN NEW;
61     END IF;
62   END IF;
63
64   -- cannot combine IF's because plpgsql does not always evaluate left-to-right
65   IF (TG_OP = 'INSERT') THEN
66     IF (NEW.metasql_grade <= 0 AND NOT _isdba) THEN
67       RAISE EXCEPTION 'You may not create grade 0 MetaSQL statements in packages except using the xTuple Updater utility';
68     END IF;
69
70   ELSIF (TG_OP = 'UPDATE') THEN
71     IF (NEW.metasql_grade <= 0 AND NOT _isdba) THEN
72       RAISE EXCEPTION 'You may not alter grade 0 MetaSQL statements in packages except using the xTuple Updater utility';
73     END IF;
74
75   ELSIF (TG_OP = 'DELETE') THEN
76     IF (OLD.metasql_grade <= 0 AND NOT _isdba) THEN
77       RAISE EXCEPTION 'You may not delete grade 0 MetaSQL statements from packages. Try deleting or disabling the package.';
78     ELSE
79       RETURN OLD;
80     END IF;
81
82   END IF;
83
84   RETURN NEW;
85 END;
86 $$ LANGUAGE 'plpgsql';
87
88 CREATE OR REPLACE FUNCTION _pkgmetasqlaftertrigger() RETURNS TRIGGER AS $$
89 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
90 -- See www.xtuple.com/CPAL for the full text of the software license.
91 BEGIN
92   IF (TG_OP = 'DELETE') THEN
93     RETURN OLD;
94   END IF;
95
96   RETURN NEW;
97 END;
98 $$ LANGUAGE 'plpgsql';