connector.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // +build go1.10
  2. package pq
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. )
  7. // Connector represents a fixed configuration for the pq driver with a given
  8. // name. Connector satisfies the database/sql/driver Connector interface and
  9. // can be used to create any number of DB Conn's via the database/sql OpenDB
  10. // function.
  11. //
  12. // See https://golang.org/pkg/database/sql/driver/#Connector.
  13. // See https://golang.org/pkg/database/sql/#OpenDB.
  14. type connector struct {
  15. name string
  16. }
  17. // Connect returns a connection to the database using the fixed configuration
  18. // of this Connector. Context is not used.
  19. func (c *connector) Connect(_ context.Context) (driver.Conn, error) {
  20. return (&Driver{}).Open(c.name)
  21. }
  22. // Driver returnst the underlying driver of this Connector.
  23. func (c *connector) Driver() driver.Driver {
  24. return &Driver{}
  25. }
  26. var _ driver.Connector = &connector{}
  27. // NewConnector returns a connector for the pq driver in a fixed configuration
  28. // with the given name. The returned connector can be used to create any number
  29. // of equivalent Conn's. The returned connector is intended to be used with
  30. // database/sql.OpenDB.
  31. //
  32. // See https://golang.org/pkg/database/sql/driver/#Connector.
  33. // See https://golang.org/pkg/database/sql/#OpenDB.
  34. func NewConnector(name string) (driver.Connector, error) {
  35. return &connector{name: name}, nil
  36. }