| 1 |
package apimethods |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"fsutils" |
| 7 |
firestore "go-genproto/googleapis/firestore/v1beta1" |
| 8 |
"io" |
| 9 |
"userutil" |
| 10 |
) |
| 11 |
|
| 12 |
//RunQuery ... Run a Structured Query |
| 13 |
func RunQuery() { |
| 14 |
fmt.Println("\n:: Running a Query ::\n") |
| 15 |
|
| 16 |
client, conn := fsutils.MakeFSClient() |
| 17 |
|
| 18 |
defer conn.Close() |
| 19 |
|
| 20 |
fmt.Print("Enter field to query: ") |
| 21 |
fieldPath := userutil.ReadFromConsole() |
| 22 |
|
| 23 |
runQueryQuery := firestore.StructuredQuery{ |
| 24 |
Select: &firestore.StructuredQuery_Projection{ |
| 25 |
Fields: []*firestore.StructuredQuery_FieldReference{ |
| 26 |
&firestore.StructuredQuery_FieldReference{ |
| 27 |
FieldPath: fieldPath, |
| 28 |
}, |
| 29 |
}, |
| 30 |
}, |
| 31 |
} |
| 32 |
|
| 33 |
runStructQueryReq := firestore.RunQueryRequest_StructuredQuery{ |
| 34 |
StructuredQuery: &runQueryQuery, |
| 35 |
} |
| 36 |
|
| 37 |
runQueryReq := firestore.RunQueryRequest{ |
| 38 |
Parent: "projects/firestoretestclient/databases/(default)/documents", |
| 39 |
QueryType: &runStructQueryReq, |
| 40 |
} |
| 41 |
|
| 42 |
call, err := client.RunQuery(context.Background(), &runQueryReq) |
| 43 |
|
| 44 |
if err != nil { |
| 45 |
fmt.Println("Error getting QueryClient: ", err) |
| 46 |
} |
| 47 |
|
| 48 |
for { |
| 49 |
results, err := call.Recv() |
| 50 |
if err == io.EOF { |
| 51 |
break |
| 52 |
} |
| 53 |
if err != nil { |
| 54 |
fmt.Println("Error received from QueryClient: ", err) |
| 55 |
} |
| 56 |
userutil.DrawDocument(*results.Document) |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
} |