2016-03-03 15:16:59 +00:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
2018-01-26 23:51:00 +00:00
|
|
|
"context"
|
2016-03-03 15:16:59 +00:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
2017-01-13 14:06:34 +00:00
|
|
|
"net"
|
|
|
|
"reflect"
|
2016-03-03 15:16:59 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
var driverInstance = &Driver{processQueryText: true}
|
|
|
|
var driverInstanceNoProcess = &Driver{processQueryText: false}
|
2017-01-04 21:47:38 +00:00
|
|
|
|
2016-03-03 15:16:59 +00:00
|
|
|
func init() {
|
2017-01-04 21:47:38 +00:00
|
|
|
sql.Register("mssql", driverInstance)
|
2017-01-13 14:06:34 +00:00
|
|
|
sql.Register("sqlserver", driverInstanceNoProcess)
|
2018-01-26 23:51:00 +00:00
|
|
|
createDialer = func(p *connectParams) dialer {
|
2018-07-06 16:09:34 +00:00
|
|
|
return tcpDialer{&net.Dialer{KeepAlive: p.keepAlive}}
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
2017-01-13 14:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abstract the dialer for testing and for non-TCP based connections.
|
|
|
|
type dialer interface {
|
2018-07-06 16:09:34 +00:00
|
|
|
Dial(ctx context.Context, addr string) (net.Conn, error)
|
2017-01-13 14:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var createDialer func(p *connectParams) dialer
|
|
|
|
|
|
|
|
type tcpDialer struct {
|
|
|
|
nd *net.Dialer
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (d tcpDialer) Dial(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
return d.nd.DialContext(ctx, "tcp", addr)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
type Driver struct {
|
2017-01-04 21:47:38 +00:00
|
|
|
log optionalLogger
|
2017-01-13 14:06:34 +00:00
|
|
|
|
|
|
|
processQueryText bool
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// OpenConnector opens a new connector. Useful to dial with a context.
|
|
|
|
func (d *Driver) OpenConnector(dsn string) (*Connector, error) {
|
|
|
|
params, err := parseConnectParams(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Connector{
|
|
|
|
params: params,
|
|
|
|
driver: d,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Open(dsn string) (driver.Conn, error) {
|
|
|
|
return d.open(context.Background(), dsn)
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
func SetLogger(logger Logger) {
|
|
|
|
driverInstance.SetLogger(logger)
|
2017-01-13 14:06:34 +00:00
|
|
|
driverInstanceNoProcess.SetLogger(logger)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (d *Driver) SetLogger(logger Logger) {
|
2017-01-04 21:47:38 +00:00
|
|
|
d.log = optionalLogger{logger}
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// NewConnector creates a new connector from a DSN.
|
|
|
|
// The returned connector may be used with sql.OpenDB.
|
|
|
|
func NewConnector(dsn string) (*Connector, error) {
|
|
|
|
params, err := parseConnectParams(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c := &Connector{
|
|
|
|
params: params,
|
|
|
|
driver: driverInstanceNoProcess,
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connector holds the parsed DSN and is ready to make a new connection
|
|
|
|
// at any time.
|
|
|
|
//
|
|
|
|
// In the future, settings that cannot be passed through a string DSN
|
|
|
|
// may be set directly on the connector.
|
|
|
|
type Connector struct {
|
|
|
|
params connectParams
|
|
|
|
driver *Driver
|
|
|
|
|
|
|
|
// SessionInitSQL is executed after marking a given session to be reset.
|
|
|
|
// When not present, the next query will still reset the session to the
|
|
|
|
// database defaults.
|
|
|
|
//
|
|
|
|
// When present the connection will immediately mark the session to
|
|
|
|
// be reset, then execute the SessionInitSQL text to setup the session
|
|
|
|
// that may be different from the base database defaults.
|
|
|
|
//
|
|
|
|
// For Example, the application relies on the following defaults
|
|
|
|
// but is not allowed to set them at the database system level.
|
|
|
|
//
|
|
|
|
// SET XACT_ABORT ON;
|
|
|
|
// SET TEXTSIZE -1;
|
|
|
|
// SET ANSI_NULLS ON;
|
|
|
|
// SET LOCK_TIMEOUT 10000;
|
|
|
|
//
|
|
|
|
// SessionInitSQL should not attempt to manually call sp_reset_connection.
|
|
|
|
// This will happen at the TDS layer.
|
|
|
|
//
|
|
|
|
// SessionInitSQL is optional. The session will be reset even if
|
|
|
|
// SessionInitSQL is empty.
|
|
|
|
SessionInitSQL string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Conn struct {
|
|
|
|
connector *Connector
|
2017-01-13 14:06:34 +00:00
|
|
|
sess *tdsSession
|
2017-01-04 21:47:38 +00:00
|
|
|
transactionCtx context.Context
|
2018-07-06 16:09:34 +00:00
|
|
|
resetSession bool
|
2017-01-13 14:06:34 +00:00
|
|
|
|
|
|
|
processQueryText bool
|
2017-10-27 19:06:04 +00:00
|
|
|
connectionGood bool
|
2017-09-05 22:06:47 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
outs map[string]interface{}
|
2017-09-05 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) checkBadConn(err error) error {
|
2017-09-05 22:06:47 +00:00
|
|
|
// this is a hack to address Issue #275
|
|
|
|
// we set connectionGood flag to false if
|
|
|
|
// error indicates that connection is not usable
|
|
|
|
// but we return actual error instead of ErrBadConn
|
|
|
|
// this will cause connection to stay in a pool
|
|
|
|
// but next request to this connection will return ErrBadConn
|
|
|
|
|
|
|
|
// it might be possible to revise this hack after
|
|
|
|
// https://github.com/golang/go/issues/20807
|
|
|
|
// is implemented
|
2017-10-27 19:06:04 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-09-05 22:06:47 +00:00
|
|
|
return nil
|
2017-10-27 19:06:04 +00:00
|
|
|
case io.EOF:
|
2018-07-06 16:09:34 +00:00
|
|
|
c.connectionGood = false
|
2017-09-05 22:06:47 +00:00
|
|
|
return driver.ErrBadConn
|
2017-10-27 19:06:04 +00:00
|
|
|
case driver.ErrBadConn:
|
|
|
|
// It is an internal programming error if driver.ErrBadConn
|
|
|
|
// is ever passed to this function. driver.ErrBadConn should
|
2018-07-06 16:09:34 +00:00
|
|
|
// only ever be returned in response to a *mssql.Conn.connectionGood == false
|
2017-10-27 19:06:04 +00:00
|
|
|
// check in the external facing API.
|
|
|
|
panic("driver.ErrBadConn in checkBadConn. This should not happen.")
|
2017-09-05 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case net.Error:
|
|
|
|
c.connectionGood = false
|
|
|
|
return err
|
|
|
|
case StreamError:
|
|
|
|
c.connectionGood = false
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2016-12-14 00:12:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) clearOuts() {
|
2017-10-27 19:06:04 +00:00
|
|
|
c.outs = nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) simpleProcessResp(ctx context.Context) error {
|
2016-12-16 18:31:07 +00:00
|
|
|
tokchan := make(chan tokenStruct, 5)
|
2017-10-27 19:06:04 +00:00
|
|
|
go processResponse(ctx, c.sess, tokchan, c.outs)
|
|
|
|
c.clearOuts()
|
2016-12-16 18:31:07 +00:00
|
|
|
for tok := range tokchan {
|
|
|
|
switch token := tok.(type) {
|
2017-01-04 21:47:38 +00:00
|
|
|
case doneStruct:
|
|
|
|
if token.isError() {
|
2017-09-05 22:06:47 +00:00
|
|
|
return c.checkBadConn(token.getError())
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
2016-12-16 18:31:07 +00:00
|
|
|
case error:
|
2017-09-05 22:06:47 +00:00
|
|
|
return c.checkBadConn(token)
|
2016-12-14 00:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) Commit() error {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !c.connectionGood {
|
|
|
|
return driver.ErrBadConn
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if err := c.sendCommitRequest(); err != nil {
|
2017-09-05 22:06:47 +00:00
|
|
|
return c.checkBadConn(err)
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
return c.simpleProcessResp(c.transactionCtx)
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) sendCommitRequest() error {
|
2016-12-14 00:12:26 +00:00
|
|
|
headers := []headerStruct{
|
|
|
|
{hdrtype: dataStmHdrTransDescr,
|
|
|
|
data: transDescrHdr{c.sess.tranid, 1}.pack()},
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
reset := c.resetSession
|
|
|
|
c.resetSession = false
|
|
|
|
if err := sendCommitXact(c.sess.buf, headers, "", 0, 0, "", reset); err != nil {
|
2017-01-04 21:47:38 +00:00
|
|
|
if c.sess.logFlags&logErrors != 0 {
|
|
|
|
c.sess.log.Printf("Failed to send CommitXact with %v", err)
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
c.connectionGood = false
|
|
|
|
return fmt.Errorf("Faild to send CommitXact: %v", err)
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) Rollback() error {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !c.connectionGood {
|
|
|
|
return driver.ErrBadConn
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if err := c.sendRollbackRequest(); err != nil {
|
2017-09-05 22:06:47 +00:00
|
|
|
return c.checkBadConn(err)
|
2016-12-16 18:31:07 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
return c.simpleProcessResp(c.transactionCtx)
|
|
|
|
}
|
2016-12-16 18:31:07 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) sendRollbackRequest() error {
|
2017-01-04 21:47:38 +00:00
|
|
|
headers := []headerStruct{
|
|
|
|
{hdrtype: dataStmHdrTransDescr,
|
|
|
|
data: transDescrHdr{c.sess.tranid, 1}.pack()},
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
reset := c.resetSession
|
|
|
|
c.resetSession = false
|
|
|
|
if err := sendRollbackXact(c.sess.buf, headers, "", 0, 0, "", reset); err != nil {
|
2017-01-04 21:47:38 +00:00
|
|
|
if c.sess.logFlags&logErrors != 0 {
|
|
|
|
c.sess.log.Printf("Failed to send RollbackXact with %v", err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
c.connectionGood = false
|
|
|
|
return fmt.Errorf("Failed to send RollbackXact: %v", err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) Begin() (driver.Tx, error) {
|
2017-01-04 21:47:38 +00:00
|
|
|
return c.begin(context.Background(), isolationUseCurrent)
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) begin(ctx context.Context, tdsIsolation isoLevel) (tx driver.Tx, err error) {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
err = c.sendBeginRequest(ctx, tdsIsolation)
|
2017-01-04 21:47:38 +00:00
|
|
|
if err != nil {
|
2017-09-05 22:06:47 +00:00
|
|
|
return nil, c.checkBadConn(err)
|
|
|
|
}
|
|
|
|
tx, err = c.processBeginResponse(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, c.checkBadConn(err)
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
return
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) sendBeginRequest(ctx context.Context, tdsIsolation isoLevel) error {
|
2017-01-04 21:47:38 +00:00
|
|
|
c.transactionCtx = ctx
|
2016-03-03 15:16:59 +00:00
|
|
|
headers := []headerStruct{
|
|
|
|
{hdrtype: dataStmHdrTransDescr,
|
|
|
|
data: transDescrHdr{0, 1}.pack()},
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
reset := c.resetSession
|
|
|
|
c.resetSession = false
|
|
|
|
if err := sendBeginXact(c.sess.buf, headers, tdsIsolation, "", reset); err != nil {
|
2017-01-04 21:47:38 +00:00
|
|
|
if c.sess.logFlags&logErrors != 0 {
|
|
|
|
c.sess.log.Printf("Failed to send BeginXact with %v", err)
|
2016-12-16 18:31:07 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
c.connectionGood = false
|
2018-07-06 16:09:34 +00:00
|
|
|
return fmt.Errorf("Failed to send BeginXact: %v", err)
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) processBeginResponse(ctx context.Context) (driver.Tx, error) {
|
2017-01-04 21:47:38 +00:00
|
|
|
if err := c.simpleProcessResp(ctx); err != nil {
|
|
|
|
return nil, err
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
// successful BEGINXACT request will return sess.tranid
|
|
|
|
// for started transaction
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (d *Driver) open(ctx context.Context, dsn string) (*Conn, error) {
|
2016-12-01 19:46:57 +00:00
|
|
|
params, err := parseConnectParams(dsn)
|
2016-03-03 15:16:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return d.connect(ctx, params)
|
|
|
|
}
|
2016-03-03 15:16:59 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// connect to the server, using the provided context for dialing only.
|
|
|
|
func (d *Driver) connect(ctx context.Context, params connectParams) (*Conn, error) {
|
|
|
|
sess, err := connect(ctx, d.log, params)
|
2016-03-03 15:16:59 +00:00
|
|
|
if err != nil {
|
2016-12-01 19:46:57 +00:00
|
|
|
// main server failed, try fail-over partner
|
|
|
|
if params.failOverPartner == "" {
|
2016-06-30 18:19:03 +00:00
|
|
|
return nil, err
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:46:57 +00:00
|
|
|
params.host = params.failOverPartner
|
|
|
|
if params.failOverPort != 0 {
|
|
|
|
params.port = params.failOverPort
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2016-06-30 18:19:03 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
sess, err = connect(ctx, d.log, params)
|
2016-12-01 19:46:57 +00:00
|
|
|
if err != nil {
|
|
|
|
// fail-over partner also failed, now fail
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
conn := &Conn{
|
2017-10-27 19:06:04 +00:00
|
|
|
sess: sess,
|
|
|
|
transactionCtx: context.Background(),
|
|
|
|
processQueryText: d.processQueryText,
|
|
|
|
connectionGood: true,
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
conn.sess.log = d.log
|
2018-07-06 16:09:34 +00:00
|
|
|
|
2016-12-01 19:46:57 +00:00
|
|
|
return conn, nil
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) Close() error {
|
2016-03-03 15:16:59 +00:00
|
|
|
return c.sess.buf.transport.Close()
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
type Stmt struct {
|
|
|
|
c *Conn
|
2016-03-03 15:16:59 +00:00
|
|
|
query string
|
|
|
|
paramCount int
|
2016-04-26 00:18:04 +00:00
|
|
|
notifSub *queryNotifSub
|
|
|
|
}
|
|
|
|
|
|
|
|
type queryNotifSub struct {
|
|
|
|
msgText string
|
|
|
|
options string
|
|
|
|
timeout uint32
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
2017-03-31 00:03:13 +00:00
|
|
|
if len(query) > 10 && strings.EqualFold(query[:10], "INSERTBULK") {
|
2018-07-06 16:09:34 +00:00
|
|
|
return c.prepareCopyIn(context.Background(), query)
|
2017-03-31 00:03:13 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
return c.prepareContext(context.Background(), query)
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (c *Conn) prepareContext(ctx context.Context, query string) (*Stmt, error) {
|
2017-01-13 14:06:34 +00:00
|
|
|
paramCount := -1
|
|
|
|
if c.processQueryText {
|
|
|
|
query, paramCount = parseParams(query)
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return &Stmt{c, query, paramCount, nil}, nil
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) Close() error {
|
2016-03-03 15:16:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) SetQueryNotification(id, options string, timeout time.Duration) {
|
2016-04-26 00:18:04 +00:00
|
|
|
to := uint32(timeout / time.Second)
|
|
|
|
if to < 1 {
|
|
|
|
to = 1
|
|
|
|
}
|
|
|
|
s.notifSub = &queryNotifSub{id, options, to}
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) NumInput() int {
|
2016-03-03 15:16:59 +00:00
|
|
|
return s.paramCount
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) sendQuery(args []namedValue) (err error) {
|
2016-03-03 15:16:59 +00:00
|
|
|
headers := []headerStruct{
|
|
|
|
{hdrtype: dataStmHdrTransDescr,
|
|
|
|
data: transDescrHdr{s.c.sess.tranid, 1}.pack()},
|
|
|
|
}
|
2016-04-26 00:18:04 +00:00
|
|
|
|
|
|
|
if s.notifSub != nil {
|
2017-10-27 19:06:04 +00:00
|
|
|
headers = append(headers,
|
|
|
|
headerStruct{
|
|
|
|
hdrtype: dataStmHdrQueryNotif,
|
|
|
|
data: queryNotifHdr{
|
|
|
|
s.notifSub.msgText,
|
|
|
|
s.notifSub.options,
|
|
|
|
s.notifSub.timeout,
|
|
|
|
}.pack(),
|
|
|
|
})
|
2016-04-26 00:18:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
conn := s.c
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
// no need to check number of parameters here, it is checked by database/sql
|
2018-07-06 16:09:34 +00:00
|
|
|
if conn.sess.logFlags&logSQL != 0 {
|
|
|
|
conn.sess.log.Println(s.query)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
if conn.sess.logFlags&logParams != 0 && len(args) > 0 {
|
2016-03-03 15:16:59 +00:00
|
|
|
for i := 0; i < len(args); i++ {
|
2017-10-27 19:06:04 +00:00
|
|
|
if len(args[i].Name) > 0 {
|
|
|
|
s.c.sess.log.Printf("\t@%s\t%v\n", args[i].Name, args[i].Value)
|
|
|
|
} else {
|
|
|
|
s.c.sess.log.Printf("\t@p%d\t%v\n", i+1, args[i].Value)
|
|
|
|
}
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
|
|
|
|
reset := conn.resetSession
|
|
|
|
conn.resetSession = false
|
2016-03-03 15:16:59 +00:00
|
|
|
if len(args) == 0 {
|
2018-07-06 16:09:34 +00:00
|
|
|
if err = sendSqlBatch72(conn.sess.buf, s.query, headers, reset); err != nil {
|
|
|
|
if conn.sess.logFlags&logErrors != 0 {
|
|
|
|
conn.sess.log.Printf("Failed to send SqlBatch with %v", err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
conn.connectionGood = false
|
2017-10-27 19:06:04 +00:00
|
|
|
return fmt.Errorf("failed to send SQL Batch: %v", err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-06 16:09:34 +00:00
|
|
|
proc := sp_ExecuteSql
|
|
|
|
var params []param
|
2017-10-27 19:06:04 +00:00
|
|
|
if isProc(s.query) {
|
|
|
|
proc.name = s.query
|
|
|
|
params, _, err = s.makeRPCParams(args, 0)
|
2018-07-06 16:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
} else {
|
|
|
|
var decls []string
|
|
|
|
params, decls, err = s.makeRPCParams(args, 2)
|
2016-03-03 15:16:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
params[0] = makeStrParam(s.query)
|
|
|
|
params[1] = makeStrParam(strings.Join(decls, ","))
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
if err = sendRpc(conn.sess.buf, headers, proc, 0, params, reset); err != nil {
|
|
|
|
if conn.sess.logFlags&logErrors != 0 {
|
|
|
|
conn.sess.log.Printf("Failed to send Rpc with %v", err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
conn.connectionGood = false
|
2017-10-27 19:06:04 +00:00
|
|
|
return fmt.Errorf("Failed to send RPC: %v", err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
// isProc takes the query text in s and determines if it is a stored proc name
|
|
|
|
// or SQL text.
|
|
|
|
func isProc(s string) bool {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s[0] == '[' && s[len(s)-1] == ']' && strings.ContainsAny(s, "\n\r") == false {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !strings.ContainsAny(s, " \t\n\r;")
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) makeRPCParams(args []namedValue, offset int) ([]param, []string, error) {
|
2017-10-27 19:06:04 +00:00
|
|
|
var err error
|
2018-07-06 16:09:34 +00:00
|
|
|
params := make([]param, len(args)+offset)
|
2017-10-27 19:06:04 +00:00
|
|
|
decls := make([]string, len(args))
|
|
|
|
for i, val := range args {
|
|
|
|
params[i+offset], err = s.makeParam(val.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
var name string
|
|
|
|
if len(val.Name) > 0 {
|
|
|
|
name = "@" + val.Name
|
|
|
|
} else {
|
|
|
|
name = fmt.Sprintf("@p%d", val.Ordinal)
|
|
|
|
}
|
|
|
|
params[i+offset].Name = name
|
|
|
|
decls[i] = fmt.Sprintf("%s %s", name, makeDecl(params[i+offset].ti))
|
|
|
|
}
|
|
|
|
return params, decls, nil
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
type namedValue struct {
|
2017-01-13 14:06:34 +00:00
|
|
|
Name string
|
2017-01-04 21:47:38 +00:00
|
|
|
Ordinal int
|
2017-01-13 14:06:34 +00:00
|
|
|
Value driver.Value
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func convertOldArgs(args []driver.Value) []namedValue {
|
|
|
|
list := make([]namedValue, len(args))
|
|
|
|
for i, v := range args {
|
|
|
|
list[i] = namedValue{
|
2017-01-13 14:06:34 +00:00
|
|
|
Ordinal: i + 1,
|
|
|
|
Value: v,
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
2016-12-14 00:12:26 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
|
2017-01-04 21:47:38 +00:00
|
|
|
return s.queryContext(context.Background(), convertOldArgs(args))
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) queryContext(ctx context.Context, args []namedValue) (rows driver.Rows, err error) {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !s.c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
if err = s.sendQuery(args); err != nil {
|
|
|
|
return nil, s.c.checkBadConn(err)
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
return s.processQueryResponse(ctx)
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) processQueryResponse(ctx context.Context) (res driver.Rows, err error) {
|
2016-03-03 15:16:59 +00:00
|
|
|
tokchan := make(chan tokenStruct, 5)
|
2017-01-27 01:16:19 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-10-27 19:06:04 +00:00
|
|
|
go processResponse(ctx, s.c.sess, tokchan, s.c.outs)
|
|
|
|
s.c.clearOuts()
|
2016-03-03 15:16:59 +00:00
|
|
|
// process metadata
|
2016-12-01 19:46:57 +00:00
|
|
|
var cols []columnStruct
|
2016-03-03 15:16:59 +00:00
|
|
|
loop:
|
|
|
|
for tok := range tokchan {
|
|
|
|
switch token := tok.(type) {
|
2017-03-31 00:03:13 +00:00
|
|
|
// By ignoring DONE token we effectively
|
|
|
|
// skip empty result-sets.
|
|
|
|
// This improves results in queries like that:
|
2016-04-26 00:18:04 +00:00
|
|
|
// set nocount on; select 1
|
|
|
|
// see TestIgnoreEmptyResults test
|
|
|
|
//case doneStruct:
|
2016-06-08 14:33:08 +00:00
|
|
|
//break loop
|
2016-03-03 15:16:59 +00:00
|
|
|
case []columnStruct:
|
2016-12-01 19:46:57 +00:00
|
|
|
cols = token
|
2016-03-03 15:16:59 +00:00
|
|
|
break loop
|
2017-01-04 21:47:38 +00:00
|
|
|
case doneStruct:
|
|
|
|
if token.isError() {
|
2017-09-05 22:06:47 +00:00
|
|
|
return nil, s.c.checkBadConn(token.getError())
|
2016-12-16 18:31:07 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
case error:
|
2017-09-05 22:06:47 +00:00
|
|
|
return nil, s.c.checkBadConn(token)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
res = &Rows{stmt: s, tokchan: tokchan, cols: cols, cancel: cancel}
|
2016-12-01 19:46:57 +00:00
|
|
|
return
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
|
2017-01-04 21:47:38 +00:00
|
|
|
return s.exec(context.Background(), convertOldArgs(args))
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) exec(ctx context.Context, args []namedValue) (res driver.Result, err error) {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !s.c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
if err = s.sendQuery(args); err != nil {
|
|
|
|
return nil, s.c.checkBadConn(err)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
if res, err = s.processExec(ctx); err != nil {
|
|
|
|
return nil, s.c.checkBadConn(err)
|
|
|
|
}
|
|
|
|
return
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) processExec(ctx context.Context) (res driver.Result, err error) {
|
2016-03-03 15:16:59 +00:00
|
|
|
tokchan := make(chan tokenStruct, 5)
|
2017-10-27 19:06:04 +00:00
|
|
|
go processResponse(ctx, s.c.sess, tokchan, s.c.outs)
|
|
|
|
s.c.clearOuts()
|
2016-03-03 15:16:59 +00:00
|
|
|
var rowCount int64
|
|
|
|
for token := range tokchan {
|
|
|
|
switch token := token.(type) {
|
|
|
|
case doneInProcStruct:
|
|
|
|
if token.Status&doneCount != 0 {
|
2017-01-13 14:06:34 +00:00
|
|
|
rowCount += int64(token.RowCount)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
case doneStruct:
|
|
|
|
if token.Status&doneCount != 0 {
|
2017-01-13 14:06:34 +00:00
|
|
|
rowCount += int64(token.RowCount)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if token.isError() {
|
|
|
|
return nil, token.getError()
|
2016-12-16 18:31:07 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
case error:
|
|
|
|
return nil, token
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return &Result{s.c, rowCount}, nil
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
type Rows struct {
|
|
|
|
stmt *Stmt
|
2016-12-01 19:46:57 +00:00
|
|
|
cols []columnStruct
|
2016-03-03 15:16:59 +00:00
|
|
|
tokchan chan tokenStruct
|
2016-12-01 19:46:57 +00:00
|
|
|
|
|
|
|
nextCols []columnStruct
|
2017-01-27 01:16:19 +00:00
|
|
|
|
|
|
|
cancel func()
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (rc *Rows) Close() error {
|
2017-01-27 01:16:19 +00:00
|
|
|
rc.cancel()
|
2016-03-03 15:16:59 +00:00
|
|
|
for _ = range rc.tokchan {
|
|
|
|
}
|
|
|
|
rc.tokchan = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (rc *Rows) Columns() (res []string) {
|
2016-12-01 19:46:57 +00:00
|
|
|
res = make([]string, len(rc.cols))
|
|
|
|
for i, col := range rc.cols {
|
|
|
|
res[i] = col.ColName
|
|
|
|
}
|
|
|
|
return
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (rc *Rows) Next(dest []driver.Value) error {
|
2017-09-05 22:06:47 +00:00
|
|
|
if !rc.stmt.c.connectionGood {
|
|
|
|
return driver.ErrBadConn
|
|
|
|
}
|
2016-12-01 19:46:57 +00:00
|
|
|
if rc.nextCols != nil {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2016-03-03 15:16:59 +00:00
|
|
|
for tok := range rc.tokchan {
|
|
|
|
switch tokdata := tok.(type) {
|
|
|
|
case []columnStruct:
|
2016-12-01 19:46:57 +00:00
|
|
|
rc.nextCols = tokdata
|
|
|
|
return io.EOF
|
2016-03-03 15:16:59 +00:00
|
|
|
case []interface{}:
|
|
|
|
for i := range dest {
|
|
|
|
dest[i] = tokdata[i]
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-04 21:47:38 +00:00
|
|
|
case doneStruct:
|
|
|
|
if tokdata.isError() {
|
2017-09-05 22:06:47 +00:00
|
|
|
return rc.stmt.c.checkBadConn(tokdata.getError())
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
2016-03-03 15:16:59 +00:00
|
|
|
case error:
|
2017-09-05 22:06:47 +00:00
|
|
|
return rc.stmt.c.checkBadConn(tokdata)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (rc *Rows) HasNextResultSet() bool {
|
2016-12-01 19:46:57 +00:00
|
|
|
return rc.nextCols != nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (rc *Rows) NextResultSet() error {
|
2016-12-01 19:46:57 +00:00
|
|
|
rc.cols = rc.nextCols
|
|
|
|
rc.nextCols = nil
|
|
|
|
if rc.cols == nil {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should return
|
|
|
|
// the value type that can be used to scan types into. For example, the database
|
|
|
|
// column type "bigint" this should return "reflect.TypeOf(int64(0))".
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Rows) ColumnTypeScanType(index int) reflect.Type {
|
2016-12-01 19:46:57 +00:00
|
|
|
return makeGoLangScanType(r.cols[index].ti)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RowsColumnTypeDatabaseTypeName may be implemented by Rows. It should return the
|
|
|
|
// database system type name without the length. Type names should be uppercase.
|
|
|
|
// Examples of returned types: "VARCHAR", "NVARCHAR", "VARCHAR2", "CHAR", "TEXT",
|
|
|
|
// "DECIMAL", "SMALLINT", "INT", "BIGINT", "BOOL", "[]BIGINT", "JSONB", "XML",
|
|
|
|
// "TIMESTAMP".
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
|
2016-12-01 19:46:57 +00:00
|
|
|
return makeGoLangTypeName(r.cols[index].ti)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RowsColumnTypeLength may be implemented by Rows. It should return the length
|
|
|
|
// of the column type if the column is a variable length type. If the column is
|
|
|
|
// not a variable length type ok should return false.
|
|
|
|
// If length is not limited other than system limits, it should return math.MaxInt64.
|
|
|
|
// The following are examples of returned values for various types:
|
|
|
|
// TEXT (math.MaxInt64, true)
|
|
|
|
// varchar(10) (10, true)
|
|
|
|
// nvarchar(10) (10, true)
|
|
|
|
// decimal (0, false)
|
|
|
|
// int (0, false)
|
|
|
|
// bytea(30) (30, true)
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Rows) ColumnTypeLength(index int) (int64, bool) {
|
2016-12-01 19:46:57 +00:00
|
|
|
return makeGoLangTypeLength(r.cols[index].ti)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should return
|
|
|
|
// the precision and scale for decimal types. If not applicable, ok should be false.
|
|
|
|
// The following are examples of returned values for various types:
|
|
|
|
// decimal(38, 4) (38, 4, true)
|
|
|
|
// int (0, 0, false)
|
|
|
|
// decimal (math.MaxInt64, math.MaxInt64, true)
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Rows) ColumnTypePrecisionScale(index int) (int64, int64, bool) {
|
2016-12-01 19:46:57 +00:00
|
|
|
return makeGoLangTypePrecisionScale(r.cols[index].ti)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The nullable value should
|
|
|
|
// be true if it is known the column may be null, or false if the column is known
|
|
|
|
// to be not nullable.
|
|
|
|
// If the column nullability is unknown, ok should be false.
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Rows) ColumnTypeNullable(index int) (nullable, ok bool) {
|
2017-01-13 14:06:34 +00:00
|
|
|
nullable = r.cols[index].Flags&colFlagNullable != 0
|
2016-12-01 19:46:57 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func makeStrParam(val string) (res param) {
|
2017-01-04 21:47:38 +00:00
|
|
|
res.ti.TypeId = typeNVarChar
|
|
|
|
res.buffer = str2ucs2(val)
|
|
|
|
res.ti.Size = len(res.buffer)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (s *Stmt) makeParam(val driver.Value) (res param, err error) {
|
2016-03-03 15:16:59 +00:00
|
|
|
if val == nil {
|
2017-09-15 16:44:57 +00:00
|
|
|
res.ti.TypeId = typeNull
|
2016-03-03 15:16:59 +00:00
|
|
|
res.buffer = nil
|
2017-09-15 16:44:57 +00:00
|
|
|
res.ti.Size = 0
|
2016-03-03 15:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch val := val.(type) {
|
|
|
|
case int64:
|
|
|
|
res.ti.TypeId = typeIntN
|
|
|
|
res.buffer = make([]byte, 8)
|
|
|
|
res.ti.Size = 8
|
|
|
|
binary.LittleEndian.PutUint64(res.buffer, uint64(val))
|
2018-07-06 16:09:34 +00:00
|
|
|
case sql.NullInt64:
|
|
|
|
// only null values should be getting here
|
|
|
|
res.ti.TypeId = typeIntN
|
|
|
|
res.ti.Size = 8
|
|
|
|
res.buffer = []byte{}
|
|
|
|
|
2016-03-03 15:16:59 +00:00
|
|
|
case float64:
|
|
|
|
res.ti.TypeId = typeFltN
|
|
|
|
res.ti.Size = 8
|
|
|
|
res.buffer = make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(res.buffer, math.Float64bits(val))
|
2018-07-06 16:09:34 +00:00
|
|
|
case sql.NullFloat64:
|
|
|
|
// only null values should be getting here
|
|
|
|
res.ti.TypeId = typeFltN
|
|
|
|
res.ti.Size = 8
|
|
|
|
res.buffer = []byte{}
|
|
|
|
|
2016-03-03 15:16:59 +00:00
|
|
|
case []byte:
|
|
|
|
res.ti.TypeId = typeBigVarBin
|
|
|
|
res.ti.Size = len(val)
|
|
|
|
res.buffer = val
|
|
|
|
case string:
|
2017-01-04 21:47:38 +00:00
|
|
|
res = makeStrParam(val)
|
2018-07-06 16:09:34 +00:00
|
|
|
case sql.NullString:
|
|
|
|
// only null values should be getting here
|
|
|
|
res.ti.TypeId = typeNVarChar
|
|
|
|
res.buffer = nil
|
|
|
|
res.ti.Size = 8000
|
2016-03-03 15:16:59 +00:00
|
|
|
case bool:
|
|
|
|
res.ti.TypeId = typeBitN
|
|
|
|
res.ti.Size = 1
|
|
|
|
res.buffer = make([]byte, 1)
|
|
|
|
if val {
|
|
|
|
res.buffer[0] = 1
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
case sql.NullBool:
|
|
|
|
// only null values should be getting here
|
|
|
|
res.ti.TypeId = typeBitN
|
|
|
|
res.ti.Size = 1
|
|
|
|
res.buffer = []byte{}
|
|
|
|
|
2016-03-03 15:16:59 +00:00
|
|
|
case time.Time:
|
|
|
|
if s.c.sess.loginAck.TDSVersion >= verTDS73 {
|
|
|
|
res.ti.TypeId = typeDateTimeOffsetN
|
|
|
|
res.ti.Scale = 7
|
2018-07-06 16:09:34 +00:00
|
|
|
res.buffer = encodeDateTimeOffset(val, int(res.ti.Scale))
|
|
|
|
res.ti.Size = len(res.buffer)
|
2016-03-03 15:16:59 +00:00
|
|
|
} else {
|
|
|
|
res.ti.TypeId = typeDateTimeN
|
2018-07-06 16:09:34 +00:00
|
|
|
res.buffer = encodeDateTime(val)
|
|
|
|
res.ti.Size = len(res.buffer)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
default:
|
2017-10-27 19:06:04 +00:00
|
|
|
return s.makeParamExtra(val)
|
2016-03-03 15:16:59 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
type Result struct {
|
|
|
|
c *Conn
|
2016-03-03 15:16:59 +00:00
|
|
|
rowsAffected int64
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Result) RowsAffected() (int64, error) {
|
2016-03-03 15:16:59 +00:00
|
|
|
return r.rowsAffected, nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func (r *Result) LastInsertId() (int64, error) {
|
2016-03-03 15:16:59 +00:00
|
|
|
s, err := r.c.Prepare("select cast(@@identity as bigint)")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
rows, err := s.Query(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
dest := make([]driver.Value, 1)
|
|
|
|
err = rows.Next(dest)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if dest[0] == nil {
|
|
|
|
return -1, errors.New("There is no generated identity value")
|
|
|
|
}
|
|
|
|
lastInsertId := dest[0].(int64)
|
|
|
|
return lastInsertId, nil
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
|
|
|
|
var _ driver.Pinger = &Conn{}
|
|
|
|
|
|
|
|
// Ping is used to check if the remote server is available and satisfies the Pinger interface.
|
|
|
|
func (c *Conn) Ping(ctx context.Context) error {
|
|
|
|
if !c.connectionGood {
|
|
|
|
return driver.ErrBadConn
|
|
|
|
}
|
|
|
|
stmt := &Stmt{c, `select 1;`, 0, nil}
|
|
|
|
_, err := stmt.ExecContext(ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ driver.ConnBeginTx = &Conn{}
|
|
|
|
|
|
|
|
// BeginTx satisfies ConnBeginTx.
|
|
|
|
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
|
|
|
if !c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
if opts.ReadOnly {
|
|
|
|
return nil, errors.New("Read-only transactions are not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
var tdsIsolation isoLevel
|
|
|
|
switch sql.IsolationLevel(opts.Isolation) {
|
|
|
|
case sql.LevelDefault:
|
|
|
|
tdsIsolation = isolationUseCurrent
|
|
|
|
case sql.LevelReadUncommitted:
|
|
|
|
tdsIsolation = isolationReadUncommited
|
|
|
|
case sql.LevelReadCommitted:
|
|
|
|
tdsIsolation = isolationReadCommited
|
|
|
|
case sql.LevelWriteCommitted:
|
|
|
|
return nil, errors.New("LevelWriteCommitted isolation level is not supported")
|
|
|
|
case sql.LevelRepeatableRead:
|
|
|
|
tdsIsolation = isolationRepeatableRead
|
|
|
|
case sql.LevelSnapshot:
|
|
|
|
tdsIsolation = isolationSnapshot
|
|
|
|
case sql.LevelSerializable:
|
|
|
|
tdsIsolation = isolationSerializable
|
|
|
|
case sql.LevelLinearizable:
|
|
|
|
return nil, errors.New("LevelLinearizable isolation level is not supported")
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Isolation level is not supported or unknown")
|
|
|
|
}
|
|
|
|
return c.begin(ctx, tdsIsolation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
|
|
|
if !c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
if len(query) > 10 && strings.EqualFold(query[:10], "INSERTBULK") {
|
|
|
|
return c.prepareCopyIn(ctx, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.prepareContext(ctx, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
|
|
|
if !s.c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
list := make([]namedValue, len(args))
|
|
|
|
for i, nv := range args {
|
|
|
|
list[i] = namedValue(nv)
|
|
|
|
}
|
|
|
|
return s.queryContext(ctx, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
|
|
|
if !s.c.connectionGood {
|
|
|
|
return nil, driver.ErrBadConn
|
|
|
|
}
|
|
|
|
list := make([]namedValue, len(args))
|
|
|
|
for i, nv := range args {
|
|
|
|
list[i] = namedValue(nv)
|
|
|
|
}
|
|
|
|
return s.exec(ctx, list)
|
|
|
|
}
|