-
Notifications
You must be signed in to change notification settings - Fork 3
/
mysql.go
50 lines (44 loc) · 1001 Bytes
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package sql
import (
"github.com/go-sql-driver/mysql"
)
const (
duplicateEntryCode uint16 = 1062
)
// NewMysql returns a mysql connection.
func (c *Client) NewMysql(opts ...SqlOption) SqlConn {
opts = append(opts, withMysqlAcceptable())
poolOpt := &pool{
MaxLifetime: c.MaxLifetime,
MaxIdleTime: c.MaxIdleTime,
MaxOpenConns: c.MaxOpenConns,
MaxIdleConns: c.MaxIdleConns,
DisableMetric: c.DisableMetric,
DisableTrace: c.DisableTrace,
DisablePrepare: c.DisablePrepare,
DriverName: c.DriverName,
DbName: c.Database,
Addr: c.Host,
}
return NewSqlConn(c.DriverName, c.dns(), poolOpt, opts...)
}
func mysqlAcceptable(err error) bool {
if err == nil {
return true
}
myerr, ok := err.(*mysql.MySQLError)
if !ok {
return false
}
switch myerr.Number {
case duplicateEntryCode:
return true
default:
return false
}
}
func withMysqlAcceptable() SqlOption {
return func(conn *commonSqlConn) {
conn.accept = mysqlAcceptable
}
}