mysql.go 650 B

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