-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.go
155 lines (145 loc) · 3.47 KB
/
updater.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
package morm
import (
"context"
"github.com/NotFound1911/morm/errors"
"reflect"
)
type Updater[T any] struct {
builder
val *T
assigns []Assignable
sess session
}
func NewUpdater[T any](sess session) *Updater[T] {
c := sess.getCore()
return &Updater[T]{
sess: sess,
builder: builder{
core: c,
dialect: c.dialect,
quoter: c.dialect.quoter(),
},
}
}
func (u *Updater[T]) Update(t *T) *Updater[T] {
u.val = t
return u
}
func (u *Updater[T]) Set(assigns ...Assignable) *Updater[T] {
u.assigns = assigns
return u
}
func (u *Updater[T]) Build() (*Query, error) {
var (
t T
err error
)
u.model, err = u.r.Get(&t)
if err != nil {
return nil, err
}
if len(u.assigns) == 0 {
return nil, errs.NewErrNoUpdatedColumns()
}
u.sqlBuilder.WriteString("UPDATE ")
if err = u.buildTable(u.table); err != nil {
return nil, err
}
u.sqlBuilder.WriteString(" SET ")
val := u.valCreator(u.val, u.model)
for i := 0; i < len(u.assigns); i++ {
if i > 0 {
u.sqlBuilder.WriteByte(',')
}
switch assign := u.assigns[i].(type) {
case Column:
if err := u.buildColumn(assign.table, assign.name); err != nil {
return nil, err
}
u.sqlBuilder.WriteString("=?")
arg, err := val.Field(assign.name)
if err != nil {
return nil, err
}
u.addArgs(arg)
case Assignment:
if err := u.buildAssignment(assign); err != nil {
return nil, err
}
default:
return nil, errs.NewErrUnsupportedAssignableType(assign)
}
}
if len(u.where) > 0 {
u.sqlBuilder.WriteString(" WHERE ")
if err := u.buildPredicates(u.where); err != nil {
return nil, err
}
}
u.sqlBuilder.WriteByte(';')
return &Query{
SQL: u.sqlBuilder.String(),
Args: u.args,
}, nil
}
func (u *Updater[T]) buildTable(table TableReference) error {
switch tab := table.(type) {
case nil:
u.quote(u.model.TableName)
case Table:
model, err := u.r.Get(tab.entity)
if err != nil {
return err
}
u.quote(model.TableName)
if tab.alias != "" {
u.sqlBuilder.WriteString(" AS ")
u.quote(tab.alias)
}
default:
return errs.NewErrUnsupportedExpressionType(tab)
}
return nil
}
func (u *Updater[T]) buildAssignment(assign Assignment) error {
if err := u.buildColumn(nil, assign.Column.name); err != nil {
return err
}
u.sqlBuilder.WriteByte('=')
return u.buildExpression(assign.val)
}
func (u *Updater[T]) Where(ps ...Predicate) *Updater[T] {
u.where = ps
return u
}
func (u *Updater[T]) Exec(ctx context.Context) Result {
return exec(ctx, u.sess, u.core, &QueryContext{Builder: u, Type: "UPDATE"})
}
func AssignNotNilColumns(entity interface{}) []Assignable {
return AssignColumns(entity, func(typ reflect.StructField, val reflect.Value) bool {
switch val.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return !val.IsNil()
}
return true
})
}
func AssignNotZeroColumns(entity interface{}) []Assignable {
return AssignColumns(entity, func(typ reflect.StructField, val reflect.Value) bool {
return !val.IsZero()
})
}
func AssignColumns(entity interface{}, filter func(typ reflect.StructField, val reflect.Value) bool) []Assignable {
val := reflect.ValueOf(entity).Elem()
typ := reflect.TypeOf(entity).Elem()
numField := val.NumField()
res := make([]Assignable, 0, numField)
for i := 0; i < numField; i++ {
fieldVal := val.Field(i)
fieldTyp := typ.Field(i)
if filter(fieldTyp, fieldVal) {
res = append(res, Assign(fieldTyp.Name, fieldVal.Interface()))
}
}
return res
}