-
Notifications
You must be signed in to change notification settings - Fork 15
/
mask.go
126 lines (101 loc) · 2.62 KB
/
mask.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
package dm
import (
"database/sql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"reflect"
"strings"
)
type STable struct {
Conn *gorm.DB
Table interface{}
}
var conn *gorm.DB
func Table(table ...interface{}) *STable {
var tb interface{}
if len(table) == 1 {
tb = table[0]
}
return &STable{Table: tb, Conn: conn}
}
func TB(db *gorm.DB) {
conn = db
}
func (stb *STable) DB() (*sql.DB, error) {
return stb.Conn.DB()
}
func (stb *STable) Model() *gorm.DB {
if reflect.ValueOf(stb.Table).Kind() == reflect.String {
return stb.Conn.Table(stb.Table.(string))
}
return stb.Conn.Model(stb.Table)
}
func (stb *STable) Create() error {
return stb.Conn.Create(stb.Table).Error
}
func (stb *STable) Get(dest interface{}, conds ...interface{}) error {
return stb.Conn.Where(stb.Table).First(dest, conds...).Error
}
func (stb *STable) GetWhere(dest interface{}) error {
return stb.Conn.Model(stb.Table).Where(stb.Table).Find(dest).Error
}
func (stb *STable) GetAll(dest interface{}) error {
return stb.Conn.Model(stb.Table).Find(dest).Error
}
func (stb *STable) ClausesAssignmentColumns(name string, doUpdates []string) error {
return stb.Conn.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: name}},
DoUpdates: clause.AssignmentColumns(doUpdates),
}).Create(stb.Table).Error
}
func (stb *STable) Delete() error {
tx := stb.Conn.Begin()
if err := tx.Where(stb.Table).Delete(stb.Table).Error; err != nil {
tx.Rollback()
return err
}
return tx.Commit().Error
}
func (stb *STable) Update(dest interface{}) error {
tx := stb.Conn.Begin()
if err := tx.Where(stb.Table).Updates(dest).Error; err != nil {
tx.Rollback()
return err
}
return tx.Commit().Error
}
func (stb *STable) AutoMigrate(dst ...interface{}) error {
return stb.Conn.AutoMigrate(dst...)
}
func (stb *STable) DropTable(dest ...interface{}) error {
return stb.Conn.Migrator().DropTable(dest...)
}
func (stb *STable) HasTable(dest interface{}) bool {
return stb.Conn.Migrator().HasTable(dest)
}
func RefClone(inter interface{}) interface{} {
nInter := reflect.New(reflect.TypeOf(inter).Elem())
val := reflect.ValueOf(inter).Elem()
nVal := nInter.Elem()
for i := 0; i < val.NumField(); i++ {
nvField := nVal.Field(i)
nvField.Set(val.Field(i))
}
return nInter.Interface()
}
func RefInclude(inter interface{}, inc []string) interface{} {
v := reflect.ValueOf(inter).Elem()
for i := 0; i < v.Type().NumField(); i++ {
n := v.Type().Field(i).Name
var ins bool
for _, str := range inc {
if strings.ToLower(n) == strings.ToLower(str) {
ins = true
}
}
if !ins {
v.Field(i).Set(reflect.Zero(v.Field(i).Type()))
}
}
return inter
}