Skip to content

Commit b594cb3

Browse files
committed
mssql: update docs, rename ResetSQL to SessionInitSQL and document
1 parent 270bc38 commit b594cb3

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Other supported formats are listed below.
6868
* `sqlserver://username:password@host:port?param1=value&param2=value`
6969
* `sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30` // `SQLExpress instance.
7070
* `sqlserver://sa:mypass@localhost?database=master&connection+timeout=30` // username=sa, password=mypass.
71-
* `sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30"` // port 1234 on localhost.
71+
* `sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30` // port 1234 on localhost.
7272
* `sqlserver://sa:my%7Bpass@somehost?connection+timeout=30` // password is "my{pass"
7373

7474
A string of this format can be constructed using the `URL` type in the `net/url` package.
@@ -126,6 +126,21 @@ the sql query to be in the form of either `@Name` or `@p1` to `@pN` (ordinal pos
126126
db.QueryContext(ctx, `select * from t where ID = @ID and Name = @p2;`, sql.Named("ID", 6), "Bob")
127127
```
128128
129+
## Important Notes
130+
131+
* [LastInsertId](https://golang.org/pkg/database/sql/#Result.LastInsertId) should
132+
not be used with this driver (or SQL Server) due to how the TDS protocol
133+
works. Please use the [OUTPUT Clause](https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql)
134+
or add a `select ID = convert(bigint, SCOPE_IDENTITY());` to the end of your
135+
query (ref [SCOPE_IDENTITY](https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql)).
136+
This will ensure you are getting the correct ID and will prevent a network round trip.
137+
* [NewConnector](https://godoc.org/github.com/denisenkom/go-mssqldb#NewConnector)
138+
may be used with [OpenDB](https://golang.org/pkg/database/sql/#OpenDB).
139+
* [Connector.SessionInitSQL](https://godoc.org/github.com/denisenkom/go-mssqldb#Connector.SessionInitSQL)
140+
may be set to set any driver specific session settings after the session
141+
has been reset. If empty the session will still be reset but use the database
142+
defaults in Go1.10+.
143+
129144
## Features
130145
131146
* Can be used with SQL Server 2005 or newer

doc.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// package mssql implements the TDS protocol used to connect to MS SQL Server (sqlserver)
22
// database servers.
33
//
4-
// This package registers two drivers:
4+
// This package registers the driver:
55
// sqlserver: uses native "@" parameter placeholder names and does no pre-processing.
6-
// mssql: expects identifiers to be prefixed with ":" and pre-processes queries.
76
//
87
// If the ordinal position is used for query parameters, identifiers will be named
98
// "@p1", "@p2", ... "@pN".
109
//
11-
// Please refer to the README for the format of the DSN.
10+
// Please refer to the README for the format of the DSN. There are multiple DSN
11+
// formats accepted: ADO style, ODBC style, and URL style. The following is an
12+
// example of a URL style DSN:
13+
// sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30
1214
package mssql

mssql.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ func (d *Driver) SetLogger(logger Logger) {
7272
d.log = optionalLogger{logger}
7373
}
7474

75+
// NewConnector creates a new connector from a DSN.
76+
// The returned connector may be used with sql.OpenDB.
77+
func NewConnector(dsn string) (*Connector, error) {
78+
params, err := parseConnectParams(dsn)
79+
if err != nil {
80+
return nil, err
81+
}
82+
c := &Connector{
83+
params: params,
84+
driver: driverInstanceNoProcess,
85+
}
86+
return c, nil
87+
}
88+
7589
// Connector holds the parsed DSN and is ready to make a new connection
7690
// at any time.
7791
//
@@ -81,11 +95,12 @@ type Connector struct {
8195
params connectParams
8296
driver *Driver
8397

84-
// ResetSQL is executed after marking a given connection to be reset.
85-
// When not present, the next query will be reset to the database
86-
// defaults.
87-
// When present the connection will immediately mark the connection to
88-
// be reset, then execute the ResetSQL text to setup the session
98+
// SessionInitSQL is executed after marking a given session to be reset.
99+
// When not present, the next query will still reset the session to the
100+
// database defaults.
101+
//
102+
// When present the connection will immediately mark the session to
103+
// be reset, then execute the SessionInitSQL text to setup the session
89104
// that may be different from the base database defaults.
90105
//
91106
// For Example, the application relies on the following defaults
@@ -96,9 +111,12 @@ type Connector struct {
96111
// SET ANSI_NULLS ON;
97112
// SET LOCK_TIMEOUT 10000;
98113
//
99-
// ResetSQL should not attempt to manually call sp_reset_connection.
114+
// SessionInitSQL should not attempt to manually call sp_reset_connection.
100115
// This will happen at the TDS layer.
101-
ResetSQL string
116+
//
117+
// SessionInitSQL is optional. The session will be reset even if
118+
// SessionInitSQL is empty.
119+
SessionInitSQL string
102120
}
103121

104122
type Conn struct {

mssql_go110.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func (c *Conn) ResetSession(ctx context.Context) error {
1616
}
1717
c.resetSession = true
1818

19-
if c.connector == nil || len(c.connector.ResetSQL) == 0 {
19+
if c.connector == nil || len(c.connector.SessionInitSQL) == 0 {
2020
return nil
2121
}
2222

23-
s, err := c.prepareContext(ctx, c.connector.ResetSQL)
23+
s, err := c.prepareContext(ctx, c.connector.SessionInitSQL)
2424
if err != nil {
2525
return driver.ErrBadConn
2626
}

queries_go110_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99
)
1010

11-
func TestResetSQL(t *testing.T) {
11+
func TestSessionInitSQL(t *testing.T) {
1212
checkConnStr(t)
1313
SetLogger(testLogger{t})
1414

@@ -17,7 +17,13 @@ func TestResetSQL(t *testing.T) {
1717
if err != nil {
1818
t.Fatal("unable to open connector", err)
1919
}
20-
connector.ResetSQL = `
20+
21+
// Do not use these settings in your application
22+
// unless you know what they do.
23+
// Thes are for this unit test only.
24+
//
25+
// Sessions will be reset even if SessionInitSQL is not set.
26+
connector.SessionInitSQL = `
2127
SET XACT_ABORT ON; -- 16384
2228
SET ANSI_NULLS ON; -- 32
2329
SET ARITHIGNORE ON; -- 128

0 commit comments

Comments
 (0)