forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries_go18_test.go
414 lines (379 loc) · 12.4 KB
/
queries_go18_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
// +build go1.8
package mssql
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strings"
"testing"
"time"
)
func TestNextResultSet(t *testing.T) {
conn := open(t)
defer conn.Close()
rows, err := conn.Query("select 1; select 2")
if err != nil {
t.Fatal("Query failed", err.Error())
}
defer func() {
err := rows.Err()
if err != nil {
t.Error("unexpected error:", err)
}
}()
defer rows.Close()
if !rows.Next() {
t.Fatal("Query didn't return row")
}
var fld1, fld2 int32
err = rows.Scan(&fld1)
if err != nil {
t.Fatal("Scan failed", err)
}
if fld1 != 1 {
t.Fatal("Returned value doesn't match")
}
if rows.Next() {
t.Fatal("Query returned unexpected second row.")
}
if !rows.NextResultSet() {
t.Fatal("NextResultSet should return true but returned false")
}
if !rows.Next() {
t.Fatal("Query didn't return row")
}
err = rows.Scan(&fld2)
if err != nil {
t.Fatal("Scan failed", err)
}
if fld2 != 2 {
t.Fatal("Returned value doesn't match")
}
}
func TestColumnTypeIntrospection(t *testing.T) {
type tst struct {
expr string
typeName string
reflType reflect.Type
hasSize bool
size int64
hasPrecScale bool
precision int64
scale int64
}
tests := []tst{
{"cast(1 as bit)", "BIT", reflect.TypeOf(true), false, 0, false, 0, 0},
{"cast(1 as tinyint)", "TINYINT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
{"cast(1 as smallint)", "SMALLINT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
{"1", "INT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
{"cast(1 as bigint)", "BIGINT", reflect.TypeOf(int64(0)), false, 0, false, 0, 0},
{"cast(1 as real)", "REAL", reflect.TypeOf(0.0), false, 0, false, 0, 0},
{"cast(1 as float)", "FLOAT", reflect.TypeOf(0.0), false, 0, false, 0, 0},
{"cast('abc' as varbinary(3))", "VARBINARY", reflect.TypeOf([]byte{}), true, 3, false, 0, 0},
{"cast('abc' as varbinary(max))", "VARBINARY", reflect.TypeOf([]byte{}), true, 2147483645, false, 0, 0},
{"cast(1 as datetime)", "DATETIME", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
{"cast(1 as smalldatetime)", "SMALLDATETIME", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
{"cast(getdate() as datetime2(7))", "DATETIME2", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
{"cast(getdate() as datetimeoffset(7))", "DATETIMEOFFSET", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
{"cast(getdate() as date)", "DATE", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
{"cast(getdate() as time)", "TIME", reflect.TypeOf(time.Time{}), false, 0, false, 0, 0},
{"'abc'", "VARCHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
{"cast('abc' as varchar(max))", "VARCHAR", reflect.TypeOf(""), true, 2147483645, false, 0, 0},
{"N'abc'", "NVARCHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
{"cast(N'abc' as NVARCHAR(MAX))", "NVARCHAR", reflect.TypeOf(""), true, 1073741822, false, 0, 0},
{"cast(1 as decimal)", "DECIMAL", reflect.TypeOf([]byte{}), false, 0, true, 18, 0},
{"cast(1 as decimal(5, 2))", "DECIMAL", reflect.TypeOf([]byte{}), false, 0, true, 5, 2},
{"cast(1 as numeric(10, 4))", "DECIMAL", reflect.TypeOf([]byte{}), false, 0, true, 10, 4},
{"cast(1 as money)", "MONEY", reflect.TypeOf([]byte{}), false, 0, false, 0, 0},
{"cast(1 as smallmoney)", "SMALLMONEY", reflect.TypeOf([]byte{}), false, 0, false, 0, 0},
{"cast(0x6F9619FF8B86D011B42D00C04FC964FF as uniqueidentifier)", "UNIQUEIDENTIFIER", reflect.TypeOf([]byte{}), false, 0, false, 0, 0},
{"cast('<root/>' as xml)", "XML", reflect.TypeOf(""), true, 1073741822, false, 0, 0},
{"cast('abc' as text)", "TEXT", reflect.TypeOf(""), true, 2147483647, false, 0, 0},
{"cast(N'abc' as ntext)", "NTEXT", reflect.TypeOf(""), true, 1073741823, false, 0, 0},
{"cast('abc' as image)", "IMAGE", reflect.TypeOf([]byte{}), true, 2147483647, false, 0, 0},
{"cast('abc' as char(3))", "CHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
{"cast(N'abc' as nchar(3))", "NCHAR", reflect.TypeOf(""), true, 3, false, 0, 0},
{"cast(1 as sql_variant)", "SQL_VARIANT", reflect.TypeOf(nil), false, 0, false, 0, 0},
}
conn := open(t)
defer conn.Close()
for _, tt := range tests {
rows, err := conn.Query("select " + tt.expr)
if err != nil {
t.Errorf("Query failed with unexpected error %s", err)
}
ct, err := rows.ColumnTypes()
if err != nil {
t.Errorf("Query failed with unexpected error %s", err)
}
if ct[0].DatabaseTypeName() != tt.typeName {
t.Errorf("Expected type %s but returned %s", tt.typeName, ct[0].DatabaseTypeName())
}
size, ok := ct[0].Length()
if ok != tt.hasSize {
t.Errorf("Expected has size %v but returned %v for %s", tt.hasSize, ok, tt.expr)
} else {
if ok && size != tt.size {
t.Errorf("Expected size %d but returned %d for %s", tt.size, size, tt.expr)
}
}
prec, scale, ok := ct[0].DecimalSize()
if ok != tt.hasPrecScale {
t.Errorf("Expected has prec/scale %v but returned %v for %s", tt.hasPrecScale, ok, tt.expr)
} else {
if ok && prec != tt.precision {
t.Errorf("Expected precision %d but returned %d for %s", tt.precision, prec, tt.expr)
}
if ok && scale != tt.scale {
t.Errorf("Expected scale %d but returned %d for %s", tt.scale, scale, tt.expr)
}
}
if ct[0].ScanType() != tt.reflType {
t.Errorf("Expected ScanType %v but got %v for %s", tt.reflType, ct[0].ScanType(), tt.expr)
}
}
}
func TestColumnIntrospection(t *testing.T) {
type tst struct {
expr string
fieldName string
typeName string
nullable bool
hasSize bool
size int64
hasPrecScale bool
precision int64
scale int64
}
tests := []tst{
{"f1 int null", "f1", "INT", true, false, 0, false, 0, 0},
{"f2 varchar(15) not null", "f2", "VARCHAR", false, true, 15, false, 0, 0},
{"f3 decimal(5, 2) null", "f3", "DECIMAL", true, false, 0, true, 5, 2},
}
conn := open(t)
defer conn.Close()
// making table variable with specified fields and making a select from it
exprs := make([]string, len(tests))
for i, test := range tests {
exprs[i] = test.expr
}
exprJoined := strings.Join(exprs, ",")
rows, err := conn.Query(fmt.Sprintf("declare @tbl table(%s); select * from @tbl", exprJoined))
if err != nil {
t.Errorf("Query failed with unexpected error %s", err)
}
ct, err := rows.ColumnTypes()
if err != nil {
t.Errorf("ColumnTypes failed with unexpected error %s", err)
}
for i, test := range tests {
if ct[i].Name() != test.fieldName {
t.Errorf("Field expected have name %s but it has name %s", test.fieldName, ct[i].Name())
}
if ct[i].DatabaseTypeName() != test.typeName {
t.Errorf("Invalid type name returned %s expected %s", ct[i].DatabaseTypeName(), test.typeName)
}
nullable, ok := ct[i].Nullable()
if ok {
if nullable != test.nullable {
t.Errorf("Invalid nullable value returned %v", nullable)
}
} else {
t.Error("Nullable was expected to support Nullable but it didn't")
}
size, ok := ct[i].Length()
if ok != test.hasSize {
t.Errorf("Expected has size %v but returned %v for %s", test.hasSize, ok, test.expr)
} else {
if ok && size != test.size {
t.Errorf("Expected size %d but returned %d for %s", test.size, size, test.expr)
}
}
prec, scale, ok := ct[i].DecimalSize()
if ok != test.hasPrecScale {
t.Errorf("Expected has prec/scale %v but returned %v for %s", test.hasPrecScale, ok, test.expr)
} else {
if ok && prec != test.precision {
t.Errorf("Expected precision %d but returned %d for %s", test.precision, prec, test.expr)
}
if ok && scale != test.scale {
t.Errorf("Expected scale %d but returned %d for %s", test.scale, scale, test.expr)
}
}
}
}
func TestContext(t *testing.T) {
conn := open(t)
defer conn.Close()
ctx := context.Background()
ctx = sql.IsolationContext(ctx, sql.LevelSerializable)
tx, err := conn.BeginContext(ctx)
if err != nil {
t.Errorf("BeginContext failed with unexpected error %s", err)
return
}
rows, err := tx.QueryContext(ctx, "DBCC USEROPTIONS")
properties := make(map[string]string)
for rows.Next() {
var name, value string
if err = rows.Scan(&name, &value); err != nil {
t.Errorf("Scan failed with unexpected error %s", err)
}
properties[name] = value
}
if properties["isolation level"] != "serializable" {
t.Errorf("Expected isolation level to be serializable but it is %s", properties["isolation level"])
}
row := tx.QueryRowContext(ctx, "select 1")
var val int64
if err = row.Scan(&val); err != nil {
t.Errorf("QueryRowContext failed with unexpected error %s", err)
}
if val != 1 {
t.Error("Incorrect value returned from query")
}
_, err = tx.ExecContext(ctx, "select 1")
if err != nil {
t.Errorf("ExecContext failed with unexpected error %s", err)
return
}
_, err = tx.PrepareContext(ctx, "select 1")
if err != nil {
t.Errorf("PrepareContext failed with unexpected error %s", err)
return
}
}
func TestNamedParameters(t *testing.T) {
conn := open(t)
defer conn.Close()
row := conn.QueryRow(
"select :param2, :param1, :param2",
sql.Named("param1", 1),
sql.Named("param2", 2))
var col1, col2, col3 int64
err := row.Scan(&col1, &col2, &col3)
if err != nil {
t.Errorf("Scan failed with unexpected error %s", err)
return
}
if col1 != 2 || col2 != 1 || col3 != 2 {
t.Errorf("Unexpected values returned col1=%d, col2=%d, col3=%d", col1, col2, col3)
}
}
func TestBadNamedParameters(t *testing.T) {
conn := open(t)
defer conn.Close()
row := conn.QueryRow(
"select :param2, :param1, :param2",
sql.Named("badparam1", 1),
sql.Named("param2", 2))
var col1, col2, col3 int64
err := row.Scan(&col1, &col2, &col3)
if err == nil {
t.Error("Scan succeeded unexpectedly")
return
}
t.Logf("Scan failed as expected with error %s", err)
}
func TestMixedParameters(t *testing.T) {
conn := open(t)
defer conn.Close()
row := conn.QueryRow(
"select :2, :param1, :param2",
5, // this parameter will be unused
6,
sql.Named("param1", 1),
sql.Named("param2", 2))
var col1, col2, col3 int64
err := row.Scan(&col1, &col2, &col3)
if err != nil {
t.Errorf("Scan failed with unexpected error %s", err)
return
}
if col1 != 6 || col2 != 1 || col3 != 2 {
t.Errorf("Unexpected values returned col1=%d, col2=%d, col3=%d", col1, col2, col3)
}
}
func TestPinger(t *testing.T) {
conn := open(t)
defer conn.Close()
err := conn.Ping()
if err != nil {
t.Errorf("Failed to hit database")
}
}
func TestQueryCancelLowLevel(t *testing.T) {
drv := &MssqlDriver{}
conn, err := drv.open(makeConnStr())
if err != nil {
t.Errorf("Open failed with error %v", err)
}
defer conn.Close()
ctx, cancel := context.WithCancel(context.Background())
stmt, err := conn.prepareContext(ctx, "waitfor delay '00:00:03'")
if err != nil {
t.Errorf("Prepare failed with error %v", err)
}
err = stmt.sendQuery(ctx, []namedValue{})
if err != nil {
t.Errorf("sendQuery failed with error %v", err)
}
cancel()
_, err = stmt.processExec(ctx)
if err != context.Canceled {
t.Errorf("Expected error to be Cancelled but got %v", err)
}
// same connection should be usable again after it was cancelled
stmt, err = conn.prepareContext(context.Background(), "select 1")
if err != nil {
t.Errorf("Prepare failed with error %v", err)
}
rows, err := stmt.Query([]driver.Value{})
if err != nil {
t.Errorf("Query failed with error %v", err)
}
values := []driver.Value{nil}
err = rows.Next(values)
if err != nil {
t.Errorf("Next failed with error %v", err)
}
}
func TestQueryCancelHighLevel(t *testing.T) {
conn := open(t)
defer conn.Close()
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(1 * time.Second)
cancel()
}()
_, err := conn.ExecContext(ctx, "waitfor delay '00:00:03'")
if err != context.Canceled {
t.Errorf("ExecContext expected to fail with Cancelled but it returned %v", err)
}
// connection should be usable after timeout
row := conn.QueryRow("select 1")
var val int64
err = row.Scan(&val)
if err != nil {
t.Fatal("Scan failed with", err)
}
}
func TestQueryTimeout(t *testing.T) {
conn := open(t)
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
defer cancel()
_, err := conn.ExecContext(ctx, "waitfor delay '00:00:03'")
if err != context.DeadlineExceeded {
t.Errorf("ExecContext expected to fail with DeadlineExceeded but it returned %v", err)
}
// connection should be usable after timeout
row := conn.QueryRow("select 1")
var val int64
err = row.Scan(&val)
if err != nil {
t.Fatal("Scan failed with", err)
}
}