-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
164 lines (145 loc) · 2.99 KB
/
insert.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
package orm
import (
"context"
"github.com/valyala/bytebufferpool"
"orm/internal/errs"
"orm/model"
)
type OnConflictBuilder[T any] struct {
i *Inserter[T]
conflictColumns []string
}
type OnConflict struct {
assigns []Assignable
conflictColumns []string
}
type Inserter[T any] struct {
Builder
sess session
values []*T
columns []string
// 方案二
onConflict *OnConflict
// 方案一
// onDuplicate []Assignable
}
func NewInserter[T any](sess session) *Inserter[T] {
c := sess.getCore()
return &Inserter[T]{
sess: sess,
Builder: Builder{
core: c,
buffer: bytebufferpool.Get(),
aliasMap: make(map[string]int, 8),
quoter: c.dialect.quoter(),
},
}
}
func (i *Inserter[T]) OnConflictKey() *OnConflictBuilder[T] {
return &OnConflictBuilder[T]{
i: i,
}
}
func (i *Inserter[T]) Columns(cols ...string) *Inserter[T] {
i.columns = cols
return i
}
func (i *Inserter[T]) Values(vals ...*T) *Inserter[T] {
i.values = vals
return i
}
func (o *OnConflictBuilder[T]) ConflictColumns(cols ...string) *OnConflictBuilder[T] {
o.conflictColumns = cols
return o
}
func (o *OnConflictBuilder[T]) Update(assigns ...Assignable) *Inserter[T] {
o.i.onConflict = &OnConflict{
assigns: assigns,
conflictColumns: o.conflictColumns,
}
return o.i
}
func (i *Inserter[T]) Build() (*Query, error) {
if len(i.values) == 0 {
return nil, errs.ErrInsertZeroRow
}
defer bytebufferpool.Put(i.buffer)
var (
t T
err error
)
if i.model == nil {
i.model, err = i.r.Get(&t)
if err != nil {
return nil, err
}
}
i.writeString("INSERT INTO ")
i.quote(i.model.TableName)
fields := i.model.Fields
if len(i.columns) > 0 {
fields = make([]*model.Field, 0, len(i.columns))
for _, col := range i.columns {
fd, ok := i.model.FieldMap[col]
if !ok {
return nil, errs.NewErrUnknownField(col)
}
fields = append(fields, fd)
}
}
i.writeLeftParenthesis()
for idx, fd := range fields {
if idx > 0 {
i.writeComma()
}
i.quote(fd.ColName)
}
i.writeRightParenthesis()
i.writeSpace()
i.writeString("VALUES")
i.args = make([]any, 0, len(fields)*len(i.values)+1)
for vIdx, val := range i.values {
if vIdx > 0 {
i.writeComma()
}
refVal := i.valCreator.NewBasicTypeValue(val, i.model)
i.writeLeftParenthesis()
for fIdx, fd := range fields {
if fIdx > 0 {
i.writeComma()
}
i.writePlaceholder()
fdVal, err := refVal.Field(fd.GoName)
if err != nil {
return nil, err
}
i.addArgs(fdVal)
}
i.writeRightParenthesis()
}
if i.onConflict != nil {
err = i.dialect.buildOnConflict(&i.Builder, i.onConflict)
if err != nil {
return nil, err
}
}
i.end()
return &Query{
SQL: i.buffer.String(),
Args: i.args,
}, nil
}
func (i *Inserter[T]) Exec(ctx context.Context) Result {
if i.model == nil {
m, err := i.r.Get(new(T))
if err != nil {
return Result{err: err}
}
i.model = m
}
return exec[T](ctx, i.core, i.sess, &QueryContext{
Type: "INSERT",
Builder: i,
Meta: i.model,
})
}