-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
110 lines (93 loc) · 2.96 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
package quirk
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/dgraph-io/dgo/v2"
)
// queryUID builds a query, executes it to find any unique marked predicates.
// Then sifts through the response to see if any UIDs were returned.
// Finally returns either a uid or an empty string if nothing was found.
func queryUID(ctx context.Context, txn *dgo.Txn, b builder, dat *DupleNode) (string, error) {
defer b.Reset() // reset the strings builder.
var decode queryDecode // where the decoded query response will be stored.
// execute a query to find any UIDs that are existing for unique fields.
if err := executeQuery(ctx, txn, b, dat, &decode); err != nil {
return "", err
}
// find if the decoded query contains any UIDs.
return findDecodedUID(decode)
}
// findDecodeUID loops through the decoded response and tries to find
// any UIDs. If any are found then they are returned. Otherwise
// this function will just return an empty string.
func findDecodedUID(decode queryDecode) (string, error) {
for _, v := range decode {
if len(v) > 1 { // if there are too many responses.
return "", &QueryError{
Msg: msgTooManyResponses, Function: "findDecodedUID"}
}
if len(v) == 1 {
if v[0].UID == nil { // if the *string is nil.
return "", &QueryError{
Msg: msgNilUID, Function: "findDecodedUID"}
}
return *v[0].UID, nil
}
}
return "", nil
}
// executeQuery calls to create a Query based on the unique predicates sent in
// and then executes it using the given transaction.
func executeQuery(ctx context.Context, txn *dgo.Txn, b builder,
dat *DupleNode, decode *queryDecode) error {
// Write a query that finds the marked unique predicates to the builder.
if err := createQuery(b, dat); err != nil {
return err
}
// If the query is empty then return nil right away.
if b.String() == emptyQuery {
return nil
}
// Execute the query with the given transaction.
resp, err := txn.Query(ctx, b.String())
if err != nil {
return &QueryError{
Function: "executeQuery",
Query: b.String(),
ExtErr: err,
}
}
// Unmarshal the response into the given decode object.
if err = json.Unmarshal(resp.GetJson(), decode); err != nil {
return &QueryError{
Function: "executeQuery",
ExtErr: err,
}
}
return nil
}
// createQuery will loop through the unique predicates and write the query
// to the given io.Writer.
func createQuery(b io.Writer, dat *DupleNode) error {
if _, err := b.Write([]byte{'{'}); err != nil {
return err
}
// Loop through and add a new function per unique predicate.
for _, d := range dat.Unique() {
_, err := fmt.Fprintf(b, queryfunc, "find"+d.Predicate, d.Predicate, d.Object)
if err != nil { // returns quirk.Error for predicate and value context.
return &QueryError{
ExtErr: err,
Msg: fmt.Sprintf(msgBuilderWriting, d.Predicate, d.Object),
Function: "createQuery",
}
}
}
// End the query.
if _, err := b.Write([]byte{'}'}); err != nil {
return err
}
return nil
}