| 1 |
|
| 2 |
|
| 3 |
package mssql |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"database/sql" |
| 8 |
"database/sql/driver" |
| 9 |
"errors" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
var _ driver.Pinger = &MssqlConn{} |
| 14 |
|
| 15 |
|
| 16 |
func (c *MssqlConn) Ping(ctx context.Context) error { |
| 17 |
stmt := &MssqlStmt{c, `select 1;`, 0, nil} |
| 18 |
_, err := stmt.ExecContext(ctx, nil) |
| 19 |
return err |
| 20 |
} |
| 21 |
|
| 22 |
var _ driver.ConnBeginTx = &MssqlConn{} |
| 23 |
|
| 24 |
|
| 25 |
func (c *MssqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { |
| 26 |
if opts.ReadOnly { |
| 27 |
return nil, errors.New("Read-only transactions are not supported") |
| 28 |
} |
| 29 |
|
| 30 |
var tdsIsolation isoLevel |
| 31 |
switch sql.IsolationLevel(opts.Isolation) { |
| 32 |
case sql.LevelDefault: |
| 33 |
tdsIsolation = isolationUseCurrent |
| 34 |
case sql.LevelReadUncommitted: |
| 35 |
tdsIsolation = isolationReadUncommited |
| 36 |
case sql.LevelReadCommitted: |
| 37 |
tdsIsolation = isolationReadCommited |
| 38 |
case sql.LevelWriteCommitted: |
| 39 |
return nil, errors.New("LevelWriteCommitted isolation level is not supported") |
| 40 |
case sql.LevelRepeatableRead: |
| 41 |
tdsIsolation = isolationRepeatableRead |
| 42 |
case sql.LevelSnapshot: |
| 43 |
tdsIsolation = isolationSnapshot |
| 44 |
case sql.LevelSerializable: |
| 45 |
tdsIsolation = isolationSerializable |
| 46 |
case sql.LevelLinearizable: |
| 47 |
return nil, errors.New("LevelLinearizable isolation level is not supported") |
| 48 |
default: |
| 49 |
return nil, errors.New("Isolation level is not supported or unknown") |
| 50 |
} |
| 51 |
return c.begin(ctx, tdsIsolation) |
| 52 |
} |
| 53 |
|
| 54 |
func (c *MssqlConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { |
| 55 |
if len(query) > 10 && strings.EqualFold(query[:10], "INSERTBULK") { |
| 56 |
return c.prepareCopyIn(query) |
| 57 |
} |
| 58 |
return c.prepareContext(ctx, query) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *MssqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { |
| 62 |
list := make([]namedValue, len(args)) |
| 63 |
for i, nv := range args { |
| 64 |
list[i] = namedValue(nv) |
| 65 |
} |
| 66 |
return s.queryContext(ctx, list) |
| 67 |
} |
| 68 |
|
| 69 |
func (s *MssqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { |
| 70 |
list := make([]namedValue, len(args)) |
| 71 |
for i, nv := range args { |
| 72 |
list[i] = namedValue(nv) |
| 73 |
} |
| 74 |
return s.exec(ctx, list) |
| 75 |
} |