forked from dlintw/GoRM
/
gorm.go
262 lines (224 loc) · 5.79 KB
/
gorm.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package gorm
import (
"errors"
"fmt"
"strings"
"reflect"
"exp/sql"
)
type columnValue struct {
Val interface{}
}
func (c *columnValue) ScanInto(value interface{}) error {
// TODO: handle types better
switch v := value.(type) {
case []byte:
// since v is volatile, we must create a copy
c.Val = string(v)
default:
c.Val = v
}
return nil
}
type Conn struct {
DB *sql.DB
}
func (c *Conn) Close() error {
return c.DB.Close()
}
func NewConnection(driverName, dataSource string) (*Conn, error) {
db, err := sql.Open(driverName, dataSource)
return &Conn{db}, err
}
type Session struct {
*Conn
*sql.Tx
}
func (c *Conn) NewSession() (*Session, error) {
tx, err := c.DB.Begin()
if err != nil {
return nil, err
}
return &Session{c, tx}, nil
}
// This should only be called after a Commit or Rollback
func (s *Session) Renew() error {
// since a transaction's life ends after a commit
// or rollback, we must create a new one
tx, err := s.Conn.DB.Begin()
if err != nil {
return err
}
s.Tx = tx
return nil
}
func getTableName(obj interface{}) string {
return pluralizeString(snakeCasedName(getTypeName(obj)))
}
func (s *Session) getResultsForQuery(tableName string, args ...interface{}) (results []map[string]interface{}, err error) {
queryArgs := []interface{}{}
condition := ""
if len(args) >= 1 {
condition, _ = args[0].(string)
if len(args) > 1 {
queryArgs = args[1:]
}
}
query := fmt.Sprintf("select * from %v %v",
tableName,
condition)
rows, err := s.Conn.DB.Query(query, queryArgs...)
if err != nil {
return nil, err
}
columnNames, err := rows.Columns()
if err != nil {
return nil, err
}
numColumns := len(columnNames)
for rows.Next() {
// create a slice the size of the number of
// columns
row := make([]*columnValue, numColumns)
for i := 0; i < numColumns; i++ {
row[i] = &columnValue{nil}
}
// store them in a slice of interfaces to send to scan
rowAsInterfaces := make([]interface{}, numColumns)
for i, cv := range row {
rowAsInterfaces[i] = cv
}
// populate the row slice
err := rows.Scan(rowAsInterfaces...) // ...
if err != nil {
return nil, err
}
// a result has the column name as the key
// and column val as the value
result := make(map[string]interface{})
// store each column in the map
for columnNum, val := range rowAsInterfaces {
cv := val.(*columnValue)
// store a copy of the val in the result map
columnName := columnNames[columnNum]
result[columnName] = cv.Val
}
results = append(results, result)
}
return results, nil
}
func (s *Session) insert(tableName string, properties map[string]interface{}) (int64, error) {
var keys []string
var placeholders []string
var args []interface{}
for key, val := range properties {
keys = append(keys, key)
placeholders = append(placeholders, "?")
args = append(args, val)
}
stmt := fmt.Sprintf("insert into %v (%v) values (%v)",
tableName,
strings.Join(keys, ", "),
strings.Join(placeholders, ", "))
res, err := s.Tx.Exec(stmt, args...)
if err != nil {
return -1, err
}
id, err := res.LastInsertId()
if err != nil {
return -1, err
}
return id, nil
}
func (s *Session) Update(rowStruct interface{}) error {
results, _ := scanStructIntoMap(rowStruct)
tableName := getTableName(rowStruct)
id := results["id"].(int64)
delete(results, "id")
if id == 0 {
id, err := s.insert(tableName, results)
if err != nil {
return nil
}
structPtr := reflect.ValueOf(rowStruct)
structVal := structPtr.Elem()
structField := structVal.FieldByName("Id")
structField.Set(reflect.ValueOf(id))
return nil
}
var updates []string
var args []interface{}
for key, val := range results {
updates = append(updates, fmt.Sprintf("%v = ?", key))
args = append(args, val)
}
stmt := fmt.Sprintf("update %v set %v where id = %v",
tableName,
strings.Join(updates, ", "),
id)
_, err := s.Tx.Exec(stmt, args...)
return err
}
func (s *Session) Save(rowStruct interface{}) error {
err := s.Update(rowStruct)
if err != nil {
return err
}
return s.Tx.Commit()
}
func (s *Session) Get(rowStruct interface{}, args ...interface{}) error {
conditionStr := ""
switch args[0].(type) {
case int:
// add the condition string to the beginning of the args
// slice so the int comes second
conditionStr = "id = ?"
ci := reflect.ValueOf(conditionStr).Interface()
args = append([]interface{}{},
append([]interface{}{ci}, args...)...)
}
// modify the condition so it's part of a where clause
args[0] = reflect.ValueOf(fmt.Sprintf("where %v", args[0])).Interface()
resultsSlice, err := s.getResultsForQuery(getTableName(rowStruct), args...)
if err != nil {
return err
}
switch len(resultsSlice) {
case 0:
return errors.New("did not find any results")
case 1:
results := resultsSlice[0]
scanMapIntoStruct(rowStruct, results)
default:
return errors.New("more than one row matched")
}
return nil
}
// TODO: test
func (s *Session) GetAll(rowsSlicePtr interface{}, args ...interface{}) error {
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
if sliceValue.Kind() != reflect.Slice {
return errors.New("needs a pointer to a slice")
}
sliceElementType := sliceValue.Type().Elem()
queryArgs := []interface{}{}
condition := ""
if len(args) >= 1 {
condition = strings.TrimSpace(args[0].(string))
condition = fmt.Sprintf("where %v", condition)
if len(args) > 1 {
queryArgs = args[1:]
}
}
resultsSlice, err := s.getResultsForQuery(getTableName(rowsSlicePtr), condition, queryArgs)
if err != nil {
return err
}
for _, results := range resultsSlice {
newValue := reflect.Zero(sliceElementType)
//println("newValue = ", sliceElementType.String())
scanMapIntoStruct(newValue.Addr().Interface(), results)
sliceValue.Set(reflect.Append(sliceValue, newValue))
}
return nil
}