File: rowdatum.sql

package info (click to toggle)
postgresql-pllua 1%3A2.0.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,316 kB
  • sloc: ansic: 14,369; sql: 2,181; makefile: 163; sh: 59; javascript: 38
file content (184 lines) | stat: -rw-r--r-- 5,514 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
--

\set VERBOSITY terse

--

-- tests of operations on nested row values

create type ntype1 as (fred integer, jim numeric);
create type ntype2 as (thingy text[], wotsit ntype1);
create type ntype3 as (foo text, bar ntype1, baz ntype2);
create type ntype4 as (col0 text, col1 ntype1, col2 ntype2, col3 ntype3);

--
do language pllua $$
  local r = pgtype.ntype1(1,2)
  print(r.fred, r.jim)  -- deform
  r.fred = 3            -- explode after deform
  print(r.fred, r.jim)
  r.jim = 4             -- modify pre-exploded value
  print(pgtype.ntype1(r), r.fred, r.jim)
  r = pgtype.ntype1(1,2)
  r.fred = 3            -- explode before deform
  print(r.fred, r.jim)
  r.jim = 4             -- modify pre-exploded value
  print(pgtype.ntype1(r), r.fred, r.jim)
$$;

do language pllua $$
  local r0 = pgtype.ntype4("zzz",
       	                   { fred = 1, jim = 2 },
  	    		   { thingy = {"a","b","c"},
			     wotsit = { fred = 3, jim = 4}
			   },
			   { foo = "abcde",
			     bar = { fred = 5, jim = 6 },
			     baz = { thingy = {"x","y","z"},
			             wotsit = { fred = 7, jim = 8 } }
			   })
  local r = pgtype.ntype4(r0)
  -- deform from inner to outer
  print(r.col3.baz.wotsit.fred)
  print(r.col3.foo)
  print(r.col1.fred)
  -- and from outer to inner
  print(r.col2.wotsit.jim)
  print(r.col3.bar.jim)
  -- start over with un-deformed datum
  r = pgtype.ntype4(r0)
  --deform from outer to inner
  print(r.col1, r.col2, r.col3)
  print(r.col1.jim, r.col2.thingy[3], r.col3.foo)
  print(r.col2.wotsit.jim, r.col3.bar.jim, r.col3.baz.thingy[3])
  print(r.col3.baz.wotsit.jim)
  print(r) print(pgtype.ntype4(r))
  -- start over with un-deformed datum
  r = pgtype.ntype4(r0)
  -- explode from inner
  r.col3.baz.wotsit.jim = 10
  r.col2.thingy[2] = "k"
  print(r) print(pgtype.ntype4(r))
  -- start over with un-deformed datum
  r = pgtype.ntype4(r0)
  -- explode from middle
  r.col3.foo = "edcba"
  r.col1.fred = -1
  r.col3.baz.wotsit.jim = 80
  r.col3.baz.wotsit.fred = 0
  print(r) print(pgtype.ntype4(r))
  -- start over with un-deformed datum
  r = pgtype.ntype4(r0)
  -- explode from top
  r.col0 = "yyy"
  r.col3.baz.thingy[1] = "@"
  r.col1.jim = 20
  print(r) print(pgtype.ntype4(r))
  -- start over with un-deformed datum
  r = pgtype.ntype4(r0)
  -- partially deform then explode from a deformed element
  print(r.col0, r.col2.wotsit.fred)
  r.col2.wotsit.jim = 40
  r.col1.jim = 0
  r.col3.bar.jim = 0
  print(r) print(pgtype.ntype4(r))
  -- start over with un-deformed datum
  r = pgtype.ntype4(r0)
  -- partially deform then explode from an undeformed element
  print(r.col0, r.col2.wotsit.fred)
  r.col3.bar.jim = 0
  r.col3.bar.fred = -1
  r.col2.wotsit.fred = 0
  r.col0 = "yyy"
  r.col1 = { fred = 100, jim = 200 }
  print(r) print(pgtype.ntype4(r))
$$;

-- various dropped-column scenarios

create table ntab1 (fred integer, sheila text, jim numeric);
create table ntab2 (thingy text[], wotsit ntab1);
create table ntab3 (foo text, bar ntab1, baz ntab2);
create table ntab4 (col0 text, col1 ntab1, col2 ntab2, col3 ntab3);

insert into ntab1 values (1, 'foo', 1.23);
insert into ntab2 select array['x','y'], t from ntab1 t;
insert into ntab3 select 'wot', t1, t2 from ntab1 t1, ntab2 t2;
insert into ntab4 select 'rabbit', t1, t2, t3 from ntab1 t1, ntab2 t2, ntab3 t3;

alter table ntab1 drop column sheila;

do language pllua $$
  local r = pgtype.ntab1(1,2)
  print(r.fred, r.jim)  -- deform
  r.fred = 3            -- explode after deform
  print(r.fred, r.jim)
  r.jim = 4             -- modify pre-exploded value
  print(pgtype.ntab1(r), r.fred, r.jim)
  r = pgtype.ntab1(1,2)
  r.fred = 3            -- explode before deform
  print(r.fred, r.jim)
  r.jim = 4             -- modify pre-exploded value
  print(pgtype.ntab1(r), r.fred, r.jim)
$$;

do language pllua $$
  local tbl = spi.execute([[ select t from ntab4 t, generate_series(1,8) ]])
  local r = tbl[1].t
  -- deform from inner to outer
  print(r.col3.baz.wotsit.fred)
  print(r.col3.foo)
  print(r.col1.fred)
  -- and from outer to inner
  print(r.col2.wotsit.jim)
  print(r.col3.bar.jim)
  -- start over with un-deformed datum
  r = tbl[2].t
  --deform from outer to inner
  print(r.col1, r.col2, r.col3)
  print(r.col1.jim, r.col2.thingy[3], r.col3.foo)
  print(r.col2.wotsit.jim, r.col3.bar.jim, r.col3.baz.thingy[3])
  print(r.col3.baz.wotsit.jim)
  print(r) print(pgtype.ntab4(r))
  -- start over with un-deformed datum
  r = tbl[3].t
  -- explode from inner
  r.col3.baz.wotsit.jim = 10
  r.col2.thingy[2] = "k"
  print(r) print(pgtype.ntab4(r))
  -- start over with un-deformed datum
  r = tbl[4].t
  -- explode from middle
  r.col3.foo = "edcba"
  r.col1.fred = -1
  r.col3.baz.wotsit.jim = 80
  r.col3.baz.wotsit.fred = 0
  print(r) print(pgtype.ntab4(r))
  -- start over with un-deformed datum
  r = tbl[5].t
  -- explode from top
  r.col0 = "yyy"
  r.col3.baz.thingy[1] = "@"
  r.col1.jim = 20
  print(r) print(pgtype.ntab4(r))
  -- start over with un-deformed datum
  r = tbl[6].t
  -- partially deform then explode from a deformed element
  print(r.col0, r.col2.wotsit.fred)
  r.col2.wotsit.jim = 40
  r.col1.jim = 0
  r.col3.bar.jim = 0
  print(r) print(pgtype.ntab4(r))
  -- start over with un-deformed datum
  r = tbl[7].t
  -- partially deform then explode from an undeformed element
  print(r.col0, r.col2.wotsit.fred)
  r.col3.bar.jim = 0
  r.col3.bar.fred = -1
  r.col2.wotsit.fred = 0
  r.col0 = "yyy"
  r.col1 = { fred = 100, jim = 200 }
  print(r) print(pgtype.ntab4(r))
$$;

--end