You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/sqlx/mysql.go

42 lines
696 B
Go

4 years ago
package sqlx
import (
"errors"
"github.com/go-sql-driver/mysql"
)
4 years ago
const (
mysqlDriverName = "mysql"
duplicateEntryCode uint16 = 1062
)
// NewMysql returns a mysql connection.
4 years ago
func NewMysql(datasource string, opts ...SqlOption) SqlConn {
opts = append(opts, withMysqlAcceptable())
return NewSqlConn(mysqlDriverName, datasource, opts...)
}
func mysqlAcceptable(err error) bool {
if err == nil {
return true
}
var myerr *mysql.MySQLError
ok := errors.As(err, &myerr)
4 years ago
if !ok {
return false
}
switch myerr.Number {
case duplicateEntryCode:
return true
default:
return false
}
}
func withMysqlAcceptable() SqlOption {
return WithAcceptable(mysqlAcceptable)
4 years ago
}