-
Notifications
You must be signed in to change notification settings - Fork 141
/
query.go
279 lines (248 loc) · 7.98 KB
/
query.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2023 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysql
import (
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/pipe-cd/pipecd/pkg/datastore"
)
var operatorMap = map[datastore.Operator]string{
datastore.OperatorEqual: "=",
datastore.OperatorNotEqual: "!=",
datastore.OperatorIn: "IN",
datastore.OperatorNotIn: "NOT IN",
datastore.OperatorGreaterThan: ">",
datastore.OperatorGreaterThanOrEqual: ">=",
datastore.OperatorLessThan: "<",
datastore.OperatorLessThanOrEqual: "<=",
datastore.OperatorContains: "MEMBER OF",
}
func buildGetQuery(table string) string {
return fmt.Sprintf("SELECT Data FROM %s WHERE Id = UUID_TO_BIN(?,true)", table)
}
func buildUpdateQuery(table string) string {
return fmt.Sprintf("UPDATE %s SET Data = ? WHERE Id = UUID_TO_BIN(?,true)", table)
}
func buildCreateQuery(table string) string {
return fmt.Sprintf("INSERT INTO %s (Id, Data) VALUE (UUID_TO_BIN(?,true), ?)", table)
}
func buildFindQuery(table string, ops datastore.ListOptions) (string, error) {
filters := refineFiltersField(ops.Filters)
whereClause, err := buildWhereClause(filters)
if err != nil {
return "", err
}
orderByClause, err := buildOrderByClause(refineOrdersField(ops.Orders))
if err != nil {
return "", err
}
rawQuery := fmt.Sprintf(
"SELECT Data FROM %s %s %s %s %s",
table,
whereClause,
buildPaginationCondition(ops),
orderByClause,
buildLimitClause(ops.Limit),
)
return strings.Join(strings.Fields(rawQuery), " "), nil
}
func buildWhereClause(filters []datastore.ListFilter) (string, error) {
if len(filters) == 0 {
return "", nil
}
conds := make([]string, len(filters))
for i, filter := range filters {
op, ok := operatorMap[filter.Operator]
if !ok {
return "", fmt.Errorf("unsupported operator given: %v", filter.Operator)
}
switch filter.Operator {
case datastore.OperatorIn, datastore.OperatorNotIn:
// Make string of (?,...) which contains the number of `?` equal to the element number of filter.Value
valLength := reflect.ValueOf(filter.Value).Len()
conds[i] = fmt.Sprintf("%s %s (?%s)", filter.Field, op, strings.Repeat(",?", valLength-1))
case datastore.OperatorContains:
conds[i] = fmt.Sprintf("? %s (%s)", op, filter.Field)
default:
conds[i] = fmt.Sprintf("%s %s ?", filter.Field, op)
}
}
return fmt.Sprintf("WHERE %s", strings.Join(conds[:], " AND ")), nil
}
func buildPaginationCondition(opts datastore.ListOptions) string {
// Skip on no cursor.
if len(opts.Cursor) == 0 {
return ""
}
// Build outer set condition. The outer set condition should be
// in format:
// X < Vx AND Y < Vy AND ...
// with x, y, etc is not Id field.
outerSetConds := make([]string, len(opts.Orders)-1)
for i, o := range opts.Orders {
if o.Field == "Id" {
continue
}
outerSetConds[i] = fmt.Sprintf("%s %s ?", o.Field, makeCompareOperatorForOuterSet(o.Direction))
}
// Build sub set condition. The sub set condition should be
// in format:
// X = Vx AND Y = Vy AND ... AND Id <= last_iterated_id
// with last_iterated_id from the given cursor.
subSetConds := make([]string, len(opts.Orders))
for i, o := range opts.Orders {
if o.Field == "Id" {
subSetConds[i] = fmt.Sprintf("%s %s UUID_TO_BIN(?,true)", o.Field, makeCompareOperatorForSubSet(o.Direction))
} else {
subSetConds[i] = fmt.Sprintf("%s = ?", o.Field)
}
}
// If there is no filter, mean pagination condition should be treated as the only where condition.
if len(opts.Filters) == 0 {
return fmt.Sprintf("WHERE %s AND NOT (%s)", strings.Join(outerSetConds, " AND "), strings.Join(subSetConds, " AND "))
}
return fmt.Sprintf("AND %s AND NOT (%s)", strings.Join(outerSetConds, " AND "), strings.Join(subSetConds, " AND "))
}
func makeCompareOperatorForOuterSet(direction datastore.OrderDirection) string {
if direction == datastore.Asc {
return ">="
}
return "<="
}
func makeCompareOperatorForSubSet(direction datastore.OrderDirection) string {
if direction == datastore.Asc {
return "<="
}
return ">="
}
func buildOrderByClause(orders []datastore.Order) (string, error) {
if len(orders) == 0 {
return "", nil
}
conds := make([]string, len(orders))
hasIDFieldInOrdering := false
for i, ord := range orders {
if ord.Field == "Id" {
hasIDFieldInOrdering = true
}
conds[i] = fmt.Sprintf("%s %s", ord.Field, toMySQLDirection(ord.Direction))
}
if !hasIDFieldInOrdering {
return "", fmt.Errorf("id field is required as ordering field")
}
return fmt.Sprintf("ORDER BY %s", strings.Join(conds[:], ", ")), nil
}
func buildLimitClause(limit int) string {
var clause string
if limit > 0 {
clause = fmt.Sprintf("LIMIT %d ", limit)
}
return clause
}
func toMySQLDirection(d datastore.OrderDirection) string {
switch d {
case datastore.Asc:
return "ASC"
case datastore.Desc:
return "DESC"
default:
return ""
}
}
func refineOrdersField(orders []datastore.Order) []datastore.Order {
out := make([]datastore.Order, len(orders))
for i, order := range orders {
switch order.Field {
case "SyncState.Status":
order.Field = "SyncState_Status"
default:
break
}
out[i] = order
}
return out
}
func refineFiltersField(filters []datastore.ListFilter) []datastore.ListFilter {
out := make([]datastore.ListFilter, len(filters))
for i, filter := range filters {
switch filter.Field {
case "SyncState.Status":
filter.Field = "SyncState_Status"
default:
break
}
out[i] = filter
}
return out
}
// refineFiltersValue destructs all slide/array type values and makes an array of all element values.
func refineFiltersValue(filters []datastore.ListFilter) []interface{} {
var filtersVals []interface{}
for _, filter := range filters {
fv := reflect.ValueOf(filter.Value)
switch fv.Kind() {
case reflect.Slice, reflect.Array:
for j := 0; j < fv.Len(); j++ {
filtersVals = append(filtersVals, fv.Index(j).Interface())
}
default:
filtersVals = append(filtersVals, filter.Value)
}
}
return filtersVals
}
// makePaginationCursorValues builds array of element values used on pagination condition check.
func makePaginationCursorValues(opts datastore.ListOptions) ([]interface{}, error) {
// Skip pagination on cursor is empty.
if len(opts.Cursor) == 0 {
return nil, nil
}
// Decode last object of previous page stored as opts.Cursor to string.
data, err := base64.StdEncoding.DecodeString(opts.Cursor)
if err != nil {
return nil, err
}
// Encode cursor data string to map[string]interface{} format for further process.
obj := make(map[string]interface{})
if err := json.Unmarshal(data, &obj); err != nil {
return nil, err
}
// The cursorVals contains values used for pagination condition.
// For each field except Id, it should be duplicated as for using in outer set and subset.
// The Id field value should be one, and it's the last value in this list.
cursorVals := make([]interface{}, 0, 2*len(opts.Orders)-1)
for _, o := range opts.Orders {
// Skip the Id field value to add it at last.
if o.Field == "Id" {
continue
}
val, ok := obj[o.Field]
if !ok {
return nil, fmt.Errorf("cursor does not contain values that match to ordering field %s", o.Field)
}
cursorVals = append(cursorVals, val)
}
// Duplicate all values in added order.
cursorVals = append(cursorVals, cursorVals...)
// Add Id value at last.
id, ok := obj["Id"]
if !ok {
return nil, fmt.Errorf("cursor does not contain required value Id")
}
cursorVals = append(cursorVals, id)
return cursorVals, nil
}