Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mssql: update to latest go1.8 BeginTx #195

Merged
merged 1 commit into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions mssql_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,41 @@ import (

var _ driver.Pinger = &MssqlConn{}

// Ping is used to check if the remote server is avaiable and satisfies the Pinger interface.
func (c *MssqlConn) Ping(ctx context.Context) error {
stmt := &MssqlStmt{c, `select 1;`, 0, nil}
_, err := stmt.ExecContext(ctx, nil)
return err
}

func (c *MssqlConn) BeginContext(ctx context.Context) (driver.Tx, error) {
if driver.ReadOnlyFromContext(ctx) {
var _ driver.ConnBeginTx = &MssqlConn{}

// BeginTx satisfies ConnBeginTx.
func (c *MssqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if opts.ReadOnly {
return nil, errors.New("Read-only transactions are not supported")
}
tdsIsolation := isolationUseCurrent
isolation, ok := driver.IsolationFromContext(ctx)
if ok {
switch sql.IsolationLevel(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")
}

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)
}
Expand Down
29 changes: 17 additions & 12 deletions queries_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,14 @@ func TestColumnIntrospection(t *testing.T) {
func TestContext(t *testing.T) {
conn := open(t)
defer conn.Close()

opts := &sql.TxOptions{
Isolation: sql.LevelSerializable,
}
ctx := context.Background()
ctx = sql.IsolationContext(ctx, sql.LevelSerializable)
tx, err := conn.BeginContext(ctx)
tx, err := conn.BeginTx(ctx, opts)
if err != nil {
t.Errorf("BeginContext failed with unexpected error %s", err)
t.Errorf("BeginTx failed with unexpected error %s", err)
return
}
rows, err := tx.QueryContext(ctx, "DBCC USEROPTIONS")
Expand Down Expand Up @@ -273,30 +276,32 @@ func TestContext(t *testing.T) {
}
}

func TestBeginContextReadOnlyNotSupported(t *testing.T) {
func TestBeginTxtReadOnlyNotSupported(t *testing.T) {
conn := open(t)
defer conn.Close()
_, err := conn.BeginContext(sql.ReadOnlyContext(context.Background()))
opts := &sql.TxOptions{ReadOnly: true}
_, err := conn.BeginTx(context.Background(), opts)
if err == nil {
t.Error("BeginContext expected to fail for read only transaction because MSSQL doesn't support it, but it succeeded")
t.Error("BeginTx expected to fail for read only transaction because MSSQL doesn't support it, but it succeeded")
}
}

func TestMssqlConn_BeginContext(t *testing.T) {
func TestMssqlConn_BeginTx(t *testing.T) {
conn := open(t)
defer conn.Close()
_, err := conn.Exec("create table test (f int)")
defer conn.Exec("drop table test")
if err != nil {
t.Fatal("create table failed with error", err)
}
tx1, err := conn.BeginContext(context.Background())

tx1, err := conn.BeginTx(context.Background(), nil)
if err != nil {
t.Fatal("BeginContext failed with error", err)
t.Fatal("BeginTx failed with error", err)
}
tx2, err := conn.BeginContext(context.Background())
tx2, err := conn.BeginTx(context.Background(), nil)
if err != nil {
t.Fatal("BeginContext failed with error", err)
t.Fatal("BeginTx failed with error", err)
}
_, err = tx1.Exec("insert into test (f) values (1)")
if err != nil {
Expand Down Expand Up @@ -479,7 +484,7 @@ func TestQueryCancelHighLevel(t *testing.T) {
func TestQueryTimeout(t *testing.T) {
conn := open(t)
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := conn.ExecContext(ctx, "waitfor delay '00:00:03'")
if err != context.DeadlineExceeded {
Expand Down