-
Notifications
You must be signed in to change notification settings - Fork 403
/
mysql.go
60 lines (47 loc) · 1.1 KB
/
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
51
52
53
54
55
56
57
58
59
60
package mysql
import (
"net/url"
"strings"
"github.com/fnproject/fn/api/datastore/sql/dbhelper"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type mysqlHelper int
func (mysqlHelper) Supports(scheme string) bool {
return scheme == "mysql"
}
func (mysqlHelper) PreConnect(url *url.URL) (string, error) {
return strings.TrimPrefix(url.String(), url.Scheme+"://"), nil
}
func (mysqlHelper) PostCreate(db *sqlx.DB) (*sqlx.DB, error) {
return db, nil
}
func (mysqlHelper) CheckTableExists(tx *sqlx.Tx, table string) (bool, error) {
query := tx.Rebind(`SELECT count(*)
FROM information_schema.TABLES
WHERE TABLE_NAME = ?`)
row := tx.QueryRow(query, table)
var count int
err := row.Scan(&count)
if err != nil {
return false, err
}
exists := count > 0
return exists, nil
}
func (mysqlHelper) String() string {
return "mysql"
}
func (mysqlHelper) IsDuplicateKeyError(err error) bool {
switch mErr := err.(type) {
case *mysql.MySQLError:
if mErr.Number == 1062 {
return true
}
}
return false
}
func init() {
dbhelper.Register(mysqlHelper(0))
}