/
select.go
264 lines (228 loc) · 5.68 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
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
package gosqler
import (
"errors"
"fmt"
"strings"
"github.com/tingxin/go-sqler/sql"
)
var (
// ErrorEmptyParams occur when param is empty
ErrorEmptyParams = errors.New("params should not be empty")
)
// Select used to express select sql
type Select interface {
sql.Wherer
sql.Haver
Select(fields ...string)
Choice(field string)
From(tableNames ...string)
Join(table string, conditions ...string)
LeftJoin(table string, conditions ...string)
RightJoin(table string, conditions ...string)
FullJoin(table string, conditions ...string)
Orderby(field string, desc bool)
GroupBy(fields ...string)
BeginWhere()
EndWhere()
BeginHaving()
EndHaving()
Limit(count int)
Offset(offset int)
String() string
}
// Where used to build where condition
type selecter struct {
selectCache []string
fromCache []string
groupCache []string
orderCache []string
joinCache []string
limit int
offset int
where sql.Filter
having sql.Filter
}
// NewSelect used to create where object
func NewSelect() Select {
query := &selecter{}
query.offset = 0
query.limit = -1
return query
}
func (p *selecter) Select(fields ...string) {
if p.selectCache == nil {
p.selectCache = fields
} else {
p.selectCache = append(p.selectCache, fields...)
}
}
func (p *selecter) Choice(field string) {
if p.selectCache == nil {
p.selectCache = make([]string, 1, 1)
p.selectCache[0] = field
} else {
p.selectCache = append(p.selectCache, field)
}
}
func (p *selecter) From(tableNames ...string) {
if p.fromCache == nil {
p.fromCache = tableNames
} else {
p.fromCache = append(p.fromCache, tableNames...)
}
}
func (p *selecter) Join(table string, conditions ...string) {
p.joinBy("INNER", table, conditions...)
}
func (p *selecter) LeftJoin(table string, conditions ...string) {
p.joinBy("LEFT", table, conditions...)
}
func (p *selecter) RightJoin(table string, conditions ...string) {
p.joinBy("RIGHT", table, conditions...)
}
func (p *selecter) FullJoin(table string, conditions ...string) {
p.joinBy("FULL", table, conditions...)
}
// Where used to add where condition
func (p *selecter) Where(field, operator string, value interface{}) {
if p.where == nil {
p.where = sql.NewFilter("WHERE")
}
p.where.And(field, operator, value)
}
// AndWhere used to add where condition
func (p *selecter) AndWhere(field, operator string, value interface{}) {
p.where.And(field, operator, value)
}
// OrWhere used to add where condition
func (p *selecter) OrWhere(field, operator string, value interface{}) {
p.where.OR(field, operator, value)
}
func (p *selecter) BeginWhere() {
if p.where == nil {
p.where = sql.NewFilter("WHERE")
}
p.where.BeginGroup()
}
func (p *selecter) EndWhere() {
if p.where == nil {
panic("Call BeginWhere First")
}
p.where.EndGroup()
}
func (p *selecter) Orderby(field string, desc bool) {
if p.orderCache == nil {
p.orderCache = make([]string, 1, 1)
if desc {
p.orderCache[0] = fmt.Sprintf("%s DESC", field)
} else {
p.orderCache[0] = fmt.Sprintf("%s ASC", field)
}
} else {
if desc {
p.orderCache = append(p.orderCache, fmt.Sprintf("%s DESC", field))
} else {
p.orderCache = append(p.orderCache, fmt.Sprintf("%s ASC", field))
}
}
}
func (p *selecter) GroupBy(fields ...string) {
if p.groupCache == nil {
p.groupCache = fields
} else {
p.groupCache = append(p.groupCache, fields...)
}
}
func (p *selecter) Having(field, operator string, value interface{}) {
if p.having == nil {
p.having = sql.NewFilter("HAVING")
}
p.having.And(field, operator, value)
}
func (p *selecter) AndHaving(field, operator string, value interface{}) {
p.having.And(field, operator, value)
}
func (p *selecter) OrHaving(field, operator string, value interface{}) {
p.having.OR(field, operator, value)
}
func (p *selecter) BeginHaving() {
if p.having == nil {
p.having = sql.NewFilter("HAVING")
}
p.having.BeginGroup()
}
func (p *selecter) EndHaving() {
if p.having == nil {
panic("Call BeginHaving First")
}
p.having.EndGroup()
}
func (p *selecter) Limit(count int) {
p.limit = count
}
func (p *selecter) Offset(count int) {
p.offset = count
}
func (p *selecter) String() string {
if p.selectCache == nil || p.fromCache == nil {
return ""
}
cache := make([]string, 4, 4)
selectStr := strings.Join(p.selectCache, ",")
cache[0] = "SELECT"
cache[1] = selectStr
fromStr := strings.Join(p.fromCache, ",")
cache[2] = "FROM"
cache[3] = fromStr
if len(p.joinCache) > 0 {
joinStr := strings.Join(p.joinCache, " ")
cache = append(cache, joinStr)
}
if p.where != nil {
whereStr := p.where.String()
if whereStr != "" {
cache = append(cache, whereStr)
}
}
if p.groupCache != nil {
groupStr := strings.Join(p.groupCache, ",")
cache = append(cache, "GROUP BY")
cache = append(cache, groupStr)
}
if p.having != nil {
havingStr := p.having.String()
if havingStr != "" {
cache = append(cache, havingStr)
}
}
if p.orderCache != nil {
orderStr := strings.Join(p.orderCache, ",")
cache = append(cache, "ORDER BY")
cache = append(cache, orderStr)
}
if p.limit > 0 {
var limitStr string
if p.offset > 0 {
limitStr = fmt.Sprintf("LIMIT %d, %d", p.offset, p.limit)
} else {
limitStr = fmt.Sprintf("LIMIT %d", p.limit)
}
cache = append(cache, limitStr)
}
return strings.Join(cache, " ")
}
func (p *selecter) joinBy(typeStr, table string, conditions ...string) {
if table == "" || len(conditions) == 0 {
panic(ErrorEmptyParams)
}
head := fmt.Sprintf("%s JOIN %s ON", typeStr, table)
body := strings.Join(conditions, " AND ")
if p.joinCache == nil {
p.joinCache = make([]string, 2, 2)
p.joinCache[0] = head
p.joinCache[1] = body
} else {
p.joinCache = append(p.joinCache, head)
p.joinCache = append(p.joinCache, body)
}
}