-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathcomposite_type_test.go
320 lines (263 loc) · 7.58 KB
/
composite_type_test.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package pgtype_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/jackc/pgtype"
"github.com/jackc/pgtype/testutil"
pgx "github.com/jackc/pgx/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCompositeTypeSetAndGet(t *testing.T) {
ci := pgtype.NewConnInfo()
ct, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{
{"a", pgtype.TextOID},
{"b", pgtype.Int4OID},
}, ci)
require.NoError(t, err)
assert.Equal(t, pgtype.Undefined, ct.Get())
nilTests := []struct {
src interface{}
}{
{nil}, // nil interface
{(*[]interface{})(nil)}, // typed nil
}
for i, tt := range nilTests {
err := ct.Set(tt.src)
assert.NoErrorf(t, err, "%d", i)
assert.Equal(t, nil, ct.Get())
}
compatibleValuesTests := []struct {
src []interface{}
expected map[string]interface{}
}{
{
src: []interface{}{"foo", int32(42)},
expected: map[string]interface{}{"a": "foo", "b": int32(42)},
},
{
src: []interface{}{nil, nil},
expected: map[string]interface{}{"a": nil, "b": nil},
},
{
src: []interface{}{&pgtype.Text{String: "hi", Status: pgtype.Present}, &pgtype.Int4{Int: 7, Status: pgtype.Present}},
expected: map[string]interface{}{"a": "hi", "b": int32(7)},
},
}
for i, tt := range compatibleValuesTests {
err := ct.Set(tt.src)
assert.NoErrorf(t, err, "%d", i)
assert.EqualValues(t, tt.expected, ct.Get())
}
}
func TestCompositeTypeAssignTo(t *testing.T) {
ci := pgtype.NewConnInfo()
ct, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{
{"a", pgtype.TextOID},
{"b", pgtype.Int4OID},
}, ci)
require.NoError(t, err)
{
err := ct.Set([]interface{}{"foo", int32(42)})
assert.NoError(t, err)
var a string
var b int32
err = ct.AssignTo([]interface{}{&a, &b})
assert.NoError(t, err)
assert.Equal(t, "foo", a)
assert.Equal(t, int32(42), b)
}
{
err := ct.Set([]interface{}{"foo", int32(42)})
assert.NoError(t, err)
var a pgtype.Text
var b pgtype.Int4
err = ct.AssignTo([]interface{}{&a, &b})
assert.NoError(t, err)
assert.Equal(t, pgtype.Text{String: "foo", Status: pgtype.Present}, a)
assert.Equal(t, pgtype.Int4{Int: 42, Status: pgtype.Present}, b)
}
// Allow nil destination component as no-op
{
err := ct.Set([]interface{}{"foo", int32(42)})
assert.NoError(t, err)
var b int32
err = ct.AssignTo([]interface{}{nil, &b})
assert.NoError(t, err)
assert.Equal(t, int32(42), b)
}
// *[]interface{} dest when null
{
err := ct.Set(nil)
assert.NoError(t, err)
var a pgtype.Text
var b pgtype.Int4
dst := []interface{}{&a, &b}
err = ct.AssignTo(&dst)
assert.NoError(t, err)
assert.Nil(t, dst)
}
// *[]interface{} dest when not null
{
err := ct.Set([]interface{}{"foo", int32(42)})
assert.NoError(t, err)
var a pgtype.Text
var b pgtype.Int4
dst := []interface{}{&a, &b}
err = ct.AssignTo(&dst)
assert.NoError(t, err)
assert.NotNil(t, dst)
assert.Equal(t, pgtype.Text{String: "foo", Status: pgtype.Present}, a)
assert.Equal(t, pgtype.Int4{Int: 42, Status: pgtype.Present}, b)
}
// Struct fields positionally via reflection
{
err := ct.Set([]interface{}{"foo", int32(42)})
assert.NoError(t, err)
s := struct {
A string
B int32
}{}
err = ct.AssignTo(&s)
if assert.NoError(t, err) {
assert.Equal(t, "foo", s.A)
assert.Equal(t, int32(42), s.B)
}
}
}
func TestCompositeTypeTranscode(t *testing.T) {
conn := testutil.MustConnectPgx(t)
defer testutil.MustCloseContext(t, conn)
_, err := conn.Exec(context.Background(), `drop type if exists ct_test;
create type ct_test as (
a text,
b int4
);`)
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type ct_test")
var oid uint32
err = conn.QueryRow(context.Background(), `select 'ct_test'::regtype::oid`).Scan(&oid)
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type ct_test")
ct, err := pgtype.NewCompositeType("ct_test", []pgtype.CompositeTypeField{
{"a", pgtype.TextOID},
{"b", pgtype.Int4OID},
}, conn.ConnInfo())
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: ct, Name: ct.TypeName(), OID: oid})
// Use simple protocol to force text or binary encoding
simpleProtocols := []bool{true, false}
var a string
var b int32
for _, simpleProtocol := range simpleProtocols {
err := conn.QueryRow(context.Background(), "select $1::ct_test", pgx.QuerySimpleProtocol(simpleProtocol),
pgtype.CompositeFields{"hi", int32(42)},
).Scan(
[]interface{}{&a, &b},
)
if assert.NoErrorf(t, err, "Simple Protocol: %v", simpleProtocol) {
assert.EqualValuesf(t, "hi", a, "Simple Protocol: %v", simpleProtocol)
assert.EqualValuesf(t, 42, b, "Simple Protocol: %v", simpleProtocol)
}
}
}
// https://github.com/jackc/pgx/issues/874
func TestCompositeTypeTextDecodeNested(t *testing.T) {
newCompositeType := func(name string, fieldNames []string, vals ...pgtype.ValueTranscoder) *pgtype.CompositeType {
fields := make([]pgtype.CompositeTypeField, len(fieldNames))
for i, name := range fieldNames {
fields[i] = pgtype.CompositeTypeField{Name: name}
}
rowType, err := pgtype.NewCompositeTypeValues(name, fields, vals)
require.NoError(t, err)
return rowType
}
dimensionsType := func() pgtype.ValueTranscoder {
return newCompositeType(
"dimensions",
[]string{"width", "height"},
&pgtype.Int4{},
&pgtype.Int4{},
)
}
productImageType := func() pgtype.ValueTranscoder {
return newCompositeType(
"product_image_type",
[]string{"source", "dimensions"},
&pgtype.Text{},
dimensionsType(),
)
}
productImageSetType := newCompositeType(
"product_image_set_type",
[]string{"name", "orig_image", "images"},
&pgtype.Text{},
productImageType(),
pgtype.NewArrayType("product_image", 0, func() pgtype.ValueTranscoder {
return productImageType()
}),
)
err := productImageSetType.DecodeText(nil, []byte(`(name,"(img1,""(11,11)"")","{""(img2,\\""(22,22)\\"")"",""(img3,\\""(33,33)\\"")""}")`))
require.NoError(t, err)
}
func Example_composite() {
conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
if err != nil {
fmt.Println(err)
return
}
defer conn.Close(context.Background())
_, err = conn.Exec(context.Background(), `drop type if exists mytype;`)
if err != nil {
fmt.Println(err)
return
}
_, err = conn.Exec(context.Background(), `create type mytype as (
a int4,
b text
);`)
if err != nil {
fmt.Println(err)
return
}
defer conn.Exec(context.Background(), "drop type mytype")
var oid uint32
err = conn.QueryRow(context.Background(), `select 'mytype'::regtype::oid`).Scan(&oid)
if err != nil {
fmt.Println(err)
return
}
ct, err := pgtype.NewCompositeType("mytype", []pgtype.CompositeTypeField{
{"a", pgtype.Int4OID},
{"b", pgtype.TextOID},
}, conn.ConnInfo())
if err != nil {
fmt.Println(err)
return
}
conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: ct, Name: ct.TypeName(), OID: oid})
var a int
var b *string
err = conn.QueryRow(context.Background(), "select $1::mytype", []interface{}{2, "bar"}).Scan([]interface{}{&a, &b})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("First: a=%d b=%s\n", a, *b)
err = conn.QueryRow(context.Background(), "select (1, NULL)::mytype").Scan([]interface{}{&a, &b})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Second: a=%d b=%v\n", a, b)
scanTarget := []interface{}{&a, &b}
err = conn.QueryRow(context.Background(), "select NULL::mytype").Scan(&scanTarget)
E(err)
fmt.Printf("Third: isNull=%v\n", scanTarget == nil)
// Output:
// First: a=2 b=bar
// Second: a=1 b=<nil>
// Third: isNull=true
}