-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.go
207 lines (177 loc) · 4.36 KB
/
extra.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
package lorm
import (
"github.com/lontten/lorm/field"
"github.com/lontten/lorm/insert-type"
"github.com/lontten/lorm/return-type"
"github.com/lontten/lorm/types"
)
// ExtraContext 扩展参数
type ExtraContext struct {
insertType insert_type.InsertType
returnType return_type.ReturnType
showSql bool
noRun bool
skipSoftDelete bool
tableName string
selectColumns []string // select 的 字段名,空为返回所有字段
limit *int64
offset *int64
orderByTokens []string // 排序
columns []string
columnValues []field.Value
// 唯一索引字段名列表
duplicateKeyNames []string
set *SetContext
err error
}
func E() *ExtraContext {
return &ExtraContext{
set: Set(),
}
}
// set 中的错误已经被上抛到 ExtraContext,所以只用判断 ExtraContext 的 err
func (e *ExtraContext) GetErr() error {
if e.err != nil {
return e.err
}
return nil
}
func (e *ExtraContext) ShowSql() *ExtraContext {
e.showSql = true
return e
}
func (e *ExtraContext) NoRun() *ExtraContext {
e.noRun = true
return e
}
func (e *ExtraContext) SkipSoftDelete() *ExtraContext {
e.skipSoftDelete = true
return e
}
func (e *ExtraContext) TableName(name string) *ExtraContext {
e.tableName = name
return e
}
func (e *ExtraContext) Select(name ...string) *ExtraContext {
e.selectColumns = name
return e
}
func (e *ExtraContext) OrderBy(name string, condition ...bool) *ExtraContext {
for _, b := range condition {
if !b {
return e
}
}
e.orderByTokens = append(e.orderByTokens, name)
return e
}
func (e *ExtraContext) OrderDescBy(name string, condition ...bool) *ExtraContext {
for _, b := range condition {
if !b {
return e
}
}
e.orderByTokens = append(e.orderByTokens, name+" desc")
return e
}
func (e *ExtraContext) Limit(num int64, condition ...bool) *ExtraContext {
for _, b := range condition {
if !b {
return e
}
}
e.limit = types.NewInt64(num)
return e
}
func (e *ExtraContext) Offset(num int64, condition ...bool) *ExtraContext {
for _, b := range condition {
if !b {
return e
}
}
e.offset = types.NewInt64(num)
return e
}
func (e *ExtraContext) ReturnType(typ return_type.ReturnType) *ExtraContext {
e.returnType = typ
return e
}
func (e *ExtraContext) SetNull(name string) *ExtraContext {
e.columns = append(e.columns, name)
e.columnValues = append(e.columnValues, field.Value{
Type: field.Null,
})
return e
}
func (e *ExtraContext) SetNow(name string) *ExtraContext {
e.columns = append(e.columns, name)
e.columnValues = append(e.columnValues, field.Value{
Type: field.Now,
})
return e
}
func (e *ExtraContext) Set(name string, value any) *ExtraContext {
e.columns = append(e.columns, name)
e.columnValues = append(e.columnValues, field.Value{
Type: field.Val,
Value: value,
})
return e
}
// 自增,自减
func (e *ExtraContext) SetIncrement(name string, num any) *ExtraContext {
e.columns = append(e.columns, name)
e.columnValues = append(e.columnValues, field.Value{
Type: field.Increment,
Value: num,
})
return e
}
// 自定义表达式
// SetExpression("name", "substr(time('now'), 12)") // sqlite 设置时分秒
func (e *ExtraContext) SetExpression(name string, expression string) *ExtraContext {
e.columns = append(e.columns, name)
e.columnValues = append(e.columnValues, field.Value{
Type: field.Expression,
Value: expression,
})
return e
}
type DuplicateKey struct {
e *ExtraContext
}
//.whenDuplicateKey(name ...string, )
//.do(nothing, nil)
//.do(update, all, .set(), select ("name", "age"))
//.do(replace, all, .set(), select ("name", "age"))
// 唯一索引冲突
func (e *ExtraContext) WhenDuplicateKey(name ...string) *DuplicateKey {
e.duplicateKeyNames = name
return &DuplicateKey{
e: e,
}
}
func (dk *DuplicateKey) DoNothing() *ExtraContext {
dk.e.insertType = insert_type.Ignore
return dk.e
}
func (dk *DuplicateKey) update(insertType insert_type.InsertType, set ...*SetContext) *ExtraContext {
dk.e.insertType = insertType
if len(set) == 0 {
return dk.e
}
if sc := set[0]; sc != nil {
dk.e.set = sc
// err上抛到 ExtraContext
if sc.err != nil {
dk.e.err = sc.err
}
}
return dk.e
}
func (dk *DuplicateKey) DoUpdate(set ...*SetContext) *ExtraContext {
return dk.update(insert_type.Update, set...)
}
func (dk *DuplicateKey) DoReplace(set ...*SetContext) *ExtraContext {
return dk.update(insert_type.Replace, set...)
}