-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgorm.go
189 lines (150 loc) · 3.92 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
package gorm
import (
"os"
"sdegutis/sqlite"
"fmt"
"strings"
// "log"
"reflect"
)
type Conn struct {
conn *sqlite.Conn
}
func (c *Conn) Close() os.Error {
return c.conn.Close()
}
func OpenDB(filename string) (*Conn, os.Error) {
conn, err := sqlite.Open(filename)
return &Conn{conn: conn}, err
}
func getTableName(obj interface{}) string {
return pluralizeString(snakeCasedName(getTypeName(obj)))
}
func (c *Conn) getResultsForQuery(tableName, condition string, args []interface{}) (resultsSlice []map[string][]byte, err os.Error) {
s, err := c.conn.Prepare(fmt.Sprintf("select * from %v %v", tableName, condition))
if err != nil {
return nil, err
}
defer s.Finalize()
err = s.Exec(args...)
if err != nil {
return nil, err
}
for s.Next() {
results, err := s.ResultsAsMap()
if err != nil {
return nil, err
}
resultsSlice = append(resultsSlice, results)
}
return
}
func (c *Conn) insert(tableName string, properties map[string]interface{}) (int, os.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)
}
statement := fmt.Sprintf("insert into %v (%v) values (%v)",
tableName,
strings.Join(keys, ", "),
strings.Join(placeholders, ", "))
err := c.conn.Exec(statement, args...)
if err != nil {
return -1, err
}
s, err := c.conn.Prepare("select last_insert_rowid()")
if err != nil {
return -1, err
}
defer s.Finalize()
err = s.Exec()
if err != nil {
return -1, err
}
id := -1
if s.Next() {
err := s.Scan(&id)
if err != nil {
return -1, err
}
}
return id, nil
}
func (c *Conn) Save(rowStruct interface{}) os.Error {
results, _ := scanStructIntoMap(rowStruct)
tableName := getTableName(rowStruct)
id := results["id"]
results["id"] = 0, false
if id == 0 {
id, err := c.insert(tableName, results)
if err != nil {
return nil
}
structPtr := reflect.NewValue(rowStruct).(*reflect.PtrValue)
structVal := structPtr.Elem().(*reflect.StructValue)
structField := structVal.FieldByName("Id")
structField.SetValue(reflect.NewValue(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)
}
statement := fmt.Sprintf("update %v set %v where id = %v",
tableName,
strings.Join(updates, ", "),
id)
return c.conn.Exec(statement, args...)
}
func (c *Conn) Get(rowStruct interface{}, condition interface{}, args ...interface{}) os.Error {
conditionStr := ""
switch condition := condition.(type) {
case string:
conditionStr = condition
case int:
conditionStr = "id = ?"
args = append(args, condition)
}
conditionStr = fmt.Sprintf("where %v", conditionStr)
resultsSlice, err := c.getResultsForQuery(getTableName(rowStruct), conditionStr, args)
if err != nil {
return err
}
switch len(resultsSlice) {
case 0:
return os.NewError("did not find any results")
case 1:
results := resultsSlice[0]
scanMapIntoStruct(rowStruct, results)
default:
return os.NewError("more than one row matched")
}
return nil
}
func (c *Conn) GetAll(rowsSlicePtr interface{}, condition string, args ...interface{}) os.Error {
sliceValue, ok := reflect.Indirect(reflect.NewValue(rowsSlicePtr)).(*reflect.SliceValue)
if !ok {
return os.NewError("needs a pointer to a slice")
}
sliceElementType := sliceValue.Type().(*reflect.SliceType).Elem()
condition = strings.TrimSpace(condition)
if len(condition) > 0 {
condition = fmt.Sprintf("where %v", condition)
}
resultsSlice, err := c.getResultsForQuery(getTableName(rowsSlicePtr), condition, args)
if err != nil {
return err
}
for _, results := range resultsSlice {
newValue := reflect.MakeZero(sliceElementType)
scanMapIntoStruct(newValue.Addr().Interface(), results)
sliceValue.SetValue(reflect.Append(sliceValue, newValue))
}
return nil
}