-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.go
394 lines (349 loc) · 11.9 KB
/
table.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
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
package crud
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
)
// Table 是对CRUD进一层的封装
type Table struct {
*DataBase
*Search
tableName string
Columns Columns
}
// Name 返回名称
func (t *Table) Name() string {
return t.tableName
}
// HaveColumn 是否有这个列
func (t *Table) HaveColumn(key string) bool {
return t.Columns.HaveColumn(key)
}
// UpdateTime 查找表的更新时间
func (t *Table) UpdateTime() string {
return t.Query("SELECT `UPDATE_TIME` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?", t.Schema, t.tableName).String()
}
// AutoIncrement 查找表的自增ID的值
func (t *Table) AutoIncrement() int {
return t.Query("SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?", t.Schema, t.tableName).Int()
}
// SetAutoIncrement 设置自动增长ID
func (t *Table) SetAutoIncrement(id int) error {
_, err := t.Exec("ALTER TABLE `" + t.tableName + "` AUTO_INCREMENT = " + strconv.Itoa(id)).RowsAffected()
return err
}
// MaxID 查找表的最大ID,如果为NULL的话则为0
func (t *Table) MaxID() int {
return t.Query("SELECT IFNULL(MAX(id), 0) as id FROM `" + t.tableName + "`").Int()
}
// IDIn 查找多个ID对应的列
func (t *Table) IDIn(ids ...interface{}) *SQLRows {
if len(ids) == 0 {
return &SQLRows{}
}
return t.Query(fmt.Sprintf("SELECT * FROM `%s` WHERE id in (%s)", t.tableName, argslice(len(ids))), ids...)
}
// Create 创建
// check 如果有,则会判断表里面以这几个字段为唯一的话,数据库是否存在此条数据,如果有就不插入了。
// 所有ORM的底层。FormXXX, (*DataBase)CRUD
//
func (t *Table) Create(m map[string]interface{}, checks ...string) (int64, error) {
//INSERT INTO `feedback` (`task_id`, `template_question_id`, `question_options_id`, `suggestion`, `member_id`) VALUES ('1', '1', '1', '1', '1')
if len(checks) > 0 {
names := []string{}
values := []interface{}{}
for _, check := range checks {
names = append(names, "`"+check+"`"+" = ? ")
values = append(values, m[check])
}
// SELECT COUNT(*) FROM `feedback` WHERE `task_id` = ? AND `member_id` = ?
if t.Query(fmt.Sprintf("SELECT COUNT(*) FROM `%s` WHERE %s", t.tableName, strings.Join(names, "AND ")), values...).Int() > 0 {
return 0, ErrInsertRepeat
}
}
if t.tableColumns[t.tableName].HaveColumn(CreatedAt) {
m[CreatedAt] = time.Now().Format(TimeFormat)
}
ks, vs := ksvs(m)
id, err := t.Exec(fmt.Sprintf("INSERT INTO `%s` (%s) VALUES (%s)", t.tableName, strings.Join(ks, ","), argslice(len(ks))), vs...).LastInsertId()
if err != nil {
return 0, errors.New("SQL语句异常")
}
if id <= 0 {
return 0, errors.New("插入数据库异常")
}
return id, nil
}
// Creates 创建多列
func (t *Table) Creates(ms []map[string]interface{}) (int, error) {
if len(ms) == 0 {
return 0, nil
}
// INSERT INTO `feedback` (`task_id`, `template_question_id`, `question_options_id`, `suggestion`, `member_id`) VALUES ('1', '1', '1', '1', '1'),('1', '1', '1', '1', '1')
fields := []string{}
args := []interface{}{}
sqlFields := []string{}
sqlArgs := []string{}
sqlArg := "(" + argslice(len(ms[0])) + ")"
for i := 0; i < len(ms); i++ {
sqlArgs = append(sqlArgs, sqlArg)
}
for k := range ms[0] {
fields = append(fields, k)
sqlFields = append(sqlFields, "`"+k+"`")
}
for _, v := range ms {
for _, field := range fields {
args = append(args, v[field])
}
}
rows, err := t.Exec(fmt.Sprintf("INSERT INTO `%s` (%s) VALUES %s ", t.tableName, strings.Join(sqlFields, ","), strings.Join(sqlArgs, ",")), args...).RowsAffected()
return int(rows), err
}
// Read 查找单条数据
func (t *Table) Read(m map[string]interface{}) RowMap {
rs := t.Reads(m)
if len(rs) > 0 {
return rs[0]
}
return RowMap{}
}
// Reads 查找多条数据
func (t *Table) Reads(m map[string]interface{}) RowsMap {
if t.tableColumns[t.tableName].HaveColumn(IsDeleted) {
m[IsDeleted] = 0
}
//SELECT * FROM address WHERE id = 1 AND uid = 27
ks, vs := ksvs(m, " = ? ")
return t.Query(fmt.Sprintf("SELECT * FROM %s WHERE %s", t.tableName, strings.Join(ks, "AND")), vs...).RowsMap()
}
// Update 更新
// 如果map里面有id的话会自动删除id,然后使用id来作为更新的条件。
func (t *Table) Update(mo map[string]interface{}, keys ...string) error {
// 因为会删除id,所以使用的时候要copy一个map
m := copyMap(mo)
if len(keys) == 0 {
keys = append(keys, "id")
}
if t.tableColumns[t.tableName].HaveColumn(UpdatedAt) {
m[UpdatedAt] = time.Now().Format(TimeFormat)
}
keysValue := []interface{}{}
whereks := []string{}
for _, key := range keys {
val, ok := m[key]
if !ok {
return errors.New("没有更新主键")
}
keysValue = append(keysValue, val)
delete(m, key)
whereks = append(whereks, "`"+key+"` = ? ")
}
//因为在更新的时候最好不要更新ID,而有时候又会将ID传入进来,所以id每次都会被删除,如果要更新id的话使用Exec()
delete(m, "id")
ks, vs := ksvs(m, " = ? ")
for _, val := range keysValue {
vs = append(vs, val)
}
_, err := t.Exec(fmt.Sprintf("UPDATE `%s` SET %s WHERE %s LIMIT 1", t.tableName, strings.Join(ks, ","), strings.Join(whereks, "AND")), vs...).RowsAffected()
if err != nil {
return errors.New("SQL语句异常")
}
return nil
}
// CreateOrUpdate 创建或者更新
func (t *Table) CreateOrUpdate(m map[string]interface{}, keys ...string) error {
_, err := t.Create(m, keys...)
if err != nil {
if err == ErrInsertRepeat {
// 在len(map) <= len(keys)的时候可以不用执行更新操作,因为没有任何东西需要更新。
if len(m) > len(keys) {
return t.Update(m, keys...)
}
return nil
}
return err
}
return nil
}
// Delete 删除
func (t *Table) Delete(m map[string]interface{}) (int64, error) {
if len(m) == 0 {
return 0, errors.New("delete map len not be 0")
}
ks, vs := ksvs(m, " = ? ")
if t.tableColumns[t.tableName].HaveColumn(IsDeleted) {
return t.Exec(fmt.Sprintf("UPDATE `%s` SET is_deleted = '1', deleted_at = '%s' WHERE %s", t.tableName, time.Now().Format(TimeFormat), strings.Join(ks, "AND")), vs...).RowsAffected()
}
return t.Exec(fmt.Sprintf("DELETE FROM `%s` WHERE %s", t.tableName, strings.Join(ks, "AND")), vs...).RowsAffected()
}
// Clone 克隆
// 克隆要保证状态在每个链式操作后都是独立的。
func (t *Table) Clone() *Table {
newTable := &Table{
DataBase: t.DataBase,
tableName: t.tableName,
}
if t.Search == nil {
newTable.Search = &Search{table: newTable, tableName: t.tableName}
} else {
newTable.Search = t.Search.Clone()
newTable.Search.table = newTable
}
return newTable
}
// Where field = arg
func (t *Table) Where(query string, args ...interface{}) *Table {
return t.Clone().Search.Where(query, args...).table
}
// WhereNotEmpty if arg empty,will do nothing
func (t *Table) WhereNotEmpty(query, arg string) *Table {
if arg == "" {
return t
}
return t.Clone().Search.Where(query, arg).table
}
// WherePeriod [st,et)
func (t *Table) WherePeriod(field, st, et string) *Table {
return t.Clone().Search.Where(fmt.Sprintf("%s >= ? AND %s < ?", field, field), st, et).table
}
// WhereStartEndDay DATE_FORMAT(field, '%Y-%m-%d') >= startTime AND DATE_FORMAT(field, '%Y-%m-%d') <= endTime
// if startDay == "", will do nothing
// if endDay == "", endDay = startDay
// '','' => return
// '2017-07-01', '' => '2017-07-01', '2017-07-01'
// '', '2017-07-02' => '','2017-07-02' (TODO)
// '2017-07-01','2017-07-02' => '2017-07-02','2017-07-01'
func (t *Table) WhereStartEndDay(field, startDay, endDay string) *Table {
if startDay == "" && endDay == "" {
return t
}
if startDay != "" && endDay == "" {
endDay = startDay
}
// return t.Clone().Search.Where("DATE_FORMAT("+field+",'%Y-%m-%d') >= ? AND DATE_FORMAT("+field+",'%Y-%m-%d') <= ?", startDay, endDay).table
return t.Clone().Search.Where("DATE_FORMAT("+field+",'%Y-%m-%d') >= ? AND DATE_FORMAT("+field+",'%Y-%m-%d') <= ?", startDay, endDay).table
}
// WhereStartEndMonth DATE_FORMAT(field, '%Y-%m') >= startMonth AND DATE_FORMAT(field, '%Y-%m') <= endMonth
// if startMonth == "", will do nothing
// if endMonth == "", endMonth = startMonth
func (t *Table) WhereStartEndMonth(field, startMonth, endMonth string) *Table {
if startMonth == "" && endMonth == "" {
return t
}
if startMonth != "" && endMonth == "" {
endMonth = startMonth
}
return t.Clone().Search.Where("DATE_FORMAT("+field+",'%Y-%m') >= ? AND DATE_FORMAT("+field+",'%Y-%m') <= ?", startMonth, endMonth).table
}
// WhereStartEndTime DATE_FORMAT(field, '%H:%i') >= startTime AND DATE_FORMAT(field, '%H:%i') <= endTime
// if startTime == "", will do nothing
// if endTime == "", endTime = startTime
func (t *Table) WhereStartEndTime(field, startTime, endTime string) *Table {
if startTime == "" && endTime == "" {
return t
}
if startTime != "" && endTime == "" {
endTime = startTime
}
return t.Clone().Search.Where("DATE_FORMAT("+field+",'%H:%i') >= ? AND DATE_FORMAT("+field+",'%H:%i') <= ?", startTime, endTime).table
}
// WhereToday DATE_FORMAT(field, '%Y-%m-%d') = {today}
// (field >= '2017-01-01 00:00:00' AND %s < '2017-01-02 00:00:00' )
func (t *Table) WhereToday(field string) *Table {
return t.WhereDay(field, time.Now().Format("2006-01-02"))
}
// WhereDay DATE_FORMAT(field, '%Y-%m-%d') = day
func (t *Table) WhereDay(field, day string) *Table {
return t.Clone().Search.Where(WhereTimeParse(field, day, 0, 0, 1)).table
}
// WhereMonth DATE_FORMAT(field, '%Y-%m') = month
func (t *Table) WhereMonth(field, month string) *Table {
return t.Clone().Search.Where(WhereTimeParse(field, month, 0, 1, 0)).table
}
// WhereBeforeToday DATE_FORMAT(field, '%Y-%m-%d') < {today}
func (t *Table) WhereBeforeToday(field string) *Table {
return t.Clone().Search.Where("DATE_FORMAT("+field+",'%Y-%m-%d') < ?", time.Now().Format("2006-01-02")).table
}
// WhereLike field LIKE %like%
// If like == "", will do nothing.
func (t *Table) WhereLike(field, like string) *Table {
if like == "" {
return t
}
return t.Clone().Search.Where(field+" LIKE ?", "%"+like+"%").table
}
// WhereLikeLeft field LIKE %like
func (t *Table) WhereLikeLeft(field, like string) *Table {
if like == "" {
return t
}
return t.Clone().Search.Where(field+" LIKE ?", "%"+like).table
}
// WhereLikeRight field LIKE like%
func (t *Table) WhereLikeRight(field, like string) *Table {
if like == "" {
return t
}
return t.Clone().Search.Where(field+" LIKE ?", like+"%").table
}
// WhereID id = ?
func (t *Table) WhereID(id interface{}) *Table {
return t.Clone().Search.WhereID(id).table
}
// In In(field, a,b,c)
func (t *Table) In(field string, args ...interface{}) *Table {
return t.Clone().Search.In(field, args...).table
}
// NotIn not in
func (t *Table) NotIn(field string, args ...interface{}) *Table {
return t.Clone().Search.NotIn(field, args...).table
}
// Joins LEFT JOIN
// with auto join map
func (t *Table) Joins(query string, args ...string) *Table {
return t.Clone().Search.Joins(query, args...).table
}
// OrderBy ORDER BY
func (t *Table) OrderBy(field string, isDESC ...bool) *Table {
return t.Clone().Search.OrderBy(field, isDESC...).table
}
// Limit LIMIT
func (t *Table) Limit(n interface{}) *Table {
return t.Clone().Search.Limit(n).table
}
// Fields fields
func (t *Table) Fields(args ...string) *Table {
if len(args) == 0 {
return t
}
return t.Clone().Search.Fields(args...).table
}
// FieldCount equal Fields("COUNT(1) AS total")
func (t *Table) FieldCount(as ...string) *Table {
asWhat := "total"
if len(as) > 0 {
sp := strings.Split(as[0], " ")
asWhat = sp[0]
}
return t.Clone().Search.Fields("COUNT(1) AS " + asWhat).table
}
// Group GROUP BY
func (t *Table) Group(fields ...string) *Table {
return t.Clone().Search.Group(fields...).table
}
// Having Having
func (t *Table) Having(query string, args ...interface{}) *Table {
return t.Clone().Search.Having(query, args...).table
}
// Count count
func (t *Table) Count() int {
s := t.Clone().Search
var count int
s.fields = []string{"COUNT(1)"}
query, args := s.Parse()
s.table.Query(query, args...).Find(&count)
return count
}