forked from marianogappa/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.go
More file actions
238 lines (222 loc) · 4.69 KB
/
Copy pathquery.go
File metadata and controls
238 lines (222 loc) · 4.69 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package sqlparser
import (
"fmt"
"strings"
)
// Query represents a parsed query
type Query struct {
Type Type
TableName string
Conditions []Condition
Updates map[string]string
Inserts [][]string
Fields []string // Used for SELECT (i.e. SELECTed field names) and INSERT (INSERTEDed field names)
Aliases map[string]string
CreateFields map[string]string // name1 type, name2 type ...
}
func (q Query) String() string {
var sb strings.Builder
switch q.Type {
case Select:
sb.WriteString("SELECT ")
if len(q.Fields) > 0 {
for i, field := range q.Fields {
sb.WriteString(field)
if alias, ok := q.Aliases[field]; ok {
sb.WriteString(" AS ")
sb.WriteString(alias)
}
if i < len(q.Fields)-1 {
sb.WriteString(", ")
}
}
} else {
sb.WriteString("*")
}
sb.WriteString(" FROM ")
sb.WriteString(q.TableName)
case Insert:
sb.WriteString("INSERT INTO ")
sb.WriteString(q.TableName)
sb.WriteString(" (")
sb.WriteString(strings.Join(q.Fields, ", "))
sb.WriteString(") VALUES ")
for i, row := range q.Inserts {
sb.WriteString("('")
sb.WriteString(strings.Join(row, "', '"))
sb.WriteString("')")
if i < len(q.Inserts)-1 {
sb.WriteString(", ")
}
}
case Update:
sb.WriteString("UPDATE ")
sb.WriteString(q.TableName)
sb.WriteString(" SET ")
i := 0
for field, value := range q.Updates {
sb.WriteString(field)
sb.WriteString(" = '")
sb.WriteString(value)
sb.WriteString("'")
if i < len(q.Updates)-1 {
sb.WriteString(", ")
}
i++
}
case Delete:
sb.WriteString("DELETE FROM ")
sb.WriteString(q.TableName)
case Create:
sb.WriteString("CREATE TABLE ")
sb.WriteString(q.TableName)
sb.WriteString(" (")
i := 0
for field, fieldType := range q.CreateFields {
sb.WriteString(field)
sb.WriteString(" ")
sb.WriteString(fieldType)
if i < len(q.CreateFields)-1 {
sb.WriteString(", ")
}
i++
}
sb.WriteString(")")
default:
return ""
}
if len(q.Conditions) > 0 {
sb.WriteString(" WHERE ")
for i, cond := range q.Conditions {
if cond.Operand1IsField {
sb.WriteString(cond.Operand1)
} else {
sb.WriteString(fmt.Sprintf("'%s'", cond.Operand1))
}
sb.WriteString(" ")
sb.WriteString(cond.Operator.String())
sb.WriteString(" ")
if cond.Operator == In || cond.Operator == NotIn {
sb.WriteString("('")
sb.WriteString(strings.Join(cond.InValues, "', '"))
sb.WriteString("')")
} else {
if cond.Operand2IsField {
sb.WriteString(cond.Operand2)
} else {
sb.WriteString(fmt.Sprintf("'%s'", cond.Operand2))
}
}
if i < len(q.Conditions)-1 {
sb.WriteString(" AND ")
}
}
}
return sb.String()
}
// Type is the type of SQL query, e.g. SELECT/UPDATE
type Type int
const (
// UnknownType is the zero value for a Type
UnknownType Type = iota
// Select represents a SELECT query
Select
// Update represents an UPDATE query
Update
// Insert represents an INSERT query
Insert
// Delete represents a DELETE query
Delete
// Create
Create
)
// TypeString is a string slice with the names of all types in order
var TypeString = []string{
"UnknownType",
"Select",
"Update",
"Insert",
"Delete",
"Create",
}
// Operator is between operands in a condition
type Operator int
func (i Operator) String() string {
switch i {
case Eq:
return "="
case Ne:
return "!="
case Gt:
return ">"
case Lt:
return "<"
case Gte:
return ">="
case Lte:
return "<="
case Like:
return "LIKE"
case NotLike:
return "NOT LIKE"
case In:
return "IN"
case NotIn:
return "NOT IN"
default:
return "UnknownOperator"
}
}
const (
// UnknownOperator is the zero value for an Operator
UnknownOperator Operator = iota
// Eq -> "="
Eq
// Ne -> "!="
Ne
// Gt -> ">"
Gt
// Lt -> "<"
Lt
// Gte -> ">="
Gte
// Lte -> "<="
Lte
// Like -> "LIKE"
Like
// NotLike -> "NOT LIKE"
NotLike
// In -> "IN"
In
// NotIn -> "NOT IN"
NotIn
)
// OperatorString is a string slice with the names of all operators in order
var OperatorString = []string{
"UnknownOperator",
"Eq",
"Ne",
"Gt",
"Lt",
"Gte",
"Lte",
"Like",
"NotLike",
"In",
"NotIn",
}
// Condition is a single boolean condition in a WHERE clause
type Condition struct {
// Operand1 is the left hand side operand
Operand1 string
// Operand1IsField determines if Operand1 is a literal or a field name
Operand1IsField bool
// Operator is e.g. "=", ">", "LIKE", "IN"
Operator Operator
// Operand2 is the right hand side operand (for LIKE, IN this can be a single value or list)
Operand2 string
// Operand2IsField determines if Operand2 is a literal or a field name
Operand2IsField bool
// InValues holds the list of values for IN operator
InValues []string
}