-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenum.sql
More file actions
80 lines (68 loc) · 1.58 KB
/
enum.sql
File metadata and controls
80 lines (68 loc) · 1.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- ENUMs aren't very special.
-- They just need the typoid as the second argument.
DROP DOMAIN IF EXISTS dfoo;
DROP TYPE IF EXISTS foo;
CREATE TYPE foo AS ENUM (
'zero',
'one',
'two',
'three',
'four'
);
CREATE DOMAIN dfoo AS foo;
CREATE OR REPLACE FUNCTION enummy(e foo) RETURNS foo LANGUAGE python AS
$python$
def main(e):
return 'zero'
$python$;
SELECT enummy('two'::foo);
SELECT enummy('zero'::foo);
SELECT enummy('three'::foo);
-- Check instantiation about some comparisons.
CREATE OR REPLACE FUNCTION enum_ops(e foo) RETURNS foo LANGUAGE python AS
$python$
def main(e):
et = type(e)
zero = et('zero')
one = et('one')
two = et('two')
assert zero == zero
assert zero < one
assert one < two
assert zero < two
assert not (zero > two)
assert not (zero > one)
return et('three')
$python$;
SELECT enum_ops('one'::foo);
SELECT enum_ops('two'::foo);
SELECT enum_ops('zero'::foo);
-- same code, but now the domain
CREATE OR REPLACE FUNCTION enummy(e dfoo) RETURNS foo LANGUAGE python AS
$python$
def main(e):
return 'zero'
$python$;
SELECT enummy('two'::dfoo);
SELECT enummy('zero'::dfoo);
SELECT enummy('three'::dfoo);
-- Check instantiation about some comparisons.
CREATE OR REPLACE FUNCTION enum_ops(e dfoo) RETURNS foo LANGUAGE python AS
$python$
import Postgres
def main(e):
et = type(e)
zero = et('zero')
one = et('one')
two = et('two')
assert zero == zero
assert zero < one
assert one < two
assert zero < two
assert not (zero > two)
assert not (zero > one)
return et('three')
$python$;
SELECT enum_ops('one'::dfoo);
SELECT enum_ops('two'::dfoo);
SELECT enum_ops('zero'::dfoo);