Antoine GIRARD 149758c912 Update to github.com/lafriks/xormstore@v1.3.0 (#8317) | 5 년 전 | |
---|---|---|
.. | ||
internal | 5 년 전 | |
LICENSE.txt | 7 년 전 | |
README.md | 5 년 전 | |
appveyor.yml | 5 년 전 | |
buf.go | 5 년 전 | |
bulkcopy.go | 5 년 전 | |
bulkcopy_sql.go | 5 년 전 | |
conn_str.go | 5 년 전 | |
convert.go | 5 년 전 | |
doc.go | 5 년 전 | |
error.go | 7 년 전 | |
go.mod | 5 년 전 | |
go.sum | 5 년 전 | |
log.go | 5 년 전 | |
mssql.go | 5 년 전 | |
mssql_go19.go | 5 년 전 | |
mssql_go19pre.go | 5 년 전 | |
mssql_go110.go | 5 년 전 | |
mssql_go110pre.go | 5 년 전 | |
net.go | 5 년 전 | |
ntlm.go | 5 년 전 | |
rpc.go | 5 년 전 | |
sspi_windows.go | 5 년 전 | |
tds.go | 5 년 전 | |
token.go | 5 년 전 | |
token_string.go | 5 년 전 | |
tran.go | 5 년 전 | |
tvp_go19.go | 5 년 전 | |
types.go | 5 년 전 | |
uniqueidentifier.go | 5 년 전 |
Requires Go 1.8 or above.
Install with go get github.com/denisenkom/go-mssqldb
.
The recommended connection string uses a URL format:
sqlserver://username:password@host/instance?param1=value¶m2=value
Other supported formats are listed below.
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 timeoutencrypt
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)server
- host or host\instance (default localhost)port
- used only when there is no instance in server (default 1433)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)
TrustServerCertificate
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.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¶m2=value
sqlserver://username:password@host:port?param1=value¶m2=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())
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
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”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}),
)
To read output parameters from a stored procedure with resultset, make sure you read all the rows before reading the output parameters:
sqltextcreate := `
CREATE PROCEDURE spwithoutputandrows
@bitparam BIT OUTPUT
AS BEGIN
SET @bitparam = 1
SELECT 'Row 1'
END
`
var bitout int64
rows, err := db.QueryContext(ctx, "spwithoutputandrows", sql.Named("bitparam", sql.Out{Dest: &bitout}))
var strrow string
for rows.Next() {
err = rows.Scan(&strrow)
}
fmt.Printf("bitparam is %d", bitout)
Due to protocol limitations, temporary tables will only be allocated on the connection
as a result of executing a query with zero parameters. The following query
will, due to the use of a parameter, execute in its own session,
and #mytemp
will be de-allocated right away:
conn, err := pool.Conn(ctx)
defer conn.Close()
_, err := conn.ExecContext(ctx, "select @p1 as x into #mytemp", 1)
// at this point #mytemp is already dropped again as the session of the ExecContext is over
To work around this, always explicitly create the local temporary table in a query without any parameters. As a special case, the driver will then be able to execute the query directly on the connection-scoped session. The following example works:
conn, err := pool.Conn(ctx)
// Set us up so that temp table is always cleaned up, since conn.Close()
// merely returns conn to pool, rather than actually closing the connection.
defer func() {
_, _ = conn.ExecContext(ctx, "drop table #mytemp") // always clean up
conn.Close() // merely returns conn to pool
}()
// Since we not pass any parameters below, the query will execute on the scope of
// the connection and succeed in creating the table.
_, err := conn.ExecContext(ctx, "create table #mytemp ( x int )")
// #mytemp is now available even if you pass parameters
_, err := conn.ExecContext(ctx, "insert into #mytemp (x) values (@p1)", 1)
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)
or
var rs mssql.ReturnStatus
_, err := db.QueryContext(ctx, "theproc", &rs)
for rows.Next() {
err = rows.Scan(&val)
}
log.Printf("status=%d", rs)
Limitation: ReturnStatus cannot be retrieved using QueryRow
.
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")
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:
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.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
These features still exist in the driver, but they are are deprecated.
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
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.