Debian Codegraph @

queries_go18_test.go

raw symbols SCIP
1 // +build go1.8
2
3 package mssql
4
5 import (
6 "context"
7 "database/sql"
8 "database/sql/driver"
9 "fmt"
10 "net"
11 "reflect"
12 "strings"
13 "sync"
14 "testing"
15 "time"
16 )
17
18 func TestNextResultSet(t *testing.T) {
19 conn := open(t)
20 defer conn.Close()
21 rows, err := conn.Query("select 1; select 2")
22 if err != nil {
23 t.Fatal("Query failed", err.Error())
24 }
25 defer func() {
26 err := rows.Err()
27 if err != nil {
28 t.Error("unexpected error:", err)
29 }
30 }()
31
32 defer rows.Close()
33
34 if !rows.Next() {
35 t.Fatal("Query didn't return row")
36 }
37 var fld1, fld2 int32
38 err = rows.Scan(&fld1)
39 if err != nil {
40 t.Fatal("Scan failed", err)
41 }
42 if fld1 != 1 {
43 t.Fatal("Returned value doesn't match")
44 }
45 if rows.Next() {
46 t.Fatal("Query returned unexpected second row.")
47 }
48 // calling next again should still return false
49 if rows.Next() {
50 t.Fatal("Query returned unexpected second row.")
51 }
52 if !rows.NextResultSet() {
53 t.Fatal("NextResultSet should return true but returned false")
54 }
55 if !rows.Next() {
56 t.Fatal("Query didn't return row")
57 }
58 err = rows.Scan(&fld2)
59 if err != nil {
60 t.Fatal("Scan failed", err)
61 }
62 if fld2 != 2 {
63 t.Fatal("Returned value doesn't match")
64 }
65 if rows.NextResultSet() {
66 t.Fatal("NextResultSet should return false but returned true")
67 }
68 }
69
70 func TestColumnTypeIntrospection(t *testing.T) {
71 type tst struct {
72 expr string
73 typeName string
74 reflType reflect.Type
75 hasSize bool
76 size int64
77 hasPrecScale bool
78 precision int64
79 scale int64
80 }
81 tests := []tst{
82 {"cast(1 as bit)", "BIT", reflect.TypeOf(true), false, 0, false, 0, 0},
83 {"cast(1 as tinyint)", "TINYINT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
84 {"cast(1 as smallint)", "SMALLINT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
85 {"1", "INT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
86 {"cast(1 as bigint)", "BIGINT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
87 {"cast(1 as real)", "REAL", reflect.TypeOf(0.0), false, 0, false, 0, 0},
88 {"cast(1 as float)", "FLOAT", reflect.TypeOf(0.0), false, 0, false, 0, 0},
89 {"cast('abc' as varbinary(3))", "VARBINARY", reflect.TypeOf([]byte{}), true, 3, false, 0, 0},
90 {"cast('abc' as varbinary(max))", "VARBINARY", reflect.TypeOf([]byte{}), true, 2147483645, false, 0, 0},
91 {"cast(1 as datetime)", "DATETIME", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
92 {"cast(1 as smalldatetime)", "SMALLDATETIME", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
93 {"cast(getdate() as datetime2(7))", "DATETIME2", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
94 {"cast(getdate() as datetimeoffset(7))", "DATETIMEOFFSET", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
95 {"cast(getdate() as date)", "DATE", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
96 {"cast(getdate() as time)", "TIME", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
97 {"'abc'", "VARCHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
98 {"cast('abc' as varchar(max))", "VARCHAR", reflect.TypeOf(""), true, 2147483645, false, 0, 0},
99 {"N'abc'", "NVARCHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
100 {"cast(N'abc' as NVARCHAR(MAX))", "NVARCHAR", reflect.TypeOf(""), true, 1073741822, false, 0, 0},
101 {"cast(1 as decimal)", "DECIMAL", reflect.TypeOf([]byte{}), false, 0, true, 18, 0},
102 {"cast(1 as decimal(5, 2))", "DECIMAL", reflect.TypeOf([]byte{}), false, 0, true, 5, 2},
103 {"cast(1 as numeric(10, 4))", "DECIMAL", reflect.TypeOf([]byte{}), false, 0, true, 10, 4},
104 {"cast(1 as money)", "MONEY", reflect.TypeOf([]byte{}), false, 0, false, 0, 0},
105 {"cast(1 as smallmoney)", "SMALLMONEY", reflect.TypeOf([]byte{}), false, 0, false, 0, 0},
106 {"cast(0x6F9619FF8B86D011B42D00C04FC964FF as uniqueidentifier)", "UNIQUEIDENTIFIER", reflect.TypeOf([]byte{}), false, 0, false, 0, 0},
107 {"cast('<root/>' as xml)", "XML", reflect.TypeOf(""), true, 1073741822, false, 0, 0},
108 {"cast('abc' as text)", "TEXT", reflect.TypeOf(""), true, 2147483647, false, 0, 0},
109 {"cast(N'abc' as ntext)", "NTEXT", reflect.TypeOf(""), true, 1073741823, false, 0, 0},
110 {"cast('abc' as image)", "IMAGE", reflect.TypeOf([]byte{}), true, 2147483647, false, 0, 0},
111 {"cast('abc' as char(3))", "CHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
112 {"cast(N'abc' as nchar(3))", "NCHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
113 {"cast(1 as sql_variant)", "SQL_VARIANT", reflect.TypeOf(nil), false, 0, false, 0, 0},
114 }
115 conn := open(t)
116 defer conn.Close()
117 for _, tt := range tests {
118 rows, err := conn.Query("select " + tt.expr)
119 if err != nil {
120 t.Errorf("Query failed with unexpected error %s", err)
121 }
122 ct, err := rows.ColumnTypes()
123 if err != nil {
124 t.Errorf("Query failed with unexpected error %s", err)
125 }
126 if ct[0].DatabaseTypeName() != tt.typeName {
127 t.Errorf("Expected type %s but returned %s", tt.typeName, ct[0].DatabaseTypeName())
128 }
129 size, ok := ct[0].Length()
130 if ok != tt.hasSize {
131 t.Errorf("Expected has size %v but returned %v for %s", tt.hasSize, ok, tt.expr)
132 } else {
133 if ok && size != tt.size {
134 t.Errorf("Expected size %d but returned %d for %s", tt.size, size, tt.expr)
135 }
136 }
137
138 prec, scale, ok := ct[0].DecimalSize()
139 if ok != tt.hasPrecScale {
140 t.Errorf("Expected has prec/scale %v but returned %v for %s", tt.hasPrecScale, ok, tt.expr)
141 } else {
142 if ok && prec != tt.precision {
143 t.Errorf("Expected precision %d but returned %d for %s", tt.precision, prec, tt.expr)
144 }
145 if ok && scale != tt.scale {
146 t.Errorf("Expected scale %d but returned %d for %s", tt.scale, scale, tt.expr)
147 }
148 }
149
150 if ct[0].ScanType() != tt.reflType {
151 t.Errorf("Expected ScanType %v but got %v for %s", tt.reflType, ct[0].ScanType(), tt.expr)
152 }
153 }
154 }
155
156 func TestColumnIntrospection(t *testing.T) {
157 type tst struct {
158 expr string
159 fieldName string
160 typeName string
161 nullable bool
162 hasSize bool
163 size int64
164 hasPrecScale bool
165 precision int64
166 scale int64
167 }
168 tests := []tst{
169 {"f1 int null", "f1", "INT", true, false, 0, false, 0, 0},
170 {"f2 varchar(15) not null", "f2", "VARCHAR", false, true, 15, false, 0, 0},
171 {"f3 decimal(5, 2) null", "f3", "DECIMAL", true, false, 0, true, 5, 2},
172 }
173 conn := open(t)
174 defer conn.Close()
175
176 // making table variable with specified fields and making a select from it
177 exprs := make([]string, len(tests))
178 for i, test := range tests {
179 exprs[i] = test.expr
180 }
181 exprJoined := strings.Join(exprs, ",")
182 rows, err := conn.Query(fmt.Sprintf("declare @tbl table(%s); select * from @tbl", exprJoined))
183 if err != nil {
184 t.Errorf("Query failed with unexpected error %s", err)
185 }
186
187 ct, err := rows.ColumnTypes()
188 if err != nil {
189 t.Errorf("ColumnTypes failed with unexpected error %s", err)
190 }
191 for i, test := range tests {
192 if ct[i].Name() != test.fieldName {
193 t.Errorf("Field expected have name %s but it has name %s", test.fieldName, ct[i].Name())
194 }
195
196 if ct[i].DatabaseTypeName() != test.typeName {
197 t.Errorf("Invalid type name returned %s expected %s", ct[i].DatabaseTypeName(), test.typeName)
198 }
199
200 nullable, ok := ct[i].Nullable()
201 if ok {
202 if nullable != test.nullable {
203 t.Errorf("Invalid nullable value returned %v", nullable)
204 }
205 } else {
206 t.Error("Nullable was expected to support Nullable but it didn't")
207 }
208
209 size, ok := ct[i].Length()
210 if ok != test.hasSize {
211 t.Errorf("Expected has size %v but returned %v for %s", test.hasSize, ok, test.expr)
212 } else {
213 if ok && size != test.size {
214 t.Errorf("Expected size %d but returned %d for %s", test.size, size, test.expr)
215 }
216 }
217
218 prec, scale, ok := ct[i].DecimalSize()
219 if ok != test.hasPrecScale {
220 t.Errorf("Expected has prec/scale %v but returned %v for %s", test.hasPrecScale, ok, test.expr)
221 } else {
222 if ok && prec != test.precision {
223 t.Errorf("Expected precision %d but returned %d for %s", test.precision, prec, test.expr)
224 }
225 if ok && scale != test.scale {
226 t.Errorf("Expected scale %d but returned %d for %s", test.scale, scale, test.expr)
227 }
228 }
229 }
230 }
231
232 func TestContext(t *testing.T) {
233 conn := open(t)
234 defer conn.Close()
235
236 opts := &sql.TxOptions{
237 Isolation: sql.LevelSerializable,
238 }
239 ctx := context.Background()
240 tx, err := conn.BeginTx(ctx, opts)
241 if err != nil {
242 t.Errorf("BeginTx failed with unexpected error %s", err)
243 return
244 }
245 rows, err := tx.QueryContext(ctx, "DBCC USEROPTIONS")
246 properties := make(map[string]string)
247 for rows.Next() {
248 var name, value string
249 if err = rows.Scan(&name, &value); err != nil {
250 t.Errorf("Scan failed with unexpected error %s", err)
251 }
252 properties[name] = value
253 }
254
255 if properties["isolation level"] != "serializable" {
256 t.Errorf("Expected isolation level to be serializable but it is %s", properties["isolation level"])
257 }
258
259 row := tx.QueryRowContext(ctx, "select 1")
260 var val int64
261 if err = row.Scan(&val); err != nil {
262 t.Errorf("QueryRowContext failed with unexpected error %s", err)
263 }
264 if val != 1 {
265 t.Error("Incorrect value returned from query")
266 }
267
268 _, err = tx.ExecContext(ctx, "select 1")
269 if err != nil {
270 t.Errorf("ExecContext failed with unexpected error %s", err)
271 return
272 }
273
274 _, err = tx.PrepareContext(ctx, "select 1")
275 if err != nil {
276 t.Errorf("PrepareContext failed with unexpected error %s", err)
277 return
278 }
279 }
280
281 func TestBeginTxtReadOnlyNotSupported(t *testing.T) {
282 conn := open(t)
283 defer conn.Close()
284 opts := &sql.TxOptions{ReadOnly: true}
285 _, err := conn.BeginTx(context.Background(), opts)
286 if err == nil {
287 t.Error("BeginTx expected to fail for read only transaction because MSSQL doesn't support it, but it succeeded")
288 }
289 }
290
291 func TestMssqlConn_BeginTx(t *testing.T) {
292 conn := open(t)
293 defer conn.Close()
294 _, err := conn.Exec("create table test (f int)")
295 defer conn.Exec("drop table test")
296 if err != nil {
297 t.Fatal("create table failed with error", err)
298 }
299
300 tx1, err := conn.BeginTx(context.Background(), nil)
301 if err != nil {
302 t.Fatal("BeginTx failed with error", err)
303 }
304 tx2, err := conn.BeginTx(context.Background(), nil)
305 if err != nil {
306 t.Fatal("BeginTx failed with error", err)
307 }
308 _, err = tx1.Exec("insert into test (f) values (1)")
309 if err != nil {
310 t.Fatal("insert failed with error", err)
311 }
312 _, err = tx2.Exec("insert into test (f) values (2)")
313 if err != nil {
314 t.Fatal("insert failed with error", err)
315 }
316 tx1.Rollback()
317 tx2.Commit()
318
319 rows, err := conn.Query("select f from test")
320 if err != nil {
321 t.Fatal("select failed with error", err)
322 }
323 values := []int64{}
324 for rows.Next() {
325 var val int64
326 err = rows.Scan(&val)
327 if err != nil {
328 t.Fatal("scan failed with error", err)
329 }
330 values = append(values, val)
331 }
332 if !reflect.DeepEqual(values, []int64{2}) {
333 t.Errorf("Values is expected to be [1] but it is %v", values)
334 }
335 }
336
337 func TestNamedParameters(t *testing.T) {
338 conn := open(t)
339 defer conn.Close()
340 row := conn.QueryRow(
341 "select :param2, :param1, :param2",
342 sql.Named("param1", 1),
343 sql.Named("param2", 2))
344 var col1, col2, col3 int64
345 err := row.Scan(&col1, &col2, &col3)
346 if err != nil {
347 t.Errorf("Scan failed with unexpected error %s", err)
348 return
349 }
350 if col1 != 2 || col2 != 1 || col3 != 2 {
351 t.Errorf("Unexpected values returned col1=%d, col2=%d, col3=%d", col1, col2, col3)
352 }
353 }
354
355 func TestBadNamedParameters(t *testing.T) {
356 conn := open(t)
357 defer conn.Close()
358 row := conn.QueryRow(
359 "select :param2, :param1, :param2",
360 sql.Named("badparam1", 1),
361 sql.Named("param2", 2))
362 var col1, col2, col3 int64
363 err := row.Scan(&col1, &col2, &col3)
364 if err == nil {
365 t.Error("Scan succeeded unexpectedly")
366 return
367 }
368 t.Logf("Scan failed as expected with error %s", err)
369 }
370
371 func TestMixedParameters(t *testing.T) {
372 conn := open(t)
373 defer conn.Close()
374 row := conn.QueryRow(
375 "select :2, :param1, :param2",
376 5, // this parameter will be unused
377 6,
378 sql.Named("param1", 1),
379 sql.Named("param2", 2))
380 var col1, col2, col3 int64
381 err := row.Scan(&col1, &col2, &col3)
382 if err != nil {
383 t.Errorf("Scan failed with unexpected error %s", err)
384 return
385 }
386 if col1 != 6 || col2 != 1 || col3 != 2 {
387 t.Errorf("Unexpected values returned col1=%d, col2=%d, col3=%d", col1, col2, col3)
388 }
389 }
390
391 /*
392 func TestMixedParametersExample(t *testing.T) {
393 conn := open(t)
394 defer conn.Close()
395 row := conn.QueryRow(
396 "select :id, ?",
397 sql.Named("id", 1),
398 2,
399 )
400 var col1, col2 int64
401 err := row.Scan(&col1, &col2)
402 if err != nil {
403 t.Errorf("Scan failed with unexpected error %s", err)
404 return
405 }
406 if col1 != 1 || col2 != 2 {
407 t.Errorf("Unexpected values returned col1=%d, col2=%d", col1, col2)
408 }
409 }
410 */
411
412 func TestPinger(t *testing.T) {
413 conn := open(t)
414 defer conn.Close()
415 err := conn.Ping()
416 if err != nil {
417 t.Errorf("Failed to hit database")
418 }
419 }
420
421 func TestQueryCancelLowLevel(t *testing.T) {
422 checkConnStr(t)
423 drv := driverWithProcess(t)
424 conn, err := drv.open(makeConnStr(t).String())
425 if err != nil {
426 t.Fatalf("Open failed with error %v", err)
427 }
428
429 defer conn.Close()
430 ctx, cancel := context.WithCancel(context.Background())
431 stmt, err := conn.prepareContext(ctx, "waitfor delay '00:00:03'")
432 if err != nil {
433 t.Fatalf("Prepare failed with error %v", err)
434 }
435 err = stmt.sendQuery([]namedValue{})
436 if err != nil {
437 t.Fatalf("sendQuery failed with error %v", err)
438 }
439
440 cancel()
441
442 _, err = stmt.processExec(ctx)
443 if err != context.Canceled {
444 t.Errorf("Expected error to be Cancelled but got %v", err)
445 }
446
447 // same connection should be usable again after it was cancelled
448 stmt, err = conn.prepareContext(context.Background(), "select 1")
449 if err != nil {
450 t.Fatalf("Prepare failed with error %v", err)
451 }
452 rows, err := stmt.Query([]driver.Value{})
453 if err != nil {
454 t.Fatalf("Query failed with error %v", err)
455 }
456
457 values := []driver.Value{nil}
458 err = rows.Next(values)
459 if err != nil {
460 t.Fatalf("Next failed with error %v", err)
461 }
462 }
463
464 func TestQueryCancelHighLevel(t *testing.T) {
465 conn := open(t)
466 defer conn.Close()
467
468 ctx, cancel := context.WithCancel(context.Background())
469 go func() {
470 time.Sleep(200 * time.Millisecond)
471 cancel()
472 }()
473 _, err := conn.ExecContext(ctx, "waitfor delay '00:00:03'")
474 if err != context.Canceled {
475 t.Errorf("ExecContext expected to fail with Cancelled but it returned %v", err)
476 }
477
478 // connection should be usable after timeout
479 row := conn.QueryRow("select 1")
480 var val int64
481 err = row.Scan(&val)
482 if err != nil {
483 t.Fatal("Scan failed with", err)
484 }
485 }
486
487 func TestQueryTimeout(t *testing.T) {
488 conn := open(t)
489 defer conn.Close()
490 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
491 defer cancel()
492 _, err := conn.ExecContext(ctx, "waitfor delay '00:00:03'")
493 if err != context.DeadlineExceeded {
494 t.Errorf("ExecContext expected to fail with DeadlineExceeded but it returned %v", err)
495 }
496
497 // connection should be usable after timeout
498 row := conn.QueryRow("select 1")
499 var val int64
500 err = row.Scan(&val)
501 if err != nil {
502 t.Fatal("Scan failed with", err)
503 }
504 }
505
506 func TestDriverParams(t *testing.T) {
507 checkConnStr(t)
508 SetLogger(testLogger{t})
509 type sqlCmd struct {
510 Name string
511 Driver string
512 Query string
513 Param []interface{}
514 Expect []interface{}
515 }
516
517 list := []sqlCmd{
518 {
519 Name: "preprocess-ordinal",
520 Driver: "mssql",
521 Query: `select V1=:1`,
522 Param: []interface{}{"abc"},
523 Expect: []interface{}{"abc"},
524 },
525 {
526 Name: "preprocess-name",
527 Driver: "mssql",
528 Query: `select V1=:First`,
529 Param: []interface{}{sql.Named("First", "abc")},
530 Expect: []interface{}{"abc"},
531 },
532 {
533 Name: "raw-ordinal",
534 Driver: "sqlserver",
535 Query: `select V1=@p1`,
536 Param: []interface{}{"abc"},
537 Expect: []interface{}{"abc"},
538 },
539 {
540 Name: "raw-name",
541 Driver: "sqlserver",
542 Query: `select V1=@First`,
543 Param: []interface{}{sql.Named("First", "abc")},
544 Expect: []interface{}{"abc"},
545 },
546 }
547
548 for cmdIndex, cmd := range list {
549 t.Run(cmd.Name, func(t *testing.T) {
550 db, err := sql.Open(cmd.Driver, makeConnStr(t).String())
551 if err != nil {
552 t.Fatalf("failed to open driver %q", cmd.Driver)
553 }
554 defer db.Close()
555
556 rows, err := db.Query(cmd.Query, cmd.Param...)
557 if err != nil {
558 t.Fatalf("failed to run query %q %v", cmd.Query, err)
559 }
560 defer rows.Close()
561
562 columns, err := rows.Columns()
563 if err != nil {
564 t.Fatal("failed to get column schema %v", err)
565 }
566 clen := len(columns)
567
568 if clen != len(cmd.Expect) {
569 t.Fatal("query column has %d, expect %d columns", clen, len(cmd.Expect))
570 }
571
572 values := make([]interface{}, clen)
573 into := make([]interface{}, clen)
574 for i := 0; i < clen; i++ {
575 into[i] = &values[i]
576 }
577 for rows.Next() {
578 err = rows.Scan(into...)
579 if err != nil {
580 t.Fatalf("failed to scan into row for %d %q", cmdIndex, cmd.Driver)
581 }
582 for i := range cmd.Expect {
583 if values[i] != cmd.Expect[i] {
584 t.Fatal("expected value in index %d %v != actual value %v", i, cmd.Expect[i], values[i])
585 }
586 }
587 }
588 })
589 }
590 }
591
592 type connInterrupt struct {
593 net.Conn
594
595 mu sync.Mutex
596 disruptRead bool
597 disruptWrite bool
598 }
599
600 func (c *connInterrupt) Interrupt(write bool) {
601 c.mu.Lock()
602 if write {
603 c.disruptWrite = true
604 } else {
605 c.disruptRead = true
606 }
607 c.mu.Unlock()
608 }
609
610 func (c *connInterrupt) Read(b []byte) (n int, err error) {
611 c.mu.Lock()
612 dis := c.disruptRead
613 c.mu.Unlock()
614 if dis {
615 return 0, disconnectError{}
616 }
617 return c.Conn.Read(b)
618 }
619
620 func (c *connInterrupt) Write(b []byte) (n int, err error) {
621 c.mu.Lock()
622 dis := c.disruptWrite
623 c.mu.Unlock()
624 if dis {
625 return 0, disconnectError{}
626 }
627 return c.Conn.Write(b)
628 }
629
630 type dialerInterrupt struct {
631 nd tcpDialer
632
633 mu sync.Mutex
634 list []*connInterrupt
635 }
636
637 func (d *dialerInterrupt) Dial(addr string) (net.Conn, error) {
638 conn, err := d.nd.Dial(addr)
639 if err != nil {
640 return nil, err
641 }
642 ci := &connInterrupt{Conn: conn}
643 d.mu.Lock()
644 d.list = append(d.list, ci)
645 d.mu.Unlock()
646 return ci, err
647 }
648
649 func (d *dialerInterrupt) Interrupt(write bool) {
650 d.mu.Lock()
651 defer d.mu.Unlock()
652
653 for _, ci := range d.list {
654 ci.Interrupt(write)
655 }
656 }
657
658 var _ net.Error = disconnectError{}
659
660 type disconnectError struct{}
661
662 func (disconnectError) Error() string {
663 return "disconnect"
664 }
665
666 func (disconnectError) Timeout() bool {
667 return true
668 }
669
670 func (disconnectError) Temporary() bool {
671 return true
672 }
673
674 // TestDisconnect1 ensures errors and states are handled correctly if
675 // the server is disconnected mid-query.
676 func TestDisconnect1(t *testing.T) {
677 if testing.Short() {
678 t.Skip("short")
679 }
680 checkConnStr(t)
681 SetLogger(testLogger{t})
682
683 // Revert to the normal dialer after the test is done.
684 normalCreateDialer := createDialer
685 defer func() {
686 createDialer = normalCreateDialer
687 }()
688
689 waitDisrupt := make(chan struct{})
690 ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
691 defer cancel()
692
693 createDialer = func(p *connectParams) dialer {
694 nd := tcpDialer{&net.Dialer{Timeout: p.dial_timeout, KeepAlive: p.keepAlive}}
695 di := &dialerInterrupt{nd: nd}
696 go func() {
697 <-waitDisrupt
698 di.Interrupt(true)
699 di.Interrupt(false)
700 }()
701 return di
702 }
703 db, err := sql.Open("sqlserver", makeConnStr(t).String())
704 if err != nil {
705 t.Fatal(err)
706 }
707
708 if err := db.PingContext(ctx); err != nil {
709 t.Fatal(err)
710 }
711 defer db.Close()
712
713 _, err = db.ExecContext(ctx, `SET LOCK_TIMEOUT 1800;`)
714 if err != nil {
715 t.Fatal(err)
716 }
717
718 go func() {
719 time.Sleep(time.Second * 1)
720 close(waitDisrupt)
721 }()
722 t.Log("prepare for query")
723 _, err = db.ExecContext(ctx, `waitfor delay '00:00:3';`)
724 if err != nil {
725 t.Log("expected error after disconnect", err)
726 return
727 }
728 t.Fatal("wanted error after Exec")
729 }
730
731 // TestDisconnect2 tests a read error so the query is started
732 // but results cannot be read.
733 func TestDisconnect2(t *testing.T) {
734 if testing.Short() {
735 t.Skip("short")
736 }
737 checkConnStr(t)
738 SetLogger(testLogger{t})
739
740 // Revert to the normal dialer after the test is done.
741 normalCreateDialer := createDialer
742 defer func() {
743 createDialer = normalCreateDialer
744 }()
745
746 end := make(chan error)
747
748 ctx, cancel := context.WithCancel(context.Background())
749 defer cancel()
750
751 go func() {
752 waitDisrupt := make(chan struct{})
753 ctx, cancel = context.WithTimeout(ctx, time.Second*2)
754 defer cancel()
755
756 createDialer = func(p *connectParams) dialer {
757 nd := tcpDialer{&net.Dialer{Timeout: p.dial_timeout, KeepAlive: p.keepAlive}}
758 di := &dialerInterrupt{nd: nd}
759 go func() {
760 <-waitDisrupt
761 di.Interrupt(false)
762 }()
763 return di
764 }
765 db, err := sql.Open("sqlserver", makeConnStr(t).String())
766 if err != nil {
767 t.Fatal(err)
768 }
769
770 if err := db.PingContext(ctx); err != nil {
771 t.Fatal(err)
772 }
773 defer db.Close()
774
775 _, err = db.ExecContext(ctx, `SET LOCK_TIMEOUT 1800;`)
776 if err != nil {
777 t.Fatal(err)
778 }
779 close(waitDisrupt)
780
781 _, err = db.ExecContext(ctx, `waitfor delay '00:00:3';`)
782 end <- err
783 }()
784
785 timeout := time.After(10 * time.Second)
786 select {
787 case err := <-end:
788 if err == nil {
789 t.Fatal("test err")
790 }
791 case <-timeout:
792 t.Fatal("timeout")
793 }
794 }
795
796 // TestAzureDatabase tests reading from an encrypted azure database connection
797 // from a SELECT read.
798 func TestAzureDatabase(t *testing.T) {
799 t.Skip("currently failing")
800
801 query := `
802 ;with
803 config_cte (config) as (
804 select *
805 from ( values
806 ('_partition:{\"Fill\":{\"PatternType\":\"solid\",\"FgColor\":\"99ff99\"}}')
807 , ('_separation:{\"Fill\":{\"PatternType\":\"solid\",\"FgColor\":\"99ffff\"}}')
808 , ('Monthly Earnings:\$#,##0.00 ;(\$#,##0.00)')
809 , ('Weekly Earnings:\$#,##0.00 ;(\$#,##0.00)')
810 , ('Total Earnings:\$#,##0.00 ;(\$#,##0.00)')
811 , ('Average Earnings:\$#,##0.00 ;(\$#,##0.00)')
812 , ('Last Month Earning:#,##0.00 ;(#,##0.00)')
813 , ('Award:\$#,##0.00 ;(\$#,##0.00)')
814 , ('Amount:\$#,##0.00 ;(\$#,##0.00)')
815 , ('Grand Total:\$#,##0.00 ;(\$#,##0.00)')
816 , ('Total:\$#,##0.00 ;(\$#,##0.00)')
817 , ('Price Each:\$#,##0.00 ;(\$#,##0.00)')
818 , ('Hyperwallet:\$#,##0.00 ;(\$#,##0.00)')
819 , ('Credit/Debit:\$#,##0.00 ;(\$#,##0.00)')
820 , ('Earning:#,##0.00 ;(#,##0.00)')
821 , ('Change Earning:#,##0.00 ;(#,##0.00)')
822 , ('CheckAmount:#,##0.00 ;(#,##0.00)')
823 , ('Residual:#,##0.00 ;(#,##0.00)')
824 , ('Prev Residual:#,##0.00 ;(#,##0.00)')
825 , ('Team Bonuses:#,##0.00 ;(#,##0.00)')
826 , ('Change:#,##0.00 ;(#,##0.00)')
827 , ('Shipping Total:#,##0.00 ;(#,##0.00)')
828 , ('SubTotal:\$#,##0.00 ;(\$#,##0.00)')
829 , ('Total Diff:#,##0.00 ;(#,##0.00)')
830 , ('SubTotal Diff:#,##0.00 ;(#,##0.00)')
831 , ('Return Total:#,##0.00 ;(#,##0.00)')
832 , ('Return SubTotal:#,##0.00 ;(#,##0.00)')
833 , ('Return Total Diff:#,##0.00 ;(#,##0.00)')
834 , ('Return SubTotal Diff:#,##0.00 ;(#,##0.00)')
835 , ('Cancel Total:#,##0.00 ;(#,##0.00)')
836 , ('Cancel SubTotal:#,##0.00 ;(#,##0.00)')
837 , ('Cancel Total Diff:#,##0.00 ;(#,##0.00)')
838 , ('Cancel SubTotal Diff:#,##0.00 ;(#,##0.00)')
839 , ('Replacement Total:#,##0.00 ;(#,##0.00)')
840 , ('Replacement SubTotal:#,##0.00 ;(#,##0.00)')
841 , ('Replacement Total Diff:#,##0.00 ;(#,##0.00)')
842 , ('Replacement SubTotal Diff:#,##0.00 ;(#,##0.00)')
843 , ('Jan Residual:#,##0.00 ;(#,##0.00)')
844 , ('Jan Bonus:#,##0.00 ;(#,##0.00)')
845 , ('Jan Total:#,##0.00 ;(#,##0.00)')
846 , ('January Residual:#,##0.00 ;(#,##0.00)')
847 , ('Feb Residual:#,##0.00 ;(#,##0.00)')
848 , ('Feb Bonus:#,##0.00 ;(#,##0.00)')
849 , ('Feb Total:#,##0.00 ;(#,##0.00)')
850 , ('February Residual:#,##0.00 ;(#,##0.00)')
851 , ('Mar Residual:#,##0.00 ;(#,##0.00)')
852 , ('Mar Bonus:#,##0.00 ;(#,##0.00)')
853 , ('Mar Total:#,##0.00 ;(#,##0.00)')
854 , ('March Residual:#,##0.00 ;(#,##0.00)')
855 , ('Apr Residual:#,##0.00 ;(#,##0.00)')
856 , ('Apr Bonus:#,##0.00 ;(#,##0.00)')
857 , ('Apr Total:#,##0.00 ;(#,##0.00)')
858 , ('April Residual:#,##0.00 ;(#,##0.00)')
859 , ('May Residual:#,##0.00 ;(#,##0.00)')
860 , ('May Bonus:#,##0.00 ;(#,##0.00)')
861 , ('May Total:#,##0.00 ;(#,##0.00)')
862 , ('Jun Residual:#,##0.00 ;(#,##0.00)')
863 , ('Jun Bonus:#,##0.00 ;(#,##0.00)')
864 , ('Jun Total:#,##0.00 ;(#,##0.00)')
865 , ('June Residual:#,##0.00 ;(#,##0.00)')
866 , ('Jul Residual:#,##0.00 ;(#,##0.00)')
867 , ('Jul Bonus:#,##0.00 ;(#,##0.00)')
868 , ('Jul Total:#,##0.00 ;(#,##0.00)')
869 , ('July Residual:#,##0.00 ;(#,##0.00)')
870 , ('Aug Residual:#,##0.00 ;(#,##0.00)')
871 , ('Aug Bonus:#,##0.00 ;(#,##0.00)')
872 , ('Aug Total:#,##0.00 ;(#,##0.00)')
873 , ('August Residual:#,##0.00 ;(#,##0.00)')
874 , ('Sep Residual:#,##0.00 ;(#,##0.00)')
875 , ('Sep Bonus:#,##0.00 ;(#,##0.00)')
876 , ('Sep Total:#,##0.00 ;(#,##0.00)')
877 , ('September Residual:#,##0.00 ;(#,##0.00)')
878 , ('Oct Residual:#,##0.00 ;(#,##0.00)')
879 , ('Oct Bonus:#,##0.00 ;(#,##0.00)')
880 , ('Oct Total:#,##0.00 ;(#,##0.00)')
881 , ('October Residual:#,##0.00 ;(#,##0.00)')
882 , ('Nov Residual:#,##0.00 ;(#,##0.00)')
883 , ('Nov Bonus:#,##0.00 ;(#,##0.00)')
884 , ('Nov Total:#,##0.00 ;(#,##0.00)')
885 , ('November Residual:#,##0.00 ;(#,##0.00)')
886 , ('Dec Residual:#,##0.00 ;(#,##0.00)')
887 , ('Dec Bonus:#,##0.00 ;(#,##0.00)')
888 , ('Dec Total:#,##0.00 ;(#,##0.00)')
889 , ('December Residual:#,##0.00 ;(#,##0.00)')
890 , ('January Bonus:#,##0.00 ;(#,##0.00)')
891 , ('February Bonus:#,##0.00 ;(#,##0.00)')
892 , ('March Bonus:#,##0.00 ;(#,##0.00)')
893 , ('April Bonus:#,##0.00 ;(#,##0.00)')
894 , ('May Bonus:#,##0.00 ;(#,##0.00)')
895 , ('June Bonus:#,##0.00 ;(#,##0.00)')
896 , ('July Bonus:#,##0.00 ;(#,##0.00)')
897 , ('August Bonus:#,##0.00 ;(#,##0.00)')
898 , ('September Bonus:#,##0.00 ;(#,##0.00)')
899 , ('October Bonus:#,##0.00 ;(#,##0.00)')
900 , ('November Bonus:#,##0.00 ;(#,##0.00)')
901 , ('December Bonus:#,##0.00 ;(#,##0.00)')
902 , ('January Adj:#,##0.00 ;(#,##0.00)')
903 , ('February Adj:#,##0.00 ;(#,##0.00)')
904 , ('March Adj:#,##0.00 ;(#,##0.00)')
905 , ('April Adj:#,##0.00 ;(#,##0.00)')
906 , ('May Adj:#,##0.00 ;(#,##0.00)')
907 , ('June Adj:#,##0.00 ;(#,##0.00)')
908 , ('July Adj:#,##0.00 ;(#,##0.00)')
909 , ('August Adj:#,##0.00 ;(#,##0.00)')
910 , ('September Adj:#,##0.00 ;(#,##0.00)')
911 , ('October Adj:#,##0.00 ;(#,##0.00)')
912 , ('November Adj:#,##0.00 ;(#,##0.00)')
913 , ('December Adj:#,##0.00 ;(#,##0.00)')
914 , ('2016- 2015 YTD Dif:#,##0.00 ;(#,##0.00)')
915 , ('2017- 2016 YTD Dif:#,##0.00 ;(#,##0.00)')
916 , ('2018- 2017 YTD Dif:#,##0.00 ;(#,##0.00)')
917 , ('Dec to Jan Dif Residual:#,##0.00 ;(#,##0.00)')
918 , ('Jan to Feb Dif Residual:#,##0.00 ;(#,##0.00)')
919 , ('Feb to Mar Dif Residual:#,##0.00 ;(#,##0.00)')
920 , ('Mar to Apr Dif Residual:#,##0.00 ;(#,##0.00)')
921 , ('Apr to May Dif Residual:#,##0.00 ;(#,##0.00)')
922 , ('May to Jun Dif Residual:#,##0.00 ;(#,##0.00)')
923 , ('Jun to Jul Dif Residual:#,##0.00 ;(#,##0.00)')
924 , ('Jul to Aug Dif Residual:#,##0.00 ;(#,##0.00)')
925 , ('Aug to Sep Dif Residual:#,##0.00 ;(#,##0.00)')
926 , ('Sep to Oct Dif Residual:#,##0.00 ;(#,##0.00)')
927 , ('Oct to Nov Dif Residual:#,##0.00 ;(#,##0.00)')
928 , ('Nov to Dec Dif Residual:#,##0.00 ;(#,##0.00)')
929 , ('Dec to Jan Dif Bonus:#,##0.00 ;(#,##0.00)')
930 , ('Jan to Feb Dif Bonus:#,##0.00 ;(#,##0.00)')
931 , ('Feb to Mar Dif Bonus:#,##0.00 ;(#,##0.00)')
932 , ('Mar to Apr Dif Bonus:#,##0.00 ;(#,##0.00)')
933 , ('Apr to May Dif Bonus:#,##0.00 ;(#,##0.00)')
934 , ('May to Jun Dif Bonus:#,##0.00 ;(#,##0.00)')
935 , ('Jun to Jul Dif Bonus:#,##0.00 ;(#,##0.00)')
936 , ('Jul to Aug Dif Bonus:#,##0.00 ;(#,##0.00)')
937 , ('Aug to Sep Dif Bonus:#,##0.00 ;(#,##0.00)')
938 , ('Sep to Oct Dif Bonus:#,##0.00 ;(#,##0.00)')
939 , ('Oct to Nov Dif Bonus:#,##0.00 ;(#,##0.00)')
940 , ('Nov to Dec Dif Bonus:#,##0.00 ;(#,##0.00)')
941 , ('Dec to Jan Dif Total:#,##0.00 ;(#,##0.00)')
942 , ('Jan to Feb Dif Total:#,##0.00 ;(#,##0.00)')
943 , ('Feb to Mar Dif Total:#,##0.00 ;(#,##0.00)')
944 , ('Mar to Apr Dif Total:#,##0.00 ;(#,##0.00)')
945 , ('Apr to May Dif Total:#,##0.00 ;(#,##0.00)')
946 , ('May to Jun Dif Total:#,##0.00 ;(#,##0.00)')
947 , ('Jun to Jul Dif Total:#,##0.00 ;(#,##0.00)')
948 , ('Jul to Aug Dif Total:#,##0.00 ;(#,##0.00)')
949 , ('Aug to Sep Dif Total:#,##0.00 ;(#,##0.00)')
950 , ('Sep to Oct Dif Total:#,##0.00 ;(#,##0.00)')
951 , ('Oct to Nov Dif Total:#,##0.00 ;(#,##0.00)')
952 , ('Nov to Dec Dif Total:#,##0.00 ;(#,##0.00)')
953 , ('Jan Refund Cnt:#,##0 ;(#,##0)')
954 , ('Feb Refund Cnt:#,##0 ;(#,##0)')
955 , ('Mar Refund Cnt:#,##0 ;(#,##0)')
956 , ('Apr Refund Cnt:#,##0 ;(#,##0)')
957 , ('May Refund Cnt:#,##0 ;(#,##0)')
958 , ('Jun Refund Cnt:#,##0 ;(#,##0)')
959 , ('Jul Refund Cnt:#,##0 ;(#,##0)')
960 , ('Aug Refund Cnt:#,##0 ;(#,##0)')
961 , ('Sep Refund Cnt:#,##0 ;(#,##0)')
962 , ('Oct Refund Cnt:#,##0 ;(#,##0)')
963 , ('Nov Refund Cnt:#,##0 ;(#,##0)')
964 , ('Dec Refund Cnt:#,##0 ;(#,##0)')
965 , ('Jan Purchase Cnt:#,##0 ;(#,##0)')
966 , ('Feb Purchase Cnt:#,##0 ;(#,##0)')
967 , ('Mar Purchase Cnt:#,##0 ;(#,##0)')
968 , ('Apr Purchase Cnt:#,##0 ;(#,##0)')
969 , ('May Purchase Cnt:#,##0 ;(#,##0)')
970 , ('Jun Purchase Cnt:#,##0 ;(#,##0)')
971 , ('Jul Purchase Cnt:#,##0 ;(#,##0)')
972 , ('Aug Purchase Cnt:#,##0 ;(#,##0)')
973 , ('Sep Purchase Cnt:#,##0 ;(#,##0)')
974 , ('Oct Purchase Cnt:#,##0 ;(#,##0)')
975 , ('Nov Purchase Cnt:#,##0 ;(#,##0)')
976 , ('Dec Purchase Cnt:#,##0 ;(#,##0)')
977 , ('Jan Refund Amt:#,##0.00 ;(#,##0.00)')
978 , ('Feb Refund Amt:#,##0.00 ;(#,##0.00)')
979 , ('Mar Refund Amt:#,##0.00 ;(#,##0.00)')
980 , ('Apr Refund Amt:#,##0.00 ;(#,##0.00)')
981 , ('May Refund Amt:#,##0.00 ;(#,##0.00)')
982 , ('Jun Refund Amt:#,##0.00 ;(#,##0.00)')
983 , ('Jul Refund Amt:#,##0.00 ;(#,##0.00)')
984 , ('Aug Refund Amt:#,##0.00 ;(#,##0.00)')
985 , ('Sep Refund Amt:#,##0.00 ;(#,##0.00)')
986 , ('Oct Refund Amt:#,##0.00 ;(#,##0.00)')
987 , ('Nov Refund Amt:#,##0.00 ;(#,##0.00)')
988 , ('Dec Refund Amt:#,##0.00 ;(#,##0.00)')
989 , ('Jan Purchase Amt:#,##0.00 ;(#,##0.00)')
990 , ('Feb Purchase Amt:#,##0.00 ;(#,##0.00)')
991 , ('Mar Purchase Amt:#,##0.00 ;(#,##0.00)')
992 , ('Apr Purchase Amt:#,##0.00 ;(#,##0.00)')
993 , ('May Purchase Amt:#,##0.00 ;(#,##0.00)')
994 , ('Jun Purchase Amt:#,##0.00 ;(#,##0.00)')
995 , ('Jul Purchase Amt:#,##0.00 ;(#,##0.00)')
996 , ('Aug Purchase Amt:#,##0.00 ;(#,##0.00)')
997 , ('Sep Purchase Amt:#,##0.00 ;(#,##0.00)')
998 , ('Oct Purchase Amt:#,##0.00 ;(#,##0.00)')
999 , ('Nov Purchase Amt:#,##0.00 ;(#,##0.00)')
1000 , ('Dec Purchase Amt:#,##0.00 ;(#,##0.00)')
1001 ) X(a))
1002 select * from config_cte
1003 `
1004
1005 db := open(t)
1006 defer db.Close()
1007
1008 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1009 defer cancel()
1010
1011 rows, err := db.QueryContext(ctx, query)
1012 if err != nil {
1013 t.Fatal(err)
1014 }
1015 defer rows.Close()
1016 var s string
1017 for rows.Next() {
1018 err = rows.Scan(&s)
1019 if err != nil {
1020 t.Fatal(err)
1021 }
1022 }
1023 }