MCPcopy
hub / github.com/dgraph-io/dgraph / parseUpsertBlock

Function parseUpsertBlock

dql/parser_mutation.go:53–143  ·  view source on GitHub ↗

parseUpsertBlock parses the upsert block

(it *lex.ItemIterator)

Source from the content-addressed store, hash-verified

51
52// parseUpsertBlock parses the upsert block
53func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {
54 var req *api.Request
55 var queryText, condText string
56 var queryFound bool
57
58 // ===>upsert<=== {...}
59 if !it.Next() {
60 return nil, it.Errorf("Unexpected end of upsert block")
61 }
62
63 // upsert ===>{<=== ....}
64 item := it.Item()
65 if item.Typ != itemLeftCurl {
66 return nil, it.Errorf("Expected { at the start of block. Got: [%s]", item.Val)
67 }
68
69 for it.Next() {
70 item = it.Item()
71 switch {
72 // upsert {... ===>}<===
73 case item.Typ == itemRightCurl:
74 switch {
75 case req == nil:
76 return nil, it.Errorf("Empty mutation block")
77 case !queryFound:
78 return nil, it.Errorf("Query op not found in upsert block")
79 default:
80 req.Query = queryText
81 return req, nil
82 }
83
84 // upsert { mutation{...} ===>query<==={...}}
85 case item.Typ == itemUpsertBlockOp && item.Val == "query":
86 if queryFound {
87 return nil, it.Errorf("Multiple query ops inside upsert block")
88 }
89 queryFound = true
90 if !it.Next() {
91 return nil, it.Errorf("Unexpected end of upsert block")
92 }
93 item = it.Item()
94 if item.Typ != itemUpsertBlockOpContent {
95 return nil, it.Errorf("Expecting brace, found '%s'", item.Val)
96 }
97 queryText += item.Val
98
99 // upsert { ===>mutation<=== {...} query{...}}
100 case item.Typ == itemUpsertBlockOp && item.Val == "mutation":
101 if !it.Next() {
102 return nil, it.Errorf("Unexpected end of upsert block")
103 }
104
105 // upsert { mutation ===>@if(...)<=== {....} query{...}}
106 item = it.Item()
107 if item.Typ == itemUpsertBlockOpContent {
108 condText = item.Val
109 if !it.Next() {
110 return nil, it.Errorf("Unexpected end of upsert block")

Callers 2

ParseMutationFunction · 0.85
ParseDQLFunction · 0.85

Calls 4

parseMutationBlockFunction · 0.85
ItemMethod · 0.80
NextMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected