forked from dapr/components-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmosdb_query.go
247 lines (212 loc) · 6.45 KB
/
cosmosdb_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
/*
Copyright 2021 The Dapr 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 cosmosdb
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
jsoniter "github.com/json-iterator/go"
"github.com/dapr/components-contrib/state"
"github.com/dapr/components-contrib/state/query"
)
// Internal query object is created here since azcosmos has no notion of a query object
type InternalQuery struct {
query string
parameters []azcosmos.QueryParameter
}
type Query struct {
query InternalQuery
limit int
token string
}
func (q *Query) VisitEQ(f *query.EQ) (string, error) {
// <key> = <val>
val, ok := f.Val.(string)
if !ok {
return "", fmt.Errorf("unsupported type of value %#v; expected string", f.Val)
}
name := q.setNextParameter(val)
return replaceKeywords("c.value."+f.Key) + " = " + name, nil
}
func (q *Query) VisitIN(f *query.IN) (string, error) {
// <key> IN ( <val1>, <val2>, ... , <valN> )
if len(f.Vals) == 0 {
return "", fmt.Errorf("empty IN operator for key %q", f.Key)
}
names := make([]string, len(f.Vals))
for i, v := range f.Vals {
val, ok := v.(string)
if !ok {
return "", fmt.Errorf("unsupported type of value %#v; expected string", v)
}
names[i] = q.setNextParameter(val)
}
return fmt.Sprintf("%s IN (%s)", replaceKeywords("c.value."+f.Key), strings.Join(names, ", ")), nil
}
func (q *Query) visitFilters(op string, filters []query.Filter) (string, error) {
var (
arr []string
str string
err error
)
for _, fil := range filters {
switch f := fil.(type) {
case *query.EQ:
if str, err = q.VisitEQ(f); err != nil {
return "", err
}
arr = append(arr, str)
case *query.IN:
if str, err = q.VisitIN(f); err != nil {
return "", err
}
arr = append(arr, str)
case *query.OR:
if str, err = q.VisitOR(f); err != nil {
return "", err
}
arr = append(arr, "("+str+")")
case *query.AND:
if str, err = q.VisitAND(f); err != nil {
return "", err
}
arr = append(arr, "("+str+")")
default:
return "", fmt.Errorf("unsupported filter type %#v", f)
}
}
return strings.Join(arr, " "+op+" "), nil
}
func (q *Query) VisitAND(f *query.AND) (string, error) {
// <expression1> AND <expression2> AND ... AND <expressionN>
return q.visitFilters("AND", f.Filters)
}
func (q *Query) VisitOR(f *query.OR) (string, error) {
// <expression1> OR <expression2> OR ... OR <expressionN>
return q.visitFilters("OR", f.Filters)
}
func (q *Query) Finalize(filters string, qq *query.Query) error {
var filter, orderBy string
if len(filters) != 0 {
filter = " WHERE " + filters
}
if sz := len(qq.Sort); sz != 0 {
order := make([]string, sz)
for i, item := range qq.Sort {
if item.Order == query.DESC {
order[i] = replaceKeywords("c.value."+item.Key) + " DESC"
} else {
order[i] = replaceKeywords("c.value."+item.Key) + " ASC"
}
}
orderBy = " ORDER BY " + strings.Join(order, ", ")
}
q.query.query = "SELECT * FROM c" + filter + orderBy
q.limit = qq.Page.Limit
q.token = qq.Page.Token
return nil
}
func (q *Query) setNextParameter(val string) string {
pname := fmt.Sprintf("@__param__%d__", len(q.query.parameters))
q.query.parameters = append(q.query.parameters, azcosmos.QueryParameter{Name: pname, Value: val})
return pname
}
func (q *Query) execute(ctx context.Context, client *azcosmos.ContainerClient) ([]state.QueryItem, string, error) {
opts := &azcosmos.QueryOptions{}
resultLimit := q.limit
opts.QueryParameters = append(opts.QueryParameters, q.query.parameters...)
if len(q.token) != 0 {
opts.ContinuationToken = q.token
}
items := []CosmosItem{}
pk := azcosmos.NewPartitionKeyBool(true)
queryPager := client.NewQueryItemsPager(q.query.query, pk, opts)
token := ""
for queryPager.More() {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, defaultTimeout)
queryResponse, innerErr := queryPager.NextPage(ctx)
cancel()
if innerErr != nil {
return nil, "", innerErr
}
token = queryResponse.ContinuationToken
for _, item := range queryResponse.Items {
tempItem := CosmosItem{}
err := json.Unmarshal(item, &tempItem)
if err != nil {
return nil, "", err
}
if (resultLimit != 0) && (len(items) >= resultLimit) {
break
}
items = append(items, tempItem)
}
if (resultLimit != 0) && (len(items) >= resultLimit) {
break
}
}
ret := make([]state.QueryItem, len(items))
var err error
for i := range items {
ret[i].Key = items[i].ID
ret[i].ETag = &items[i].Etag
if items[i].IsBinary {
ret[i].Data, _ = base64.StdEncoding.DecodeString(items[i].Value.(string))
continue
}
ret[i].Data, err = jsoniter.ConfigFastest.Marshal(&items[i].Value)
if err != nil {
ret[i].Error = err.Error()
continue
}
}
return ret, token, nil
}
func replaceKeywords(key string) string {
reserved := []string{"value"}
for _, keyword := range reserved {
key = replaceKeyword(key, keyword)
}
return key
}
// Replaces reserved keywords. If a replacement of a reserved keyword is made, all other words will be changed from .word to ['word']
func replaceKeyword(key, keyword string) string {
indx := strings.Index(strings.ToUpper(key), "."+strings.ToUpper(keyword))
if indx == -1 {
return key
}
// Grab the next index to check and ensure that it doesn't over-index
nextIndx := indx + len(keyword) + 1
if nextIndx == len(key) || !isLetter(key[nextIndx]) {
// Get the new keyword to replace
newKeyword := keyword
if nextIndx < len(key)-1 {
// Get the index of the next period (Note that it grabs the index relative to the beginning of the initial string)
idxOfPeriod := strings.Index(key[nextIndx+1:], ".")
if idxOfPeriod != -1 {
newKeyword = key[nextIndx+1 : nextIndx+idxOfPeriod+1]
} else {
newKeyword = key[nextIndx+1:]
}
}
return fmt.Sprintf("%s['%s']%s", key[:indx], key[indx+1:nextIndx], replaceKeyword(key[nextIndx:], newKeyword))
}
return key
}
func isLetter(ch byte) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}