-
Notifications
You must be signed in to change notification settings - Fork 4
/
collection.internal.go
208 lines (167 loc) · 5.83 KB
/
collection.internal.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
package mysql
import (
"crypto/rand"
"encoding/hex"
"strconv"
"strings"
"time"
"github.com/ottemo/commerce/db"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
)
// makes SQL filter string based on ColumnName, Operator and Value parameters or returns nil
// - internal usage function for AddFilter and AddStaticFilter routines
func (it *DBCollection) makeSQLFilterString(ColumnName string, Operator string, Value interface{}) (string, error) {
if !it.HasColumn(ColumnName) {
return "", env.ErrorNew(ConstErrorModule, ConstErrorLevel, "8a113e66-b2e8-472d-a92c-476794b2d127", "can't find column '"+ColumnName+"'")
}
Operator = strings.ToUpper(Operator)
allowedOperators := []string{"=", "!=", "<>", ">", ">=", "<", "<=", "LIKE", "IN"}
if !utils.IsInListStr(Operator, allowedOperators) {
return "", env.ErrorNew(ConstErrorModule, ConstErrorLevel, "11a51df3-83bb-4250-bff7-60e2e8bb6b49", "unknown operator '"+Operator+"' for column '"+ColumnName+"', allowed: '"+strings.Join(allowedOperators, "', ")+"'")
}
columnType := it.GetColumnType(ColumnName)
// array column - special case
if strings.HasPrefix(columnType, "[]") {
value := strings.Trim(convertValueForSQL(Value), "'")
template := "(', ' || `" + ColumnName + "` || ',') LIKE '%, $value,%'"
var resultItems []string
for _, arrayItem := range strings.Split(value, ", ") {
item := utils.InterfaceToString(arrayItem)
resultItems = append(resultItems, strings.Replace(template, "$value", item, 1))
}
if len(resultItems) == 1 {
return resultItems[0], nil
}
return strings.Join(resultItems, " OR "), nil
}
// regular columns - default case
switch Operator {
case "LIKE":
if typedValue, ok := Value.(string); ok {
if !strings.Contains(typedValue, "%") {
Value = "'%" + typedValue + "%'"
} else {
newValue := strings.Trim(Value.(string), "'")
newValue = strings.Trim(newValue, "\"")
Value = "'" + newValue + "'"
}
} else {
Value = "''"
}
case "IN":
if typedValue, ok := Value.(*DBCollection); ok {
Value = "(" + typedValue.getSelectSQL() + ")"
} else {
newValue := "("
for _, arrayItem := range utils.InterfaceToArray(Value) {
newValue += convertValueForSQL(arrayItem) + ", "
}
newValue = strings.TrimRight(newValue, ", ") + ")"
Value = newValue
}
default:
Value = convertValueForSQL(Value)
}
return "`" + ColumnName + "` " + Operator + " " + utils.InterfaceToString(Value), nil
}
// returns SQL select statement for current collection
func (it *DBCollection) getSelectSQL() string {
SQL := "SELECT " + it.getSQLResultColumns() + " FROM " + it.Name + it.getSQLFilters() + it.getSQLOrder() + it.Limit
return SQL
}
// un-serialize object values
func (it *DBCollection) modifyResultRow(row RowMap) RowMap {
for columnName, columnValue := range row {
columnType, present := dbEngine.attributeTypes[it.Name][columnName]
if !present {
columnType = ""
}
if columnName != "_id" && columnType != "" {
row[columnName] = db.ConvertTypeFromDbToGo(columnValue, columnType)
}
}
if _, present := row["_id"]; present {
row["_id"] = utils.InterfaceToString(row["_id"])
}
return row
}
// joins result columns in string
func (it *DBCollection) getSQLResultColumns() string {
sqlColumns := "`" + strings.Join(it.ResultColumns, "`, `") + "`"
if sqlColumns == "``" {
sqlColumns = "*"
}
return sqlColumns
}
// joins order olumns in one string with preceding keyword
func (it *DBCollection) getSQLOrder() string {
sqlOrder := strings.Join(it.Order, ", ")
if sqlOrder != "" {
sqlOrder = " ORDER BY " + sqlOrder
}
return sqlOrder
}
// collects all filters in a single string (for internal usage)
func (it *DBCollection) getSQLFilters() string {
var collectSubfilters func(string) []string
collectSubfilters = func(parentGroupName string) []string {
var result []string
for filterGroupName, filterGroup := range it.FilterGroups {
if filterGroup.ParentGroup == parentGroupName {
joinOperator := " AND "
if filterGroup.OrSequence {
joinOperator = " OR "
}
subFilters := collectSubfilters(filterGroupName)
if len(subFilters) > 0 {
subFilters = append([]string{""}, subFilters...)
}
filterValue := "(" + strings.Join(filterGroup.FilterValues, joinOperator) + strings.Join(subFilters, joinOperator) + ")"
result = append(result, filterValue)
}
}
return result
}
sqlFilters := strings.Join(collectSubfilters(""), " AND ")
if sqlFilters != "" {
sqlFilters = " WHERE " + sqlFilters
}
return sqlFilters
}
// returns filter group, creates new one if not exists
func (it *DBCollection) getFilterGroup(groupName string) *StructDBFilterGroup {
filterGroup, present := it.FilterGroups[groupName]
if !present {
filterGroup = &StructDBFilterGroup{Name: groupName, FilterValues: make([]string, 0)}
it.FilterGroups[groupName] = filterGroup
}
return filterGroup
}
// adds filter(combination of [column, operator, value]) in named filter group
func (it *DBCollection) updateFilterGroup(groupName string, columnName string, operator string, value interface{}) error {
/*if !it.HasColumn(columnName) {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "67fa360b-ba93-407c-a787-1c013ebb8947", "not existing column " + columnName)
}*/
newValue, err := it.makeSQLFilterString(columnName, operator, value)
if err != nil {
return err
}
filterGroup := it.getFilterGroup(groupName)
filterGroup.FilterValues = append(filterGroup.FilterValues, newValue)
return nil
}
// generates new UUID for _id column
func (it *DBCollection) makeUUID(id string) string {
if len(id) != 24 {
timeStamp := strconv.FormatInt(time.Now().Unix(), 16)
randomBytes := make([]byte, 8)
if _, err := rand.Reader.Read(randomBytes); err != nil {
_ = env.ErrorDispatch(err)
}
randomHex := make([]byte, 16)
hex.Encode(randomHex, randomBytes)
id = timeStamp + string(randomHex)
}
return id
}