Debian Codegraph @

README.md

raw symbols SCIP
1 # A pure Go MSSQL driver for Go's database/sql package
2
3 [![GoDoc](https://godoc.org/github.com/denisenkom/go-mssqldb?status.svg)](http://godoc.org/github.com/denisenkom/go-mssqldb)
4 [![Build status](https://ci.appveyor.com/api/projects/status/ujv21jd241h8o5s7?svg=true)](https://ci.appveyor.com/project/denisenk/go-mssqldb)
5 [![codecov](https://codecov.io/gh/denisenkom/go-mssqldb/branch/master/graph/badge.svg)](https://codecov.io/gh/denisenkom/go-mssqldb)
6
7 ## Install
8
9 go get github.com/denisenkom/go-mssqldb
10
11 ## Connection Parameters and DSN
12
13 * "server" - host or host\instance (default localhost)
14 * "port" - used only when there is no instance in server (default 1433)
15 * "failoverpartner" - host or host\instance (default is no partner).
16 * "failoverport" - used only when there is no instance in failoverpartner (default 1433)
17 * "user id" - enter the SQL Server Authentication user id or the Windows Authentication user id in the DOMAIN\User format. On Windows, if user id is empty or missing Single-Sign-On is used.
18 * "password"
19 * "database"
20 * "connection timeout" - in seconds (default is 30)
21 * "dial timeout" - in seconds (default is 5)
22 * "keepAlive" - in seconds; 0 to disable (default is 0)
23 * "packet size" - in bytes; 512 to 32767 (default is 4096)
24 * Encrypted connections have a maximum packet size of 16383 bytes
25 * Further information on usage: https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-network-packet-size-server-configuration-option
26 * "log" - logging flags (default 0/no logging, 63 for full logging)
27 * 1 log errors
28 * 2 log messages
29 * 4 log rows affected
30 * 8 trace sql statements
31 * 16 log statement parameters
32 * 32 log transaction begin/end
33 * "encrypt"
34 * disable - Data send between client and server is not encrypted.
35 * false - Data sent between client and server is not encrypted beyond the login packet. (Default)
36 * true - Data sent between client and server is encrypted.
37 * "TrustServerCertificate"
38 * false - Server certificate is checked. Default is false if encypt is specified.
39 * true - Server certificate is not checked. Default is true if encrypt is not specified. If trust server certificate is true, driver accepts any certificate presented by the server and any host name in that certificate. In this mode, TLS is susceptible to man-in-the-middle attacks. This should be used only for testing.
40 * "certificate" - The file that contains the public key certificate of the CA that signed the SQL Server certificate. The specified certificate overrides the go platform specific CA certificates.
41 * "hostNameInCertificate" - Specifies the Common Name (CN) in the server certificate. Default value is the server host.
42 * "ServerSPN" - The kerberos SPN (Service Principal Name) for the server. Default is MSSQLSvc/host:port.
43 * "Workstation ID" - The workstation name (default is the host name)
44 * "app name" - The application name (default is go-mssqldb)
45 * "ApplicationIntent" - Can be given the value "ReadOnly" to initiate a read-only connection to an Availability Group listener.
46
47 The connection string can be specified in one of three formats:
48
49 1. ADO: `key=value` pairs separated by `;`. Values may not contain `;`, leading and trailing whitespace is ignored.
50 Examples:
51
52 * `server=localhost\\SQLExpress;user id=sa;database=master;connection timeout=30`
53 * `server=localhost;user id=sa;database=master;connection timeout=30`
54
55 2. ODBC: Prefix with `odbc`, `key=value` pairs separated by `;`. Allow `;` by wrapping
56 values in `{}`. Examples:
57
58 * `odbc:server=localhost\\SQLExpress;user id=sa;database=master;connection timeout=30`
59 * `odbc:server=localhost;user id=sa;database=master;connection timeout=30`
60 * `odbc:server=localhost;user id=sa;password={foo;bar}` // Value marked with `{}`, password is "foo;bar"
61 * `odbc:server=localhost;user id=sa;password={foo{bar}` // Value marked with `{}`, password is "foo{bar"
62 * `odbc:server=localhost;user id=sa;password={foobar }` // Value marked with `{}`, password is "foobar "
63 * `odbc:server=localhost;user id=sa;password=foo{bar` // Literal `{`, password is "foo{bar"
64 * `odbc:server=localhost;user id=sa;password=foo}bar` // Literal `}`, password is "foo}bar"
65 * `odbc:server=localhost;user id=sa;password={foo{bar}` // Literal `{`, password is "foo{bar"
66 * `odbc:server=localhost;user id=sa;password={foo}}bar}` // Escaped `} with `}}`, password is "foo}bar"
67
68 3. URL: with `sqlserver` scheme. username and password appears before the host. Any instance appears as
69 the first segment in the path. All other options are query parameters. Examples:
70
71 * `sqlserver://username:password@host/instance?param1=value&param2=value`
72 * `sqlserver://username:password@host:port?param1=value&param2=value`
73 * `sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30` // `SQLExpress instance.
74 * `sqlserver://sa:mypass@localhost?database=master&connection+timeout=30` // username=sa, password=mypass.
75 * `sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30"` // port 1234 on localhost.
76 * `sqlserver://sa:my%7Bpass@somehost?connection+timeout=30` // password is "my{pass"
77
78 A string of this format can be constructed using the `URL` type in the `net/url` package.
79
80 ```go
81 query := url.Values{}
82 query.Add("connection timeout", fmt.Sprintf("%d", connectionTimeout))
83
84 u := &url.URL{
85 Scheme: "sqlserver",
86 User: url.UserPassword(username, password),
87 Host: fmt.Sprintf("%s:%d", hostname, port),
88 // Path: instance, // if connecting to an instance instead of a port
89 RawQuery: query.Encode(),
90 }
91
92 connectionString := u.String()
93
94 db, err := sql.Open("sqlserver", connectionString)
95 // or
96 db, err := sql.Open("mssql", connectionString)
97 ```
98
99 ## Statement Parameters
100
101 The `sqlserver` driver uses normal MS SQL Server syntax and expects parameters in
102 the sql query to be in the form of either `@Name` or `@p1` to `@pN` (ordinal position).
103
104 ```go
105 db.QueryContext(ctx, `select * from t where ID = @ID;`, sql.Named("ID", 6))
106 ```
107
108
109 For the `mssql` driver, the SQL statement text will be processed and literals will
110 be replaced by a parameter that matches one of the following:
111
112 * ?
113 * ?nnn
114 * :nnn
115 * $nnn
116
117 where nnn represents an integer that specifies a 1-indexed positional parameter. Ex:
118
119 ```go
120 db.Query("SELECT * FROM t WHERE a = ?3, b = ?2, c = ?1", "x", "y", "z")
121 ```
122
123 will expand to roughly
124
125 ```sql
126 SELECT * FROM t WHERE a = 'z', b = 'y', c = 'x'
127 ```
128
129 ## Features
130
131 * Can be used with SQL Server 2005 or newer
132 * Can be used with Microsoft Azure SQL Database
133 * Can be used on all go supported platforms (e.g. Linux, Mac OS X and Windows)
134 * Supports new date/time types: date, time, datetime2, datetimeoffset
135 * Supports string parameters longer than 8000 characters
136 * Supports encryption using SSL/TLS
137 * Supports SQL Server and Windows Authentication
138 * Supports Single-Sign-On on Windows
139 * Supports connections to AlwaysOn Availability Group listeners, including re-direction to read-only replicas.
140 * Supports query notifications
141
142 ## Tests
143
144 `go test` is used for testing. A running instance of MSSQL server is required.
145 Environment variables are used to pass login information.
146
147 Example:
148
149 env HOST=localhost SQLUSER=sa SQLPASSWORD=sa DATABASE=test go test
150
151 ## Known Issues
152
153 * SQL Server 2008 and 2008 R2 engine cannot handle login records when SSL encryption is not disabled.
154 To fix SQL Server 2008 R2 issue, install SQL Server 2008 R2 Service Pack 2.
155 To fix SQL Server 2008 issue, install Microsoft SQL Server 2008 Service Pack 3 and Cumulative update package 3 for SQL Server 2008 SP3.
156 More information: http://support.microsoft.com/kb/2653857