forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
107 lines (90 loc) · 2.05 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
package dbr
import (
"bytes"
"reflect"
)
// InsertStmt builds `INSERT INTO ...`
type InsertStmt struct {
raw
Table string
Column []string
Value [][]interface{}
}
// Build builds `INSERT INTO ...` in dialect
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
}
buf.WriteString("INSERT INTO ")
buf.WriteString(d.QuoteIdent(b.Table))
placeholderBuf := new(bytes.Buffer)
placeholderBuf.WriteString("(")
buf.WriteString(" (")
for i, col := range b.Column {
if i > 0 {
buf.WriteString(",")
placeholderBuf.WriteString(",")
}
buf.WriteString(d.QuoteIdent(col))
placeholderBuf.WriteString(d.Placeholder())
}
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...)
}
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,
},
}
}
// Columns adds columns
func (b *InsertStmt) Columns(column ...string) *InsertStmt {
b.Column = column
return b
}
// Values adds a tuple for 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
func (b *InsertStmt) Record(structValue interface{}) *InsertStmt {
v := reflect.Indirect(reflect.ValueOf(structValue))
if v.Kind() == reflect.Struct {
var value []interface{}
m := structMap(v)
for _, key := range b.Column {
if val, ok := m[key]; ok {
value = append(value, val.Interface())
} else {
value = append(value, nil)
}
}
b.Values(value...)
}
return b
}