-
Notifications
You must be signed in to change notification settings - Fork 8
/
modify.go
109 lines (84 loc) · 2.73 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
package crud
import (
"fmt"
"time"
"reflect"
"strings"
)
/*
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]
placeholderId := 0
var id int64 = 0
for sqlName, meta := range fieldMap {
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 = $%d", sqlName, placeholderId))
newValues = append(newValues, fieldVal)
placeholderId += 1
}
}
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 = $%d", table, strings.Join(sqlFields, ", "), sqlIdFieldName, placeholderId)
_, 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]
for sqlName, meta := range fieldMap {
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, fmt.Sprintf("$%d", len(placeholders) + 1))
}
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 {
return 0, er
}
return res.LastInsertId()
}