xugu_driver.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package xugu
  2. import (
  3. "context"
  4. "database/sql"
  5. "database/sql/driver"
  6. "fmt"
  7. "time"
  8. )
  9. // XuguDriver is exported to make the driver directly accessible
  10. type XuguDriver struct{}
  11. /* Register Driver */
  12. func init() {
  13. fmt.Println(">>>>>init()")
  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. fmt.Println(">>>>>(db XuguDriver) Open")
  40. conn := &connector{dsn: dsn}
  41. return conn.Connect(context.Background())
  42. }
  43. func (db XuguDriver) OpenConnector(dsn string) (driver.Connector, error) {
  44. fmt.Println(">>>>>(db XuguDriver) OpenConnector")
  45. return &connector{dsn: dsn}, nil
  46. }