forked from nakagami/firebirdsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
1398 lines (1190 loc) · 35.5 KB
/
driver_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
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2013-2019 Hajime Nakagami
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
import (
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math/big"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
var (
longQueryNonSelectable = `
execute block
as
declare c integer = 0;
begin
while (c < 9000000000 ) do
begin
c = c + 1;
end
end`
longQuerySelectable = `
execute block returns (i integer) as declare c integer = 0;
begin
i = 0;
while (c < 9000000000 ) do
begin
c = c + 1;
i = c;
suspend;
end
end`
)
func get_firebird_major_version(conn *sql.DB) int {
var s string
conn.QueryRow("SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') from rdb$database").Scan(&s)
major_version, _ := strconv.Atoi(s[:strings.Index(s, ".")])
return major_version
}
func GetTestDSN(prefix string) string {
var tmppath string
randBytes := make([]byte, 16)
rand.Read(randBytes)
tmppath = filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+".fdb")
if runtime.GOOS == "windows" {
tmppath = "/" + tmppath
}
test_user := "sysdba"
if isc_user := os.Getenv("ISC_USER"); isc_user != "" {
test_user = isc_user
}
test_password := "masterkey"
if isc_password := os.Getenv("ISC_PASSWORD"); isc_password != "" {
test_password = isc_password
}
retorno := test_user + ":" + test_password + "@localhost:3050"
return retorno + tmppath
}
func TestBasic(t *testing.T) {
test_dsn := GetTestDSN("test_basic_")
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
var n int
query := "SELECT Count(*) FROM rdb$relations where rdb$relation_name='FOO'"
err = conn.QueryRow(query).Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n > 0 {
conn.Exec("DROP TABLE foo")
}
query = `
CREATE TABLE foo (
a INTEGER NOT NULL,
b VARCHAR(30) NOT NULL UNIQUE,
c VARCHAR(1024),
d DECIMAL(16,3) DEFAULT -0.123,
e DATE DEFAULT '1967-08-11',
f TIMESTAMP DEFAULT '1967-08-11 23:45:01',
g TIME DEFAULT '23:45:01',
h BLOB SUB_TYPE 1,
i DOUBLE PRECISION DEFAULT 0.0,
j FLOAT DEFAULT 0.0,
PRIMARY KEY (a),
CONSTRAINT CHECK_A CHECK (a <> 0)
)
`
conn.Exec(query)
conn.Close()
time.Sleep(1 * time.Second)
conn, err = sql.Open("firebirdsql", test_dsn)
_, err = conn.Exec("CREATE TABLE foo (a INTEGER)")
if err == nil {
t.Fatalf("Need metadata update error")
}
if !strings.Contains(err.Error(), "unsuccessful metadata update\n") {
t.Fatalf("Bad message:%v", err.Error())
}
// 3 records insert
conn.Exec("insert into foo(a, b, c, h) values (1, 'a', 'b','This is a memo')")
conn.Exec("insert into foo(a, b, c, e, g, i, j) values (2, 'A', 'B', '1999-01-25', '00:00:01', 0.1, 0.1)")
conn.Exec("insert into foo(a, b, c, e, g, i, j) values (3, 'X', 'Y', '2001-07-05', '00:01:02', 0.2, 0.2)")
err = conn.QueryRow("select count(*) cnt from foo").Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n != 3 {
t.Fatalf("Error bad record count: %v", n)
}
rows, err := conn.Query("select a, b, c, d, e, f, g, h, i, j from foo")
columns, err := rows.Columns()
if !reflect.DeepEqual(columns, []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}) {
t.Fatalf("Columns() mismatch: %v", columns)
}
var a int
var b, c string
var d float64
var e time.Time
var f time.Time
var g time.Time
var h []byte
var i float64
var j float32
for rows.Next() {
rows.Scan(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j)
// fmt.Println(a, b, c, d, e, f, g, h, i, j)
}
stmt, _ := conn.Prepare("select count(*) from foo where a=? and b=? and d=? and e=? and f=? and g=?")
ep := time.Date(1967, 8, 11, 0, 0, 0, 0, time.Local)
fp := time.Date(1967, 8, 11, 23, 45, 1, 0, time.Local)
gp, err := time.Parse("15:04:05", "23:45:01")
err = stmt.QueryRow(1, "a", -0.123, ep, fp, gp).Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n != 1 {
t.Fatalf("Error bad record count: %v", n)
}
// Issue #169
stmt, _ = conn.Prepare("select * from foo where a=?")
for k := 1; k < 5; k++ {
rows, err := stmt.Query(k)
require.NoError(t, err)
rows.Next()
err = rows.Close()
require.NoError(t, err)
}
// Issue #174
// https://github.com/nakagami/firebirdsql/issues/174#issue-2312621571
stmt1, err := conn.Prepare("select * from foo where a=?")
require.NoError(t, err)
stmt2, err := conn.Prepare("select * from foo where a=?")
require.NoError(t, err)
for k := 0; k < 3; k++ {
rows1, err := stmt1.Query(k)
require.NoError(t, err)
rows2, err := stmt2.Query(1)
require.NoError(t, err)
err = rows1.Close()
require.NoError(t, err)
err = rows2.Close()
require.NoError(t, err)
}
err = stmt1.Close()
require.NoError(t, err)
err = stmt2.Close()
require.NoError(t, err)
// https://github.com/nakagami/firebirdsql/issues/174#issuecomment-2134693366
stmt, err = conn.Prepare("select * from foo where a=?")
require.NoError(t, err)
tx, err := conn.Begin()
require.NoError(t, err)
err = tx.Commit()
require.NoError(t, err)
rows, err = stmt.Query(1)
require.NoError(t, err)
rows.Close()
require.NoError(t, err)
// https://github.com/nakagami/firebirdsql/issues/174#issuecomment-2139296394
// START TX1
tx, err = conn.Begin()
require.NoError(t, err)
stmt1, err = conn.Prepare(`select * from foo where a=?`)
require.NoError(t, err)
rows1, err := tx.Stmt(stmt1).Query(1)
require.NoError(t, err)
err = rows1.Close()
require.NoError(t, err)
err = stmt1.Close()
require.NoError(t, err)
stmt2, err = conn.Prepare(`select * from foo where a=?`)
require.NoError(t, err)
_, err = tx.Stmt(stmt2).Exec(333)
require.NoError(t, err)
err = tx.Commit()
require.NoError(t, err)
// END TX1
// START TX2
tx, err = conn.Begin()
require.NoError(t, err)
_, err = tx.Stmt(stmt2).Exec(333)
require.NoError(t, err)
err = tx.Rollback()
require.NoError(t, err)
// END TX2
err = conn.Close()
require.NoError(t, err)
// Issue #174 end
conn.Close()
}
func TestReturning(t *testing.T) {
test_dsn := GetTestDSN("test_returning_")
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error sql.Open() : %v", err)
}
conn.Exec(`
CREATE TABLE test_returning (
f1 integer NOT NULL,
f2 integer default 2,
f3 varchar(20) default 'abc')`)
conn.Close()
time.Sleep(1 * time.Second)
conn, err = sql.Open("firebirdsql", test_dsn)
if err != nil {
t.Fatalf("Error sql.Open() : %v", err)
}
for i := 0; i < 2; i++ {
rows, err := conn.Query("INSERT INTO test_returning (f1) values (1) returning f2, f3")
if err != nil {
t.Fatalf("Error Insert returning : %v", err)
}
var f2 int
var f3 string
rows.Next()
rows.Scan(&f2, &f3)
if f2 != 2 || f3 != "abc" {
t.Fatalf("Bad value insert returning: %v,%v", f2, f3)
}
}
conn.Close()
}
func TestInsertBlobsWithParams(t *testing.T) {
test_dsn := GetTestDSN("test_insert_blobs_with_params")
conn, _ := sql.Open("firebirdsql_createdb", test_dsn)
conn.Exec("CREATE TABLE test_blobs (f1 BLOB SUB_TYPE 0, f2 BLOB SUB_TYPE 1)")
conn.Close()
time.Sleep(1 * time.Second)
conn, _ = sql.Open("firebirdsql", test_dsn)
s0 := "Test Text"
b0 := []byte{0, 1, 2, 3, 4, 13, 10, 5, 6, 7}
if _, err := conn.Exec("INSERT INTO test_blobs (f1, f2) values (?, ?)", b0, s0); err != nil {
t.Fatalf("Error inserting blobs with params: %v", err)
}
var s string
var b []byte
err := conn.QueryRow("SELECT f1, f2 from test_blobs").Scan(&b, &s)
if err != nil {
t.Fatalf("Error in query: %v", err)
}
if s != s0 {
t.Fatalf("Text blob: expected <%s>, got <%s>", s0, s)
}
if !reflect.DeepEqual(b, b0) {
t.Fatalf("Binary blob: expected <%v>, got <%v> (%s)", b0, b, string(b))
}
conn.Close()
}
func TestError(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_error_"))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
_, err = conn.Exec("incorrect sql statement")
if err == nil {
t.Fatalf("Incorrect error")
} else if err.Error() != "Dynamic SQL Error\nSQL error code = -104\nToken unknown - line 1, column 1\nincorrect\n" {
t.Fatalf("Incorrect error: %v", err.Error())
}
conn.Close()
}
func TestRole(t *testing.T) {
test_dsn := GetTestDSN("test_role_")
conn1, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error creating: %v", err)
}
conn1.Exec("CREATE TABLE test_role (f1 integer)")
conn1.Exec("INSERT INTO test_role (f1) values (1)")
_, err = conn1.Exec("CREATE ROLE DRIVERROLE")
if err != nil {
t.Fatalf("Error creating role: %v", err)
}
conn1.Exec("GRANT DRIVERROLE TO SYSDBA")
if err != nil {
t.Fatalf("Error creating role: %v", err)
}
conn1.Exec("GRANT SELECT ON test_role TO DRIVERROLE")
if err != nil {
t.Fatalf("Error granting right to role: %v", err)
}
conn1.Close()
time.Sleep(1 * time.Second)
conn2, err := sql.Open("firebirdsql", test_dsn+"?role=driverrole")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
rows, err := conn2.Query("SELECT f1 FROM test_role")
if err != nil {
t.Fatalf("Error Query: %v", err)
}
for rows.Next() {
}
conn2.Close()
}
func TestInsertTimestamp(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_timestamp_"))
if err != nil {
t.Fatalf("Error creating: %v", err)
}
_, err = conn.Exec("CREATE TABLE TEST (VAL1 TIMESTAMP, VAL2 TIMESTAMP, VAL3 TIMESTAMP, VAL4 TIMESTAMP)")
if err != nil {
t.Fatalf("Error creating table: %v", err)
}
dt1 := time.Date(2015, 2, 9, 19, 25, 50, 740500000, time.Local)
dt2 := "2015/2/9 19:25:50.7405"
dt3 := "2015-2-9 19:25:50.7405"
if _, err = conn.Exec("INSERT INTO TEST (VAL1, VAL2, VAL3, VAL4) VALUES (?, ?, ?, '2015/2/9 19:25:50.7405')", dt1, dt2, dt3); err != nil {
t.Fatalf("Error executing insert: %s", err)
}
var rt1, rt2, rt3, rt4 time.Time
err = conn.QueryRow("SELECT * FROM TEST").Scan(&rt1, &rt2, &rt3, &rt4)
if err != nil {
t.Fatalf("Unexpected error in select: %s", err)
}
if rt1 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt1)
}
if rt2 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt2)
}
if rt3 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt3)
}
if rt4 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt4)
}
conn.Close()
}
func TestBoolean(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_boolean_"))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
firebird_major_version := get_firebird_major_version(conn)
if firebird_major_version < 3 {
return
}
var sql string
var n int
sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='TEST_FB3'"
err = conn.QueryRow(sql).Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n > 0 {
conn.Exec("DROP TABLE test_fb3")
}
sql = `
CREATE TABLE test_fb3 (
b BOOLEAN
)
`
conn.Exec(sql)
conn.Exec("insert into test_fb3(b) values (true)")
conn.Exec("insert into test_fb3(b) values (false)")
var b bool
err = conn.QueryRow("select * from test_fb3 where b is true").Scan(&b)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if b != true {
conn.Exec("Invalid boolean value")
}
err = conn.QueryRow("select * from test_fb3 where b is false").Scan(&b)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if b != false {
conn.Exec("Invalid boolean value")
}
stmt, _ := conn.Prepare("select * from test_fb3 where b=?")
err = stmt.QueryRow(true).Scan(&b)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if b != false {
conn.Exec("Invalid boolean value")
}
conn.Close()
}
func TestDecFloat(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_decfloat_"))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
firebird_major_version := get_firebird_major_version(conn)
if firebird_major_version < 4 {
return
}
query := `
CREATE TABLE test_decfloat (
i integer,
d DECIMAL(20, 2),
df16 DECFLOAT(16),
df34 DECFLOAT(34),
s varchar(32)
)
`
conn.Exec(query)
conn.Exec("insert into test_decfloat(i, d, df16, df34, s) values (1, 0.0, 0.0, 0.0, '0.0')")
conn.Exec("insert into test_decfloat(i, d, df16, df34, s) values (2, 1.1, 1.1, 1.1, '1.1')")
conn.Exec("insert into test_decfloat(i, d, df16, df34, s) values (3, 120.2, 120.2, 120.2, '120.2')")
conn.Exec("insert into test_decfloat(i, d, df16, df34, s) values (4, -1.1, -1.1, -1.1, '-1.1')")
conn.Exec("insert into test_decfloat(i, d, df16, df34, s) values (5, -120.2, -120.2, -120.2, '-120.2')")
var n int
err = conn.QueryRow("select count(*) cnt from test_decfloat").Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n != 5 {
t.Fatalf("Error bad record count: %v", n)
}
rows, err := conn.Query("select df16, df34, s from test_decfloat order by i")
var df16, df34 sql.NullFloat64
var s string
for rows.Next() {
rows.Scan(&df16, &df34, &s)
f, _ := strconv.ParseFloat(s, 64)
df16v, _ := df16.Value()
df34v, _ := df34.Value()
if df16v != f || df34v != f {
fmt.Printf("Error decfloat value : %v,%v,%v\n", df16v, df34v, f)
}
}
conn.Close()
}
func TestTimeZone(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_timezone_")+"?timezone=Asia/Tokyo")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
firebird_major_version := get_firebird_major_version(conn)
if firebird_major_version < 4 {
return
}
sql := `
CREATE TABLE test_timezone (
id INTEGER NOT NULL,
a TIME WITH TIME ZONE DEFAULT '12:34:56',
b TIMESTAMP WITH TIME ZONE DEFAULT '1967-08-11 23:45:01',
PRIMARY KEY (id)
)
`
conn.Exec(sql)
conn.Exec("insert into test_timezone (id) values (0)")
conn.Exec("insert into test_timezone (id, a, b) values (1, '12:34:56 Asia/Seoul', '1967-08-11 23:45:01.0000 Asia/Seoul')")
conn.Exec("insert into test_timezone (id, a, b) values (2, '03:34:56 UTC', '1967-08-11 14:45:01.0000 UTC')")
var id int
var a time.Time
var b time.Time
rows, _ := conn.Query("select * from test_timezone")
expected := []string{
"0000-01-01 12:34:56 +0900 JST, 1967-08-11 23:45:01 +0900 JST",
"0000-01-01 12:34:56 +0900 KST, 1967-08-11 23:45:01 +0900 KST",
"0000-01-01 03:34:56 +0000 UTC, 1967-08-11 14:45:01 +0000 UTC"}
for rows.Next() {
rows.Scan(&id, &a, &b)
s := fmt.Sprintf("%v, %v", a, b)
if s != expected[id] {
t.Fatalf("Incorrect result: %v", s)
}
}
conn.Close()
}
func TestInt128(t *testing.T) {
// https://github.com/nakagami/firebirdsql/issues/129
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_int128_"))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
firebird_major_version := get_firebird_major_version(conn)
if firebird_major_version < 4 {
return
}
sql := `
CREATE TABLE test_int128 (
i int128
)
`
conn.Exec(sql)
conn.Exec("insert into test_int128(i) values (170141183460469231731687303715884105727)")
var i128 *big.Int
err = conn.QueryRow("SELECT i FROM test_int128").Scan(&i128)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}
var toCmp = new(big.Int)
toCmp, _ = toCmp.SetString("170141183460469231731687303715884105727", 10)
if i128.Cmp(toCmp) != 0 {
t.Fatalf("INT128 Error: %v", i128)
}
conn.Close()
}
func TestNegativeInt128(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_negative_int128_"))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
firebird_major_version := get_firebird_major_version(conn)
if firebird_major_version < 4 {
return
}
sql := `
CREATE TABLE test_negative_int128 (
i int128
)
`
conn.Exec(sql)
conn.Exec("insert into test_negative_int128(i) values (-170141183460469231731687303715884105727)")
var i128 *big.Int
err = conn.QueryRow("SELECT i FROM test_negative_int128").Scan(&i128)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}
var toCmp = new(big.Int)
toCmp, _ = toCmp.SetString("-170141183460469231731687303715884105727", 10)
if i128.Cmp(toCmp) != 0 {
t.Fatalf("Negative INT128 Error: %v", i128)
}
conn.Close()
}
func TestLegacyAuthWireCrypt(t *testing.T) {
test_dsn := GetTestDSN("test_legacy_auth_")
var n int
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.Ping()
if err != nil {
t.Fatalf("Error ping: %v", err)
}
conn.Close()
time.Sleep(1 * time.Second)
conn, err = sql.Open("firebirdsql", test_dsn+"?auth_plugin_anme=Legacy_Auth")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}
conn.Close()
conn, err = sql.Open("firebirdsql", test_dsn+"?wire_crypt=false")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}
conn.Close()
conn, err = sql.Open("firebirdsql", test_dsn+"?auth_plugin_name=Legacy_Auth&wire_auth=true")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}
conn.Close()
conn, err = sql.Open("firebirdsql", test_dsn+"?auth_plugin_name=Legacy_Auth&wire_auth=false")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}
conn.Close()
}
func TestErrorConnect(t *testing.T) {
var n int
conn, err := sql.Open("firebirdsql", "foo:bar@something_wrong_hostname:3050/dbname")
if err != nil {
t.Fatalf("Error occured at sql.Open()")
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
if err == nil {
t.Fatalf("Error not occured")
}
conn.Close()
}
func TestGoIssue44(t *testing.T) {
conn, err := sql.Open("firebirdsql", "SomethingWrongConnectionString")
err = conn.Ping()
if err == nil {
t.Fatalf("Error not occured")
}
conn.Close()
}
func TestGoIssue45(t *testing.T) {
test_dsn := GetTestDSN("test_issue45_")
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error occured at sql.Open()")
}
conn.Exec(`
CREATE TABLE person (
name VARCHAR(60) NOT NULL,
created TIMESTAMP
)
`)
conn.Exec(`
insert into person (name, created)
values ('Giovanni', null)
`)
conn.Close()
time.Sleep(1 * time.Second)
conn, err = sql.Open("firebirdsql", test_dsn)
// select null value
type response struct {
name string
created *time.Time
}
r := response{}
err = conn.QueryRow(`
select name, created from person
`).Scan(&r.name, &r.created)
if err != nil {
t.Fatalf("Error: %v", err)
}
if r.created != nil {
t.Fatalf("created is not nil")
}
// insert returning not null value
err = conn.QueryRow(`
insert into person (name, created)
values ('Giovanni Gaspar', current_timestamp)
returning name, created
`).Scan(&r.name, &r.created)
if err != nil {
t.Fatalf("Error: %v", err)
}
if r.created == nil {
t.Fatalf("created is nil")
}
// insert returning null value
err = conn.QueryRow(`
insert into person (name, created)
values ('Nakagami', null)
returning name, created
`).Scan(
&r.name, &r.created)
if err != nil {
t.Fatalf("Error: %v", err)
}
if r.created != nil {
t.Fatalf("created is not nil")
}
conn.Close()
}
func TestGoIssue49(t *testing.T) {
test_dsn := GetTestDSN("test_issue49_")
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error occured at sql.Open()")
}
defer conn.Close()
sqlCreate := `
CREATE TABLE NullTest (
name VARCHAR(60) NOT NULL,
nullname VARCHAR(10),
nullDate DATE,
bug1 SMALLINT,
bug2 INTEGER
)
`
conn.Exec(sqlCreate)
//Worked
sqlTest1 := `insert into NullTest (name, nullDate)values ('value', null)`
_, err = conn.Exec(sqlTest1)
if err != nil {
t.Error(err)
}
//Worked
sqlTest1 = `insert into NullTest (name, nullDate)values (?, ?)`
_, err = conn.Exec(sqlTest1, "value", nil)
if err != nil {
t.Error(err)
}
//Failed
sqlTest1 = `insert into NullTest (name, nullDate)values (?, ?)`
_, err = conn.Exec(sqlTest1, "value", nil)
if err != nil {
t.Error(err)
}
// Failed
sqlTest1 = `insert into NullTest (name, bug1) values ('value', ?)`
_, err = conn.Exec(sqlTest1, nil)
if err != nil {
t.Error(err)
}
// Failed
sqlTest1 = `insert into NullTest (name, bug1,bug2) values ('value', ?,?)`
_, err = conn.Exec(sqlTest1, nil, nil)
if err != nil {
t.Error(err)
}
// must be failed!
sqlTest1 = `insert into NullTest (name, bug1) values ('value', ?)`
_, err = conn.Exec(sqlTest1)
if err == nil {
t.Error("Expected error!")
}
}
func TestGoIssue53(t *testing.T) {
timeout := time.Second * 40
test_dsn := GetTestDSN("test_issue53_")
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error occured at sql.Open()")
}
defer conn.Close()
tests := []int{
31,
BLOB_SEGMENT_SIZE,
BLOB_SEGMENT_SIZE + 1,
22*BLOB_SEGMENT_SIZE + 21,
97*BLOB_SEGMENT_SIZE + 21,
}
conn.Exec(`CREATE TABLE BlobTest (bugField blob sub_type binary )`)
for _, test := range tests {
t.Run(fmt.Sprintf("%d", test), func(t *testing.T) {
sqlDelete := `delete from BlobTest`
_, err = conn.Exec(sqlDelete)
if err != nil {
t.Error(err)
}
sqlTest1 := `insert into BlobTest(bugField) values(?)`
str := strings.Repeat("F", test)
done := make(chan bool)
go func(ch chan bool) {
_, err = conn.Exec(sqlTest1, str)
if err != nil {
t.Error(err)
}
close(done)
}(done)
select {
case <-done:
case <-time.After(timeout):
t.Fatal("Test timed out after ", timeout)
}
sqlget := `select bugField from BlobTest`
rows, err := conn.Query(sqlget)
if err != nil {
t.Error(err)
return
}
for rows.Next() {
var buf []byte
err = rows.Scan(&buf)
if err != nil {
t.Error(err)
}
if len(buf) != test {
t.Errorf("Expected size blob %d, got %d", test, len(buf))
}
}
rows.Close()
})
}
}
func TestGoIssue65(t *testing.T) {
test_dsn := GetTestDSN("test_issue65_")
conn, err := sql.Open("firebirdsql_createdb", test_dsn)
if err != nil {
t.Fatalf("Error occured at sql.Open()")
}
defer conn.Close()
conn.Exec(`CREATE TABLE FPI_MOVTO_MOVIMIENTOS
(
RFCEMPRESA varchar(20) NOT NULL,
NOSUCURSAL integer NOT NULL,
TIPO integer NOT NULL,
SERIE varchar(5) NOT NULL,
NODOCTO integer NOT NULL,
LINEA integer NOT NULL,
CODART varchar(20),
NOMART varchar(80),
CLAVEPRODSERV varchar(10),
UNIDADCLAVE varchar(10),
UNIDADNOMBRE varchar(80),
CANT1 double precision,
CATN2 double precision,
PUNIT double precision,
MONTO double precision,
IMPTO1 double precision,
IMPTO2 double precision,
PIMPTO1 double precision,
PIMPTO2 double precision,
TIMPTO1 varchar(10),
TIMPTO2 varchar(10),
TFIMPTO1 varchar(10),
TFIMPTO2 varchar(10),
PDESCTO double precision,
IDESCTO double precision,
CONSTRAINT PXFPI_MOVTO_MOVIMIENTOS PRIMARY KEY (RFCEMPRESA,NOSUCURSAL,TIPO,SERIE,NODOCTO,LINEA)
);`)
//Worked
sqlTest1 := `INSERT INTO FPI_MOVTO_MOVIMIENTOS (RFCEMPRESA, NOSUCURSAL, TIPO, SERIE, NODOCTO, LINEA, CODART, NOMART, CLAVEPRODSERV, UNIDADCLAVE, UNIDADNOMBRE, CANT1, CATN2, PUNIT, MONTO, IMPTO1, IMPTO2, PIMPTO1, PIMPTO2, TIMPTO1, TIMPTO2, TFIMPTO1, TFIMPTO2, PDESCTO, IDESCTO) VALUES ('p2', '0', '700', 'X', '1', '1', 'ART-001', 'PRUEBA DE ARTICULO', '01010101', 'ACT', 'Actividad', '10.000000', '0.000000', '2.500000', '25.000000', '4.000000', '0.000000', '16.000000', '0.000000', '002', '', 'Tasa', '', '0.000000', '0.000000');`
_, err = conn.Exec(sqlTest1)
if err != nil {
t.Error(err)