-
Notifications
You must be signed in to change notification settings - Fork 67
/
z_oracle_test.go
3797 lines (3528 loc) · 103 KB
/
z_oracle_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 Rana Ian. All rights reserved.
// Use of this source code is governed by The MIT License
// found in the accompanying LICENSE file.
package ora_test
import (
"bytes"
"context"
"crypto/rand"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"unsafe"
_ "net/http/pprof"
"github.com/pkg/errors"
"gopkg.in/rana/ora.v4"
"gopkg.in/rana/ora.v4/tstlg"
)
type oracleColumnType string
const (
// numeric
numberP38S0Identity oracleColumnType = "number(38,0) generated always as identity (start with 1 increment by 1)"
numberP38S0 oracleColumnType = "number(38,0) not null"
numberP38S0Null oracleColumnType = "number(38,0) null"
numberP16S15 oracleColumnType = "number(16,15) not null"
numberP16S15Null oracleColumnType = "number(16,15) null"
binaryDouble oracleColumnType = "binary_double not null"
binaryDoubleNull oracleColumnType = "binary_double null"
binaryFloat oracleColumnType = "binary_float not null"
binaryFloatNull oracleColumnType = "binary_float null"
floatP126 oracleColumnType = "float(126) not null"
floatP126Null oracleColumnType = "float(126) null"
// time
dateNotNull oracleColumnType = "date not null"
dateNull oracleColumnType = "date null"
timestampP9 oracleColumnType = "timestamp(9) not null"
timestampP9Null oracleColumnType = "timestamp(9) null"
timestampTzP9 oracleColumnType = "timestamp(9) with time zone not null"
timestampTzP9Null oracleColumnType = "timestamp(9) with time zone null"
timestampLtzP9 oracleColumnType = "timestamp(9) with local time zone not null"
timestampLtzP9Null oracleColumnType = "timestamp(9) with local time zone null"
// interval
intervalYM oracleColumnType = "interval year to month not null"
intervalYMNull oracleColumnType = "interval year to month null"
intervalDS oracleColumnType = "interval day to second(9) not null"
intervalDSNull oracleColumnType = "interval day to second(9) null"
// string
charB48 oracleColumnType = "char(48 byte) not null"
charB48Null oracleColumnType = "char(48 byte) null"
charC48 oracleColumnType = "char(48 char) not null"
charC48Null oracleColumnType = "char(48 char) null"
nchar48 oracleColumnType = "nchar(48) not null"
nchar48Null oracleColumnType = "nchar(48) null"
varcharB48 oracleColumnType = "varchar(48 byte) not null"
varcharB48Null oracleColumnType = "varchar(48 byte) null"
varcharC48 oracleColumnType = "varchar(48 char) not null"
varcharC48Null oracleColumnType = "varchar(48 char) null"
varchar2B48 oracleColumnType = "varchar2(48 byte) not null"
varchar2B48Null oracleColumnType = "varchar2(48 byte) null"
varchar2C48 oracleColumnType = "varchar2(48 char) not null"
varchar2C48Null oracleColumnType = "varchar2(48 char) null"
nvarchar248 oracleColumnType = "nvarchar2(48) not null"
nvarchar248Null oracleColumnType = "nvarchar2(48) null"
long oracleColumnType = "long not null"
longNull oracleColumnType = "long null"
clob oracleColumnType = "clob not null"
clobNull oracleColumnType = "clob null"
nclob oracleColumnType = "nclob not null"
nclobNull oracleColumnType = "nclob null"
// string or bool
charB1 oracleColumnType = "char(1 byte) not null"
charB1Null oracleColumnType = "char(1 byte) null"
charC1 oracleColumnType = "char(1 char) not null"
charC1Null oracleColumnType = "char(1 char) null"
// bytes
longRaw oracleColumnType = "long raw not null"
longRawNull oracleColumnType = "long raw null"
raw2000 oracleColumnType = "raw(2000) not null"
raw2000Null oracleColumnType = "raw(2000) null"
blob oracleColumnType = "blob not null"
blobNull oracleColumnType = "blob null"
// bfile
bfile oracleColumnType = "bfile not null"
bfileNull oracleColumnType = "bfile null"
)
var _T_colType = map[string]oracleColumnType{
"charB1": charB1,
"charB1Null": charB1Null,
"charC1": charC1,
"charC1Null": charC1Null,
"longRaw": longRaw,
"longRawNull": longRawNull,
"raw2000": raw2000,
"raw2000Null": raw2000Null,
"blob": blob,
"blobNull": blobNull,
"intervalYM": intervalYM,
"intervalYMNull": intervalYMNull,
"intervalDS": intervalDS,
"intervalDSNull": intervalDSNull,
"numberP38S0": numberP38S0,
"numberP38S0Null": numberP38S0Null,
"numberP16S15": numberP16S15,
"numberP16S15Null": numberP16S15Null,
"binaryDouble": binaryDouble,
"binaryDoubleNull": binaryDoubleNull,
"binaryFloat": binaryFloat,
"binaryFloatNull": binaryFloatNull,
"floatP126": floatP126,
"floatP126Null": floatP126Null,
"charB48": charB48,
"charB48Null": charB48Null,
"charC48": charC48,
"charC48Null": charC48Null,
"nchar48": nchar48,
"nchar48Null": nchar48Null,
"varcharB48": varcharB48,
"varcharB48Null": varcharB48Null,
"varcharC48": varcharC48,
"varcharC48Null": varcharC48Null,
"varchar2B48": varchar2B48,
"varchar2B48Null": varchar2B48Null,
"varchar2C48": varchar2C48,
"varchar2C48Null": varchar2C48Null,
"nvarchar248": nvarchar248,
"nvarchar248Null": nvarchar248Null,
"long": long,
"longNull": longNull,
"clob": clob,
"clobNull": clobNull,
"nclob": nclob,
"nclobNull": nclobNull,
"date": dateNotNull,
"dateNull": dateNull,
"time": dateNotNull,
"timeNull": dateNull,
"timestampP9": timestampP9,
"timestampP9Null": timestampP9Null,
"timestampTzP9": timestampTzP9,
"timestampTzP9Null": timestampTzP9Null,
"timestampLtzP9": timestampLtzP9,
"timestampLtzP9Null": timestampLtzP9Null,
}
var testSrvCfg ora.SrvCfg
var testSesCfg ora.SesCfg
var testConStr string
var testDbsessiontimezone *time.Location
var testTableID uint32
var testWorkloadColumnCount int
var testDb *sql.DB
var testSesPool *ora.Pool
var tableNamePrefix = fmt.Sprintf("test_%d_", os.Getpid())
func init() {
testSrvCfg = ora.SrvCfg{
Dblink: os.Getenv("GO_ORA_DRV_TEST_DB"),
StmtCfg: ora.NewStmtCfg(),
}
if testSrvCfg.IsZero() {
panic("testSrvCfg is Zero")
}
testSesCfg = ora.SesCfg{
Username: os.Getenv("GO_ORA_DRV_TEST_USERNAME"),
Password: os.Getenv("GO_ORA_DRV_TEST_PASSWORD"),
StmtCfg: testSrvCfg.StmtCfg,
}
if testSesCfg.IsZero() {
panic("testSesCfg is Zero")
}
testConStr = fmt.Sprintf("%v/%v@%v", testSesCfg.Username, testSesCfg.Password, testSrvCfg.Dblink)
fmt.Printf("Read environment variable GO_ORA_DRV_TEST_DB = '%v'\n", testSrvCfg.Dblink)
fmt.Printf("Read environment variable GO_ORA_DRV_TEST_USERNAME = '%v'\n", testSesCfg.Username)
fmt.Printf("Read environment variable GO_ORA_DRV_TEST_PASSWORD = '%v'\n", testSesCfg.Password)
testWorkloadColumnCount = 20
var err error
// setup test environment, server and session
testEnv, err := ora.OpenEnv()
if err != nil {
panic(fmt.Sprintf("initError: %v", err))
}
testSesPool = testEnv.NewPool(testSrvCfg, testSesCfg, 4)
testSes, err := testSesPool.Get()
if err != nil {
panic(fmt.Sprintf("initError: %v", err))
}
defer testSes.Close()
//ora.SetCfg(func() StmtCfg { cfg = ora.Cfg(); cfg.RTrimChar = false; return cfg }())
// load session time zone
testDbsessiontimezone, err = loadDbtimezone()
if err != nil {
panic("Error loading session time zone from database: " + err.Error())
}
fmt.Printf("Read session time zone from database: %s\n", testDbsessiontimezone)
// drop all tables from previous test run
fmt.Println("Dropping previous tables...")
stmt, err := testSes.Prep(`
BEGIN
FOR c IN (SELECT table_name FROM user_tables WHERE TABLE_NAME LIKE UPPER('` + tableNamePrefix + `')||'%') LOOP
EXECUTE IMMEDIATE ('DROP TABLE ' || c.table_name || ' CASCADE CONSTRAINTS');
END LOOP;
END;`)
if err != nil {
fmt.Println("initError: ", err)
}
defer stmt.Close()
start := time.Now()
_, err = stmt.Exe()
if err != nil {
fmt.Println("initError: ", err)
}
fmt.Printf("Tables dropped (%s).\n", time.Since(start))
// setup test db
start = time.Now()
testDb, err = sql.Open(ora.Name, testConStr)
if err != nil {
fmt.Println("initError: ", err)
} else {
fmt.Printf("Connected to %q (%s).\n", testConStr, time.Since(start))
}
if os.Getenv("BLOCKPROFILE") == "1" {
go func() {
addr := "localhost:8642"
fmt.Println("blockprofile: go tool pprof http://" + addr + "/debug/pprof/block")
runtime.SetBlockProfileRate(1)
fmt.Println(http.ListenAndServe(addr, nil))
}()
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
err = testDb.PingContext(ctx)
cancel()
if err != nil {
panic(err)
}
if err := testSes.Ping(); err != nil {
panic(err)
}
}
func getSes(t testing.TB) *ora.Ses {
testSes, err := testSesPool.Get()
if err == nil {
return testSes
}
t.Fatal(err)
return nil
}
func enableLogging(t *testing.T) {
if t == nil {
return
}
cfg := ora.Cfg()
cfg.Log.Logger = tstlg.New(t)
ora.SetCfg(cfg)
}
func testIterations() int {
if testing.Short() {
return 1
} else {
return 1
}
}
func testBindDefine(expected interface{}, oct oracleColumnType, t *testing.T, c ora.StmtCfg, goColumnTypes ...ora.GoColumnType) {
var gct ora.GoColumnType
if len(goColumnTypes) > 0 {
gct = goColumnTypes[0]
} else {
gct = goColumnTypeFromValue(expected)
}
t.Logf("testBindDefine gct (%v, %v)", gct, ora.GctName(gct))
testSes, err := testSesPool.Get()
if err != nil {
t.Fatal(err)
}
defer testSes.Close()
tableName, err := createTable(1, oct, testSes)
testErr(err, t)
defer dropTable(tableName, testSes, t)
// insert
qry := fmt.Sprintf("insert into %v (c1) values (:c1)", tableName)
insertStmt, err := testSes.Prep(qry)
testErr(errors.Wrap(err, qry), t)
defer insertStmt.Close()
if !c.IsZero() {
insertStmt.SetCfg(c)
}
rowsAffected, err := insertStmt.Exe(expected)
testErr(errors.Wrapf(err, "%q, %#v", qry, expected), t)
expLen := length(expected)
if gct == ora.Bin || gct == ora.OraBin {
expLen = 1
}
if expLen != int(rowsAffected) {
t.Fatalf("insert rows affected: expected(%v), actual(%v)", expLen, rowsAffected)
}
// select
selectStmt, err := testSes.Prep(fmt.Sprintf("select c1 from %v", tableName), gct)
testErr(err, t)
defer selectStmt.Close()
rset, err := selectStmt.Qry()
testErr(err, t)
defer rset.Exhaust()
// validate
validate(expected, rset, t)
}
func testBindDefineDB(expected interface{}, t *testing.T, oct oracleColumnType) {
for n := 0; n < testIterations(); n++ {
func() {
tableName := createTableDB(testDb, t, oct)
defer dropTableDB(testDb, t, tableName)
// insert
stmt, err := testDb.Prepare(fmt.Sprintf("insert into %v (c1) values (:c1)", tableName))
testErr(err, t)
defer stmt.Close()
execResult, err := stmt.Exec(expected)
testErr(err, t)
rowsAffected, err := execResult.RowsAffected()
testErr(err, t)
if 1 != rowsAffected {
t.Fatalf("insert rowsAffected: expected(%v), actual(%v)", 1, rowsAffected)
}
// query
rows, err := testDb.Query(fmt.Sprintf("select c1 from %v", tableName))
testErr(err, t)
defer rows.Close()
testErr(err, t)
if rows == nil {
t.Fatalf("no rows returned")
} else {
var rowCount int
var goColumnType ora.GoColumnType
if oct == longRaw || oct == longRawNull || oct == raw2000 || oct == raw2000Null || oct == blob || oct == blobNull {
goColumnType = ora.Bin
} else {
goColumnType = goColumnTypeFromValue(expected)
}
for rows.Next() {
var actual interface{}
err := rows.Scan(&actual)
testErr(err, t)
compare(expected, actual, goColumnType, t)
rowCount++
}
if 1 != rowCount {
t.Fatalf("query row count: expected(%v), actual(%v)", 1, rowCount)
}
}
}()
}
}
func testBindPtr(expected interface{}, oct oracleColumnType, t *testing.T) {
testSes := getSes(t)
defer testSes.Close()
t.Logf("expected=%T", expected)
for n := 0; n < testIterations(); n++ {
tableName, err := createTable(1, oct, testSes)
testErr(err, t)
defer dropTable(tableName, testSes, t)
// create pointer to receive actual value
var actual interface{}
switch expected.(type) {
case int64:
var value int64
actual = &value
case int32:
var value int32
actual = &value
case int16:
var value int16
actual = &value
case int8:
var value int8
actual = &value
case uint64:
var value uint64
actual = &value
case uint32:
var value uint32
actual = &value
case uint16:
var value uint16
actual = &value
case uint8:
var value uint8
actual = &value
case float64:
var value float64
actual = &value
case float32:
var value float32
actual = &value
case time.Time:
var value time.Time
actual = &value
case string:
var value string
actual = &value
case bool:
var value bool
actual = &value
default:
t.Fatalf("no value for %T", expected)
}
// insert
qry := "insert into " + tableName + " (c1) values (:1) returning c1 into :2"
stmt, err := testSes.Prep(qry)
testErr(err, t)
defer stmt.Close()
t.Logf("%q, [%#v %T]", qry, expected, actual)
rowsAffected, err := stmt.Exe(expected, actual)
testErr(err, t)
if rowsAffected != 1 {
t.Fatalf("insert rows affected: expected(%v), actual(%v)", 1, rowsAffected)
}
// validate
t.Logf("actual=%T (%v)", actual, actual)
compare2(expected, actual, t)
}
}
func testMultiDefine(expected interface{}, oct oracleColumnType, t *testing.T) {
testSes := getSes(t)
defer testSes.Close()
for n := 0; n < testIterations(); n++ {
tableName, err := createTable(1, oct, testSes)
testErr(err, t)
defer dropTable(tableName, testSes, t)
// insert
insertStmt, err := testSes.Prep(fmt.Sprintf("insert into %v (c1) values (:c1)", tableName))
testErr(err, t)
defer insertStmt.Close()
rowsAffected, err := insertStmt.Exe(expected)
testErr(err, t)
if rowsAffected != 1 {
t.Fatalf("insert rows affected: expected(%v), actual(%v)", 1, rowsAffected)
}
// select
var qry string
var params []ora.GoColumnType
if isNumeric(expected) {
qry = fmt.Sprintf("select c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1, c1 from %v", tableName)
params = append(params, ora.I64, ora.I32, ora.I16, ora.I8, ora.U64, ora.U32, ora.U16, ora.U8, ora.F64, ora.F32, ora.OraI64, ora.OraI32, ora.OraI16, ora.OraI8, ora.OraU64, ora.OraU32, ora.OraU16, ora.OraU8, ora.OraF64, ora.OraF32)
} else if isTime(expected) {
qry = fmt.Sprintf("select c1, c1 from %v", tableName)
params = append(params, ora.T, ora.OraT)
} else if isString(expected) {
qry = fmt.Sprintf("select c1 from %v", tableName)
params = append(params, ora.S)
} else if isBool(expected) {
qry = fmt.Sprintf("select c1, c1 from %v", tableName)
params = append(params, ora.B, ora.OraB)
} else if isBytes(expected) {
// one LOB cannot be opened twice in the same transaction (c1, c1 not works here)
col := ora.Bin
if n%2 == 1 {
col = ora.OraBin
}
qry = fmt.Sprintf("select c1 from %v", tableName)
params = append(params, col)
}
selectStmt, err := testSes.Prep(qry, params...)
testErr(err, t)
defer selectStmt.Close()
rset, err := selectStmt.Qry()
testErr(err, t)
defer rset.Exhaust()
// validate
hasRow := rset.Next()
testErr(errors.Wrapf(rset.Err(), "%q %v", qry, params), t)
if !hasRow {
t.Fatalf("no row returned")
} else if len(rset.Row) != len(selectStmt.Gcts()) {
t.Fatalf("select column count: expected(%v), actual(%v)", len(selectStmt.Gcts()), len(rset.Row))
} else {
for n, goColumnType := range selectStmt.Gcts() {
if isNumeric(expected) {
compare(castInt(expected, goColumnType), rset.Row[n], goColumnType, t)
}
switch goColumnType {
case ora.T:
compare_time(expected, rset.Row[n], t)
case ora.OraT:
if value, ok := rset.Row[n].(ora.Time); ok {
compare_time(expected, value.Value, t)
//} else if value, ok := rset.Row[n].(ora.Date); ok {
// compare_time(expected, value.Value, t)
} else {
t.Fatalf("Unpexected rset.Row[n] value (got %v, expected %v). (%T, %v)", rset.Row[n], expected, rset.Row[n], rset.Row[n])
}
case ora.S:
compare_string(expected, rset.Row[n], t)
case ora.OraS:
value, ok := rset.Row[n].(ora.String)
if ok {
compare_string(expected, value.Value, t)
} else {
t.Fatalf("Unpexected rset.Row[n] value. (%T, %v)", rset.Row[n], rset.Row[n])
}
case ora.B, ora.OraB:
compare_bool(expected, rset.Row[n], t)
case ora.Bin, ora.OraBin:
compare_bytes(expected, rset.Row[n], t)
}
}
}
}
}
// Workload tests proper functioning of bind struct re-use and define struct re-use.
// Bind structs and define structs are stored in pools on a per-type basis.
// This function also tests that bind/defines are cleared and properly reused.
// Insert and query are also exercised.
// Creating multiple columns of the same type will instantiate multiple bind/define structs.
// Running the insert and query multiple times will ensure reuse of those structs.
// Slice binding may have fewer columns tested due to OCI memory contraints.
func testWorkload(oct oracleColumnType, t *testing.T) {
testSes := getSes(t)
defer testSes.Close()
for i := 0; i < testIterations(); i++ {
currentMultiple := testWorkloadColumnCount
for m := 0; m < 3 && currentMultiple > 0; m++ {
tableName, err := createTable(currentMultiple, oct, testSes)
testErr(err, t)
defer dropTable(tableName, testSes, t)
// build insert statement and values
var sql bytes.Buffer
sql.WriteString(fmt.Sprintf("insert into %v (", tableName))
for c := 1; c <= currentMultiple; c++ {
if c > 1 {
sql.WriteString(", ")
}
sql.WriteString(fmt.Sprintf("c%v", c))
}
sql.WriteString(") values (")
expected := make([]interface{}, currentMultiple)
gcts := make([]ora.GoColumnType, currentMultiple)
for c := 0; c < currentMultiple; c++ {
switch oct {
case numberP38S0, numberP38S0Null:
expected[c] = gen_int64()
gcts[c] = ora.I64
case numberP16S15, numberP16S15Null, binaryDouble, binaryDoubleNull, floatP126, floatP126Null:
expected[c] = gen_float64()
gcts[c] = ora.F64
case binaryFloat, binaryFloatNull:
expected[c] = gen_float32()
gcts[c] = ora.F32
case dateNotNull, dateNull:
expected[c] = gen_date()
gcts[c] = ora.T
case timestampP9, timestampP9Null, timestampTzP9, timestampTzP9Null, timestampLtzP9, timestampLtzP9Null:
expected[c] = gen_time()
gcts[c] = ora.T
case charB48, charB48Null, charC48, charC48Null, nchar48, nchar48Null:
expected[c] = gen_string48()
gcts[c] = ora.S
case varcharB48, varcharB48Null, varcharC48, varcharC48Null, varchar2B48, varchar2B48Null, varchar2C48, varchar2C48Null, nvarchar248, nvarchar248Null, long, longNull, clob, clobNull, nclob, nclobNull:
expected[c] = gen_string()
gcts[c] = ora.S
case charB1, charB1Null, charC1, charC1Null:
if gct := ora.Cfg().Char1(); gct == ora.B || gct == ora.OraB {
expected[c] = gen_boolTrue()
gcts[c] = ora.B
} else {
expected[c] = gen_string()[:1]
gcts[c] = ora.S
}
case blob, blobNull, longRaw, longRawNull:
expected[c] = gen_bytes(9)
gcts[c] = ora.Bin
case raw2000, raw2000Null:
expected[c] = gen_bytes(2000)
gcts[c] = ora.Bin
}
if c > 0 {
sql.WriteString(", ")
}
sql.WriteString(fmt.Sprintf(":c%v", c+1))
}
sql.WriteString(")")
// insert values
//fmt.Println(sql.String())
_, err = testSes.PrepAndExe(sql.String(), expected...)
testErr(err, t)
// insertStmt, err := testCon.Prepare(sql.String())
// testErr(err, t)
// _, err = insertStmt.Exec(expected)
// testErr(err, t)
// insertStmt.Close()
// fetch values and compare
sql.Reset()
sql.WriteString(fmt.Sprintf("select * from %v", tableName))
fetchStmt, err := testSes.Prep(sql.String())
testErr(err, t)
fetchStmt.SetGcts(gcts)
rset, err := fetchStmt.Qry()
testErr(err, t)
defer rset.Exhaust()
for rset.Next() {
if currentMultiple != len(rset.Row) {
t.Fatalf("select column count: expected(%v), actual(%v)", currentMultiple, len(rset.Row))
} else {
for n := 0; n < currentMultiple; n++ {
expectedElem := elemAt(expected, n)
compare(expectedElem, rset.Row[n], gcts[n], t)
}
}
}
testErr(errors.Wrap(rset.Err(), sql.String()), t)
fetchStmt.Close()
// Reduce the multiple by half
currentMultiple = currentMultiple / 2
}
}
}
func loadDbtimezone() (*time.Location, error) {
testSes, err := testSesPool.Get()
if err != nil {
return nil, err
}
defer testSes.Close()
return testSes.Timezone()
}
func validate(expected interface{}, rset *ora.Rset, t *testing.T) {
row := rset.NextRow()
//t.Logf("Row=%v (%v) Index=%d", rset.Row, row, rset.Index)
if 1 != len(rset.Row) {
t.Fatalf("column count: expected(%v), actual(%v)", 1, len(rset.Row))
}
switch expected.(type) {
case int64:
compare_int64(expected, row[0], t)
case int32:
compare_int32(expected, row[0], t)
case int16:
compare_int16(expected, row[0], t)
case int8:
compare_int8(expected, row[0], t)
case uint64:
compare_uint64(expected, row[0], t)
case uint32:
compare_uint32(expected, row[0], t)
case uint16:
compare_uint16(expected, row[0], t)
case uint8:
compare_uint8(expected, row[0], t)
case float64:
compare_float64(expected, row[0], t)
case float32:
compare_float32(expected, row[0], t)
case ora.Int64:
compare_OraInt64(expected, row[0], t)
case ora.Int32:
compare_OraInt32(expected, row[0], t)
case ora.Int16:
compare_OraInt16(expected, row[0], t)
case ora.Int8:
compare_OraInt8(expected, row[0], t)
case ora.Uint64:
compare_OraUint64(expected, row[0], t)
case ora.Uint32:
compare_OraUint32(expected, row[0], t)
case ora.Uint16:
compare_OraUint16(expected, row[0], t)
case ora.Uint8:
compare_OraUint8(expected, row[0], t)
case ora.Float64:
compare_OraFloat64(expected, row[0], t)
case ora.Float32:
compare_OraFloat32(expected, row[0], t)
case ora.IntervalYM:
compare_OraIntervalYM(expected, row[0], t)
case ora.IntervalDS:
compare_OraIntervalDS(expected, row[0], t)
case ora.Bfile:
compare_OraBfile(expected, row[0], t)
case []int64:
for {
//t.Logf("Row=%v Index=%d", rset.Row, rset.Index)
expectedElem := elemAt(expected, rset.Len()-1)
compare_int64(expectedElem, rset.Row[0], t)
if !rset.Next() {
break
}
}
case []ora.IntervalYM:
for {
expectedElem := elemAt(expected, rset.Len()-1)
compare_OraIntervalYM(expectedElem, rset.Row[0], t)
if !rset.Next() {
break
}
}
case []ora.IntervalDS:
for {
expectedElem := elemAt(expected, rset.Len()-1)
compare_OraIntervalDS(expectedElem, rset.Row[0], t)
if !rset.Next() {
break
}
}
}
testErr(rset.Err(), t)
}
func compare2(expected interface{}, actual interface{}, t *testing.T) {
switch expected.(type) {
case int64:
compare_int64(expected, actual, t)
case int32:
compare_int32(expected, actual, t)
case int16:
compare_int16(expected, actual, t)
case int8:
compare_int8(expected, actual, t)
case uint64:
compare_uint64(expected, actual, t)
case uint32:
compare_uint32(expected, actual, t)
case uint16:
compare_uint16(expected, actual, t)
case uint8:
compare_uint8(expected, actual, t)
case float64:
compare_float64(expected, actual, t)
case float32:
compare_float32(expected, actual, t)
case ora.Int64:
compare_OraInt64(expected, actual, t)
case ora.Int32:
t.Logf("actual=%T (%#v)", actual, actual)
compare_OraInt32(expected, actual, t)
case ora.Int16:
compare_OraInt16(expected, actual, t)
case ora.Int8:
compare_OraInt8(expected, actual, t)
case ora.Uint64:
compare_OraUint64(expected, actual, t)
case ora.Uint32:
compare_OraUint32(expected, actual, t)
case ora.Uint16:
compare_OraUint16(expected, actual, t)
case ora.Uint8:
compare_OraUint8(expected, actual, t)
case ora.Float64:
compare_OraFloat64(expected, actual, t)
case ora.Float32:
compare_OraFloat32(expected, actual, t)
case ora.IntervalYM:
compare_OraIntervalYM(expected, actual, t)
case ora.IntervalDS:
compare_OraIntervalDS(expected, actual, t)
}
}
func createTable(multiple int, oct oracleColumnType, ses *ora.Ses) (string, error) {
tableName := fmt.Sprintf("%v_%v", tableName(), multiple)
qry := createTableSql(tableName, multiple, oct)
stmt, err := ses.Prep(qry)
if err != nil {
return "", errors.Wrap(err, qry)
}
defer stmt.Close()
_, err = stmt.Exe()
return tableName, errors.Wrap(err, qry)
}
func dropTable(tableName string, ses *ora.Ses, t testing.TB) {
qry := fmt.Sprintf("drop table %v", tableName)
stmt, err := ses.Prep(qry)
if err != nil {
t.Log(err)
return
}
//testErr(errors.Wrap(err, qry), t)
defer stmt.Close()
if _, err = stmt.Exe(); err != nil {
t.Log(err)
}
//testErr(errors.Wrap(err, qry), t)
}
func createTableDB(db *sql.DB, t *testing.T, octs ...oracleColumnType) string {
tableName := tableName()
qry := createTableSql(tableName, 1, octs...)
stmt, err := db.Prepare(qry)
testErr(errors.Wrap(err, qry), t)
defer stmt.Close()
_, err = stmt.Exec()
testErr(errors.Wrap(err, qry), t)
return tableName
}
func dropTableDB(db *sql.DB, t *testing.T, tableName string) {
qry := fmt.Sprintf("drop table %v", tableName)
stmt, err := db.Prepare(qry)
testErr(errors.Wrap(err, qry), t)
defer stmt.Close()
_, err = stmt.Exec()
testErr(errors.Wrap(err, qry), t)
}
func createTableSql(tableName string, multiple int, columns ...oracleColumnType) string {
var b bytes.Buffer
b.WriteString(fmt.Sprintf("create table %v (", tableName))
for m := 1; m <= multiple; m++ {
for n, column := range columns {
position := (n + 1) * m
if position > 1 {
b.WriteString(", ")
}
b.WriteString(fmt.Sprintf("c%v %v", position, column))
}
}
b.WriteString(")")
return b.String()
}
func tableName() string {
nm := tableNamePrefix + strconv.FormatUint(uint64(atomic.AddUint32(&testTableID, 1)), 10)
return nm
}
func testErr(err error, t testing.TB, expectedErrs ...error) {
if err == nil {
return
}
for _, expectedErr := range expectedErrs {
if expectedErr == err { // skip it
return
}
}
done := make(chan struct{})
msg := fmt.Sprintf("%+v: %s", err, getStack(2))
if true {
go func() {
select {
case <-time.After(300 * time.Second):
fmt.Printf("\n\nPRINT TIMEOUT\n%s", msg)
case <-done:
}
}()
}
t.Fatal(msg)
close(done)
if strings.Contains(err.Error(), "ORA-01000:") {
os.Exit(1)
}
}
func goColumnTypeFromValue(value interface{}) ora.GoColumnType {
switch value.(type) {
case int64, []int64:
return ora.I64
case int32, []int32:
return ora.I32
case int16, []int16:
return ora.I16
case int8, []int8:
return ora.I8
case uint64, []uint64:
return ora.U64
case uint32, []uint32:
return ora.U32
case uint16, []uint16:
return ora.U16
case uint8, []uint8:
return ora.U8
case float64, []float64:
return ora.F64
case float32, []float32:
return ora.F32
case ora.Int64, []ora.Int64:
return ora.OraI64
case ora.Int32, []ora.Int32:
return ora.OraI32
case ora.Int16, []ora.Int16:
return ora.OraI16
case ora.Int8, []ora.Int8:
return ora.OraI8
case ora.Uint64, []ora.Uint64:
return ora.OraU64
case ora.Uint32, []ora.Uint32:
return ora.OraU32
case ora.Uint16, []ora.Uint16:
return ora.OraU16
case ora.Uint8, []ora.Uint8:
return ora.OraU8
case ora.Float64, []ora.Float64:
return ora.OraF64
case ora.Float32, []ora.Float32:
return ora.OraF32
case ora.Num, []ora.Num:
return ora.N
case ora.OraNum, []ora.OraNum:
return ora.OraN
case time.Time, []time.Time:
return ora.T
case ora.Time, []ora.Time:
return ora.OraT
case string, []string:
return ora.S
case ora.String, []ora.String:
return ora.OraS
case bool, []bool:
return ora.B
case ora.Bool, []ora.Bool:
return ora.OraB
case ora.Raw:
return ora.OraBin
}
return ora.D
}
func isNumeric(value interface{}) bool {
switch value.(type) {
case int8, int16, int32, int64, int, ora.Int8, ora.Int16, ora.Int32, ora.Int64:
return true
case uint, uint8, uint16, uint32, uint64, ora.Uint8, ora.Uint16, ora.Uint32, ora.Uint64:
return true
case float32, float64, ora.Float32, ora.Float64, ora.Num, ora.OraNum:
return true
}
return false
}
func isTime(value interface{}) bool {
if _, ok := value.(time.Time); ok {
return true
}