forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·34 lines (29 loc) · 1.02 KB
/
main.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
// Package sqlutils contains utility functions for manipulating strings of SQL
package sqlutils
import (
"regexp"
"strings"
)
// AllStatements takes a sql script, possibly containing comments and multiple
// statements, and returns a slice of strings that correspond to each individual
// SQL statement within the script
func AllStatements(script string) (ret []string) {
for _, s := range strings.Split(RemoveComments(script), ";") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
ret = append(ret, s)
}
return
}
// RemoveComments trims out any comment blocks or lines from the provided SQL
// script
func RemoveComments(script string) string {
withoutBlocks := sqlBlockComments.ReplaceAllString(script, "")
return sqlLineComments.ReplaceAllString(withoutBlocks, "")
}
// SQLBlockComments is a regex that matches against SQL block comments
var sqlBlockComments = regexp.MustCompile(`/\*.*?\*/`)
// SQLLineComments is a regex that matches against SQL line comments
var sqlLineComments = regexp.MustCompile("--.*?\n")