-
Notifications
You must be signed in to change notification settings - Fork 28
/
insert.go
205 lines (177 loc) · 5.26 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
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
package sqlingo
import (
"context"
"database/sql"
"errors"
"fmt"
"reflect"
)
type insertStatus struct {
method string
scope scope
fields []Field
values []interface{}
models []interface{}
onDuplicateKeyUpdateAssignments []assignment
ctx context.Context
}
type insertWithTable interface {
Fields(fields ...Field) insertWithValues
Values(values ...interface{}) insertWithValues
Models(models ...interface{}) insertWithModels
}
type insertWithValues interface {
toInsertWithContext
toInsertFinal
Values(values ...interface{}) insertWithValues
OnDuplicateKeyIgnore() toInsertFinal
OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin
}
type insertWithModels interface {
toInsertWithContext
toInsertFinal
Models(models ...interface{}) insertWithModels
OnDuplicateKeyIgnore() toInsertFinal
OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin
}
type insertWithOnDuplicateKeyUpdateBegin interface {
Set(Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
SetIf(condition bool, Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
}
type insertWithOnDuplicateKeyUpdate interface {
toInsertWithContext
toInsertFinal
Set(Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
SetIf(condition bool, Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
}
type toInsertWithContext interface {
WithContext(ctx context.Context) toInsertFinal
}
type toInsertFinal interface {
GetSQL() (string, error)
Execute() (result sql.Result, err error)
}
func (d *database) InsertInto(table Table) insertWithTable {
return insertStatus{method: "INSERT", scope: scope{Database: d, Tables: []Table{table}}}
}
func (d *database) ReplaceInto(table Table) insertWithTable {
return insertStatus{method: "REPLACE", scope: scope{Database: d, Tables: []Table{table}}}
}
func (s insertStatus) Fields(fields ...Field) insertWithValues {
s.fields = fields
return s
}
func (s insertStatus) Values(values ...interface{}) insertWithValues {
s.values = append([]interface{}{}, s.values...)
s.values = append(s.values, values)
return s
}
func addModel(models *[]Model, model interface{}) error {
if model, ok := model.(Model); ok {
*models = append(*models, model)
return nil
}
value := reflect.ValueOf(model)
switch value.Kind() {
case reflect.Ptr:
value = reflect.Indirect(value)
return addModel(models, value.Interface())
case reflect.Slice, reflect.Array:
for i := 0; i < value.Len(); i++ {
elem := value.Index(i)
addr := elem.Addr()
inter := addr.Interface()
if err := addModel(models, inter); err != nil {
return err
}
}
return nil
default:
return fmt.Errorf("unknown model type (kind = %d)", value.Kind())
}
}
func (s insertStatus) Models(models ...interface{}) insertWithModels {
s.models = models
return s
}
func (s insertStatus) OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin {
return s
}
func (s insertStatus) SetIf(condition bool, field Field, value interface{}) insertWithOnDuplicateKeyUpdate {
if condition {
return s.Set(field, value)
}
return s
}
func (s insertStatus) Set(field Field, value interface{}) insertWithOnDuplicateKeyUpdate {
s.onDuplicateKeyUpdateAssignments = append([]assignment{}, s.onDuplicateKeyUpdateAssignments...)
s.onDuplicateKeyUpdateAssignments = append(s.onDuplicateKeyUpdateAssignments, assignment{
field: field,
value: value,
})
return s
}
func (s insertStatus) OnDuplicateKeyIgnore() toInsertFinal {
firstField := s.scope.Tables[0].GetFields()[0]
return s.OnDuplicateKeyUpdate().Set(firstField, firstField)
}
func (s insertStatus) GetSQL() (string, error) {
var fields []Field
var values []interface{}
if len(s.models) > 0 {
models := make([]Model, 0, len(s.models))
for _, model := range s.models {
if err := addModel(&models, model); err != nil {
return "", err
}
}
if len(models) > 0 {
fields = models[0].GetTable().GetFields()
for _, model := range models {
if model.GetTable().GetName() != s.scope.Tables[0].GetName() {
return "", errors.New("invalid table from model")
}
values = append(values, model.GetValues())
}
}
} else {
if len(s.fields) == 0 {
fields = s.scope.Tables[0].GetFields()
} else {
fields = s.fields
}
values = s.values
}
if len(values) == 0 {
return "/* INSERT without VALUES */ DO 0", nil
}
tableSql := s.scope.Tables[0].GetSQL(s.scope)
fieldsSql, err := commaFields(s.scope, fields)
if err != nil {
return "", err
}
valuesSql, err := commaValues(s.scope, values)
if err != nil {
return "", err
}
sqlString := s.method + " INTO " + tableSql + " (" + fieldsSql + ") VALUES " + valuesSql
if len(s.onDuplicateKeyUpdateAssignments) > 0 {
assignmentsSql, err := commaAssignments(s.scope, s.onDuplicateKeyUpdateAssignments)
if err != nil {
return "", err
}
sqlString += " ON DUPLICATE KEY UPDATE " + assignmentsSql
}
return sqlString, nil
}
func (s insertStatus) WithContext(ctx context.Context) toInsertFinal {
s.ctx = ctx
return s
}
func (s insertStatus) Execute() (result sql.Result, err error) {
sqlString, err := s.GetSQL()
if err != nil {
return nil, err
}
return s.scope.Database.ExecuteContext(s.ctx, sqlString)
}