-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
187 lines (170 loc) · 4.66 KB
/
helper.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
package cql
import (
"fmt"
"reflect"
"strings"
"time"
"unicode"
)
func (t *Table) getSelectedColumns(query Q, options QOpt) string {
selects, ok := query["select"].([]string)
if !ok {
selects = make([]string, 0)
}
var err error
var columns []string
if options.View != "" {
for _, view := range t.MaterializedView {
if options.View == view.Name {
columns = view.Select
}
}
} else if len(t.MaterializedView) >= options.ViewID && options.ViewID > 0 {
columns = t.MaterializedView[options.ViewID-1].Select
}
if len(columns) == 0 {
columns, err = t.getColumns()
}
selectedColumns := make([]string, 0)
ignoredColumns := make([]string, 0)
if len(selects) > 0 {
for _, key := range selects {
if string(key[0]) == "-" || err != nil {
ignoredColumns = append(ignoredColumns, string(key[1:]))
} else {
selectedColumns = append(selectedColumns, key)
}
}
} else {
return "*"
}
if len(columns) > 0 && len(ignoredColumns) > 0 && len(selectedColumns) == 0 {
for _, key := range columns {
if !isExistsS(key, ignoredColumns) {
selectedColumns = append(selectedColumns, key)
}
}
return strings.Join(selectedColumns, ",")
} else if len(columns) > 0 && len(selectedColumns) == 0 {
return strings.Join(columns, ",")
} else if len(selectedColumns) > 0 {
return strings.Join(selectedColumns, ",")
}
return "*"
}
func (t *Table) getColumns() ([]string, error) {
sv := reflect.ValueOf(t.Model).Elem()
st := reflect.TypeOf(t.Model).Elem()
if sv.Kind() == reflect.Ptr && !sv.IsNil() {
return nil, fmt.Errorf("Invalid model")
}
if sv.Kind() != reflect.Struct && sv.Kind() != reflect.Interface {
return nil, fmt.Errorf("Invalid string")
}
result := make([]string, 0)
nFields := st.NumField()
for i := 0; i < nFields; i++ {
fieldName := st.Field(i).Name
if !unicode.IsUpper(rune(fieldName[0])) {
continue
}
result = append(result, toSnakeCase(fieldName))
}
return result, nil
}
func parseQuery(q Q, values *[]interface{}) string {
cur := ""
for key, value := range q {
if key == "$eq" || key == "$gt" || key == "$gte" || key == "$lt" || key == "$lte" {
*values = append(*values, value)
cur += fmt.Sprintf("%s?", operators[key])
} else if key == "$in" {
cur += fmt.Sprintf("%s (", operators[key])
for _, val := range value.([]interface{}) {
*values = append(*values, val)
cur += "?,"
}
cur = strings.Trim(cur, ",")
cur += ")"
} else if newQ, ok := value.(Q); ok {
next := parseQuery(newQ, values)
next = strings.Trim(next, " AND")
cur += fmt.Sprintf(" %s %s %s", "AND", key, next)
} else {
*values = append(*values, value)
cur += fmt.Sprintf(" %s %s=?", "AND", key)
}
}
cur = strings.Trim(cur, " AND")
return cur
}
// BindStruct is used to bind map[string]interface{} to given struct
func BindStruct(model interface{}, m map[string]interface{}) error {
sv := reflect.ValueOf(model).Elem()
st := reflect.TypeOf(model).Elem()
if sv.Kind() == reflect.Ptr && !sv.IsNil() {
return fmt.Errorf("Unexpected Pointer")
}
if sv.Kind() != reflect.Struct && sv.Kind() != reflect.Interface {
return fmt.Errorf("Expecting struct")
}
nFields := sv.NumField()
for i := 0; i < nFields; i++ {
fieldName := st.Field(i).Name
if !unicode.IsUpper(rune(fieldName[0])) {
continue
}
fieldValue := sv.Field(i)
if !fieldValue.IsValid() {
continue
}
if !fieldValue.CanSet() {
continue
}
fieldName = toSnakeCase(fieldName)
tag := st.Field(i).Tag.Get("cql")
if tag == "-" {
continue
}
tagList := strings.Split(tag, ",")
for _, cqlTag := range tagList {
cqlTagItem := strings.Split(cqlTag, "=")
if len(cqlTagItem) == 2 && "column" == strings.ToLower(cqlTagItem[0]) {
fieldName = cqlTagItem[1]
break
}
}
fieldType := fieldValue.Type()
mapValue, ok := m[fieldName]
if !ok {
continue
}
value := reflect.ValueOf(mapValue)
if fieldType == value.Type() {
fieldValue.Set(value)
} else if fieldType.Kind() == reflect.Ptr {
var j interface{}
if fieldType.String() == "*int" && value.Type().String() == "int" {
var i = mapValue.(int)
j = &i
} else if fieldType.String() == "*int64" && value.Type().String() == "int64" {
var i = mapValue.(int64)
j = &i
} else if fieldType.String() == "*bool" && value.Type().String() == "bool" {
var i = mapValue.(bool)
j = &i
} else if fieldType.String() == "*string" && value.Type().String() == "string" {
var i = mapValue.(int64)
j = &i
} else if fieldType.String() == "*time.Time" && value.Type().String() == "time.Time" {
var i = mapValue.(time.Time)
j = &i
} else {
continue
}
value = reflect.ValueOf(j)
fieldValue.Set(value)
}
}
return nil
}