-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathversion.go
More file actions
94 lines (83 loc) · 2.6 KB
/
version.go
File metadata and controls
94 lines (83 loc) · 2.6 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package proxy
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/xelabs/go-mysqlstack/sqldb"
)
var (
defaultMySQLVersionStr = "5.7.25"
defaultMySQLVersion = serverVersion{5, 7, 25, ""}
authenticationMySQLVersion = serverVersion{5, 7, 0, ""}
versionRegex = regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)`)
)
type serverVersion struct {
Major, Minor, Patch int
Tag string
}
func (v *serverVersion) atLeast(compare serverVersion) bool {
if v.Major > compare.Major {
return true
}
if v.Major == compare.Major && v.Minor > compare.Minor {
return true
}
if v.Major == compare.Major && v.Minor == compare.Minor && v.Patch >= compare.Patch {
return true
}
return false
}
func (v *serverVersion) equal(compare serverVersion) bool {
if v.Major == compare.Major && v.Minor == compare.Minor && v.Patch == compare.Patch {
return true
}
return false
}
func (v *serverVersion) toStr() string {
vStr := strconv.Itoa(v.Major) + "." + strconv.Itoa(v.Minor) + "." + strconv.Itoa(v.Patch) + "-" + v.Tag
return vStr
}
// parseVersionString parse the string of version.
func parseVersionString(version string, withTag bool) (ver serverVersion, err error) {
if withTag {
versions := strings.SplitN(version, "-", 2)
if len(versions) > 1 {
ver.Tag = versions[1]
}
}
v := versionRegex.FindStringSubmatch(version)
if len(v) != 4 {
return ver, fmt.Errorf("could not parse server version from: %s", version)
}
ver.Major, err = strconv.Atoi(string(v[1]))
if err != nil {
return ver, fmt.Errorf("could not parse server version from: %s", version)
}
ver.Minor, err = strconv.Atoi(string(v[2]))
if err != nil {
return ver, fmt.Errorf("could not parse server version from: %s", version)
}
ver.Patch, err = strconv.Atoi(string(v[3]))
if err != nil {
return ver, fmt.Errorf("could not parse server version from: %s", version)
}
return
}
// getBackendVersion get the backend MySQL version
func getBackendVersion(spanner *Spanner) (version serverVersion, err error) {
log := spanner.log
versionQuery := fmt.Sprintf("select version() as version")
vr, err := spanner.ExecuteSingle(versionQuery)
if err != nil || len(vr.Rows) == 0 {
log.Error("proxy: get MySQL version error:%+v", err)
return version, sqldb.NewSQLErrorf(sqldb.CR_VERSION_ERROR, "Cann't get MySQL version")
}
versionStr := vr.Rows[0][0].String()
backendVersion, err := parseVersionString(versionStr, false)
if err != nil {
log.Error("proxy: parse MySQL version error:%+v", err)
return version, sqldb.NewSQLErrorf(sqldb.CR_VERSION_ERROR, "Cann't get MySQL version")
}
return backendVersion, nil
}