xugu_driver.go 1.6 KB

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