1-- 2-- Tests for PL/pgSQL variable properties: CONSTANT, NOT NULL, initializers 3-- 4 5create type var_record as (f1 int4, f2 int4); 6create domain int_nn as int not null; 7create domain var_record_nn as var_record not null; 8create domain var_record_colnn as var_record check((value).f2 is not null); 9 10-- CONSTANT 11 12do $$ 13declare x constant int := 42; 14begin 15 raise notice 'x = %', x; 16end$$; 17 18do $$ 19declare x constant int; 20begin 21 x := 42; -- fail 22end$$; 23 24do $$ 25declare x constant int; y int; 26begin 27 for x, y in select 1, 2 loop -- fail 28 end loop; 29end$$; 30 31do $$ 32declare x constant int[]; 33begin 34 x[1] := 42; -- fail 35end$$; 36 37do $$ 38declare x constant int[]; y int; 39begin 40 for x[1], y in select 1, 2 loop -- fail (currently, unsupported syntax) 41 end loop; 42end$$; 43 44do $$ 45declare x constant var_record; 46begin 47 x.f1 := 42; -- fail 48end$$; 49 50do $$ 51declare x constant var_record; y int; 52begin 53 for x.f1, y in select 1, 2 loop -- fail 54 end loop; 55end$$; 56 57-- initializer expressions 58 59do $$ 60declare x int := sin(0); 61begin 62 raise notice 'x = %', x; 63end$$; 64 65do $$ 66declare x int := 1/0; -- fail 67begin 68 raise notice 'x = %', x; 69end$$; 70 71do $$ 72declare x bigint[] := array[1,3,5]; 73begin 74 raise notice 'x = %', x; 75end$$; 76 77do $$ 78declare x record := row(1,2,3); 79begin 80 raise notice 'x = %', x; 81end$$; 82 83do $$ 84declare x var_record := row(1,2); 85begin 86 raise notice 'x = %', x; 87end$$; 88 89-- NOT NULL 90 91do $$ 92declare x int not null; -- fail 93begin 94 raise notice 'x = %', x; 95end$$; 96 97do $$ 98declare x int not null := 42; 99begin 100 raise notice 'x = %', x; 101 x := null; -- fail 102end$$; 103 104do $$ 105declare x int not null := null; -- fail 106begin 107 raise notice 'x = %', x; 108end$$; 109 110do $$ 111declare x record not null; -- fail 112begin 113 raise notice 'x = %', x; 114end$$; 115 116do $$ 117declare x record not null := row(42); 118begin 119 raise notice 'x = %', x; 120 x := row(null); -- ok 121 raise notice 'x = %', x; 122 x := null; -- fail 123end$$; 124 125do $$ 126declare x record not null := null; -- fail 127begin 128 raise notice 'x = %', x; 129end$$; 130 131do $$ 132declare x var_record not null; -- fail 133begin 134 raise notice 'x = %', x; 135end$$; 136 137do $$ 138declare x var_record not null := row(41,42); 139begin 140 raise notice 'x = %', x; 141 x := row(null,null); -- ok 142 raise notice 'x = %', x; 143 x := null; -- fail 144end$$; 145 146do $$ 147declare x var_record not null := null; -- fail 148begin 149 raise notice 'x = %', x; 150end$$; 151 152-- Check that variables are reinitialized on block re-entry. 153 154do $$ 155begin 156 for i in 1..3 loop 157 declare 158 x int; 159 y int := i; 160 r record; 161 c var_record; 162 begin 163 if i = 1 then 164 x := 42; 165 r := row(i, i+1); 166 c := row(i, i+1); 167 end if; 168 raise notice 'x = %', x; 169 raise notice 'y = %', y; 170 raise notice 'r = %', r; 171 raise notice 'c = %', c; 172 end; 173 end loop; 174end$$; 175 176-- Check enforcement of domain constraints during initialization 177 178do $$ 179declare x int_nn; -- fail 180begin 181 raise notice 'x = %', x; 182end$$; 183 184do $$ 185declare x int_nn := null; -- fail 186begin 187 raise notice 'x = %', x; 188end$$; 189 190do $$ 191declare x int_nn := 42; 192begin 193 raise notice 'x = %', x; 194 x := null; -- fail 195end$$; 196 197do $$ 198declare x var_record_nn; -- fail 199begin 200 raise notice 'x = %', x; 201end$$; 202 203do $$ 204declare x var_record_nn := null; -- fail 205begin 206 raise notice 'x = %', x; 207end$$; 208 209do $$ 210declare x var_record_nn := row(1,2); 211begin 212 raise notice 'x = %', x; 213 x := row(null,null); -- ok 214 x := null; -- fail 215end$$; 216 217do $$ 218declare x var_record_colnn; -- fail 219begin 220 raise notice 'x = %', x; 221end$$; 222 223do $$ 224declare x var_record_colnn := null; -- fail 225begin 226 raise notice 'x = %', x; 227end$$; 228 229do $$ 230declare x var_record_colnn := row(1,null); -- fail 231begin 232 raise notice 'x = %', x; 233end$$; 234 235do $$ 236declare x var_record_colnn := row(1,2); 237begin 238 raise notice 'x = %', x; 239 x := null; -- fail 240end$$; 241 242do $$ 243declare x var_record_colnn := row(1,2); 244begin 245 raise notice 'x = %', x; 246 x := row(null,null); -- fail 247end$$; 248