forked from mgutz/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
174 lines (150 loc) · 4.16 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
package dat
import (
"bytes"
"reflect"
)
// InsertBuilder contains the clauses for an INSERT statement
type InsertBuilder struct {
Execer
isInterpolated bool
table string
cols []string
isBlacklist bool
vals [][]interface{}
records []interface{}
returnings []string
}
// NewInsertBuilder creates a new InsertBuilder for the given table.
func NewInsertBuilder(table string) *InsertBuilder {
if table == "" {
logger.Error("InsertInto requires a table name.")
return nil
}
return &InsertBuilder{table: table, isInterpolated: EnableInterpolation}
}
// Columns appends columns to insert in the statement
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
return b.Whitelist(columns...)
}
// Blacklist defines a blacklist of columns and should only be used
// in conjunction with Record.
func (b *InsertBuilder) Blacklist(columns ...string) *InsertBuilder {
b.isBlacklist = true
b.cols = columns
return b
}
// Whitelist defines a whitelist of columns to be inserted. To
// specify all columns of a record use "*".
func (b *InsertBuilder) Whitelist(columns ...string) *InsertBuilder {
b.cols = columns
return b
}
// Values appends a set of values to the statement
func (b *InsertBuilder) Values(vals ...interface{}) *InsertBuilder {
b.vals = append(b.vals, vals)
return b
}
// Record pulls in values to match Columns from the record
func (b *InsertBuilder) Record(record interface{}) *InsertBuilder {
b.records = append(b.records, record)
return b
}
// Returning sets the columns for the RETURNING clause
func (b *InsertBuilder) Returning(columns ...string) *InsertBuilder {
b.returnings = columns
return b
}
// Pair adds a key/value pair to the statement
func (b *InsertBuilder) Pair(column string, value interface{}) *InsertBuilder {
b.cols = append(b.cols, column)
lenVals := len(b.vals)
if lenVals == 0 {
args := []interface{}{value}
b.vals = [][]interface{}{args}
} else if lenVals == 1 {
b.vals[0] = append(b.vals[0], value)
} else {
panic("pair only allows you to specify 1 record to insert")
}
return b
}
// ToSQL serialized the InsertBuilder to a SQL string
// It returns the string with placeholders and a slice of query arguments
func (b *InsertBuilder) ToSQL() (string, []interface{}) {
if len(b.table) == 0 {
panic("no table specified")
}
lenCols := len(b.cols)
lenRecords := len(b.records)
if lenCols == 0 {
panic("no columns specified")
}
if len(b.vals) == 0 && lenRecords == 0 {
panic("no values or records specified")
}
if lenRecords == 0 && b.cols[0] == "*" {
panic(`"*" can only be used in conjunction with Record`)
}
if lenRecords == 0 && b.isBlacklist {
panic(`Blacklist can only be used in conjunction with Record`)
}
// reflect fields removing blacklisted columns
if lenRecords > 0 && b.isBlacklist {
b.cols = reflectExcludeColumns(b.records[0], b.cols)
}
// reflect all fields
if lenRecords > 0 && b.cols[0] == "*" {
b.cols = reflectColumns(b.records[0])
}
var sql bytes.Buffer
var args []interface{}
sql.WriteString("INSERT INTO ")
sql.WriteString(b.table)
sql.WriteString(" (")
for i, c := range b.cols {
if i > 0 {
sql.WriteRune(',')
}
Dialect.WriteIdentifier(&sql, c)
}
sql.WriteString(") VALUES ")
start := 1
// Go thru each value we want to insert. Write the placeholders, and collect args
for i, row := range b.vals {
if i > 0 {
sql.WriteRune(',')
}
buildPlaceholders(&sql, start, len(row))
for _, v := range row {
args = append(args, v)
start++
}
}
anyVals := len(b.vals) > 0
// Go thru the records. Write the placeholders, and do reflection on the records to extract args
for i, rec := range b.records {
if i > 0 || anyVals {
sql.WriteRune(',')
}
ind := reflect.Indirect(reflect.ValueOf(rec))
vals, err := valuesFor(ind.Type(), ind, b.cols)
if err != nil {
panic(err.Error())
}
buildPlaceholders(&sql, start, len(vals))
for _, v := range vals {
args = append(args, v)
start++
}
}
// Go thru the returning clauses
for i, c := range b.returnings {
if i == 0 {
sql.WriteString(" RETURNING ")
} else {
sql.WriteRune(',')
}
Dialect.WriteIdentifier(&sql, c)
}
return sql.String(), args
}