Create database support for IS [NOT] NULL expressions
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0269.schema.query-xisnull.sql
1 BEGIN;
2
3 -- Create new expression type for IS [NOT] NULL
4
5 INSERT INTO config.upgrade_log (version) VALUES ('0269'); -- Scott McKellar
6
7 ALTER TABLE query.expression
8         DROP CONSTRAINT expression_type;
9
10 ALTER TABLE query.expression
11         ADD CONSTRAINT expression_type CHECK
12     ( type IN (
13                 'xbet',    -- between
14                 'xbind',   -- bind variable
15                 'xbool',   -- boolean
16                 'xcase',   -- case
17                 'xcast',   -- cast
18                 'xcol',    -- column
19                 'xex',     -- exists
20                 'xfld',    -- field
21                 'xfunc',   -- function
22                 'xin',     -- in
23                 'xisnull'  -- is null
24                 'xnull',   -- null
25                 'xnum',    -- number
26                 'xop',     -- operator
27                 'xstr',    -- string
28                 'xsubq'    -- subquery
29         ) );
30
31 -- Create updatable view for IS NULL expressions
32
33 CREATE OR REPLACE VIEW query.expr_xisnull AS
34     SELECT
35                 id,
36                 parenthesize,
37                 parent_expr,
38                 seq_no,
39                 left_operand,
40                 negate
41     FROM
42         query.expression
43     WHERE
44         type = 'xisnull';
45
46 CREATE OR REPLACE RULE query_expr_xisnull_insert_rule AS
47     ON INSERT TO query.expr_xisnull
48     DO INSTEAD
49     INSERT INTO query.expression (
50                 id,
51                 type,
52                 parenthesize,
53                 parent_expr,
54                 seq_no,
55                 left_operand,
56                 negate
57     ) VALUES (
58         COALESCE(NEW.id, NEXTVAL('query.expression_id_seq'::REGCLASS)),
59         'xisnull',
60         COALESCE(NEW.parenthesize, FALSE),
61         NEW.parent_expr,
62         COALESCE(NEW.seq_no, 1),
63                 NEW.left_operand,
64                 NEW.negate
65     );
66
67 CREATE OR REPLACE RULE query_expr_xisnull_update_rule AS
68     ON UPDATE TO query.expr_xisnull
69     DO INSTEAD
70     UPDATE query.expression SET
71         id = NEW.id,
72         parenthesize = NEW.parenthesize,
73         parent_expr = NEW.parent_expr,
74         seq_no = NEW.seq_no,
75                 left_operand = NEW.left_operand,
76                 negate = NEW.negate
77     WHERE
78         id = OLD.id;
79
80 CREATE OR REPLACE RULE query_expr_xisnull_delete_rule AS
81     ON DELETE TO query.expr_xisnull
82     DO INSTEAD
83     DELETE FROM query.expression WHERE id = OLD.id;
84
85 COMMIT;