-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_sqls.go
99 lines (86 loc) · 1.93 KB
/
split_sqls.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
package gofixtures
import (
"io"
"strings"
)
func splitSqls(r io.Reader) (sqls []string) {
s := NewScanner(r)
s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
// fmt.Printf("data left = %q\n\n\n", data)
inQuote := false
inLineComment := false
inBlockComment := false
for i := 0; i < len(data); i++ {
// fmt.Print(i, ",")
if len(data) > i+2 {
if string(data[i:i+3]) == "\n--" || (i == 0 && string(data[i:i+2]) == "--") {
inLineComment = true
}
}
if len(data) > i+1 {
if string(data[i:i+2]) == "\n#" || (i == 0 && string(data[i:i+1]) == "#") {
inLineComment = true
}
}
if inLineComment {
if len(data) > i+1 && data[i+1] == '\n' {
inLineComment = false
advance = i + 1
token = []byte{}
// fmt.Printf("skipping inline comment: %q\n", data[:i+1])
return
} else {
continue
}
}
if len(data) > i+2 {
if string(data[i:i+3]) == "\n/*" || (i == 0 && string(data[i:i+2]) == "/*") {
inBlockComment = true
}
}
if inBlockComment {
if len(data) > i+3 && (string(data[i:i+4]) == "*/;\n" || string(data[i:i+4]) == "*/\n") {
inBlockComment = false
advance = i + 4
// fmt.Printf("skipping block comment: %q\n", data[:i+4])
token = []byte{}
return
} else {
continue
}
}
if data[i] == '\'' {
if inQuote {
inQuote = false
} else {
inQuote = true
}
}
if data[i] == ';' && !inQuote {
advance = i + 1
token = data[:i]
// fmt.Printf("found token: %q\n", token)
return
}
if atEOF && i+1 == len(data) {
token = data[:i]
advance = i + 1
// fmt.Printf("found token last: %q\n", token)
return
}
}
return
})
for s.Scan() {
sql := strings.TrimSpace(s.Text())
// fmt.Println("token!!!! =", string(sql))
if len(sql) == 0 {
continue
}
sqls = append(sqls, sql)
}
if s.Err() != nil {
panic(s.Err())
}
return
}