-
Notifications
You must be signed in to change notification settings - Fork 3
/
crud.go
69 lines (56 loc) · 2.59 KB
/
crud.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
package mysql
import (
"context"
"github.com/zhufuyi/pkg/mysql/query"
"gorm.io/gorm"
)
// TableName get table name
func TableName(table interface{}) string {
return GetTableName(table)
}
// Create a new record
// the param of 'table' must be pointer, eg: &StructName
func Create(ctx context.Context, db *gorm.DB, table interface{}) error {
return db.WithContext(ctx).Create(table).Error
}
// Delete record
// the param of 'table' must be pointer, eg: &StructName
func Delete(ctx context.Context, db *gorm.DB, table interface{}, query interface{}, args ...interface{}) error {
return db.WithContext(ctx).Where(query, args...).Delete(table).Error
}
// DeleteByID delete record by id
// the param of 'table' must be pointer, eg: &StructName
func DeleteByID(ctx context.Context, db *gorm.DB, table interface{}, id interface{}) error {
return db.WithContext(ctx).Where("id = ?", id).Delete(table).Error
}
// Update record
// the param of 'table' must be pointer, eg: &StructName
func Update(ctx context.Context, db *gorm.DB, table interface{}, column string, value interface{}, query interface{}, args ...interface{}) error {
return db.WithContext(ctx).Model(table).Where(query, args...).Update(column, value).Error
}
// Updates record
// the param of 'table' must be pointer, eg: &StructName
func Updates(ctx context.Context, db *gorm.DB, table interface{}, update KV, query interface{}, args ...interface{}) error {
return db.WithContext(ctx).Model(table).Where(query, args...).Updates(update).Error
}
// Get one record
// the param of 'table' must be pointer, eg: &StructName
func Get(ctx context.Context, db *gorm.DB, table interface{}, query interface{}, args ...interface{}) error {
return db.WithContext(ctx).Where(query, args...).First(table).Error
}
// GetByID get record by id
func GetByID(ctx context.Context, db *gorm.DB, table interface{}, id interface{}) error {
return db.WithContext(ctx).Where("id = ?", id).First(table).Error
}
// List multiple records, starting from page 0
// the param of 'tables' must be slice, eg: []StructName
func List(ctx context.Context, db *gorm.DB, tables interface{}, page *query.Page, query interface{}, args ...interface{}) error {
return db.WithContext(ctx).Order(page.Sort()).Limit(page.Size()).Offset(page.Offset()).Where(query, args...).Find(tables).Error
}
// Count number of records
// the param of 'table' must be pointer, eg: &StructName
func Count(ctx context.Context, db *gorm.DB, table interface{}, query interface{}, args ...interface{}) (int64, error) {
var count int64
err := db.WithContext(ctx).Model(table).Where(query, args...).Count(&count).Error
return count, err
}