-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqueries.go
58 lines (46 loc) · 1.1 KB
/
queries.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
package qry
import (
"bytes"
"strings"
)
// QuerySet represents set of queries
type QuerySet map[string]Query
// Query represents single query from a .sql file
type Query string
// Replace part of a query
func (q Query) Replace(o, r string) string {
if len(o) == 0 || len(r) == 0 {
return string(q)
}
return strings.Replace(string(q), o, r, 1)
}
func removeMultilineComments(q []byte) []byte {
for {
index := bytes.IndexAny(q, "/*")
if index == -1 {
return q
}
closeIndex := bytes.Index(q, []byte("*/"))
if closeIndex == -1 {
return q
}
q = append(q[:index], q[closeIndex+2:]...)
}
}
func normalize(q []byte) Query {
q = bytes.TrimSpace(q)
q = removeMultilineComments(q)
q = rgxLineComment.ReplaceAll(q, []byte(" "))
q = bytes.Replace(q, []byte("\n"), []byte(" "), -1)
q = rgxMultiSpace.ReplaceAll(q, []byte(" "))
q = bytes.Replace(q, []byte("\\"), []byte("\\\\"), -1)
q = bytes.Replace(q, []byte("\""), []byte("\\\""), -1)
return Query(q)
}
// In returns string with N sql query placeholders
func In(l int) string {
if l <= 0 {
return ""
}
return strings.Repeat(ph, l)[1:]
}