@@ -143,6 +143,13 @@ func TestPostgres(t *testing.T) {
143
143
t .Fatalf ("%v" , err .Error ())
144
144
}
145
145
146
+ t .Log ("Reading test data (multi-row)" )
147
+ err = readMultiTestData (ctx , db )
148
+ if err != nil {
149
+ db .Close ()
150
+ t .Fatalf ("%v" , err .Error ())
151
+ }
152
+
146
153
db .Close ()
147
154
}
148
155
@@ -168,13 +175,13 @@ func checkSettings(t *testing.T) {
168
175
169
176
func createTestTable (ctx context.Context , db * postgres.Database ) error {
170
177
// Destroy old test table if exists
171
- _ , err := db .Exec (ctx , postgres . NewQueryParams ( `DROP TABLE IF EXISTS go_postgres_test_table CASCADE` ) )
178
+ _ , err := db .Exec (ctx , `DROP TABLE IF EXISTS go_postgres_test_table CASCADE` )
172
179
if err != nil {
173
180
return fmt .Errorf ("Unable to drop tables [err=%v]" , err .Error ())
174
181
}
175
182
176
183
// Create the test table
177
- _ , err = db .Exec (ctx , postgres . NewQueryParams ( `CREATE TABLE go_postgres_test_table (
184
+ _ , err = db .Exec (ctx , `CREATE TABLE go_postgres_test_table (
178
185
id INT NOT NULL,
179
186
num NUMERIC(24, 0) NULL,
180
187
sm SMALLINT NULL,
@@ -192,7 +199,7 @@ func createTestTable(ctx context.Context, db *postgres.Database) error {
192
199
js JSONB NULL,
193
200
194
201
PRIMARY KEY (id)
195
- )` ))
202
+ )` )
196
203
if err != nil {
197
204
return fmt .Errorf ("Unable to create test table [err=%v]" , err .Error ())
198
205
}
@@ -202,7 +209,6 @@ func createTestTable(ctx context.Context, db *postgres.Database) error {
202
209
}
203
210
204
211
func insertTestData (ctx context.Context , db * postgres.Database ) error {
205
-
206
212
return db .WithinTx (ctx , func (ctx context.Context , tx postgres.Tx ) error {
207
213
for idx := 1 ; idx <= 2 ; idx ++ {
208
214
rd := genTestRowDef (idx , true )
@@ -220,21 +226,54 @@ func insertTestData(ctx context.Context, db *postgres.Database) error {
220
226
// Done
221
227
return nil
222
228
})
223
-
224
229
}
225
230
226
231
func readTestData (ctx context.Context , db * postgres.Database ) error {
227
232
for idx := 1 ; idx <= 2 ; idx ++ {
228
- rd := genTestRowDef (idx , false )
229
- err := readTestRowDef (ctx , db , rd )
233
+ compareRd := genTestRowDef (idx , false )
234
+ rd , err := readTestRowDef (ctx , db , compareRd . id )
230
235
if err != nil {
231
- return fmt .Errorf ("Unable to verify test data [id=%v/err=%v]" , rd .id , err .Error ())
236
+ return fmt .Errorf ("Unable to verify test data [id=%v/err=%v]" , compareRd .id , err .Error ())
237
+ }
238
+ // Do deep comparison
239
+ if ! reflect .DeepEqual (compareRd , rd ) {
240
+ return errors .New ("data mismatch" )
232
241
}
233
242
234
- nrd := genTestNullableRowDef (idx , false )
235
- err = readTestNullableRowDef (ctx , db , nrd )
243
+ compareNrd := genTestNullableRowDef (idx , false )
244
+ nrd , err : = readTestNullableRowDef (ctx , db , compareNrd . id )
236
245
if err != nil {
237
- return fmt .Errorf ("Unable to verify test data [id=%v/err=%v]" , nrd .id , err .Error ())
246
+ return fmt .Errorf ("Unable to verify test data [id=%v/err=%v]" , compareNrd .id , err .Error ())
247
+ }
248
+
249
+ // Do deep comparison
250
+ if ! reflect .DeepEqual (compareNrd , nrd ) {
251
+ return fmt .Errorf ("Data mismatch while comparing test data [id=%v]" , compareNrd .id )
252
+ }
253
+ }
254
+
255
+ // Done
256
+ return nil
257
+ }
258
+
259
+ func readMultiTestData (ctx context.Context , db * postgres.Database ) error {
260
+ compareRd := make ([]TestRowDef , 0 )
261
+ for idx := 1 ; idx <= 2 ; idx ++ {
262
+ compareRd = append (compareRd , genTestRowDef (idx , false ))
263
+ }
264
+ rd , err := readMultiTestRowDef (ctx , db , compareRd )
265
+ if err != nil {
266
+ return fmt .Errorf ("Unable to verify test data [err=%v]" , err .Error ())
267
+ }
268
+
269
+ // Do deep comparison
270
+ if len (compareRd ) != len (rd ) {
271
+ return fmt .Errorf ("Data mismatch while comparing test data [len1=%d/len2=%d]" , len (compareRd ), len (rd ))
272
+ }
273
+
274
+ for idx := 0 ; idx < len (rd ); idx ++ {
275
+ if ! reflect .DeepEqual (compareRd [idx ], rd [idx ]) {
276
+ return fmt .Errorf ("Data mismatch while comparing test data [id=%v]" , compareRd [idx ].id )
238
277
}
239
278
}
240
279
@@ -348,53 +387,89 @@ func genTestNullableRowDef(index int, write bool) TestNullableRowDef {
348
387
}
349
388
350
389
func insertTestRowDef (ctx context.Context , tx postgres.Tx , rd TestRowDef ) error {
351
- _ , err := tx .Exec (ctx , postgres . NewQueryParams ( `
390
+ _ , err := tx .Exec (ctx , `
352
391
INSERT INTO go_postgres_test_table (
353
392
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
354
393
) VALUES (
355
394
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
356
395
)
357
396
` ,
358
397
rd .id , rd .num , rd .sm , rd .bi , rd .bi2 , rd .dbl , rd .va , rd .chr , rd .txt , rd .blob , rd .ts , rd .dt , rd .tim , rd .b , rd .js ,
359
- ))
398
+ )
360
399
return err
361
400
}
362
401
363
- func readTestRowDef (ctx context.Context , db * postgres.Database , compareRd TestRowDef ) error {
402
+ func readTestRowDef (ctx context.Context , db * postgres.Database , id int ) ( TestRowDef , error ) {
364
403
rd := TestRowDef {}
365
- err := db .QueryRow (ctx , postgres . NewQueryParams ( `
404
+ err := db .QueryRow (ctx , `
366
405
SELECT
367
406
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
368
407
FROM
369
408
go_postgres_test_table
370
409
WHERE
371
410
id = $1
372
- ` , compareRd . id ),
411
+ ` , id ). Scan (
373
412
& rd .id , & rd .num , & rd .sm , & rd .bi , & rd .bi2 , & rd .dbl , & rd .va , & rd .chr , & rd .txt , & rd .blob , & rd .ts , & rd .dt , & rd .tim ,
374
413
& rd .b , & rd .js ,
375
414
)
376
415
if err != nil {
377
- return err
416
+ return TestRowDef {}, err
378
417
}
379
418
380
419
// JSON data returned by Postgres can contain spaces and other encoding so re-encode the returned string
381
420
// for comparison
382
421
rd .js , err = jsonReEncode (rd .js )
383
422
if err != nil {
384
- return err
423
+ return TestRowDef {}, err
385
424
}
386
425
387
- // Do deep comparison
388
- if ! reflect .DeepEqual (compareRd , rd ) {
389
- return errors .New ("data mismatch" )
426
+ // Done
427
+ return rd , nil
428
+ }
429
+
430
+ func readMultiTestRowDef (ctx context.Context , db * postgres.Database , compareRd []TestRowDef ) ([]TestRowDef , error ) {
431
+ // Populate ids
432
+ ids := make ([]int , len (compareRd ))
433
+ for idx := 0 ; idx < len (compareRd ); idx ++ {
434
+ ids [idx ] = compareRd [idx ].id
435
+ }
436
+
437
+ rd := make ([]TestRowDef , 0 )
438
+ err := db .QueryRows (ctx , `
439
+ SELECT
440
+ id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
441
+ FROM
442
+ go_postgres_test_table
443
+ WHERE
444
+ id = ANY($1)
445
+ ` , ids ).Do (func (ctx context.Context , row postgres.Row ) (bool , error ) {
446
+ item := TestRowDef {}
447
+ err := row .Scan (& item .id , & item .num , & item .sm , & item .bi , & item .bi2 , & item .dbl , & item .va , & item .chr , & item .txt ,
448
+ & item .blob , & item .ts , & item .dt , & item .tim , & item .b , & item .js )
449
+ if err == nil {
450
+ rd = append (rd , item )
451
+ }
452
+ return true , err
453
+ })
454
+ if err != nil {
455
+ return nil , err
456
+ }
457
+
458
+ // JSON data returned by Postgres can contain spaces and other encoding so re-encode the returned string
459
+ // for comparison
460
+ for idx := range rd {
461
+ rd [idx ].js , err = jsonReEncode (rd [idx ].js )
462
+ if err != nil {
463
+ return nil , err
464
+ }
390
465
}
391
466
392
467
// Done
393
- return nil
468
+ return rd , nil
394
469
}
395
470
396
471
func insertTestNullableRowDef (ctx context.Context , tx postgres.Tx , nrd TestNullableRowDef ) error {
397
- _ , err := tx .Exec (ctx , postgres . NewQueryParams ( `
472
+ _ , err := tx .Exec (ctx , `
398
473
INSERT INTO go_postgres_test_table (
399
474
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js
400
475
) VALUES (
@@ -403,25 +478,25 @@ func insertTestNullableRowDef(ctx context.Context, tx postgres.Tx, nrd TestNulla
403
478
` ,
404
479
nrd .id , nrd .num , nrd .sm , nrd .bi , nrd .bi2 , nrd .dbl , nrd .va , nrd .chr , nrd .txt , nrd .blob , nrd .ts , nrd .dt , nrd .tim ,
405
480
nrd .b , nrd .js ,
406
- ))
481
+ )
407
482
return err
408
483
}
409
484
410
- func readTestNullableRowDef (ctx context.Context , db * postgres.Database , compareNrd TestNullableRowDef ) error {
485
+ func readTestNullableRowDef (ctx context.Context , db * postgres.Database , id int ) ( TestNullableRowDef , error ) {
411
486
nrd := TestNullableRowDef {}
412
- err := db .QueryRow (ctx , postgres . NewQueryParams ( `
487
+ err := db .QueryRow (ctx , `
413
488
SELECT
414
489
id, num, sm, bi, bi2, dbl, va, chr, txt, blob, ts, dt, tim, b, js::text
415
490
FROM
416
491
go_postgres_test_table
417
492
WHERE
418
493
id = $1
419
- ` , compareNrd . id ),
494
+ ` , id ). Scan (
420
495
& nrd .id , & nrd .num , & nrd .sm , & nrd .bi , & nrd .bi2 , & nrd .dbl , & nrd .va , & nrd .chr , & nrd .txt , & nrd .blob , & nrd .ts ,
421
496
& nrd .dt , & nrd .tim , & nrd .b , & nrd .js ,
422
497
)
423
498
if err != nil {
424
- return err
499
+ return TestNullableRowDef {}, err
425
500
}
426
501
427
502
// JSON data returned by Postgres can contain spaces and other encoding so re-encode the returned string
@@ -431,18 +506,13 @@ func readTestNullableRowDef(ctx context.Context, db *postgres.Database, compareN
431
506
432
507
js , err = jsonReEncode (* nrd .js )
433
508
if err != nil {
434
- return err
509
+ return TestNullableRowDef {}, err
435
510
}
436
511
nrd .js = & js
437
512
}
438
513
439
- // Do deep comparison
440
- if ! reflect .DeepEqual (compareNrd , nrd ) {
441
- return errors .New ("data mismatch" )
442
- }
443
-
444
514
// Done
445
- return nil
515
+ return nrd , nil
446
516
}
447
517
448
518
func addressOf [T any ](x T ) * T {
0 commit comments