-
Notifications
You must be signed in to change notification settings - Fork 7
/
requestbuilder.go
123 lines (109 loc) · 3.53 KB
/
requestbuilder.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
package yaorm
import (
"fmt"
"reflect"
"github.com/geoffreybauduin/yaorm/_vendor/github.com/lann/squirrel"
"github.com/geoffreybauduin/yaorm/tools"
)
type buildSelectColumns struct {
loadColumns []string
dontLoadColumns []string
}
func (c *buildSelectColumns) reduce(columns []string) []string {
emptyLoad := len(c.loadColumns) == 0
emptyDontLoad := len(c.dontLoadColumns) == 0
if emptyLoad && emptyDontLoad {
return columns
}
// One cache for both load & dontLoad
cache := make(map[string]bool, len(c.loadColumns)+len(c.dontLoadColumns))
for _, col := range c.loadColumns {
cache[col] = true
}
for _, col := range c.dontLoadColumns {
cache[col] = false
}
// Do not reuse columns slice to avoid polluting its backend array
forceExclude := !emptyDontLoad && emptyLoad
var ok, exists bool
res := make([]string, 0, len(columns))
for _, f := range columns {
ok, exists = cache[f]
if ok || (!exists && forceExclude) {
res = append(res, f)
}
}
return res
}
func buildSelect(dbp DBProvider, m Model, sf buildSelectColumns) (squirrel.SelectBuilder, error) {
table, err := GetTableByModel(m)
if err != nil {
return squirrel.SelectBuilder{}, err
}
fields := sf.reduce(table.Fields())
f := make([]string, 0, len(fields))
for _, field := range fields {
f = append(f, fmt.Sprintf(`%s.%s`, dbp.EscapeValue(table.Name()), dbp.EscapeValue(field)))
}
return dbp.getStatementGenerator().Select(f...).From(
fmt.Sprintf("%s AS %s", table.NameForQuery(dbp), dbp.EscapeValue(table.Name())),
), nil
}
func buildCount(dbp DBProvider, table *Table) (squirrel.SelectBuilder, error) {
return dbp.getStatementGenerator().Select("COUNT(*) AS count").From(dbp.EscapeValue(table.Name())), nil
}
func getNiceArgumentFormatted(v interface{}) string {
if v == nil {
return "nil"
}
value := reflect.ValueOf(v)
if tools.IsZeroValue(value) {
return fmt.Sprintf("%v", v)
}
switch value.Kind() {
case reflect.Ptr:
return getNiceArgumentFormatted(reflect.Indirect(value).Interface())
}
return fmt.Sprintf("%v", v)
}
func buildInsert(dbp DBProvider, m Model) (squirrel.InsertBuilder, error) {
table, err := GetTableByModel(m)
if err != nil {
return squirrel.InsertBuilder{}, err
}
reflectedM := tools.GetNonPtrValue(m)
stmt := dbp.getStatementGenerator().Insert(table.Name()).Columns(table.FieldsWithoutPK()...)
var values []interface{}
for _, field := range table.FieldsWithoutPK() {
values = append(values, getNiceArgumentFormatted(reflectedM.Field(table.FieldIndex(field)).Interface()))
}
stmt = stmt.Values(values...)
return stmt, nil
}
func buildUpdate(dbp DBProvider, m Model) (squirrel.UpdateBuilder, error) {
table, err := GetTableByModel(m)
if err != nil {
return squirrel.UpdateBuilder{}, err
}
reflectedM := tools.GetNonPtrValue(m)
stmt := dbp.getStatementGenerator().Update(table.Name())
for _, field := range table.FieldsWithoutPK() {
stmt = stmt.Set(field, getNiceArgumentFormatted(reflectedM.Field(table.FieldIndex(field)).Interface()))
}
for pk, idx := range table.KeyFields() {
stmt = stmt.Where(squirrel.Eq{pk: tools.GetNonPtrInterface(reflectedM.Field(idx).Interface())})
}
return stmt, nil
}
func buildDelete(dbp DBProvider, m Model) (squirrel.DeleteBuilder, error) {
table, err := GetTableByModel(m)
if err != nil {
return squirrel.DeleteBuilder{}, err
}
reflectedM := tools.GetNonPtrValue(m)
stmt := dbp.getStatementGenerator().Delete(table.Name())
for pk, idx := range table.KeyFields() {
stmt = stmt.Where(squirrel.Eq{pk: getNiceArgumentFormatted(reflectedM.Field(idx).Interface())})
}
return stmt, nil
}