-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_insert_values.go
52 lines (43 loc) · 1.23 KB
/
merge_insert_values.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
package gofixtures
import "strings"
func mergeInsertValues(sqls []string) (outs []string) {
lastInsertKey := ""
lastInsertSQL := ""
for _, sql := range sqls {
// log.Printf("sql: %#+v\n", sql)
upsql := strings.ToUpper(sql)
// table name contains VALUES
valuesIndex := strings.Index(upsql, " VALUES")
// ++ let valuesIndex point to V
valuesIndex++
if strings.Index(upsql, "INSERT INTO") >= 0 && valuesIndex > 0 {
key := strings.TrimSpace(sql[0 : valuesIndex+6])
insertValues := strings.TrimSpace(sql[valuesIndex+6 : len(sql)])
if lastInsertKey == key {
lastInsertSQL = lastInsertSQL + ",\n" + insertValues
continue
}
// log.Printf("lastInsertKey: %#+v\n", lastInsertKey)
// log.Printf("key: %#+v\n", key)
// log.Printf("lastInsertSQL: %#+v\n", lastInsertSQL)
if len(lastInsertKey) == 0 || lastInsertKey != key {
if len(lastInsertSQL) > 0 {
outs = append(outs, lastInsertSQL)
}
lastInsertKey = key
lastInsertSQL = key + "\n" + insertValues
continue
}
}
if len(lastInsertSQL) > 0 {
outs = append(outs, lastInsertSQL)
lastInsertKey = ""
lastInsertSQL = ""
}
outs = append(outs, sql)
}
if len(lastInsertSQL) > 0 {
outs = append(outs, lastInsertSQL)
}
return
}