]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/psql.sql
Support \if ... \elif ... \else ... \endif in psql scripting.
[postgresql] / src / test / regress / sql / psql.sql
1 --
2 -- Tests for psql features that aren't closely connected to any
3 -- specific server features
4 --
5
6 -- \set
7
8 -- fail: invalid name
9 \set invalid/name foo
10 -- fail: invalid value for special variable
11 \set AUTOCOMMIT foo
12 \set FETCH_COUNT foo
13 -- check handling of built-in boolean variable
14 \echo :ON_ERROR_ROLLBACK
15 \set ON_ERROR_ROLLBACK
16 \echo :ON_ERROR_ROLLBACK
17 \set ON_ERROR_ROLLBACK foo
18 \echo :ON_ERROR_ROLLBACK
19 \set ON_ERROR_ROLLBACK on
20 \echo :ON_ERROR_ROLLBACK
21 \unset ON_ERROR_ROLLBACK
22 \echo :ON_ERROR_ROLLBACK
23
24 -- \g and \gx
25
26 SELECT 1 as one, 2 as two \g
27 \gx
28 SELECT 3 as three, 4 as four \gx
29 \g
30
31 -- \gset
32
33 select 10 as test01, 20 as test02, 'Hello' as test03 \gset pref01_
34
35 \echo :pref01_test01 :pref01_test02 :pref01_test03
36
37 -- should fail: bad variable name
38 select 10 as "bad name"
39 \gset
40
41 -- multiple backslash commands in one line
42 select 1 as x, 2 as y \gset pref01_ \\ \echo :pref01_x
43 select 3 as x, 4 as y \gset pref01_ \echo :pref01_x \echo :pref01_y
44 select 5 as x, 6 as y \gset pref01_ \\ \g \echo :pref01_x :pref01_y
45 select 7 as x, 8 as y \g \gset pref01_ \echo :pref01_x :pref01_y
46
47 -- NULL should unset the variable
48 \set var2 xyz
49 select 1 as var1, NULL as var2, 3 as var3 \gset
50 \echo :var1 :var2 :var3
51
52 -- \gset requires just one tuple
53 select 10 as test01, 20 as test02 from generate_series(1,3) \gset
54 select 10 as test01, 20 as test02 from generate_series(1,0) \gset
55
56 -- \gset should work in FETCH_COUNT mode too
57 \set FETCH_COUNT 1
58
59 select 1 as x, 2 as y \gset pref01_ \\ \echo :pref01_x
60 select 3 as x, 4 as y \gset pref01_ \echo :pref01_x \echo :pref01_y
61 select 10 as test01, 20 as test02 from generate_series(1,3) \gset
62 select 10 as test01, 20 as test02 from generate_series(1,0) \gset
63
64 \unset FETCH_COUNT
65
66 -- \gexec
67
68 create temporary table gexec_test(a int, b text, c date, d float);
69 select format('create index on gexec_test(%I)', attname)
70 from pg_attribute
71 where attrelid = 'gexec_test'::regclass and attnum > 0
72 order by attnum
73 \gexec
74
75 -- \gexec should work in FETCH_COUNT mode too
76 -- (though the fetch limit applies to the executed queries not the meta query)
77 \set FETCH_COUNT 1
78
79 select 'select 1 as ones', 'select x.y, x.y*2 as double from generate_series(1,4) as x(y)'
80 union all
81 select 'drop table gexec_test', NULL
82 union all
83 select 'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
84 \gexec
85
86 \unset FETCH_COUNT
87
88 -- show all pset options
89 \pset
90
91 -- test multi-line headers, wrapping, and newline indicators
92 prepare q as select array_to_string(array_agg(repeat('x',2*n)),E'\n') as "ab
93
94 c", array_to_string(array_agg(repeat('y',20-2*n)),E'\n') as "a
95 bc" from generate_series(1,10) as n(n) group by n>1 order by n>1;
96
97 \pset linestyle ascii
98
99 \pset expanded off
100 \pset columns 40
101
102 \pset border 0
103 \pset format unaligned
104 execute q;
105 \pset format aligned
106 execute q;
107 \pset format wrapped
108 execute q;
109
110 \pset border 1
111 \pset format unaligned
112 execute q;
113 \pset format aligned
114 execute q;
115 \pset format wrapped
116 execute q;
117
118 \pset border 2
119 \pset format unaligned
120 execute q;
121 \pset format aligned
122 execute q;
123 \pset format wrapped
124 execute q;
125
126 \pset expanded on
127 \pset columns 20
128
129 \pset border 0
130 \pset format unaligned
131 execute q;
132 \pset format aligned
133 execute q;
134 \pset format wrapped
135 execute q;
136
137 \pset border 1
138 \pset format unaligned
139 execute q;
140 \pset format aligned
141 execute q;
142 \pset format wrapped
143 execute q;
144
145 \pset border 2
146 \pset format unaligned
147 execute q;
148 \pset format aligned
149 execute q;
150 \pset format wrapped
151 execute q;
152
153 \pset linestyle old-ascii
154
155 \pset expanded off
156 \pset columns 40
157
158 \pset border 0
159 \pset format unaligned
160 execute q;
161 \pset format aligned
162 execute q;
163 \pset format wrapped
164 execute q;
165
166 \pset border 1
167 \pset format unaligned
168 execute q;
169 \pset format aligned
170 execute q;
171 \pset format wrapped
172 execute q;
173
174 \pset border 2
175 \pset format unaligned
176 execute q;
177 \pset format aligned
178 execute q;
179 \pset format wrapped
180 execute q;
181
182 \pset expanded on
183 \pset columns 20
184
185 \pset border 0
186 \pset format unaligned
187 execute q;
188 \pset format aligned
189 execute q;
190 \pset format wrapped
191 execute q;
192
193 \pset border 1
194 \pset format unaligned
195 execute q;
196 \pset format aligned
197 execute q;
198 \pset format wrapped
199 execute q;
200
201 \pset border 2
202 \pset format unaligned
203 execute q;
204 \pset format aligned
205 execute q;
206 \pset format wrapped
207 execute q;
208
209 deallocate q;
210
211 -- test single-line header and data
212 prepare q as select repeat('x',2*n) as "0123456789abcdef", repeat('y',20-2*n) as "0123456789" from generate_series(1,10) as n;
213
214 \pset linestyle ascii
215
216 \pset expanded off
217 \pset columns 40
218
219 \pset border 0
220 \pset format unaligned
221 execute q;
222 \pset format aligned
223 execute q;
224 \pset format wrapped
225 execute q;
226
227 \pset border 1
228 \pset format unaligned
229 execute q;
230 \pset format aligned
231 execute q;
232 \pset format wrapped
233 execute q;
234
235 \pset border 2
236 \pset format unaligned
237 execute q;
238 \pset format aligned
239 execute q;
240 \pset format wrapped
241 execute q;
242
243 \pset expanded on
244 \pset columns 30
245
246 \pset border 0
247 \pset format unaligned
248 execute q;
249 \pset format aligned
250 execute q;
251 \pset format wrapped
252 execute q;
253
254 \pset border 1
255 \pset format unaligned
256 execute q;
257 \pset format aligned
258 execute q;
259 \pset format wrapped
260 execute q;
261
262 \pset border 2
263 \pset format unaligned
264 execute q;
265 \pset format aligned
266 execute q;
267 \pset format wrapped
268 execute q;
269
270 \pset expanded on
271 \pset columns 20
272
273 \pset border 0
274 \pset format unaligned
275 execute q;
276 \pset format aligned
277 execute q;
278 \pset format wrapped
279 execute q;
280
281 \pset border 1
282 \pset format unaligned
283 execute q;
284 \pset format aligned
285 execute q;
286 \pset format wrapped
287 execute q;
288
289 \pset border 2
290 \pset format unaligned
291 execute q;
292 \pset format aligned
293 execute q;
294 \pset format wrapped
295 execute q;
296
297 \pset linestyle old-ascii
298
299 \pset expanded off
300 \pset columns 40
301
302 \pset border 0
303 \pset format unaligned
304 execute q;
305 \pset format aligned
306 execute q;
307 \pset format wrapped
308 execute q;
309
310 \pset border 1
311 \pset format unaligned
312 execute q;
313 \pset format aligned
314 execute q;
315 \pset format wrapped
316 execute q;
317
318 \pset border 2
319 \pset format unaligned
320 execute q;
321 \pset format aligned
322 execute q;
323 \pset format wrapped
324 execute q;
325
326 \pset expanded on
327
328 \pset border 0
329 \pset format unaligned
330 execute q;
331 \pset format aligned
332 execute q;
333 \pset format wrapped
334 execute q;
335
336 \pset border 1
337 \pset format unaligned
338 execute q;
339 \pset format aligned
340 execute q;
341 \pset format wrapped
342 execute q;
343
344 \pset border 2
345 \pset format unaligned
346 execute q;
347 \pset format aligned
348 execute q;
349 \pset format wrapped
350 execute q;
351
352 deallocate q;
353
354 \pset linestyle ascii
355
356 prepare q as select ' | = | lkjsafi\\/ /oeu rio)(!@&*#)*(!&@*) \ (&' as " | -- | 012345678 9abc def!*@#&!@(*&*~~_+-=\ \", '11' as "0123456789", 11 as int from generate_series(1,10) as n;
357
358 \pset format asciidoc
359 \pset expanded off
360 \pset border 0
361 execute q;
362
363 \pset border 1
364 execute q;
365
366 \pset border 2
367 execute q;
368
369 \pset expanded on
370 \pset border 0
371 execute q;
372
373 \pset border 1
374 execute q;
375
376 \pset border 2
377 execute q;
378
379 deallocate q;
380
381 \pset format aligned
382 \pset expanded off
383 \pset border 1
384
385 -- tests for \if ... \endif
386
387 \if true
388   select 'okay';
389   select 'still okay';
390 \else
391   not okay;
392   still not okay
393 \endif
394
395 -- at this point query buffer should still have last valid line
396 \g
397
398 -- \if should work okay on part of a query
399 select
400   \if true
401     42
402   \else
403     (bogus
404   \endif
405   forty_two;
406
407 select \if false \\ (bogus \else \\ 42 \endif \\ forty_two;
408
409 -- test a large nested if using a variety of true-equivalents
410 \if true
411         \if 1
412                 \if yes
413                         \if on
414                                 \echo 'all true'
415                         \else
416                                 \echo 'should not print #1-1'
417                         \endif
418                 \else
419                         \echo 'should not print #1-2'
420                 \endif
421         \else
422                 \echo 'should not print #1-3'
423         \endif
424 \else
425         \echo 'should not print #1-4'
426 \endif
427
428 -- test a variety of false-equivalents in an if/elif/else structure
429 \if false
430         \echo 'should not print #2-1'
431 \elif 0
432         \echo 'should not print #2-2'
433 \elif no
434         \echo 'should not print #2-3'
435 \elif off
436         \echo 'should not print #2-4'
437 \else
438         \echo 'all false'
439 \endif
440
441 -- test simple true-then-else
442 \if true
443         \echo 'first thing true'
444 \else
445         \echo 'should not print #3-1'
446 \endif
447
448 -- test simple false-true-else
449 \if false
450         \echo 'should not print #4-1'
451 \elif true
452         \echo 'second thing true'
453 \else
454         \echo 'should not print #5-1'
455 \endif
456
457 -- invalid boolean expressions are false
458 \if invalid boolean expression
459         \echo 'will not print #6-1'
460 \else
461         \echo 'will print anyway #6-2'
462 \endif
463
464 -- test un-matched endif
465 \endif
466
467 -- test un-matched else
468 \else
469
470 -- test un-matched elif
471 \elif
472
473 -- test double-else error
474 \if true
475 \else
476 \else
477 \endif
478
479 -- test elif out-of-order
480 \if false
481 \else
482 \elif
483 \endif
484
485 -- test if-endif matching in a false branch
486 \if false
487     \if false
488         \echo 'should not print #7-1'
489     \else
490         \echo 'should not print #7-2'
491     \endif
492     \echo 'should not print #7-3'
493 \else
494     \echo 'should print #7-4'
495 \endif
496
497 -- show that vars and backticks are not expanded when ignoring extra args
498 \set foo bar
499 \echo :foo :'foo' :"foo"
500 \pset fieldsep | `nosuchcommand` :foo :'foo' :"foo"
501
502 -- show that vars and backticks are not expanded and commands are ignored
503 -- when in a false if-branch
504 \set try_to_quit '\\q'
505 \if false
506         :try_to_quit
507         \echo `nosuchcommand` :foo :'foo' :"foo"
508         \pset fieldsep | `nosuchcommand` :foo :'foo' :"foo"
509         \a \C arg1 \c arg1 arg2 arg3 arg4 \cd arg1 \conninfo
510         \copy arg1 arg2 arg3 arg4 arg5 arg6
511         \copyright \dt arg1 \e arg1 arg2
512         \ef whole_line
513         \ev whole_line
514         \echo arg1 arg2 arg3 arg4 arg5 \echo arg1 \encoding arg1 \errverbose
515         \g arg1 \gx arg1 \gexec \h \html \i arg1 \ir arg1 \l arg1 \lo arg1 arg2
516         \o arg1 \p \password arg1 \prompt arg1 arg2 \pset arg1 arg2 \q
517         \reset \s arg1 \set arg1 arg2 arg3 arg4 arg5 arg6 arg7 \setenv arg1 arg2
518         \sf whole_line
519         \sv whole_line
520         \t arg1 \T arg1 \timing arg1 \unset arg1 \w arg1 \watch arg1 \x arg1
521         -- \else here is eaten as part of OT_FILEPIPE argument
522         \w |/no/such/file \else
523         -- \endif here is eaten as part of whole-line argument
524         \! whole_line \endif
525 \else
526         \echo 'should print #8-1'
527 \endif
528
529 -- SHOW_CONTEXT
530
531 \set SHOW_CONTEXT never
532 do $$
533 begin
534   raise notice 'foo';
535   raise exception 'bar';
536 end $$;
537
538 \set SHOW_CONTEXT errors
539 do $$
540 begin
541   raise notice 'foo';
542   raise exception 'bar';
543 end $$;
544
545 \set SHOW_CONTEXT always
546 do $$
547 begin
548   raise notice 'foo';
549   raise exception 'bar';
550 end $$;