Debian Codegraph @

contrib/hstore_plpython/sql/hstore_plpython.sql

raw
1 CREATE EXTENSION hstore_plpython3u CASCADE;
2
3
4 -- test hstore -> python
5 CREATE FUNCTION test1(val hstore) RETURNS int
6 LANGUAGE plpython3u
7 TRANSFORM FOR TYPE hstore
8 AS $$
9 assert isinstance(val, dict)
10 plpy.info(sorted(val.items()))
11 return len(val)
12 $$;
13
14 SELECT test1('aa=>bb, cc=>NULL'::hstore);
15
16
17 -- the same with the versioned language name
18 CREATE FUNCTION test1n(val hstore) RETURNS int
19 LANGUAGE plpython3u
20 TRANSFORM FOR TYPE hstore
21 AS $$
22 assert isinstance(val, dict)
23 plpy.info(sorted(val.items()))
24 return len(val)
25 $$;
26
27 SELECT test1n('aa=>bb, cc=>NULL'::hstore);
28
29
30 -- test that a non-mapping result is correctly rejected
31 CREATE FUNCTION test1bad() RETURNS hstore
32 LANGUAGE plpython3u
33 TRANSFORM FOR TYPE hstore
34 AS $$
35 return "foo"
36 $$;
37
38 SELECT test1bad();
39
40
41 -- A mapping whose items() raises should be reported as an error, not crash
42 -- the backend
43 CREATE FUNCTION test1broken() RETURNS hstore
44 LANGUAGE plpython3u
45 TRANSFORM FOR TYPE hstore
46 AS $$
47 class C(dict):
48 def items(self):
49 raise ValueError('items failed')
50 d = C()
51 d['x'] = 1
52 return d
53 $$;
54
55 SELECT test1broken();
56
57
58 -- Likewise for a mapping whose items() does not return key/value pairs
59 CREATE FUNCTION test1malformed() RETURNS hstore
60 LANGUAGE plpython3u
61 TRANSFORM FOR TYPE hstore
62 AS $$
63 class C(dict):
64 def items(self):
65 return [42]
66 d = C()
67 d['x'] = 1
68 return d
69 $$;
70
71 SELECT test1malformed();
72
73
74 -- Likewise for a mapping whose items() yields fewer pairs than its length
75 CREATE FUNCTION test1short() RETURNS hstore
76 LANGUAGE plpython3u
77 TRANSFORM FOR TYPE hstore
78 AS $$
79 class C(dict):
80 def items(self):
81 return []
82 d = C()
83 d['x'] = 1
84 return d
85 $$;
86
87 SELECT test1short();
88
89
90 -- Likewise for a mapping whose __len__() raises
91 CREATE FUNCTION test1brokenlen() RETURNS hstore
92 LANGUAGE plpython3u
93 TRANSFORM FOR TYPE hstore
94 AS $$
95 class C(dict):
96 def __len__(self):
97 raise ValueError('len failed')
98 d = C()
99 d['x'] = 1
100 return d
101 $$;
102
103 SELECT test1brokenlen();
104
105
106 -- test hstore[] -> python
107 CREATE FUNCTION test1arr(val hstore[]) RETURNS int
108 LANGUAGE plpython3u
109 TRANSFORM FOR TYPE hstore
110 AS $$
111 assert(val == [{'aa': 'bb', 'cc': None}, {'dd': 'ee'}])
112 return len(val)
113 $$;
114
115 SELECT test1arr(array['aa=>bb, cc=>NULL'::hstore, 'dd=>ee']);
116
117
118 -- test python -> hstore
119 CREATE FUNCTION test2(a int, b text) RETURNS hstore
120 LANGUAGE plpython3u
121 TRANSFORM FOR TYPE hstore
122 AS $$
123 val = {'a': a, 'b': b, 'c': None}
124 return val
125 $$;
126
127 SELECT test2(1, 'boo');
128
129 --- test ruleutils
130 \sf test2
131
132
133 -- test python -> hstore[]
134 CREATE FUNCTION test2arr() RETURNS hstore[]
135 LANGUAGE plpython3u
136 TRANSFORM FOR TYPE hstore
137 AS $$
138 val = [{'a': 1, 'b': 'boo', 'c': None}, {'d': 2}]
139 return val
140 $$;
141
142 SELECT test2arr();
143
144
145 -- test python -> domain over hstore
146 CREATE DOMAIN hstore_foo AS hstore CHECK(VALUE ? 'foo');
147
148 CREATE FUNCTION test2dom(fn text) RETURNS hstore_foo
149 LANGUAGE plpython3u
150 TRANSFORM FOR TYPE hstore
151 AS $$
152 return {'a': 1, fn: 'boo', 'c': None}
153 $$;
154
155 SELECT test2dom('foo');
156 SELECT test2dom('bar'); -- fail
157
158
159 -- test as part of prepare/execute
160 CREATE FUNCTION test3() RETURNS void
161 LANGUAGE plpython3u
162 TRANSFORM FOR TYPE hstore
163 AS $$
164 rv = plpy.execute("SELECT 'aa=>bb, cc=>NULL'::hstore AS col1")
165 assert(rv[0]["col1"] == {'aa': 'bb', 'cc': None})
166
167 val = {'a': 1, 'b': 'boo', 'c': None}
168 plan = plpy.prepare("SELECT $1::text AS col1", ["hstore"])
169 rv = plpy.execute(plan, [val])
170 assert(rv[0]["col1"] == '"a"=>"1", "b"=>"boo", "c"=>NULL')
171 $$;
172
173 SELECT test3();
174
175
176 -- test trigger
177 CREATE TABLE test1 (a int, b hstore);
178 INSERT INTO test1 VALUES (1, 'aa=>bb, cc=>NULL');
179 SELECT * FROM test1;
180
181 CREATE FUNCTION test4() RETURNS trigger
182 LANGUAGE plpython3u
183 TRANSFORM FOR TYPE hstore
184 AS $$
185 assert(TD["new"] == {'a': 1, 'b': {'aa': 'bb', 'cc': None}})
186 if TD["new"]["a"] == 1:
187 TD["new"]["b"] = {'a': 1, 'b': 'boo', 'c': None}
188
189 return "MODIFY"
190 $$;
191
192 CREATE TRIGGER test4 BEFORE UPDATE ON test1 FOR EACH ROW EXECUTE PROCEDURE test4();
193
194 UPDATE test1 SET a = a;
195 SELECT * FROM test1;