-
Notifications
You must be signed in to change notification settings - Fork 4
/
modify.go
190 lines (145 loc) · 4.44 KB
/
modify.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
package crud
import (
"fmt"
"reflect"
"strings"
"time"
)
/*
Update syncs a tagged object with an existing record in the database.
The metadata contained in the crud tags don't include the table name or
the name of the SQL primary ID, so they have to be passed in manually.
If the object passed in as arg does not have a primary key set (or the
value is 0), an error is returned.
*/
func Update(db DbIsh, table, sqlIdFieldName string, arg interface{}) error {
val := indirectV(reflect.ValueOf(arg))
ty := val.Type()
fieldMap, er := sqlToGoFields(ty)
if er != nil {
return er
}
sqlFields := make([]string, len(fieldMap))[:0]
newValues := make([]interface{}, len(fieldMap))[:0]
var id int64 = 0
for sqlName, meta := range fieldMap {
if meta.ReadOnly {
continue
}
goName := meta.GoName
if sqlName == sqlIdFieldName {
id = val.FieldByName(goName).Int()
} else {
fieldVal := val.FieldByName(goName).Interface()
if timeVal, ok := fieldVal.(time.Time); ok && meta.Unix {
fieldVal = timeVal.Unix()
} else if timeVal, ok := fieldVal.(*time.Time); ok && meta.Unix && timeVal != nil {
fieldVal = timeVal.Unix()
}
sqlFields = append(sqlFields, fmt.Sprintf("%s = ?", sqlName))
newValues = append(newValues, fieldVal)
}
}
if id == 0 {
return fmt.Errorf("%s is 0 or not set, cannot update", sqlIdFieldName)
}
newValues = append(newValues, id)
q := fmt.Sprintf("UPDATE %s SET %s WHERE %s = ?", table, strings.Join(sqlFields, ", "), sqlIdFieldName)
_, er = db.Exec(q, newValues...)
return er
}
/*
Insert creates a new record in the datastore.
*/
func Insert(db DbIsh, table, sqlIdFieldName string, arg interface{}) (int64, error) {
val := indirectV(reflect.ValueOf(arg))
ty := val.Type()
fieldMap, er := sqlToGoFields(ty)
if er != nil {
return 0, er
}
sqlFields := make([]string, len(fieldMap))[:0]
newValues := make([]interface{}, len(fieldMap))[:0]
placeholders := make([]string, len(fieldMap))[:0]
placeholderN := 1
for sqlName, meta := range fieldMap {
if meta.ReadOnly {
continue
}
if sqlName == sqlIdFieldName {
continue
}
goName := meta.GoName
fieldVal := val.FieldByName(goName).Interface()
if timeVal, ok := fieldVal.(time.Time); ok && meta.Unix {
fieldVal = timeVal.Unix()
} else if timeVal, ok := fieldVal.(*time.Time); ok && meta.Unix && timeVal != nil {
fieldVal = timeVal.Unix()
}
sqlFields = append(sqlFields, sqlName)
newValues = append(newValues, fieldVal)
placeholders = append(placeholders, PlaceholderFormat(placeholderN))
placeholderN++
}
q := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", table, strings.Join(sqlFields, ", "), strings.Join(placeholders, ", "))
res, er := db.Exec(q, newValues...)
if er != nil {
fmt.Print(q)
return 0, er
}
id, err := res.LastInsertId()
if err != nil && NoLastInsertId {
return 0, nil
}
return id, nil
}
/*
Upsert creates a new record in the datastore.
*/
func Upsert(db DbIsh, table, sqlIdFieldName string, arg interface{}) (int64, error) {
if !EnableUpsert {
return Insert(db, table, sqlIdFieldName, arg)
}
val := indirectV(reflect.ValueOf(arg))
ty := val.Type()
fieldMap, er := sqlToGoFields(ty)
if er != nil {
return 0, er
}
sqlFields := make([]string, len(fieldMap))[:0]
newValues := make([]interface{}, len(fieldMap))[:0]
placeholders := make([]string, len(fieldMap))[:0]
placeholderN := 1
sqlUpsertFields := make([]string, len(fieldMap))[:0]
for sqlName, meta := range fieldMap {
if meta.ReadOnly {
continue
}
if sqlName == sqlIdFieldName {
continue
}
goName := meta.GoName
fieldVal := val.FieldByName(goName).Interface()
if timeVal, ok := fieldVal.(time.Time); ok && meta.Unix {
fieldVal = timeVal.Unix()
} else if timeVal, ok := fieldVal.(*time.Time); ok && meta.Unix && timeVal != nil {
fieldVal = timeVal.Unix()
}
sqlFields = append(sqlFields, sqlName)
newValues = append(newValues, fieldVal)
placeholders = append(placeholders, PlaceholderFormat(placeholderN))
placeholderN++
sqlUpsertFields = append(sqlUpsertFields, fmt.Sprintf("%s = ?", sqlName))
}
q := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s", table, strings.Join(sqlFields, ", "), strings.Join(placeholders, ", "), strings.Join(sqlUpsertFields, ", "))
newValues = append(newValues, newValues...)
res, er := db.Exec(q, newValues...)
if er != nil {
return 0, er
}
id, err := res.LastInsertId()
if err != nil && NoLastInsertId {
return 0, nil
}
return id, err
}