2020-07-26 17:09:05 +08:00
|
|
|
package sqlx
|
|
|
|
|
|
2023-10-21 00:00:57 +08:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
|
)
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
mysqlDriverName = "mysql"
|
|
|
|
|
duplicateEntryCode uint16 = 1062
|
|
|
|
|
)
|
|
|
|
|
|
2021-02-28 23:02:49 +08:00
|
|
|
// NewMysql returns a mysql connection.
|
2020-07-26 17:09:05 +08:00
|
|
|
func NewMysql(datasource string, opts ...SqlOption) SqlConn {
|
2024-03-08 12:23:41 +08:00
|
|
|
opts = append([]SqlOption{withMysqlAcceptable()}, opts...)
|
2020-07-26 17:09:05 +08:00
|
|
|
return NewSqlConn(mysqlDriverName, datasource, opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mysqlAcceptable(err error) bool {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 00:00:57 +08:00
|
|
|
var myerr *mysql.MySQLError
|
|
|
|
|
ok := errors.As(err, &myerr)
|
2020-07-26 17:09:05 +08:00
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch myerr.Number {
|
|
|
|
|
case duplicateEntryCode:
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func withMysqlAcceptable() SqlOption {
|
2023-07-10 09:16:45 +08:00
|
|
|
return WithAcceptable(mysqlAcceptable)
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|