mssql_go18.go 2.4 KB

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