-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface.go
97 lines (85 loc) · 2.03 KB
/
interface.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
package sq
import (
"context"
"github.com/jmoiron/sqlx"
)
// sq.Table("user",nil, nil)
// sq.Table("user", sq.Raw{"`deleted_at` IS NULL", nil}, sq.Raw{"`deleted_at` = ?" ,[]interface{}{time.Now()}})
func Table(tableName string, softDeleteWhere func() Raw, softDeleteSet func() Raw) Tabler {
if softDeleteWhere == nil {
softDeleteWhere = func() Raw {
return Raw{}
}
}
if softDeleteSet == nil {
softDeleteSet = func() Raw {
return Raw{}
}
}
return table{
tableName: tableName,
softDeleteWhere: softDeleteWhere,
softDeleteSet: softDeleteSet,
}
}
type Tabler interface {
TableName() string
SoftDeleteWhere() Raw
SoftDeleteSet() Raw
}
// 供 relation sq.Table() 使用
type table struct {
tableName string
softDeleteWhere func() Raw
softDeleteSet func() Raw
}
func (t table) TableName() string {
return t.tableName
}
func (t table) SoftDeleteWhere() Raw {
return t.softDeleteWhere()
}
func (t table) SoftDeleteSet() Raw {
return t.softDeleteSet()
}
type Raw struct {
Query string
Values []interface{}
}
func (r Raw) IsZero() bool {
return r.Query == ""
}
type Model interface {
Tabler
BeforeInsert() error
AfterInsert(result Result) error
BeforeUpdate() error
AfterUpdate() error
}
type Relation interface {
TableName() string
SoftDeleteWhere() Raw
RelationJoin() []Join
}
type DefaultLifeCycle struct {
}
func (v *DefaultLifeCycle) BeforeInsert() error { return nil }
func (v *DefaultLifeCycle) AfterInsert(result Result) error { return nil }
func (v *DefaultLifeCycle) BeforeUpdate() error { return nil }
func (v *DefaultLifeCycle) AfterUpdate() error { return nil }
type Storager interface {
getCore() StoragerCore
getSQLChecker() SQLChecker
}
type StoragerCore interface {
sqlx.Queryer
sqlx.QueryerContext
sqlx.Execer
sqlx.ExecerContext
sqlx.Preparer
sqlx.PreparerContext
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}
type sqlInsertRawer interface {
SQLInsertRaw() (query string, values []interface{})
}