-
Notifications
You must be signed in to change notification settings - Fork 15
/
mutation.go
119 lines (96 loc) · 2.96 KB
/
mutation.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
package dqlx
import (
"bytes"
"context"
"github.com/dgraph-io/dgo/v200"
"strings"
)
// MutationBuilder used to construct mutations
// in a fluent way.
//
// Example: Mutation().Set(data).Execute(ctx)
// Example: Mutation().Delete(data).Execute(ctx)
type MutationBuilder struct {
condition DQLizer
query QueryBuilder
setData interface{}
delData interface{}
unmarshalInto interface{}
client *dgo.Dgraph
}
// Mutation creates a new MutationBuilder
func Mutation() MutationBuilder {
return MutationBuilder{}
}
// Condition assigns a condition for this mutation
// Used for conditional Upserts
// DGraphDoc: https://dgraph.io/docs/mutations/conditional-upsert/
func (builder MutationBuilder) Condition(condition DQLizer) MutationBuilder {
if conditionType, ok := condition.(mutationCondition); ok {
builder.condition = conditionType
} else {
builder.condition = Condition(condition)
}
return builder
}
// Query assigns a query block for this mutation
// making it an Upsert
func (builder MutationBuilder) Query(query QueryBuilder) MutationBuilder {
builder.query = query
return builder
}
// UnmarshalInto defines where the data should be marshalled into,
// once the mutation gets executed
func (builder MutationBuilder) UnmarshalInto(value interface{}) MutationBuilder {
builder.unmarshalInto = value
return builder
}
// Set sets some data to be inserted or updated
func (builder MutationBuilder) Set(data interface{}) MutationBuilder {
builder.setData = data
return builder
}
// Delete sets some data to be deleted
func (builder MutationBuilder) Delete(data interface{}) MutationBuilder {
builder.delData = data
return builder
}
// Execute executes the mutation
func (builder MutationBuilder) Execute(ctx context.Context, options ...OperationExecutorOptionFn) (*Response, error) {
executor := NewDGoExecutor(builder.client)
for _, option := range options {
option(executor)
}
return executor.ExecuteMutations(ctx, builder)
}
// WithDClient changes Dgraph client for this mutation
func (builder MutationBuilder) WithDClient(client *dgo.Dgraph) MutationBuilder {
builder.client = client
return builder
}
type mutationCondition struct {
Filters []DQLizer
}
// Condition returns a condition statement
func Condition(filters ...DQLizer) DQLizer {
return mutationCondition{Filters: filters}
}
// ToDQL returns a DQL statement for a mutation condition
func (condition mutationCondition) ToDQL() (query string, args []interface{}, err error) {
writer := bytes.Buffer{}
writer.WriteString(" @if(")
var statements []string
for _, filter := range condition.Filters {
filterDql, filterArgs, err := filter.ToDQL()
if err != nil {
return "", nil, err
}
filterDql, _ = replacePlaceholders(filterDql, filterArgs, func(index int, value interface{}) string {
return toVariableValue(value)
})
statements = append(statements, filterDql)
}
writer.WriteString(strings.Join(statements, " "))
writer.WriteString(") ")
return writer.String(), nil, err
}