forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 67
/
newconnector_example_test.go
152 lines (133 loc) · 3.67 KB
/
newconnector_example_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
//go:build go1.10
// +build go1.10
package mssql_test
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net/url"
"strconv"
mssql "github.com/microsoft/go-mssqldb"
)
var (
debug = flag.Bool("debug", false, "enable debugging")
password = flag.String("password", "", "the database password")
port *int = flag.Int("port", 1433, "the database port")
server = flag.String("server", ".", "the database server")
user = flag.String("user", "", "the database user")
)
const (
createTableSql = "CREATE TABLE TestAnsiNull (bitcol bit, charcol char(1));"
dropTableSql = "IF OBJECT_ID('TestAnsiNull', 'U') IS NOT NULL DROP TABLE TestAnsiNull;"
insertQuery1 = "INSERT INTO TestAnsiNull VALUES (0, NULL);"
insertQuery2 = "INSERT INTO TestAnsiNull VALUES (1, 'a');"
selectNullFilter = "SELECT bitcol FROM TestAnsiNull WHERE charcol = NULL;"
selectNotNullFilter = "SELECT bitcol FROM TestAnsiNull WHERE charcol <> NULL;"
)
func makeConnURL() *url.URL {
flag.Parse()
if *debug {
fmt.Printf(" password:%s\n", *password)
fmt.Printf(" port:%d\n", *port)
fmt.Printf(" server:%s\n", *server)
fmt.Printf(" user:%s\n", *user)
}
params, err := mssql.GetConnParams()
if err == nil && params != nil {
return params.URL()
}
var userInfo *url.Userinfo
if *user != "" {
userInfo = url.UserPassword(*user, *password)
}
return &url.URL{
Scheme: "sqlserver",
Host: *server + ":" + strconv.Itoa(*port),
User: userInfo,
}
}
// This example shows the usage of Connector type
func ExampleConnector() {
connString := makeConnURL().String()
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
// Create a new connector object by calling NewConnector
connector, err := mssql.NewConnector(connString)
if err != nil {
log.Println(err)
return
}
// Use SessionInitSql to set any options that cannot be set with the dsn string
// With ANSI_NULLS set to ON, compare NULL data with = NULL or <> NULL will return 0 rows
connector.SessionInitSQL = "SET ANSI_NULLS ON"
// Pass connector to sql.OpenDB to get a sql.DB object
db := sql.OpenDB(connector)
defer db.Close()
// Create and populate table
_, err = db.Exec(createTableSql)
if err != nil {
log.Println(err)
return
}
defer db.Exec(dropTableSql)
_, err = db.Exec(insertQuery1)
if err != nil {
log.Println(err)
return
}
_, err = db.Exec(insertQuery2)
if err != nil {
log.Println(err)
return
}
var bitval bool
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// (*Row) Scan should return ErrNoRows since ANSI_NULLS is set to ON
err = db.QueryRowContext(ctx, selectNullFilter).Scan(&bitval)
if err != nil {
if err.Error() != "sql: no rows in result set" {
log.Println(err)
return
}
} else {
log.Println("Expects an ErrNoRows error. No error is returned")
return
}
// (*Row) Scan should return ErrNoRows since ANSI_NULLS is set to ON
err = db.QueryRowContext(ctx, selectNotNullFilter).Scan(&bitval)
if err != nil {
if err.Error() != "sql: no rows in result set" {
log.Println(err)
return
}
} else {
log.Println("Expects an ErrNoRows error. No error is returned")
return
}
// Set ANSI_NULLS to OFF
connector.SessionInitSQL = "SET ANSI_NULLS OFF"
// (*Row) Scan should copy data to bitval
err = db.QueryRowContext(ctx, selectNullFilter).Scan(&bitval)
if err != nil {
log.Println(err)
return
}
if bitval != false {
log.Println("Incorrect value retrieved.")
return
}
// (*Row) Scan should copy data to bitval
err = db.QueryRowContext(ctx, selectNotNullFilter).Scan(&bitval)
if err != nil {
log.Println(err)
return
}
if bitval != true {
log.Println("Incorrect value retrieved.")
return
}
}