-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.go
More file actions
187 lines (163 loc) · 3.49 KB
/
Copy pathsql.go
File metadata and controls
187 lines (163 loc) · 3.49 KB
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
179
180
181
182
183
184
185
186
187
package funnel
import (
"fmt"
"github.com/mchaynes/funnel/internal/squirrel"
"github.com/mchaynes/funnel/lex"
)
type BoolOp string
const (
And BoolOp = "AND"
Or BoolOp = "OR"
Invalid BoolOp = "INVALID"
)
// ToSql accepts FQL and returns a valid SQL statement
func ToSql(input string, fieldDict []string) (string, []interface{}, error) {
lexer := lex.NewLexer(input, fieldDict)
root, err := buildTree(lexer)
if err != nil {
return "", nil, err
}
sql, args := root.ToSql()
return sql, args, nil
}
type Format int
const (
Dollar Format = iota
Colon
)
func Replace(sql string, format Format) (string, error) {
switch format{
case Dollar:
return squirrel.Dollar.ReplacePlaceholders(sql)
case Colon:
return squirrel.Colon.ReplacePlaceholders(sql)
default:
panic("illegal format") // caller code is messed up, don't return error, panic
}
}
type sqler interface {
ToSql() (string, []interface{})
}
type root struct {
sqler sqler
}
func (r *root) ToSql() (string, []interface{}) {
return r.sqler.ToSql()
}
type boolean struct {
operation BoolOp
children []sqler
}
func (b *boolean) build(l *lex.Lexer) error {
for item := l.NextItem(); item != nil; item = l.NextItem(){
switch item.Type{
case lex.BooleanType:
b.operation = toBoolOp(item.Value)
if b.operation == Invalid {
return fmt.Errorf("%q is an invalid operation", item.Value)
}
case lex.OpenPrecedenceType:
newBool := boolean{}
err := newBool.build(l)
if err != nil {
return err
}
b.children = append(b.children, &newBool)
case lex.ClosePrecedenceType:
return nil
case lex.LiteralType, lex.FieldType:
newField := field{}
err := newField.set(*item)
if err != nil {
return err
}
err = newField.build(l)
if err != nil {
return err
}
b.children = append(b.children, &newField)
default:
return fmt.Errorf("invalid syntax")
}
}
return nil
}
func (f *field) build(l *lex.Lexer) error {
for i := 0; i < 2; i++ {
item := l.NextItem()
if item == nil {
return fmt.Errorf("unexpected eof")
}
if err := f.set(*item); err != nil {
return err
}
}
return nil
}
func (f *field) set(item lex.Item) error {
switch item.Type {
case lex.ComparatorType:
f.comp = item.Value
case lex.FieldType:
f.field = item.Value
case lex.LiteralType:
f.val = item.Value
default:
return fmt.Errorf("error parsing %v", item.Type)
}
return nil
}
func (b *boolean) ToSql() (string, []interface{}) {
var (
sql string
args []interface{}
)
sql = "("
for i, c := range b.children {
cSql, cArgs := c.ToSql()
if i > 0 {
sql = fmt.Sprintf("%s %s %s", sql, b.operation, cSql)
} else {
sql = fmt.Sprintf("%s %s", sql, cSql)
}
args = append(args, cArgs...)
}
sql = fmt.Sprintf("%s )", sql)
return sql, args
}
type field struct {
field string
comp string
val string
}
func (f *field) ToSql() (string, []interface{}) {
if len(f.comp) > 2 {
// ok to panic here, the lexer should have returned an error, so this state is very invalid and indicative of a bug
panic(fmt.Sprintf("field's comparator was longer than 2 characters: %q", f.comp))
}
return fmt.Sprintf("%s %s ?", f.field, f.comp), []interface{}{f.val}
}
func buildTree(l *lex.Lexer) (*root, error) {
r := &root{}
b := boolean{}
err := b.build(l)
if err != nil {
return nil, err
}
r.sqler = &b
return r, nil
}
func toBoolOp(b string) BoolOp {
switch b {
case string(And):
return And
case string(Or):
return Or
case lex.Or:
return Or
case lex.And:
return And
default:
return Invalid
}
}