Debian Codegraph @

src/pl/tcl/sql/pltcl_queries.sql

raw
1 -- Test composite-type arguments
2 select tcl_composite_arg_ref1(row('tkey', 42, 'ref2'));
3 select tcl_composite_arg_ref2(row('tkey', 42, 'ref2'));
4
5 -- More tests for composite argument/result types
6
7 create domain d_comp1 as T_comp1 check ((value).ref1 > 0);
8
9 create function tcl_record_arg(record, fldname text) returns int as '
10 return $1($2)
11 ' language pltcl;
12
13 select tcl_record_arg(row('tkey', 42, 'ref2')::T_comp1, 'ref1');
14 select tcl_record_arg(row('tkey', 42, 'ref2')::d_comp1, 'ref1');
15 select tcl_record_arg(row(2,4), 'f2');
16
17 create function tcl_cdomain_arg(d_comp1) returns int as '
18 return $1(ref1)
19 ' language pltcl;
20
21 select tcl_cdomain_arg(row('tkey', 42, 'ref2'));
22 select tcl_cdomain_arg(row('tkey', 42, 'ref2')::T_comp1);
23 select tcl_cdomain_arg(row('tkey', -1, 'ref2')); -- fail
24
25 -- Test argisnull primitive
26 select tcl_argisnull('foo');
27 select tcl_argisnull('');
28 select tcl_argisnull(null);
29
30 -- test some error cases
31 create function tcl_error(out a int, out b int) as $$returm 1$$ language pltcl;
32 select tcl_error();
33
34 create function bad_record(out a text, out b text) as $$return [list a]$$ language pltcl;
35 select bad_record();
36
37 create function bad_field(out a text, out b text) as $$return [list a 1 b 2 cow 3]$$ language pltcl;
38 select bad_field();
39
40 -- test compound return
41 select * from tcl_test_cube_squared(5);
42
43 -- test SRF
44 select * from tcl_test_squared_rows(0,5);
45
46 select * from tcl_test_sequence(0,5) as a;
47
48 select 1, tcl_test_sequence(0,5);
49
50 create function non_srf() returns int as $$return_next 1$$ language pltcl;
51 select non_srf();
52
53 create function bad_record_srf(out a text, out b text) returns setof record as $$
54 return_next [list a]
55 $$ language pltcl;
56 select bad_record_srf();
57
58 create function bad_field_srf(out a text, out b text) returns setof record as $$
59 return_next [list a 1 b 2 cow 3]
60 $$ language pltcl;
61 select bad_field_srf();
62
63 -- test composite and domain-over-composite results
64 create function tcl_composite_result(int) returns T_comp1 as $$
65 return [list tkey tkey1 ref1 $1 ref2 ref22]
66 $$ language pltcl;
67 select tcl_composite_result(1001);
68 select * from tcl_composite_result(1002);
69
70 create function tcl_dcomposite_result(int) returns d_comp1 as $$
71 return [list tkey tkey2 ref1 $1 ref2 ref42]
72 $$ language pltcl;
73 select tcl_dcomposite_result(1001);
74 select * from tcl_dcomposite_result(1002);
75 select * from tcl_dcomposite_result(-1); -- fail
76
77 create function tcl_record_result(int) returns record as $$
78 return [list q1 sometext q2 $1 q3 moretext]
79 $$ language pltcl;
80 select tcl_record_result(42); -- fail
81 select * from tcl_record_result(42); -- fail
82 select * from tcl_record_result(42) as (q1 text, q2 int, q3 text);
83 select * from tcl_record_result(42) as (q1 text, q2 int, q3 text, q4 int);
84 select * from tcl_record_result(42) as (q1 text, q2 int, q4 int); -- fail
85
86 -- test quote
87 select tcl_eval('quote foo bar');
88 select tcl_eval('quote [format %c 39]');
89 select tcl_eval('quote [format %c 92]');
90
91 -- Test argisnull
92 select tcl_eval('argisnull');
93 select tcl_eval('argisnull 14');
94 select tcl_eval('argisnull abc');
95
96 -- Test return_null
97 select tcl_eval('return_null 14');
98
99 -- Test spi_exec
100 select tcl_eval('spi_exec');
101 select tcl_eval('spi_exec -count');
102 select tcl_eval('spi_exec -array');
103 select tcl_eval('spi_exec -count abc');
104 select tcl_eval('spi_exec query loop body toomuch');
105 select tcl_eval('spi_exec "begin; rollback;"');
106
107 -- Test spi_execp
108 select tcl_eval('spi_execp');
109 select tcl_eval('spi_execp -count');
110 select tcl_eval('spi_execp -array');
111 select tcl_eval('spi_execp -count abc');
112 select tcl_eval('spi_execp -nulls');
113 select tcl_eval('spi_execp ""');
114
115 -- test spi_prepare
116 select tcl_eval('spi_prepare');
117 select tcl_eval('spi_prepare a b');
118 select tcl_eval('spi_prepare a "b {"');
119 select tcl_error_handling_test($tcl$spi_prepare "select moo" []$tcl$);
120
121 -- test full error text
122 select tcl_error_handling_test($tcl$
123 spi_exec "DO $$
124 BEGIN
125 RAISE 'my message'
126 USING HINT = 'my hint'
127 , DETAIL = 'my detail'
128 , SCHEMA = 'my schema'
129 , TABLE = 'my table'
130 , COLUMN = 'my column'
131 , CONSTRAINT = 'my constraint'
132 , DATATYPE = 'my datatype'
133 ;
134 END$$;"
135 $tcl$);
136
137 -- verify tcl_error_handling_test() properly reports non-postgres errors
138 select tcl_error_handling_test('moo');
139
140 -- test elog
141 select tcl_eval('elog');
142 select tcl_eval('elog foo bar');
143
144 -- test forced error
145 select tcl_eval('error "forced error"');
146
147 -- test loop control in spi_exec[p]
148 select tcl_spi_exec(true, 'break');
149 select tcl_spi_exec(true, 'continue');
150 select tcl_spi_exec(true, 'error');
151 select tcl_spi_exec(true, 'return');
152 select tcl_spi_exec(false, 'break');
153 select tcl_spi_exec(false, 'continue');
154 select tcl_spi_exec(false, 'error');
155 select tcl_spi_exec(false, 'return');
156
157 -- test that we don't get confused by multiple funcs with same SQL name
158 create schema tcls1;
159 create function tcls1.somefunc(int) returns int as $$
160 return [expr $1 * 2]
161 $$ language pltcl;
162
163 create schema tcls2;
164 create function tcls2.somefunc(int) returns int as $$
165 return [expr $1 * 3]
166 $$ language pltcl;
167
168 set search_path = tcls1;
169 select tcls1.somefunc(11);
170 set search_path = tcls2;
171 select tcls2.somefunc(12);
172 set search_path = tcls1;
173 select tcls1.somefunc(13);
174 reset search_path;
175
176 -- test that it works to replace a function that's being executed
177 create function replaceme(text) returns text as $p$
178 spi_exec {
179 create or replace function replaceme(text) returns text as $$
180 return "$1 fum"
181 $$ language pltcl;
182 }
183 spi_exec {select replaceme('foe') as inner}
184 return "fee $1 $inner"
185 $p$ language pltcl;
186
187 select replaceme('fie');
188 select replaceme('fie');
189
190 -- forcibly run the Tcl event loop for awhile, to check that we have not
191 -- messed things up too badly by disabling the Tcl notifier subsystem
192 select tcl_eval($$
193 unset -nocomplain ::tcl_vwait
194 after 100 {set ::tcl_vwait 1}
195 vwait ::tcl_vwait
196 unset -nocomplain ::tcl_vwait$$);