generated from dzungtran/echo-rest-api
/
query_builder.go
236 lines (198 loc) · 5.55 KB
/
query_builder.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
package sqlTools
import (
"context"
"database/sql"
"reflect"
"time"
"github.com/Masterminds/squirrel"
"github.com/14799678/echo_rest_api/pkg/constants"
"github.com/14799678/echo_rest_api/pkg/contexts"
"github.com/14799678/echo_rest_api/pkg/utils"
)
type options struct {
selectFields []string
ignoreFields []string
autoDateTimeFields []string
}
type CallOptionMapValues struct {
applyFunc func(*options)
}
func WithMapValuesSelectFields(fields []string) CallOptionMapValues {
return CallOptionMapValues{
applyFunc: func(opt *options) {
if len(opt.selectFields) == 0 {
opt.selectFields = make([]string, 0)
}
if len(fields) > 0 {
opt.selectFields = append(opt.selectFields, fields...)
}
},
}
}
func WithMapValuesIgnoreFields(fields []string) CallOptionMapValues {
return CallOptionMapValues{
applyFunc: func(opt *options) {
if len(opt.ignoreFields) == 0 {
opt.ignoreFields = make([]string, 0)
}
if len(fields) > 0 {
opt.ignoreFields = append(opt.ignoreFields, fields...)
}
},
}
}
func WithMapValuesAutoDateTimeFields(fields []string) CallOptionMapValues {
return CallOptionMapValues{
applyFunc: func(opt *options) {
if len(opt.autoDateTimeFields) == 0 {
opt.autoDateTimeFields = make([]string, 0)
}
if len(fields) > 0 {
opt.autoDateTimeFields = append(opt.autoDateTimeFields, fields...)
}
},
}
}
// GetMapValuesFromStruct For INSERT or UPDATE queries
func GetMapValuesFromStruct(ctx context.Context, st interface{}, callOpts ...CallOptionMapValues) map[string]interface{} {
rs := make(map[string]interface{})
t := reflect.TypeOf(st).Elem()
v := reflect.ValueOf(st)
opt := appliedFieldSelectOption(callOpts)
for index := 0; index < t.NumField(); index++ {
f := t.Field(index)
// temp fix for embedded struct
//if f.Type.Kind() == reflect.Struct {
// cols, vals := GetColumnsAndValuesFromStruct(ctx, reflect.Indirect(v).FieldByName(f.Name).Interface(), callOpts...)
// if len(cols) == 0 {
// continue
// }
// for i := 0; i < len(cols); i++ {
// rs[cols[i]] = vals[i]
// }
// continue
//}
dbKey := getKeyFromTag(f.Tag.Get("db"))
if dbKey == "-" || len(dbKey) == 0 {
continue
}
if len(opt.ignoreFields) > 0 && utils.IsSliceContains(opt.ignoreFields, dbKey) {
continue
}
if len(opt.autoDateTimeFields) > 0 && utils.IsSliceContains(opt.autoDateTimeFields, dbKey) {
rs[dbKey] = time.Now().UTC()
continue
}
if len(opt.selectFields) > 0 && !utils.IsSliceContains(opt.selectFields, dbKey) {
continue
}
ival := reflect.Indirect(v).FieldByName(f.Name).Interface()
rs[dbKey] = ival
}
return rs
}
func GetColumnsAndValuesFromStruct(ctx context.Context, st interface{}, callOpts ...CallOptionMapValues) ([]string, []interface{}) {
t := reflect.TypeOf(st).Elem()
v := reflect.ValueOf(st)
columns := make([]string, 0)
values := make([]interface{}, 0)
opt := appliedFieldSelectOption(callOpts)
for index := 0; index < t.NumField(); index++ {
f := t.Field(index)
dbKey := getKeyFromTag(f.Tag.Get("db"))
if len(opt.autoDateTimeFields) > 0 && utils.IsSliceContains(opt.autoDateTimeFields, dbKey) {
columns = append(columns, dbKey)
values = append(values, time.Now().UTC())
continue
}
// temp fix for embedded struct
if f.Type.Kind() == reflect.Struct {
str := reflect.Indirect(v).FieldByName(f.Name)
pactual := reflect.New(str.Type()).Interface()
// ignore scan some native struct
nestedReflect := true
switch pactual.(type) {
case *time.Time, *sql.NullTime:
nestedReflect = false
}
if nestedReflect {
cols, vals := GetColumnsAndValuesFromStruct(ctx, pactual, callOpts...)
if len(cols) == 0 {
continue
}
columns = append(columns, cols...)
values = append(values, vals...)
} else {
columns = append(columns, dbKey)
values = append(values, str.Interface())
}
continue
}
if dbKey == "-" || len(dbKey) == 0 || (len(opt.selectFields) > 0 && !utils.IsSliceContains(opt.selectFields, dbKey)) {
continue
}
if len(opt.ignoreFields) > 0 && utils.IsSliceContains(opt.ignoreFields, dbKey) {
continue
}
columns = append(columns, dbKey)
values = append(values, reflect.Indirect(v).FieldByName(f.Name).Interface())
}
return columns, values
}
func ParseColumnsForSelect(cols []string) []string {
for i, v := range cols {
switch v {
case "_count":
cols[i] = "count(*) over() as _count"
}
}
return cols
}
func ParseColumnsForSelectWithAlias(cols []string, alias string) []string {
for i, v := range cols {
if utils.IsSliceContains([]string{"_count"}, v) {
switch v {
case "_count":
cols[i] = "count(*) over() as _count"
}
continue
}
cols[i] = alias + "." + v
}
return cols
}
func appliedFieldSelectOption(callOptions []CallOptionMapValues) *options {
if len(callOptions) == 0 {
return &options{}
}
optCopy := &options{}
for _, f := range callOptions {
f.applyFunc(optCopy)
}
return optCopy
}
// getKeyFromTag -- get db key from struct db tag
func getKeyFromTag(tag string) string {
pieces := utils.StringSlice(tag, ",")
if len(pieces) == 0 {
return ""
}
return pieces[0]
}
func BindCommonParamsToSelectBuilder(builder squirrel.SelectBuilder, params contexts.CommonParamsForFetch) squirrel.SelectBuilder {
limit := constants.DefaultPerPage
if params.Limit > 0 {
limit = params.Limit
}
if limit > constants.MaximumPerPage {
limit = constants.DefaultPerPage
}
offset := uint64(0)
if params.Page > 1 {
offset = limit * (params.Page - 1)
}
if !params.NoLimit {
builder = builder.Limit(limit).Offset(offset)
}
return builder
}