/
select.go
174 lines (167 loc) · 3.57 KB
/
select.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
// select.go 文件提供以 Sel 类型为核心的 Select 语句生成功能
//
// NOTE
//
// Table 的 name 应该是模型的类型名而非表名,表名只在注册模型的时候显式出现
//
package exp
import (
"fmt"
"strings"
)
// desc 应该能作用到确定的排序字段
type Sel struct {
selects []Exp
from *Table
join []Exp
leftjoin []Exp
where Exp
groupby []Exp
having Exp
orderby []Exp
limit *int
offset *int
}
func SelectThem(fields ...string) *Sel {
var fs = make([]Exp, 0)
for _, fname := range fields {
fs = append(fs, &Field{nil, fname, ""})
}
return &Sel{selects: fs,
from: nil,
join: nil,
leftjoin: nil,
where: nil,
groupby: nil,
having: nil,
orderby: nil,
limit: nil,
}
}
func Select(fields ...Exp) *Sel {
return &Sel{selects: fields,
from: nil,
join: nil,
leftjoin: nil,
where: nil,
groupby: nil,
having: nil,
orderby: nil,
limit: nil,
}
}
func (sel *Sel) From(t *Table) *Sel {
sel.from = t
for _, f := range sel.selects {
if field, ok := f.(*Field); ok {
if field.Table == nil {
field.Table = t
}
}
}
return sel
}
func (sel *Sel) LeftJoin(t *Table, on Exp) *Sel {
if sel.leftjoin == nil {
sel.leftjoin = make([]Exp, 0)
}
sel.leftjoin = append(sel.leftjoin, leftjoinExp(t).onExp(on))
return sel
}
func (sel *Sel) Join(t *Table, on Exp) *Sel {
if sel.join == nil {
sel.join = make([]Exp, 0)
}
sel.join = append(sel.join, joinExp(t).onExp(on))
return sel
}
func (sel *Sel) Where(exp Exp) *Sel {
sel.where = whereExp(exp)
return sel
}
func (sel *Sel) GroupBy(fields ...Exp) *Sel {
if sel.groupby == nil {
sel.groupby = make([]Exp, 0)
}
for _, f := range fields {
sel.groupby = append(sel.groupby, f)
}
return sel
}
func (sel *Sel) Having(exp Exp) *Sel {
sel.having = exp
return sel
}
func (sel *Sel) OrderBy(fields ...Exp) *Sel {
if sel.orderby == nil {
sel.orderby = fields
} else {
for _, f := range fields {
sel.orderby = append(sel.orderby, f)
}
}
return sel
}
// 本来我想把 limit 和 offset 设定成 Exp ,但是想来想去写了这么多SQL从来没有这样的用法咯……
func (sel *Sel) Limit(limit int) *Sel {
sel.limit = &limit
return sel
}
func (sel *Sel) Offset(offset int) *Sel {
sel.offset = &offset
return sel
}
func (sel Sel) Eval(env Env) string {
var scope = env.Scope()
env.SetScope(sel)
defer env.SetScope(scope)
var command = "SELECT "
if sel.selects != nil {
var fields = make([]string, 0)
for _, f := range sel.selects {
var fname = f.Eval(env)
fields = append(fields, fname)
}
command += strings.Join(fields, ", ")
}
if sel.from != nil {
command += (" FROM " + sel.from.Eval(env))
}
if sel.join != nil {
for _, j := range sel.join {
command += (" " + j.Eval(env))
}
}
if sel.leftjoin != nil {
for _, j := range sel.leftjoin {
command += (" " + j.Eval(env))
}
}
if sel.where != nil {
command += (" " + sel.where.Eval(env))
}
if sel.groupby != nil {
var groups = make([]string, 0)
for _, g := range sel.groupby {
groups = append(groups, g.Eval(env))
}
command += (" GROUP BY " + strings.Join(groups, ", "))
}
if sel.having != nil {
command += " HAVING " + sel.having.Eval(env)
}
if sel.orderby != nil {
var orderby = make([]string, 0)
for _, o := range sel.orderby {
orderby = append(orderby, o.Eval(env))
}
command += (" ORDER BY " + strings.Join(orderby, ", "))
}
if sel.limit != nil {
command += fmt.Sprintf(" LIMIT %d", *sel.limit)
}
if sel.offset != nil {
command += fmt.Sprintf(" OFFSET %d", *sel.offset)
}
return command
}