-
Notifications
You must be signed in to change notification settings - Fork 123
/
insert.go
230 lines (183 loc) · 5.87 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"fmt"
"strings"
)
const (
insertMarkerInit injectionMarker = iota
insertMarkerAfterInsertInto
insertMarkerAfterCols
insertMarkerAfterValues
insertMarkerAfterSelect
)
// NewInsertBuilder creates a new INSERT builder.
func NewInsertBuilder() *InsertBuilder {
return DefaultFlavor.NewInsertBuilder()
}
func newInsertBuilder() *InsertBuilder {
args := &Args{}
return &InsertBuilder{
verb: "INSERT",
args: args,
injection: newInjection(),
}
}
// InsertBuilder is a builder to build INSERT.
type InsertBuilder struct {
verb string
table string
cols []string
values [][]string
args *Args
injection *injection
marker injectionMarker
sbHolder string
}
var _ Builder = new(InsertBuilder)
// InsertInto sets table name in INSERT.
func InsertInto(table string) *InsertBuilder {
return DefaultFlavor.NewInsertBuilder().InsertInto(table)
}
// InsertInto sets table name in INSERT.
func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder {
ib.table = Escape(table)
ib.marker = insertMarkerAfterInsertInto
return ib
}
// InsertIgnoreInto sets table name in INSERT IGNORE.
func InsertIgnoreInto(table string) *InsertBuilder {
return DefaultFlavor.NewInsertBuilder().InsertIgnoreInto(table)
}
// InsertIgnoreInto sets table name in INSERT IGNORE.
func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder {
ib.args.Flavor.PrepareInsertIgnore(table, ib)
return ib
}
// ReplaceInto sets table name and changes the verb of ib to REPLACE.
// REPLACE INTO is a MySQL extension to the SQL standard.
func ReplaceInto(table string) *InsertBuilder {
return DefaultFlavor.NewInsertBuilder().ReplaceInto(table)
}
// ReplaceInto sets table name and changes the verb of ib to REPLACE.
// REPLACE INTO is a MySQL extension to the SQL standard.
func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder {
ib.verb = "REPLACE"
ib.table = Escape(table)
ib.marker = insertMarkerAfterInsertInto
return ib
}
// Cols sets columns in INSERT.
func (ib *InsertBuilder) Cols(col ...string) *InsertBuilder {
ib.cols = EscapeAll(col...)
ib.marker = insertMarkerAfterCols
return ib
}
// Select returns a new SelectBuilder to build a SELECT statement inside the INSERT INTO.
func (isb *InsertBuilder) Select(col ...string) *SelectBuilder {
sb := Select(col...)
isb.sbHolder = isb.args.Add(sb)
return sb
}
// Values adds a list of values for a row in INSERT.
func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder {
placeholders := make([]string, 0, len(value))
for _, v := range value {
placeholders = append(placeholders, ib.args.Add(v))
}
ib.values = append(ib.values, placeholders)
ib.marker = insertMarkerAfterValues
return ib
}
// NumValue returns the number of values to insert.
func (ib *InsertBuilder) NumValue() int {
return len(ib.values)
}
// String returns the compiled INSERT string.
func (ib *InsertBuilder) String() string {
s, _ := ib.Build()
return s
}
// Build returns compiled INSERT string and args.
// They can be used in `DB#Query` of package `database/sql` directly.
func (ib *InsertBuilder) Build() (sql string, args []interface{}) {
return ib.BuildWithFlavor(ib.args.Flavor)
}
// BuildWithFlavor returns compiled INSERT string and args with flavor and initial args.
// They can be used in `DB#Query` of package `database/sql` directly.
func (ib *InsertBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
buf := newStringBuilder()
ib.injection.WriteTo(buf, insertMarkerInit)
if len(ib.values) > 1 && ib.args.Flavor == Oracle {
buf.WriteLeadingString(ib.verb)
buf.WriteString(" ALL")
for _, v := range ib.values {
if len(ib.table) > 0 {
buf.WriteString(" INTO ")
buf.WriteString(ib.table)
}
ib.injection.WriteTo(buf, insertMarkerAfterInsertInto)
if len(ib.cols) > 0 {
buf.WriteLeadingString("(")
buf.WriteStrings(ib.cols, ", ")
buf.WriteString(")")
ib.injection.WriteTo(buf, insertMarkerAfterCols)
}
buf.WriteLeadingString("VALUES ")
values := make([]string, 0, len(ib.values))
values = append(values, fmt.Sprintf("(%v)", strings.Join(v, ", ")))
buf.WriteStrings(values, ", ")
}
buf.WriteString(" SELECT 1 from DUAL")
ib.injection.WriteTo(buf, insertMarkerAfterValues)
return ib.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
}
if len(ib.table) > 0 {
buf.WriteLeadingString(ib.verb)
buf.WriteString(" INTO ")
buf.WriteString(ib.table)
}
ib.injection.WriteTo(buf, insertMarkerAfterInsertInto)
if len(ib.cols) > 0 {
buf.WriteLeadingString("(")
buf.WriteStrings(ib.cols, ", ")
buf.WriteString(")")
ib.injection.WriteTo(buf, insertMarkerAfterCols)
}
if ib.sbHolder != "" {
buf.WriteString(" ")
buf.WriteString(ib.sbHolder)
ib.injection.WriteTo(buf, insertMarkerAfterSelect)
return ib.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
}
if len(ib.values) > 0 {
buf.WriteLeadingString("VALUES ")
values := make([]string, 0, len(ib.values))
for _, v := range ib.values {
values = append(values, fmt.Sprintf("(%v)", strings.Join(v, ", ")))
}
buf.WriteStrings(values, ", ")
}
ib.injection.WriteTo(buf, insertMarkerAfterValues)
return ib.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
}
// SetFlavor sets the flavor of compiled sql.
func (ib *InsertBuilder) SetFlavor(flavor Flavor) (old Flavor) {
old = ib.args.Flavor
ib.args.Flavor = flavor
return
}
// Flavor returns flavor of builder
func (ib *InsertBuilder) Flavor() Flavor {
return ib.args.Flavor
}
// Var returns a placeholder for value.
func (ib *InsertBuilder) Var(arg interface{}) string {
return ib.args.Add(arg)
}
// SQL adds an arbitrary sql to current position.
func (ib *InsertBuilder) SQL(sql string) *InsertBuilder {
ib.injection.SQL(ib.marker, sql)
return ib
}