-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.go
More file actions
449 lines (369 loc) · 8.78 KB
/
command.go
File metadata and controls
449 lines (369 loc) · 8.78 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package kdb
import (
"fmt"
"github.com/sdming/kdb/ansi"
)
const nilStr string = "<nil>"
// Parameter is parameter of sql statement or store procedure
type Parameter struct {
// Name is parameter name, some driver don't support named parameter
Name string
// Value is value of this parameter
Value interface{}
// // DbType is data type
// DbType ansi.DbType
// Dir is direction, in,out, inout or return
Dir ansi.Dir
}
// String
func (p *Parameter) String() string {
if p == nil {
return nilStr
}
return fmt.Sprint(p.Name, " = ", p.Value)
}
// IsIn return true if parameter is input/inputoutput parameter
func (p *Parameter) IsIn() bool {
return p.Dir == ansi.DirIn || p.Dir == ansi.DirInOut
}
// IsOut return true if parameter is output/inputoutput parameter
func (p *Parameter) IsOut() bool {
return p.Dir == ansi.DirOut || p.Dir == ansi.DirInOut
}
// Node return NodeParameter
func (p *Parameter) Node() NodeType {
return NodeParameter
}
// Text is sql statement
type Text struct {
// Sql is raw sql statement
Sql string
// Parameters is parameters of Sql
Parameters []*Parameter
}
// String
func (t *Text) String() string {
if t == nil {
return nilStr
}
return fmt.Sprint(t.Sql, t.Parameters)
}
// Node return NodeText
func (t *Text) Node() NodeType {
return NodeText
}
// Set is shortcut of Parameter
func (t *Text) Set(name string, value interface{}) *Text {
t.Parameter(&Parameter{Name: name, Value: value})
return t
}
// Parameter append a paramter
func (t *Text) Parameter(p *Parameter) {
if p == nil {
return
}
if t.Parameters == nil {
t.Parameters = make([]*Parameter, 0, _defaultCapicity)
}
t.Parameters = append(t.Parameters, p)
}
// FindParameter return a paramter by name
func (t *Text) FindParameter(name string) (*Parameter, bool) {
l := len(t.Parameters)
for i := 0; i < l; i++ {
p := t.Parameters[i]
if p.Name == name {
return p, true
}
}
return nil, false
}
// NewText return a *Text with provided sql statement
func NewText(sql string) *Text {
return &Text{Sql: sql}
}
// Procedure is sql store procedure
type Procedure struct {
// Name is name of store procedure ot function
Name string
// Parameters is parameters of store procedure
Parameters []*Parameter
}
// String
func (pc *Procedure) String() string {
if pc == nil {
return nilStr
}
return fmt.Sprint(pc.Name, pc.Parameters)
}
// Node return NodeProcedure
func (pc *Procedure) Node() NodeType {
return NodeProcedure
}
// Set is shortcut of Parameter
func (pc *Procedure) Set(name string, value interface{}) *Procedure {
pc.Parameter(&Parameter{Name: name, Value: value})
return pc
}
// SetDir append a Parameter
func (pc *Procedure) SetDir(name string, value interface{}, dir ansi.Dir) *Procedure {
pc.Parameter(&Parameter{Name: name, Value: value, Dir: dir})
return pc
}
// ReturnParameterName return parameter name if parameter is ansi.DirReturn
func (pc *Procedure) ReturnParameterName() string {
l := len(pc.Parameters)
for i := 0; i < l; i++ {
p := pc.Parameters[i]
if p.Dir == ansi.DirReturn {
return p.Name
}
}
return ""
}
// HasOutParameter return true if any parameter is output/inputoutput
func (t *Procedure) HasOutParameter() bool {
for i := 0; i < len(t.Parameters); i++ {
if t.Parameters[i].IsOut() {
return true
}
}
return false
}
// FindParameter return a parameter by name
func (pc *Procedure) FindParameter(name string) (*Parameter, bool) {
l := len(pc.Parameters)
for i := 0; i < l; i++ {
p := pc.Parameters[i]
if p.Name == name {
return p, true
}
}
return nil, false
}
// Parameter append a paramter
func (pc *Procedure) Parameter(p *Parameter) {
if p == nil {
return
}
if pc.Parameters == nil {
pc.Parameters = make([]*Parameter, 0, _defaultCapicity)
}
pc.Parameters = append(pc.Parameters, p)
}
// NewProcedure return a *Procedure with provided name
func NewProcedure(name string) *Procedure {
return &Procedure{Name: name}
}
// Insert is sql "insert into x values(...)" clause
type Insert struct {
// Table is table to insert
Table *Table
// Sets is set[column=value]
Sets []*Set
}
// String
func (ist *Insert) String() string {
if ist == nil {
return nilStr
}
return fmt.Sprint(ansi.Insert, " ", ist.Table, " ", ist.Sets)
}
// Node return NodeInsert
func (ist *Insert) Node() NodeType {
return NodeInsert
}
// Set is shortcut of Append
func (ist *Insert) Set(column string, value interface{}) *Insert {
ist.Append(newSet(column, asExpression(value)))
return ist
}
// Append Append an *Set
func (ist *Insert) Append(a *Set) {
if a == nil {
return
}
if ist.Sets == nil {
ist.Sets = make([]*Set, 0, _defaultCapicity)
}
ist.Sets = append(ist.Sets, a)
}
// NewInsert return *Insert with provided table
func NewInsert(table string) *Insert {
return &Insert{Table: newTable(table, ""), Sets: make([]*Set, 0, _defaultCapicity)}
}
// Update is sql update clause
type Update struct {
//T able is table to update
Table *Table
// Sets is set[column=value]
Sets []*Set
// Where is where clause
Where *Where
// OrderBy is order by clause
OrderBy *OrderBy
// Count is limit count
Count int
//Output *Output
}
// String
func (u *Update) String() string {
if u == nil {
return nilStr
}
return fmt.Sprint(ansi.Update, " ", u.Table, " ", ansi.Set, " ", u.Sets, "\n", u.Where, "\n", u.OrderBy, "\n", ansi.Limit, u.Count)
}
// Node return NodeUpdate
func (u *Update) Node() NodeType {
return NodeUpdate
}
// Set is shortcut of Append
func (u *Update) Set(column string, value interface{}) *Update {
u.Append(newSet(column, asExpression(value)))
return u
}
// Append Append an *Set
func (u *Update) Append(a *Set) {
if u.Sets == nil {
u.Sets = make([]*Set, 0, _defaultCapicity)
}
u.Sets = append(u.Sets, a)
}
// Limit set rows count to update
func (u *Update) Limit(count int) *Update {
u.Count = count
return u
}
// NotImplemented
// func (u *Update) Output(sql string) *Update {
// u.Output = newOutput(sql)
// return u
// }
func NewUpdate(table string) *Update {
return &Update{
Table: newTable(table, ""),
Sets: make([]*Set, 0, _defaultCapicity),
Where: NewWhere(),
OrderBy: &OrderBy{},
}
}
// Delete is sql delete clause
type Delete struct {
//Table is the table to delete
Table *Table
// From is from clause
From *From
// Where is where clause
Where *Where
// OrderBy is order by clause
OrderBy *OrderBy
// Count is limit count
Count int
//Output *Output
}
// String
func (d *Delete) String() string {
if d == nil {
return nilStr
}
return fmt.Sprint(ansi.Delete, " ", d.Table, "\n", d.From, "\n", d.Where, "\n", d.OrderBy, "\n", ansi.Limit, d.Count)
}
// Node return NodeDelete
func (d *Delete) Node() NodeType {
return NodeDelete
}
// Limit set rows count to delete
func (d *Delete) Limit(count int) *Delete {
d.Count = count
return d
}
// UseFrom new a *From and set to d.From
func (d *Delete) UseFrom(table, alias string) *From {
d.From = NewFrom(table, alias)
return d.From
}
// UseOrderBy new a *OrderBy and set to d.OrderBy
func (d *Delete) UseOrderBy() *OrderBy {
d.OrderBy = NewOrderBy()
return d.OrderBy
}
// NotImplemented
// func (d *Delete) Output(sql string) *Delete {
// d.Output = newOutput(sql)
// return d
// }
// NewDelete return a *Delete with provided table
func NewDelete(table string) *Delete {
return &Delete{
Table: newTable(table, ""),
Where: NewWhere(),
OrderBy: NewOrderBy(),
}
}
// Query is sql query clause
type Query struct {
Select *Select
From *From
Where *Where
GroupBy *GroupBy
Having *Having
OrderBy *OrderBy
IsDistinct bool
Offset int
Count int
}
// String
func (q *Query) String() string {
if q == nil {
return nilStr
}
distinct := ""
if q.IsDistinct {
distinct = ansi.Distinct
}
return fmt.Sprint(ansi.Select, " ", distinct, " ", q.Select, "\n", q.From, "\n", q.Where, q.GroupBy, "\n", q.Having, "\n", q.OrderBy, "\n", ansi.Limit, q.Offset, q.Count)
}
// Node return NodeQuery
func (q *Query) Node() NodeType {
return NodeQuery
}
// Limit set offset and count
func (q *Query) Limit(offset, count int) *Query {
q.Offset = offset
q.Count = count
return q
}
// Distinct set IsDistinct = true
func (q *Query) Distinct() *Query {
q.IsDistinct = true
return q
}
// UseGroupBy initialize q.GroupBy then return it
func (q *Query) UseGroupBy() *GroupBy {
if q.GroupBy == nil {
q.GroupBy = NewGroupBy()
}
return q.GroupBy
}
// UseHaving initialize q.Having then return it
func (q *Query) UseHaving() *Having {
if q.Having == nil {
q.Having = NewHaving()
}
return q.Having
}
// UseOrderBy initialize q.OrderBy then return it
func (q *Query) UseOrderBy() *OrderBy {
if q.OrderBy == nil {
q.OrderBy = NewOrderBy()
}
return q.OrderBy
}
// NewQuery return *Query
func NewQuery(table, alias string) *Query {
return &Query{
From: NewFrom(table, alias),
Where: NewWhere(),
Select: NewSelect(),
}
}