forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
192 lines (159 loc) · 3.82 KB
/
parser.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package dump
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/pingcap/errors"
"github.com/cookieY/go-mysql/mysql"
)
var (
ErrSkip = errors.New("Handler error, but skipped")
)
type ParseHandler interface {
// Parse CHANGE MASTER TO MASTER_LOG_FILE=name, MASTER_LOG_POS=pos;
BinLog(name string, pos uint64) error
Data(schema string, table string, values []string) error
}
var binlogExp *regexp.Regexp
var useExp *regexp.Regexp
var valuesExp *regexp.Regexp
func init() {
binlogExp = regexp.MustCompile("^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\\d+);")
useExp = regexp.MustCompile("^USE `(.+)`;")
valuesExp = regexp.MustCompile("^INSERT INTO `(.+?)` VALUES \\((.+)\\);$")
}
// Parse the dump data with Dumper generate.
// It can not parse all the data formats with mysqldump outputs
func Parse(r io.Reader, h ParseHandler, parseBinlogPos bool) error {
rb := bufio.NewReaderSize(r, 1024*16)
var db string
var binlogParsed bool
for {
line, err := rb.ReadString('\n')
if err != nil && err != io.EOF {
return errors.Trace(err)
} else if mysql.ErrorEqual(err, io.EOF) {
break
}
// Ignore '\n' on Linux or '\r\n' on Windows
line = strings.TrimRightFunc(line, func(c rune) bool {
return c == '\r' || c == '\n'
})
if parseBinlogPos && !binlogParsed {
if m := binlogExp.FindAllStringSubmatch(line, -1); len(m) == 1 {
name := m[0][1]
pos, err := strconv.ParseUint(m[0][2], 10, 64)
if err != nil {
return errors.Errorf("parse binlog %v err, invalid number", line)
}
if err = h.BinLog(name, pos); err != nil && err != ErrSkip {
return errors.Trace(err)
}
binlogParsed = true
}
}
if m := useExp.FindAllStringSubmatch(line, -1); len(m) == 1 {
db = m[0][1]
}
if m := valuesExp.FindAllStringSubmatch(line, -1); len(m) == 1 {
table := m[0][1]
values, err := parseValues(m[0][2])
if err != nil {
return errors.Errorf("parse values %v err", line)
}
if err = h.Data(db, table, values); err != nil && err != ErrSkip {
return errors.Trace(err)
}
}
}
return nil
}
func parseValues(str string) ([]string, error) {
// values are seperated by comma, but we can not split using comma directly
// string is enclosed by single quote
// a simple implementation, may be more robust later.
values := make([]string, 0, 8)
i := 0
for i < len(str) {
if str[i] != '\'' {
// no string, read until comma
j := i + 1
for ; j < len(str) && str[j] != ','; j++ {
}
values = append(values, str[i:j])
// skip ,
i = j + 1
} else {
// read string until another single quote
j := i + 1
escaped := false
for j < len(str) {
if str[j] == '\\' {
// skip escaped character
j += 2
escaped = true
continue
} else if str[j] == '\'' {
break
} else {
j++
}
}
if j >= len(str) {
return nil, fmt.Errorf("parse quote values error")
}
value := str[i : j+1]
if escaped {
value = unescapeString(value)
}
values = append(values, value)
// skip ' and ,
i = j + 2
}
// need skip blank???
}
return values, nil
}
// unescapeString un-escapes the string.
// mysqldump will escape the string when dumps,
// Refer http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
func unescapeString(s string) string {
i := 0
value := make([]byte, 0, len(s))
for i < len(s) {
if s[i] == '\\' {
j := i + 1
if j == len(s) {
// The last char is \, remove
break
}
value = append(value, unescapeChar(s[j]))
i += 2
} else {
value = append(value, s[i])
i++
}
}
return string(value)
}
func unescapeChar(ch byte) byte {
// \" \' \\ \n \0 \b \Z \r \t ==> escape to one char
switch ch {
case 'n':
ch = '\n'
case '0':
ch = 0
case 'b':
ch = 8
case 'Z':
ch = 26
case 'r':
ch = '\r'
case 't':
ch = '\t'
}
return ch
}