-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
82 lines (69 loc) · 1.77 KB
/
set.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
package lorm
import (
"github.com/lontten/lorm/field"
"reflect"
)
type SetContext struct {
// 直接更新model中的字段
fieldNames []string
// model 需要 ormContext 才能解析
model any
hasModel bool
// 其他可以直接解析
columns []string
columnValues []field.Value
err error
}
func Set() *SetContext {
return &SetContext{}
}
func (s *SetContext) Field(name ...string) *SetContext {
s.fieldNames = append(s.fieldNames, name...)
return s
}
func (s *SetContext) Set(name string, value any) *SetContext {
s.columns = append(s.columns, name)
s.columnValues = append(s.columnValues, field.Value{
Type: field.Val,
Value: value,
})
return s
}
func (s *SetContext) SetNull(name string) *SetContext {
s.columns = append(s.columns, name)
s.columnValues = append(s.columnValues, field.Value{
Type: field.Null,
})
return s
}
// 自增,自减
func (s *SetContext) SetIncrement(name string, num any) *SetContext {
s.columns = append(s.columns, name)
s.columnValues = append(s.columnValues, field.Value{
Type: field.Increment,
Value: num,
})
return s
}
// 自定义表达式
// SetExpression("name", "substr(time('now'), 12)") // sqlite 设置时分秒
func (s *SetContext) SetExpression(name string, expression string) *SetContext {
s.columns = append(s.columns, name)
s.columnValues = append(s.columnValues, field.Value{
Type: field.Expression,
Value: expression,
})
return s
}
func (s *SetContext) Map(v map[string]any) *SetContext {
cv := getMapCVMap(reflect.ValueOf(v))
s.columns = append(s.columns, cv.columns...)
s.columnValues = append(s.columnValues, cv.columnValues...)
return s
}
// 这里不对 model 进行解析
// 在 initColumnsValueSet 中解析
func (s *SetContext) Model(v any) *SetContext {
s.model = v
return s
}