forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_builder.go
168 lines (139 loc) · 3.36 KB
/
insert_builder.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
package dbr
import (
"database/sql"
"reflect"
)
type PreInsertHook func(*InsertBuilder, interface{}) *InsertBuilder
type InsertBuilder struct {
runner
EventReceiver
Dialect Dialect
RecordID reflect.Value
*InsertStmt
PreInsertHooks []PreInsertHook
}
func (sess *Session) InsertInto(table string) *InsertBuilder {
return &InsertBuilder{
runner: sess,
EventReceiver: sess,
Dialect: sess.Dialect,
InsertStmt: InsertInto(table),
}
}
func (tx *Tx) InsertInto(table string) *InsertBuilder {
return &InsertBuilder{
runner: tx,
EventReceiver: tx,
Dialect: tx.Dialect,
InsertStmt: InsertInto(table),
}
}
func (sess *Session) InsertBySql(query string, value ...interface{}) *InsertBuilder {
return &InsertBuilder{
runner: sess,
EventReceiver: sess,
Dialect: sess.Dialect,
InsertStmt: InsertBySql(query, value...),
}
}
func (tx *Tx) InsertBySql(query string, value ...interface{}) *InsertBuilder {
return &InsertBuilder{
runner: tx,
EventReceiver: tx,
Dialect: tx.Dialect,
InsertStmt: InsertBySql(query, value...),
}
}
func (b *InsertBuilder) ToSql() (string, []interface{}) {
buf := NewBuffer()
err := b.Build(b.Dialect, buf)
if err != nil {
panic(err)
}
return buf.String(), buf.Value()
}
func (b *InsertBuilder) Pair(column string, value interface{}) *InsertBuilder {
b.Column = append(b.Column, column)
switch len(b.Value) {
case 0:
b.InsertStmt.Values(value)
case 1:
b.Value[0] = append(b.Value[0], value)
default:
panic("pair only allows one record to insert")
}
return b
}
func (b *InsertBuilder) Exec() (sql.Result, error) {
result, err := exec(b.runner, b.EventReceiver, b, b.Dialect)
if err != nil {
return nil, err
}
if b.RecordID.IsValid() {
if id, err := result.LastInsertId(); err == nil {
b.RecordID.SetInt(id)
}
}
return result, nil
}
func (b *InsertBuilder) Columns(column ...string) *InsertBuilder {
b.InsertStmt.Columns(column...)
return b
}
func (b *InsertBuilder) Record(structValue interface{}) *InsertBuilder {
v := reflect.Indirect(reflect.ValueOf(structValue))
if v.Kind() == reflect.Struct && v.CanSet() {
// ID is recommended by golint here
for _, name := range []string{"Id", "ID"} {
field := v.FieldByName(name)
if field.IsValid() && field.Kind() == reflect.Int64 {
b.RecordID = field
break
}
}
}
if len(b.PreInsertHooks) > 0 {
for _, f := range b.PreInsertHooks {
f(b, structValue)
}
}
b.InsertStmt.Record(structValue)
return b
}
func (b *InsertBuilder) Records(structValues interface{}) *InsertBuilder {
v := reflect.ValueOf(structValues)
s := reflect.Value{}
switch v.Kind() {
case reflect.Ptr:
s = v.Elem()
if s.Kind() != reflect.Slice {
return b
}
case reflect.Slice:
s = v
default:
return b
}
//t := v.Type().Elem()
sLen := s.Len()
for i := 0; i < sLen; i++ {
structValue := s.Index(i).Addr().Interface()
if len(b.PreInsertHooks) > 0 {
for _, f := range b.PreInsertHooks {
f(b, structValue)
}
}
b.InsertStmt.Record(structValue)
}
return b
}
func (b *InsertBuilder) Values(value ...interface{}) *InsertBuilder {
b.InsertStmt.Values(value...)
return b
}
func (b *InsertBuilder) SetPreInsertHooks(f ...PreInsertHook) {
b.PreInsertHooks = append(b.PreInsertHooks, f...)
}
func (b *InsertBuilder) ClearPreInsertHooks() {
b.PreInsertHooks = []PreInsertHook{}
}