-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
202 lines (170 loc) · 4.32 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
package tyr
import (
"reflect"
"strings"
"github.com/kubuskotak/tyr/dialect"
)
// InsertStmt builds `INSERT INTO ...`.
type InsertStmt struct {
Dialect
raw
Table string
Column []string
Value [][]interface{}
Ignored bool
ReturnColumn []string
RecordID *int64
comments Comments
}
type InsertBuilder = InsertStmt
func (b *InsertStmt) ToSQL(d Dialect, i Buffer) error {
builder := NewBuffer()
_ = b.Build(d, builder)
return interpolateSql(d, i, builder.String(), builder.Value())
}
func (b *InsertStmt) Build(d Dialect, buf Buffer) error {
if b.raw.Query != "" {
return b.raw.Build(d, buf)
}
if b.Table == "" {
return ErrTableNotSpecified
}
if len(b.Column) == 0 {
return ErrColumnNotSpecified
}
err := b.comments.Build(d, buf)
if err != nil {
return err
}
if b.Ignored {
_, _ = buf.WriteString("INSERT IGNORE INTO ")
} else {
_, _ = buf.WriteString("INSERT INTO ")
}
_, _ = buf.WriteString(d.QuoteIdent(b.Table))
var placeholderBuf strings.Builder
placeholderBuf.WriteString("(")
_, _ = buf.WriteString(" (")
for i, col := range b.Column {
if i > 0 {
_, _ = buf.WriteString(",")
placeholderBuf.WriteString(",")
}
_, _ = buf.WriteString(d.QuoteIdent(col))
placeholderBuf.WriteString(placeholder)
}
_, _ = buf.WriteString(")")
if d == dialect.MSSQL && len(b.ReturnColumn) > 0 {
_, _ = buf.WriteString(" OUTPUT ")
for i, col := range b.ReturnColumn {
if i > 0 {
_, _ = buf.WriteString(",")
}
_, _ = buf.WriteString("INSERTED." + d.QuoteIdent(col))
}
}
_, _ = buf.WriteString(" VALUES ")
placeholderBuf.WriteString(")")
placeholderStr := placeholderBuf.String()
for i, tuple := range b.Value {
if i > 0 {
_, _ = buf.WriteString(", ")
}
_, _ = buf.WriteString(placeholderStr)
_ = buf.WriteValue(tuple...)
}
if d != dialect.MSSQL && len(b.ReturnColumn) > 0 {
_, _ = buf.WriteString(" RETURNING ")
for i, col := range b.ReturnColumn {
if i > 0 {
_, _ = buf.WriteString(",")
}
_, _ = buf.WriteString(d.QuoteIdent(col))
}
}
return nil
}
// InsertInto creates an InsertStmt.
func InsertInto(table string) *InsertStmt {
return &InsertStmt{
Table: table,
}
}
// InsertBySql creates an InsertStmt from raw query.
func InsertBySql(query string, value ...interface{}) *InsertStmt {
return &InsertStmt{
raw: raw{
Query: query,
Value: value,
},
}
}
func (b *InsertStmt) Columns(column ...string) *InsertStmt {
b.Column = column
return b
}
// Comment adds a comment to prepended. All multi-line sql comment characters are stripped
func (b *InsertStmt) Comment(comment string) *InsertStmt {
b.comments = b.comments.Append(comment)
return b
}
// Ignore any insertion errors
func (b *InsertStmt) Ignore() *InsertStmt {
b.Ignored = true
return b
}
// Values adds a tuple to be inserted.
// The order of the tuple should match Columns.
func (b *InsertStmt) Values(value ...interface{}) *InsertStmt {
b.Value = append(b.Value, value)
return b
}
// Record adds a tuple for columns from a struct.
//
// If there is a field called "Id" or "ID" in the struct,
// it will be set to LastInsertId.
func (b *InsertStmt) Record(structValue interface{}) *InsertStmt {
v := reflect.Indirect(reflect.ValueOf(structValue))
if v.Kind() == reflect.Struct {
found := make([]interface{}, len(b.Column)+1)
// ID is recommended by golint here
s := newTagStore()
s.findValueByName(v, append(b.Column, "id"), found, false)
value := found[:len(found)-1]
for i, v := range value {
if v != nil {
value[i] = v.(reflect.Value).Interface()
}
}
if v.CanSet() {
switch idField := found[len(found)-1].(type) {
case reflect.Value:
if idField.Kind() == reflect.Int64 {
b.RecordID = idField.Addr().Interface().(*int64)
}
default:
}
}
b.Values(value...)
}
return b
}
// Returning specifies the returning columns for postgres/mssql.
func (b *InsertStmt) Returning(column ...string) *InsertStmt {
b.ReturnColumn = column
return b
}
// Pair adds (column, value) to be inserted.
// It is an error to mix Pair with Values and Record.
func (b *InsertStmt) Pair(column string, value interface{}) *InsertStmt {
b.Column = append(b.Column, column)
switch len(b.Value) {
case 0:
b.Values(value)
case 1:
b.Value[0] = append(b.Value[0], value)
default:
panic("pair only allows one record to insert")
}
return b
}