Skip to content

Commit

Permalink
refactor build-table
Browse files Browse the repository at this point in the history
  • Loading branch information
larsnovikov committed Apr 28, 2019
1 parent 2644f31 commit a9f691a
Showing 1 changed file with 60 additions and 39 deletions.
99 changes: 60 additions & 39 deletions src/tools/system/build-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,45 +97,66 @@ func parseLine(c chan string) {
for {
line := <-c

re := regexp.MustCompile(InsertRegexp)
match := re.FindStringSubmatch(line)
if len(match) > 0 {
// TODO fix me
r := strings.NewReplacer("VALUES", "",
"'", "",
"(", "",
")", "")

params := strings.Split(strings.TrimSpace(r.Replace(match[0])), ",")

slave.GetSlaveByName(helpers2.Table).ClearParams()

interfaceParams := make([]interface{}, len(params))
for i := range params {
interfaceParams[i] = params[i]
}
err := parser.ParseRow(slave.GetSlaveByName(helpers2.Table), interfaceParams)
if err != nil {
log.Fatalf(constants.ErrorParseLine, line, err)
}

header, positionSet := helpers2.GetHeader()

slave.GetSlaveByName(helpers2.Table).Insert(&header, positionSet)
} else {
// parse position
re = regexp.MustCompile(PositionRegexp)
match = re.FindStringSubmatch(line)

if len(match) > 0 {
pos, _ := strconv.Atoi(match[2])
helpers2.Position = mysql.Position{
Name: match[1],
Pos: uint32(pos),
}

helpers2.SetPosition()
}
// try to parse like insert
if parseInsert(line) == true {
continue
}

// try to parse like position setter
if parsePosition(line) == true {
continue
}
}
}

func parseInsert(line string) bool {
re := regexp.MustCompile(InsertRegexp)
match := re.FindStringSubmatch(line)
if len(match) > 0 {
// TODO fix me
r := strings.NewReplacer("VALUES", "",
"'", "",
"(", "",
")", "")

params := strings.Split(strings.TrimSpace(r.Replace(match[0])), ",")

slave.GetSlaveByName(helpers2.Table).ClearParams()

interfaceParams := make([]interface{}, len(params))
for i := range params {
interfaceParams[i] = params[i]
}
err := parser.ParseRow(slave.GetSlaveByName(helpers2.Table), interfaceParams)
if err != nil {
log.Fatalf(constants.ErrorParseLine, line, err)
}

header, positionSet := helpers2.GetHeader()

slave.GetSlaveByName(helpers2.Table).Insert(&header, positionSet)

return true
}

return false
}

func parsePosition(line string) bool {
re := regexp.MustCompile(PositionRegexp)
match := re.FindStringSubmatch(line)

if len(match) > 0 {
pos, _ := strconv.Atoi(match[2])
helpers2.Position = mysql.Position{
Name: match[1],
Pos: uint32(pos),
}

helpers2.SetPosition()

return true
}

return false
}

0 comments on commit a9f691a

Please sign in to comment.