-
Notifications
You must be signed in to change notification settings - Fork 4
/
connection.go
executable file
·235 lines (181 loc) · 4.78 KB
/
connection.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
231
232
233
234
235
package torm
import (
"database/sql"
"errors"
"log"
"reflect"
"github.com/thinkoner/torm/grammar"
)
type Connection struct {
DB *sql.DB
tablePrefix string
}
// Table Begin a fluent query against a database table.
func (c *Connection) Table(table string) *Builder {
return c.Query().From(table)
}
// Query Get a new query builder instance.
func (c *Connection) Query() *Builder {
return NewBuilder(c, c.GetQueryGrammar())
}
// SelectOne Run a select statement and return a single result.
func (c *Connection) SelectOne(query string, bindings []interface{}, dest interface{}) error {
return c.Select(query, bindings, dest)
}
// Select Run a select statement against the database.
func (c *Connection) Select(query string, bindings []interface{}, dest interface{}) error {
log.Println(query)
var err error
stmt, err := c.DB.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
rows, err := stmt.Query(bindings...)
if err != nil {
return err
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return err
}
results := reflect.Indirect(reflect.ValueOf(dest))
kind := results.Kind()
if kind != reflect.Slice && kind != reflect.Struct {
return errors.New("unsupported destination, should be slice or struct")
}
var isPtr bool
var resultType reflect.Type
var resultValue reflect.Value
resultType = results.Type()
resultValue = results
if kind == reflect.Slice {
resultType = resultType.Elem()
}
if resultType.Kind() == reflect.Ptr {
resultType = resultType.Elem()
isPtr = true
}
if kind == reflect.Slice {
resultValue = reflect.New(resultType).Elem()
}
var fields []*Field
for i := 0; i < resultType.NumField(); i++ {
fields = append(fields, NewField(resultValue.Field(i), resultType.Field(i)))
}
for rows.Next() {
err := c.scan(rows, columns, fields)
if err != nil {
return err
}
if kind == reflect.Slice {
if isPtr {
resultValue = resultValue.Addr()
}
results.Set(reflect.Append(results, resultValue))
} else {
break
}
}
return nil
}
func (c *Connection) Scan(query string, bindings []interface{}, dest ...interface{}) error {
log.Println(query)
var err error
stmt, err := c.DB.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
rows, err := stmt.Query(bindings...)
if err != nil {
return err
}
defer rows.Close()
rows.Next()
return rows.Scan(dest...)
}
// Insert Run an insert statement against the database.
func (c *Connection) Insert(query string, args ...interface{}) (int64, int64, error) {
return c.affectingStatement(query, args...)
}
// Update Run an update statement against the database.
func (c *Connection) Update(query string, args ...interface{}) (int64, error) {
_, affected, err := c.affectingStatement(query, args...)
return affected, err
}
// Delete Run a delete statement against the database.
func (c *Connection) Delete(query string, args ...interface{}) (int64, error) {
_, affected, err := c.affectingStatement(query, args...)
return affected, err
}
// Statement Execute an SQL statement and return the boolean result.
func (c *Connection) Statement(query string, args ...interface{}) error {
var err error
stmt, err := c.DB.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(args...)
return err
}
func (c *Connection) GetQueryGrammar() grammar.Grammar {
return &grammar.MySqlGrammar{}
}
// AffectingStatement Run an SQL statement and get the number of rows affected.
func (c *Connection) affectingStatement(query string, args ...interface{}) (int64, int64, error) {
var err error
log.Println(query)
stmt, err := c.DB.Prepare(query)
if err != nil {
return 0, 0, err
}
defer stmt.Close()
result, err := stmt.Exec(args...)
if err != nil {
return 0, 0, err
}
affected, err := result.RowsAffected()
if err != nil {
return 0, affected, err
}
insertId, err := result.LastInsertId()
return insertId, affected, err
}
func (c *Connection) scan(rows *sql.Rows, columns []string, fields []*Field) error {
count := len(columns)
resets := make(map[int]*Field)
values := make([]interface{}, count)
args := make([]interface{}, count)
for i, column := range columns {
args[i] = &values[i]
for _, field := range fields {
if field.Ignored {
continue
}
if field.Name != column {
continue
}
if field.Value.Kind() == reflect.Ptr {
args[i] = field.Value.Addr().Interface()
} else {
reflectValue := reflect.New(reflect.PtrTo(field.StructField.Type))
reflectValue.Elem().Set(field.Value.Addr())
args[i] = reflectValue.Interface()
resets[i] = field
}
}
}
err := rows.Scan(args...)
if err != nil {
return err
}
for index, field := range resets {
if v := reflect.ValueOf(args[index]).Elem().Elem(); v.IsValid() {
field.Value.Set(v)
}
}
return nil
}