-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdomains.sql
More file actions
50 lines (44 loc) · 1.83 KB
/
Copy pathdomains.sql
File metadata and controls
50 lines (44 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--
-- Domain types. A PL/php function sees a domain as its underlying base type
-- (scalar, array or composite), and a value returned into a domain result is
-- checked against the domain's constraints.
--
CREATE DOMAIN posint AS int CHECK (VALUE > 0);
CREATE DOMAIN shortstr AS text CHECK (length(VALUE) <= 5);
CREATE DOMAIN intvec AS int[] CHECK (cardinality(VALUE) > 0);
-- A scalar domain argument arrives as the base value; the CHECK on the input
-- has already been enforced by the caller.
CREATE FUNCTION dom_scalar(posint, shortstr) RETURNS text LANGUAGE plphp AS $$
return $args[0] . "/" . strtoupper($args[1]);
$$;
SELECT dom_scalar(7, 'abc');
-- Returning into a scalar domain enforces its CHECK.
CREATE FUNCTION dom_double(int) RETURNS posint LANGUAGE plphp AS $$
return $args[0] * 2;
$$;
SELECT dom_double(21);
SELECT dom_double(-1); -- violates posint
-- A domain over an array arrives as a PHP array, and a PHP array can be
-- returned into it (the domain CHECK still applies).
CREATE FUNCTION dom_arr_in(intvec) RETURNS int LANGUAGE plphp AS $$
return is_array($args[0]) ? array_sum($args[0]) : -1;
$$;
SELECT dom_arr_in('{10,20,30}');
CREATE FUNCTION dom_arr_out(int) RETURNS intvec LANGUAGE plphp AS $$
$out = array();
for ($i = 1; $i <= $args[0]; $i++)
$out[] = $i * $i;
return $out;
$$;
SELECT dom_arr_out(4);
SELECT dom_arr_out(0); -- empty array violates intvec's CHECK
-- A domain used as a composite field is unwrapped there too.
CREATE TYPE drow AS (id posint, tag shortstr);
CREATE FUNCTION dom_field(drow) RETURNS text LANGUAGE plphp AS $$
return $args[0]['id'] . ":" . $args[0]['tag'];
$$;
SELECT dom_field(ROW(3, 'ok')::drow);
DROP FUNCTION dom_scalar(posint, shortstr), dom_double(int),
dom_arr_in(intvec), dom_arr_out(int), dom_field(drow);
DROP TYPE drow;
DROP DOMAIN posint, shortstr, intvec;