Microsoft SQL server driver written in go language
Clone or download
Permalink
Failed to load latest commit information.
batch Fix tests for Go 1.10 Mar 15, 2018
examples mssql: rename structs to prevent stutter. Use alias for compatibility… Mar 15, 2018
internal/cp internal/cp: create new internal codepage package Mar 14, 2018
LICENSE.txt added go license Apr 11, 2014
README.md mssql: allow returning return status code Aug 24, 2018
appveyor.yml Test for go 1.9 Jun 12, 2018
buf.go more test coverage for buf.go Jun 16, 2018
buf_test.go all: gofmt (#397) Jun 17, 2018
bulkcopy.go Add "binary" data type support for `CopyIn` functionality. (#407) Sep 1, 2018
bulkcopy_sql.go mssql: fix data race in bulk insert Mar 15, 2018
bulkcopy_test.go Add "binary" data type support for `CopyIn` functionality. (#407) Sep 1, 2018
convert.go Add support for Null* types for OUTPUT parameters Jun 24, 2018
decimal.go bulk copy for decimal types (#234) Sep 14, 2017
decimal_test.go mssql: support OUTPUT parameters, detect PROC names Sep 24, 2017
doc.go mssql: update docs, rename ResetSQL to SessionInitSQL and document Mar 27, 2018
error.go Add SQLError... methods to Error type Nov 26, 2016
error_example_test.go Add SQLError... methods to Error type Nov 26, 2016
log.go Use interface for logging Dec 4, 2016
mssql.go mssql: allow returning return status code Aug 24, 2018
mssql_go110.go mssql: update docs, rename ResetSQL to SessionInitSQL and document Mar 27, 2018
mssql_go19.go mssql: allow returning return status code Aug 24, 2018
mssql_go19pre.go queries: use reflect to support other in-out value types Jun 1, 2018
mssql_test.go mssql: make isProc test more robust Aug 23, 2018
net.go make internal types and functions private May 20, 2018
ntlm.go make internal types and functions private May 20, 2018
ntlm_test.go support extended session security Aug 17, 2014
parser.go mssql: support OUTPUT parameters, detect PROC names Sep 24, 2017
parser_test.go Implemented named parameters Dec 4, 2016
queries_go110_test.go mssql: allow returning return status code Aug 24, 2018
queries_go19_test.go mssql: allow returning return status code Aug 24, 2018
queries_test.go Better test organization Jun 25, 2018
rpc.go more test coverage for buf.go Jun 16, 2018
sspi_windows.go mssql: fixup error handling, document TLS test more Sep 26, 2017
tds.go make internal types and functions private May 20, 2018
tds_test.go all: gofmt (#397) Jun 17, 2018
token.go mssql: allow returning return status code Aug 24, 2018
token_string.go fix data race when sending attn signal (#216) Jan 17, 2017
tran.go mssql: support the connection session resetter interface Mar 15, 2018
types.go Add "binary" data type support for `CopyIn` functionality. (#407) Sep 1, 2018
types_test.go Fix `rows.ColumnTypes()` for money not null type (#340) Mar 13, 2018
uniqueidentifier.go add UniqueIdentifier type (#203) Jan 6, 2017
uniqueidentifier_test.go add UniqueIdentifier type (#203) Jan 6, 2017

README.md

A pure Go MSSQL driver for Go's database/sql package

GoDoc Build status codecov

Install

Requires Go 1.8 or above.

Install with go get github.com/denisenkom/go-mssqldb .

Connection Parameters and DSN

The recommended connection string uses a URL format: sqlserver://username:password@host/instance?param1=value&param2=value Other supported formats are listed below.

Common parameters:

  • 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.
  • password
  • database
  • connection timeout - in seconds (default is 0 for no timeout), set to 0 for no timeout. Recommended to set to 0 and use context to manage query and connection timeouts.
  • dial timeout - in seconds (default is 15), set to 0 for no timeout
  • encrypt
    • disable - Data send between client and server is not encrypted.
    • false - Data sent between client and server is not encrypted beyond the login packet. (Default)
    • true - Data sent between client and server is encrypted.
  • app name - The application name (default is go-mssqldb)

Connection parameters for ODBC and ADO style connection strings:

  • server - host or host\instance (default localhost)
  • port - used only when there is no instance in server (default 1433)

Less common parameters:

  • keepAlive - in seconds; 0 to disable (default is 30)
  • failoverpartner - host or host\instance (default is no partner).
  • failoverport - used only when there is no instance in failoverpartner (default 1433)
  • packet size - in bytes; 512 to 32767 (default is 4096)
  • log - logging flags (default 0/no logging, 63 for full logging)
    • 1 log errors
    • 2 log messages
    • 4 log rows affected
    • 8 trace sql statements
    • 16 log statement parameters
    • 32 log transaction begin/end
  • TrustServerCertificate
    • false - Server certificate is checked. Default is false if encypt is specified.
    • 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.
  • 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.
  • hostNameInCertificate - Specifies the Common Name (CN) in the server certificate. Default value is the server host.
  • ServerSPN - The kerberos SPN (Service Principal Name) for the server. Default is MSSQLSvc/host:port.
  • Workstation ID - The workstation name (default is the host name)
  • ApplicationIntent - Can be given the value ReadOnly to initiate a read-only connection to an Availability Group listener.

The connection string can be specified in one of three formats:

  1. URL: with sqlserver scheme. username and password appears before the host. Any instance appears as the first segment in the path. All other options are query parameters. Examples:
  • sqlserver://username:password@host/instance?param1=value&param2=value
  • sqlserver://username:password@host:port?param1=value&param2=value
  • sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30 // `SQLExpress instance.
  • sqlserver://sa:mypass@localhost?database=master&connection+timeout=30 // username=sa, password=mypass.
  • sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30 // port 1234 on localhost.
  • sqlserver://sa:my%7Bpass@somehost?connection+timeout=30 // password is "my{pass"

A string of this format can be constructed using the URL type in the net/url package.

  query := url.Values{}
  query.Add("app name", "MyAppName")

  u := &url.URL{
      Scheme:   "sqlserver",
      User:     url.UserPassword(username, password),
      Host:     fmt.Sprintf("%s:%d", hostname, port),
      // Path:  instance, // if connecting to an instance instead of a port
      RawQuery: query.Encode(),
  }
  db, err := sql.Open("sqlserver", u.String())
  1. ADO: key=value pairs separated by ;. Values may not contain ;, leading and trailing whitespace is ignored. Examples:
  • server=localhost\\SQLExpress;user id=sa;database=master;app name=MyAppName
  • server=localhost;user id=sa;database=master;app name=MyAppName
  1. ODBC: Prefix with odbc, key=value pairs separated by ;. Allow ; by wrapping values in {}. Examples:
  • odbc:server=localhost\\SQLExpress;user id=sa;database=master;app name=MyAppName
  • odbc:server=localhost;user id=sa;database=master;app name=MyAppName
  • odbc:server=localhost;user id=sa;password={foo;bar} // Value marked with {}, password is "foo;bar"
  • odbc:server=localhost;user id=sa;password={foo{bar} // Value marked with {}, password is "foo{bar"
  • odbc:server=localhost;user id=sa;password={foobar } // Value marked with {}, password is "foobar "
  • odbc:server=localhost;user id=sa;password=foo{bar // Literal {, password is "foo{bar"
  • odbc:server=localhost;user id=sa;password=foo}bar // Literal }, password is "foo}bar"
  • odbc:server=localhost;user id=sa;password={foo{bar} // Literal {, password is "foo{bar"
  • odbc:server=localhost;user id=sa;password={foo}}bar} // Escaped } with}}`, password is "foo}bar"

Executing Stored Procedures

To run a stored procedure, set the query text to the procedure name:

var account = "abc"
_, err := db.ExecContext(ctx, "sp_RunMe",
	sql.Named("ID", 123),
	sql.Named("Account", sql.Out{Dest: &account}),
)

Return Status

To get the procedure return status, pass into the parameters a *mssql.ReturnStatus. For example:

var rs mssql.ReturnStatus
_, err := db.ExecContext(ctx, "theproc", &rs)
log.Printf("status=%d", rs)

Parameters

The sqlserver driver uses normal MS SQL Server syntax and expects parameters in the sql query to be in the form of either @Name or @p1 to @pN (ordinal position).

db.QueryContext(ctx, `select * from t where ID = @ID and Name = @p2;`, sql.Named("ID", 6), "Bob")

Parameter Types

To pass specific types to the query parameters, say varchar or date types, you must convert the types to the type before passing in. The following types are supported:

  • string -> nvarchar
  • mssql.VarChar -> varchar
  • time.Time -> datetimeoffset or datetime (TDS version dependent)
  • mssql.DateTime1 -> datetime
  • mssql.DateTimeOffset -> datetimeoffset
  • "cloud.google.com/go/civil".Date -> date
  • "cloud.google.com/go/civil".DateTime -> datetime2
  • "cloud.google.com/go/civil".Time -> time

Important Notes

  • LastInsertId should not be used with this driver (or SQL Server) due to how the TDS protocol works. Please use the OUTPUT Clause or add a select ID = convert(bigint, SCOPE_IDENTITY()); to the end of your query (ref SCOPE_IDENTITY). This will ensure you are getting the correct ID and will prevent a network round trip.
  • NewConnector may be used with OpenDB.
  • Connector.SessionInitSQL may be set to set any driver specific session settings after the session has been reset. If empty the session will still be reset but use the database defaults in Go1.10+.

Features

  • Can be used with SQL Server 2005 or newer
  • Can be used with Microsoft Azure SQL Database
  • Can be used on all go supported platforms (e.g. Linux, Mac OS X and Windows)
  • Supports new date/time types: date, time, datetime2, datetimeoffset
  • Supports string parameters longer than 8000 characters
  • Supports encryption using SSL/TLS
  • Supports SQL Server and Windows Authentication
  • Supports Single-Sign-On on Windows
  • Supports connections to AlwaysOn Availability Group listeners, including re-direction to read-only replicas.
  • Supports query notifications

Tests

go test is used for testing. A running instance of MSSQL server is required. Environment variables are used to pass login information.

Example:

env SQLSERVER_DSN=sqlserver://user:pass@hostname/instance?database=test1 go test

Deprecated

These features still exist in the driver, but they are are deprecated.

Query Parameter Token Replace (driver "mssql")

If you use the driver name "mssql" (rather then "sqlserver") the SQL text will be loosly parsed and an attempt to extract identifiers using one of

  • ?
  • ?nnn
  • :nnn
  • $nnn

will be used. This is not recommended with SQL Server. There is at least one existing won't fix issue with the query parsing.

Use the native "@Name" parameters instead with the "sqlserver" driver name.

Known Issues

  • SQL Server 2008 and 2008 R2 engine cannot handle login records when SSL encryption is not disabled. To fix SQL Server 2008 R2 issue, install SQL Server 2008 R2 Service Pack 2. To fix SQL Server 2008 issue, install Microsoft SQL Server 2008 Service Pack 3 and Cumulative update package 3 for SQL Server 2008 SP3. More information: http://support.microsoft.com/kb/2653857