-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_generator.go
257 lines (230 loc) · 5.69 KB
/
sql_generator.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
package sql_generator
import (
"fmt"
"github.com/crayoon/sql_generator/join"
"github.com/crayoon/sql_generator/where"
"strings"
"time"
)
const (
TypeSelect = "select"
TypeInsert = "insert"
TypeUpdate = "update"
TypeDelete = "delete"
)
type Query struct {
table string
//当前操作
typ string
softDelete bool
//查询
selects string
//更新
update struct {
fields []string
values []interface{}
}
//新增
insert struct {
fields []string
values []interface{}
}
//条件
wheres *where.Wheres
//限制
limit int64
offset int64
//排序
orderBy []string
//组
groupBy []string
having *where.Wheres
//关联
joins []*join.Joins
}
func NewSql(do func(q *Query), table string) (string, []interface{}) {
query := Query{
table: table,
selects: "*",
softDelete: false,
wheres: &where.Wheres{},
having: &where.Wheres{},
}
do(&query)
return query.ToSql()
}
func (q *Query) Find() *Query {
q.limit = 1
return q
}
func (q *Query) Select(fields string) *Query {
q.typ = TypeSelect
q.selects = fields
return q
}
func (q *Query) Insert(v map[string]interface{}) *Query {
q.typ = TypeInsert
for key, value := range v {
q.insert.fields = append(q.insert.fields, key)
q.insert.values = append(q.insert.values, value)
}
return q
}
func (q *Query) Update(v map[string]interface{}) *Query {
q.typ = TypeUpdate
for key, value := range v {
q.update.fields = append(q.update.fields, key+"=?")
q.update.values = append(q.update.values, value)
}
return q
}
func (q *Query) Delete() *Query {
q.typ = TypeDelete
return q
}
func (q *Query) Where(field string, operator string, value interface{}) *Query {
q.wheres.Where(field, operator, value)
return q
}
func (q *Query) WhereIn(field string, value []interface{}) *Query {
q.wheres.WhereIn(field, value)
return q
}
func (q *Query) WhereBetween(field string, first interface{}, second interface{}) *Query {
q.wheres.WhereBetween(field, first, second)
return q
}
func (q *Query) WhereNull(field string) *Query {
q.wheres.WhereNull(field)
return q
}
func (q *Query) WhereNoNull(field string) *Query {
q.wheres.WhereNoNull(field)
return q
}
func (q *Query) AddWhere(do func(w *where.Wheres)) *Query {
newWhere := where.Wheres{}
do(&newWhere)
if len(newWhere.Raws) > 0 {
q.wheres.Raws = append(q.wheres.Raws, fmt.Sprintf("( %s )", strings.Join(newWhere.Raws, " and ")))
q.wheres.Values = append(q.wheres.Values, newWhere.Values...)
}
return q
}
func (q *Query) WhereOr(do func(or *where.Wheres)) *Query {
orWhere := where.Wheres{}
do(&orWhere)
if len(orWhere.Raws) > 0 {
q.wheres.Raws = append(q.wheres.Raws, fmt.Sprintf("( %s )", strings.Join(orWhere.Raws, " or ")))
q.wheres.Values = append(q.wheres.Values, orWhere.Values...)
}
return q
}
func (q *Query) Limit(offset int64, limit int64) *Query {
q.limit = limit
q.offset = offset
return q
}
func (q *Query) UseSoftDelete() *Query {
q.WhereNull("delete_time")
q.softDelete = true
return q
}
func (q *Query) OrderBy(field string, direction string) *Query {
q.orderBy = append(q.orderBy, fmt.Sprintf("%s %s", field, direction))
return q
}
func (q *Query) GroupBy(field string) *Query {
q.orderBy = append(q.groupBy, field)
return q
}
func (q *Query) Having(do func(having *where.Wheres)) *Query {
do(q.having)
return q
}
func (q *Query) Join(item *join.Joins, do func(join *join.Joins)) *Query {
if do != nil {
do(item)
}
q.joins = append(q.joins, item)
return q
}
func (q *Query) Count(field string) *Query {
q.Select(fmt.Sprintf("count(%s) as _COUNT", field))
return q
}
func (q *Query) ToSql() (sql string, values []interface{}) {
//type
switch q.typ {
case TypeDelete:
if len(q.wheres.Raws) < 1 {
panic("删除必须加条件")
}
if q.softDelete {
sql = fmt.Sprintf("update %s set delete_time=? ", q.table)
values = []interface{}{time.Now().Format("2006-01-02 15:04:05")}
return
} else {
sql = fmt.Sprintf("delete from %s ", q.table)
}
case TypeUpdate:
if len(q.update.fields) < 1 {
return
}
sql = fmt.Sprintf("update %s set %s ", q.table, strings.Join(q.update.fields, ", "))
values = q.update.values
case TypeInsert:
temp, insLen := "?", len(q.insert.fields)
if insLen < 1 {
return
}
for i := 1; i < insLen; i++ {
temp += ", ?"
}
sql = fmt.Sprintf("insert into %s (%s) values (%s)", q.table, strings.Join(q.insert.fields, ", "), temp)
values = q.insert.values
return
default:
sql = fmt.Sprintf("select %s from %s ", q.selects, q.table)
}
//join
if len(q.joins) > 0 {
for _, joinItem := range q.joins {
if joinItem.Table == "" || joinItem.JoinOn == "" {
continue
}
if joinItem.Typ == "" {
joinItem.Typ = join.InnerJoin
}
sql += fmt.Sprintf("%s join %s on %s", joinItem.Typ, joinItem.Table, joinItem.JoinOn)
if joinItem.Wheres != nil && len(joinItem.Wheres.Raws) > 0 {
sql += fmt.Sprintf(" and %s ", strings.Join(joinItem.Wheres.Raws, " and "))
values = append(values, joinItem.Wheres.Values...)
}
}
}
//where
if len(q.wheres.Raws) > 0 {
sql += fmt.Sprintf("where %s ", strings.Join(q.wheres.Raws, " and "))
values = append(values, q.wheres.Values...)
}
//order by
if len(q.orderBy) > 0 {
sql += fmt.Sprintf("order by %s ", strings.Join(q.orderBy, ", "))
//having
sql += fmt.Sprintf("having ( %s )", strings.Join(q.having.Raws, " and "))
values = append(values, q.having.Values...)
}
//group by + having
if len(q.groupBy) > 0 {
sql += fmt.Sprintf("group by %s ", strings.Join(q.groupBy, ", "))
//having
sql += fmt.Sprintf("having ( %s )", strings.Join(q.having.Raws, " and "))
values = append(values, q.having.Values...)
}
//limit
if q.limit > 0 && q.typ == TypeSelect {
sql += fmt.Sprintf("limit %d offset %d ", q.limit, q.offset)
}
return
}