github.com/lingyao2333/mo-zero@v1.4.1/core/stores/sqlx/mysql.go (about)

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