let administrators modify package contents even if they are not marked as such to...
[xtuple] / foundation-database / public / trigger_functions / pkgimage.sql
1 -- NO create trigger statements. the updater will create them.
2 SELECT dropIfExists('TRIGGER', 'pkgimagebeforetrigger');
3 CREATE OR REPLACE FUNCTION _pkgimagebeforetrigger() RETURNS "trigger" AS $$
4 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
5 -- See www.xtuple.com/CPAL for the full text of the software license.
6 DECLARE
7   _imageid     INTEGER;
8   _debug       BOOL := false;
9
10 BEGIN
11   IF (TG_OP = 'UPDATE') THEN
12     IF (_debug) THEN
13       RAISE NOTICE 'OLD.image_name %, NEW.image_name %',
14                    OLD.image_name, NEW.image_name;
15     END IF;
16
17     IF (NEW.image_name != OLD.image_name) THEN
18       SELECT image_id INTO _imageid FROM image WHERE image_name=NEW.image_name;
19       IF (FOUND) THEN
20         RAISE EXCEPTION 'Cannot change image named % because another image with that name already exists.', NEW.image_name;
21       END IF;
22     END IF;
23
24   ELSIF (TG_OP = 'INSERT') THEN
25     IF (_debug) THEN
26       RAISE NOTICE 'inserting NEW.image_name %', NEW.image_name;
27     END IF;
28     SELECT image_id INTO _imageid FROM image WHERE image_name=NEW.image_name;
29     IF (FOUND) THEN
30       RAISE EXCEPTION 'Cannot create new image % because another image with that name already exists.', NEW.image_name;
31     END IF;
32
33   ELSIF (TG_OP = 'DELETE') THEN
34     RETURN OLD;
35
36   END IF;
37
38   RETURN NEW;
39 END;
40 $$ LANGUAGE 'plpgsql';
41
42 CREATE OR REPLACE FUNCTION _pkgimagealterTrigger() RETURNS TRIGGER AS $$
43 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
44 -- See www.xtuple.com/CPAL for the full text of the software license.
45 BEGIN
46   IF (pkgMayBeModified(TG_TABLE_SCHEMA) OR isDba()) THEN
47     IF (TG_OP = 'DELETE') THEN
48       RETURN OLD;
49     ELSE
50       RETURN NEW;
51     END IF;
52   END IF;
53
54   IF (TG_OP = 'INSERT') THEN
55     RAISE EXCEPTION 'You may not create images in packages except using the xTuple Updater utility';
56
57   ELSIF (TG_OP = 'UPDATE') THEN
58     RAISE EXCEPTION 'You may not alter images in packages except using the xTuple Updater utility';
59
60   ELSIF (TG_OP = 'DELETE') THEN
61     RAISE EXCEPTION 'You may not delete images from packages. Try deleting or disabling the package.';
62
63   END IF;
64
65   RETURN NEW;
66 END;
67 $$ LANGUAGE 'plpgsql';
68
69 CREATE OR REPLACE FUNCTION _pkgimageaftertrigger() RETURNS TRIGGER AS $$
70 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
71 -- See www.xtuple.com/CPAL for the full text of the software license.
72 BEGIN
73   IF (TG_OP = 'DELETE') THEN
74     RETURN OLD;
75   END IF;
76
77   RETURN NEW;
78 END;
79 $$ LANGUAGE 'plpgsql';