-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlizer.go
178 lines (147 loc) · 3.89 KB
/
sqlizer.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package queryme
import (
"bytes"
"fmt"
"strings"
)
// PredicateToSql converts a Predicate to its equivalent SQL form and extracts constant to a seperate array. Each referenced field is matched against a list of allowed fields.
func PredicateToSql(predicate Predicate, allowedFields []Field) (sql string, values []interface{}) {
b := newQueryBuilder(allowedFields)
predicate.Accept(b)
return b.Sql(), b.Values()
}
// SortOrderToSql converts an array of SortOrder to its equivalent SQL form. Each referenced field is matched against a list of allowed fields.
func SortOrderToSql(sortOrder []*SortOrder, allowedFields []Field) (sql string) {
b := newQueryBuilder(allowedFields)
for i, so := range sortOrder {
if i > 0 {
b.AppendSql(",")
}
b.AppendId(so.Field)
if !so.Ascending {
b.AppendSql(" DESC")
}
}
return b.Sql()
}
type queryBuilder struct {
VariablePlaceHolder string
EscapedIdentifierGenerator func(string)string
allowedFields map[Field]struct{}
sql *bytes.Buffer
values []interface{}
}
func newQueryBuilder(allowedFields []Field) *queryBuilder {
allowedFieldsIndex := make(map[Field]struct{})
for _, f := range allowedFields {
if _, ok := allowedFieldsIndex[f]; !ok {
allowedFieldsIndex[f] = struct{}{}
}
}
b := &queryBuilder{
"?",
func(id string) string { return "`" + strings.Replace(id, "`", "``", -1) + "`" },
allowedFieldsIndex,
bytes.NewBuffer(make([]byte, 0, 64)),
make([]interface{}, 0, 8)}
return b
}
func (b *queryBuilder) Sql() string {
return b.sql.String()
}
func (b *queryBuilder) Values() []interface{} {
return b.values
}
func (b *queryBuilder) AppendSql(sql string) {
b.sql.WriteString(sql)
}
func (b *queryBuilder) AppendId(id Field) {
if _, ok := b.allowedFields[id]; !ok {
panic(fmt.Errorf("unauthorized field accessed: %q", id))
}
b.sql.WriteString(b.EscapedIdentifierGenerator(string(id)))
}
func (b *queryBuilder) AppendValue(value interface{}) {
b.values = append(b.values, value)
}
func (b *queryBuilder) VisitNot(operand Predicate) {
b.AppendSql("(NOT ")
operand.Accept(b)
b.AppendSql(")")
}
func (b *queryBuilder) VisitPredicates(sqlOperator string, defaultValue string, operands []Predicate) {
b.AppendSql("(")
if len(operands) > 0 {
for i, p := range operands {
if i > 0 {
b.AppendSql(" ")
b.AppendSql(sqlOperator)
b.AppendSql(" ")
}
p.Accept(b)
}
} else {
b.AppendSql(defaultValue)
}
b.AppendSql(")")
}
func (b *queryBuilder) VisitAnd(operands []Predicate) {
b.VisitPredicates("AND", "true", operands)
}
func (b *queryBuilder) VisitOr(operands []Predicate) {
b.VisitPredicates("OR", "false", operands)
}
func (b *queryBuilder) VisitEq(field Field, operands []Value) {
switch len(operands) {
case 0:
b.AppendSql("false")
case 1:
b.AppendId(field)
b.AppendSql("=")
b.AppendSql(b.VariablePlaceHolder)
b.AppendValue(operands[0])
default:
b.AppendId(field)
b.AppendSql(" IN (")
for i, op := range operands {
if i > 0 {
b.AppendSql(",")
}
b.AppendSql(b.VariablePlaceHolder)
b.AppendValue(op)
}
b.AppendSql(")")
}
}
func (b *queryBuilder) VisitLt(field Field, operand Value) {
b.AppendId(field)
b.AppendSql("<")
b.AppendSql(b.VariablePlaceHolder)
b.AppendValue(operand)
}
func (b *queryBuilder) VisitLe(field Field, operand Value) {
b.AppendId(field)
b.AppendSql("<=")
b.AppendSql(b.VariablePlaceHolder)
b.AppendValue(operand)
}
func (b *queryBuilder) VisitGt(field Field, operand Value) {
b.AppendId(field)
b.AppendSql(">")
b.AppendSql(b.VariablePlaceHolder)
b.AppendValue(operand)
}
func (b *queryBuilder) VisitGe(field Field, operand Value) {
b.AppendId(field)
b.AppendSql(">=")
b.AppendSql(b.VariablePlaceHolder)
b.AppendValue(operand)
}
func (b *queryBuilder) VisitFts(field Field, query string) {
b.AppendSql("MATCH (")
b.AppendId(field)
b.AppendSql(") AGAINST (")
b.AppendSql(b.VariablePlaceHolder)
b.AppendSql(")")
b.AppendValue(query)
}