mysql.go 690 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package sqlx
  2. import "github.com/go-sql-driver/mysql"
  3. const (
  4. mysqlDriverName = "mysql"
  5. duplicateEntryCode uint16 = 1062
  6. )
  7. // NewMysql returns a mysql connection.
  8. func NewMysql(datasource string, opts ...SqlOption) SqlConn {
  9. opts = append(opts, withMysqlAcceptable())
  10. return NewSqlConn(mysqlDriverName, datasource, opts...)
  11. }
  12. func mysqlAcceptable(err error) bool {
  13. if err == nil {
  14. return true
  15. }
  16. myerr, ok := err.(*mysql.MySQLError)
  17. if !ok {
  18. return false
  19. }
  20. switch myerr.Number {
  21. case duplicateEntryCode:
  22. return true
  23. default:
  24. return false
  25. }
  26. }
  27. func withMysqlAcceptable() SqlOption {
  28. return func(conn *commonSqlConn) {
  29. conn.accept = mysqlAcceptable
  30. }
  31. }