mssql_go110.go 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build go1.10
  2. package mssql
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. )
  7. var _ driver.Connector = &Connector{}
  8. var _ driver.SessionResetter = &Conn{}
  9. func (c *Conn) ResetSession(ctx context.Context) error {
  10. if !c.connectionGood {
  11. return driver.ErrBadConn
  12. }
  13. c.resetSession = true
  14. if c.connector == nil || len(c.connector.SessionInitSQL) == 0 {
  15. return nil
  16. }
  17. s, err := c.prepareContext(ctx, c.connector.SessionInitSQL)
  18. if err != nil {
  19. return driver.ErrBadConn
  20. }
  21. _, err = s.exec(ctx, nil)
  22. if err != nil {
  23. return driver.ErrBadConn
  24. }
  25. return nil
  26. }
  27. // Connect to the server and return a TDS connection.
  28. func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
  29. conn, err := c.driver.connect(ctx, c.params)
  30. if conn != nil {
  31. conn.connector = c
  32. }
  33. if err == nil {
  34. err = conn.ResetSession(ctx)
  35. }
  36. return conn, err
  37. }
  38. // Driver underlying the Connector.
  39. func (c *Connector) Driver() driver.Driver {
  40. return c.driver
  41. }