xugu_driver.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package xugu
  2. import (
  3. "context"
  4. "database/sql"
  5. "database/sql/driver"
  6. "time"
  7. )
  8. // XuguDriver is exported to make the driver directly accessible
  9. type XuguDriver struct{}
  10. /* Register Driver */
  11. func init() {
  12. /* Register makes a database driver available by the provided name.
  13. * If Register is called twice with the same name or if driver is nil,
  14. * it panics.
  15. */
  16. sql.Register("xugusql", &XuguDriver{})
  17. timezone, _ := time.LoadLocation("Asia/Shanghai")
  18. time.Local = timezone
  19. }
  20. // Open opens a database specified by its database driver name and a
  21. // driver-specific data source name, usually consisting of at least a
  22. // database name and connection information.
  23. //
  24. // Most users will open a database via a driver-specific connection
  25. // helper function that returns a *DB. No database drivers are included
  26. // in the Go standard library. See https://golang.org/s/sqldrivers for
  27. // a list of third-party drivers.
  28. //
  29. // Open may just validate its arguments without creating a connection
  30. // to the database. To verify that the data source name is valid, call
  31. // Ping.
  32. // The returned DB is safe for concurrent use by multiple goroutines
  33. // and maintains its own pool of idle connections. Thus, the Open
  34. // function should be called just once. It is rarely necessary to
  35. // close a DB.
  36. func (db XuguDriver) Open(dsn string) (driver.Conn, error) {
  37. conn := &connector{dsn: dsn}
  38. return conn.Connect(context.Background())
  39. }
  40. func (db XuguDriver) OpenConnector(dsn string) (driver.Connector, error) {
  41. return &connector{dsn: dsn}, nil
  42. }