forked from sgarciac/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
191 lines (161 loc) · 5.21 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
package main
import (
"cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
"google.golang.org/api/iterator"
"strings"
)
func getDir(name string) firestore.Direction {
if name == "DESC" {
return firestore.Desc
}
return firestore.Asc
}
func operatorTokenToFirestore(operator string) string {
switch operator {
case "<in>":
return "in"
case "<not-in>":
return "not-in"
case "<array-contains-any>":
return "array-contains-any"
case "<array-contains>":
return "array-contains"
default:
return operator
}
}
// get the snapshot of a document, receiving either a document path, or a
// collection reference and a document id. The document-path of document id are
// passed in the 'document' parameter.
func documentSnapshot(client *firestore.Client, document string, collectionRef *firestore.CollectionRef, group bool) (*firestore.DocumentSnapshot, error) {
var documentRef *firestore.DocumentRef
if group && !strings.Contains(document, "/") {
return nil, cli.NewExitError("If you use the group option, you must use a document-path for pagination arguments", 83)
}
if strings.Contains(document, "/") {
documentRef = client.Doc(document)
} else {
documentRef = collectionRef.Doc(document)
}
return documentRef.Get(context.Background())
}
// query collection-path query*
func queryCommandAction(c *cli.Context) error {
collectionPathOrId := c.Args().First()
// display
extendedJson := c.Bool("extendedjson")
// pagination
startAt := c.String("startat")
startAfter := c.String("startafter")
endAt := c.String("endat")
endBefore := c.String("endbefore")
queryGroup := c.Bool("group")
selectFields := c.StringSlice("select")
orderbyFields := c.StringSlice("orderby")
orderdirFields := c.StringSlice("orderdir")
limit := c.Int("limit")
queryParser := getQueryParser()
fieldPathParser := getFieldPathParser()
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
// init the query either from a collection ref or a collection group.
var query firestore.Query
var collectionRef *firestore.CollectionRef
var collectionGroupRef *firestore.CollectionGroupRef
if queryGroup {
collectionGroupRef = client.CollectionGroup(collectionPathOrId)
query = collectionGroupRef.Limit(limit)
} else {
collectionRef = client.Collection(collectionPathOrId)
query = collectionRef.Limit(limit)
}
// add the conditions one by one.
for i := 1; i < c.NArg(); i++ {
queryString := c.Args().Get(i)
var parsedQuery Firestorequery
if err := queryParser.ParseString(queryString, &parsedQuery); err != nil {
return cli.NewExitError(fmt.Sprintf("Error parsing query '%s' %v", queryString, err), 83)
}
query = query.WherePath(parsedQuery.Key, operatorTokenToFirestore(parsedQuery.Operator), parsedQuery.Value.get())
}
// order by
for i, orderbyRaw := range orderbyFields {
var parsedOrderBy Firestorefieldpath
var orderDir string
if err := fieldPathParser.ParseString(orderbyRaw, &parsedOrderBy); err != nil {
return cli.NewExitError(fmt.Sprintf("Error parsing orderby '%s' %v",
orderbyRaw, err), 83)
}
if i < len(orderdirFields) {
orderDir = orderdirFields[i]
} else {
orderDir = "DESC"
}
query = query.OrderByPath(parsedOrderBy.Key, getDir(orderDir))
}
if startAt != "" {
docsnap, err := documentSnapshot(client, startAt, collectionRef, queryGroup)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to get '%s'", startAt), 83)
}
query = query.StartAt(docsnap)
}
if startAfter != "" {
docsnap, err := documentSnapshot(client, startAfter, collectionRef, queryGroup)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to get '%s'", startAfter), 83)
}
query = query.StartAfter(docsnap)
}
if endAt != "" {
docsnap, err := documentSnapshot(client, endAt, collectionRef, queryGroup)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to get '%s'", endAt), 83)
}
query = query.EndAt(docsnap)
}
if endBefore != "" {
docsnap, err := documentSnapshot(client, endBefore, collectionRef, queryGroup)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to get '%s'", endBefore), 83)
}
query = query.EndBefore(docsnap)
}
// selected fields.
if len(selectFields) > 0 {
var selectFieldPaths []firestore.FieldPath
for _, selectField := range selectFields {
var parsedSelect Firestorefieldpath
if err := fieldPathParser.ParseString(selectField, &parsedSelect); err != nil {
return cli.NewExitError(fmt.Sprintf("Error parsing select '%s' %v",
selectField, err), 83)
}
selectFieldPaths = append(selectFieldPaths, parsedSelect.Key)
}
query = query.SelectPaths(selectFieldPaths...)
}
displayItemWriter := newDisplayItemWriter(&c.App.Writer)
defer displayItemWriter.Close()
documentIterator := query.Documents(context.Background())
for {
doc, err := documentIterator.Next()
if err == iterator.Done {
break
} else if err != nil {
documentIterator.Stop()
return cli.NewExitError(fmt.Sprintf("Failed to get documents. \n%v", err), 84)
}
err = displayItemWriter.Write(doc, extendedJson)
if err != nil {
documentIterator.Stop()
return cli.NewExitError(fmt.Sprintf("Error while writing output. \n%v", err), 86)
}
}
documentIterator.Stop()
return nil
}