Skip to content

Commit a9f691a

Browse files
committed
refactor build-table
1 parent 2644f31 commit a9f691a

File tree

1 file changed

+60
-39
lines changed

1 file changed

+60
-39
lines changed

src/tools/system/build-table.go

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -97,45 +97,66 @@ func parseLine(c chan string) {
9797
for {
9898
line := <-c
9999

100-
re := regexp.MustCompile(InsertRegexp)
101-
match := re.FindStringSubmatch(line)
102-
if len(match) > 0 {
103-
// TODO fix me
104-
r := strings.NewReplacer("VALUES", "",
105-
"'", "",
106-
"(", "",
107-
")", "")
108-
109-
params := strings.Split(strings.TrimSpace(r.Replace(match[0])), ",")
110-
111-
slave.GetSlaveByName(helpers2.Table).ClearParams()
112-
113-
interfaceParams := make([]interface{}, len(params))
114-
for i := range params {
115-
interfaceParams[i] = params[i]
116-
}
117-
err := parser.ParseRow(slave.GetSlaveByName(helpers2.Table), interfaceParams)
118-
if err != nil {
119-
log.Fatalf(constants.ErrorParseLine, line, err)
120-
}
121-
122-
header, positionSet := helpers2.GetHeader()
123-
124-
slave.GetSlaveByName(helpers2.Table).Insert(&header, positionSet)
125-
} else {
126-
// parse position
127-
re = regexp.MustCompile(PositionRegexp)
128-
match = re.FindStringSubmatch(line)
129-
130-
if len(match) > 0 {
131-
pos, _ := strconv.Atoi(match[2])
132-
helpers2.Position = mysql.Position{
133-
Name: match[1],
134-
Pos: uint32(pos),
135-
}
136-
137-
helpers2.SetPosition()
138-
}
100+
// try to parse like insert
101+
if parseInsert(line) == true {
102+
continue
139103
}
104+
105+
// try to parse like position setter
106+
if parsePosition(line) == true {
107+
continue
108+
}
109+
}
110+
}
111+
112+
func parseInsert(line string) bool {
113+
re := regexp.MustCompile(InsertRegexp)
114+
match := re.FindStringSubmatch(line)
115+
if len(match) > 0 {
116+
// TODO fix me
117+
r := strings.NewReplacer("VALUES", "",
118+
"'", "",
119+
"(", "",
120+
")", "")
121+
122+
params := strings.Split(strings.TrimSpace(r.Replace(match[0])), ",")
123+
124+
slave.GetSlaveByName(helpers2.Table).ClearParams()
125+
126+
interfaceParams := make([]interface{}, len(params))
127+
for i := range params {
128+
interfaceParams[i] = params[i]
129+
}
130+
err := parser.ParseRow(slave.GetSlaveByName(helpers2.Table), interfaceParams)
131+
if err != nil {
132+
log.Fatalf(constants.ErrorParseLine, line, err)
133+
}
134+
135+
header, positionSet := helpers2.GetHeader()
136+
137+
slave.GetSlaveByName(helpers2.Table).Insert(&header, positionSet)
138+
139+
return true
140140
}
141+
142+
return false
143+
}
144+
145+
func parsePosition(line string) bool {
146+
re := regexp.MustCompile(PositionRegexp)
147+
match := re.FindStringSubmatch(line)
148+
149+
if len(match) > 0 {
150+
pos, _ := strconv.Atoi(match[2])
151+
helpers2.Position = mysql.Position{
152+
Name: match[1],
153+
Pos: uint32(pos),
154+
}
155+
156+
helpers2.SetPosition()
157+
158+
return true
159+
}
160+
161+
return false
141162
}

0 commit comments

Comments
 (0)