From 952fdfc96b11de4fce408aaa2f2842c804283cbf Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Thu, 15 Dec 2016 16:06:29 -0800 Subject: [PATCH] mssql: update to latest go1.8 BeginTx Fixes #193 --- mssql_go18.go | 52 +++++++++++++++++++++++--------------------- queries_go18_test.go | 29 ++++++++++++++---------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/mssql_go18.go b/mssql_go18.go index fbdd51c7..d7daa897 100644 --- a/mssql_go18.go +++ b/mssql_go18.go @@ -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) } diff --git a/queries_go18_test.go b/queries_go18_test.go index 08602978..2db0f9de 100644 --- a/queries_go18_test.go +++ b/queries_go18_test.go @@ -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") @@ -273,16 +276,17 @@ 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)") @@ -290,13 +294,14 @@ func TestMssqlConn_BeginContext(t *testing.T) { 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 { @@ -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 {