mssql_go18.go 2.0 KB

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