1 -- NO create trigger statements. the updater will create them.
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.
9 _isdba BOOLEAN := false;
12 SELECT rolsuper INTO _isdba FROM pg_roles WHERE (rolname=getEffectiveXtUser());
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;
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;
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
27 WHERE metasql_name=NEW.metasql_name AND metasql_group=NEW.metasql_group AND metasql_grade=NEW.metasql_grade;
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;
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
38 WHERE metasql_name=NEW.metasql_name AND metasql_group=NEW.metasql_group AND metasql_grade=NEW.metasql_grade;
40 RAISE EXCEPTION 'The new MetaSQL statement %-% % conflicts with an existing statement.',
41 NEW.metasql_group, NEW.metasql_name, NEW.metasql_grade;
44 ELSIF (TG_OP = 'DELETE') THEN
50 $$ LANGUAGE 'plpgsql';
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.
56 _isdba BOOLEAN := false;
59 SELECT rolsuper INTO _isdba FROM pg_roles WHERE (rolname=getEffectiveXtUser());
61 IF (pkgMayBeModified(TG_TABLE_SCHEMA)) THEN
62 IF (TG_OP = 'DELETE') THEN
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';
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';
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.';
91 $$ LANGUAGE 'plpgsql';
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.
97 IF (TG_OP = 'DELETE') THEN
103 $$ LANGUAGE 'plpgsql';