-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathdql.go
More file actions
50 lines (42 loc) · 932 Bytes
/
dql.go
File metadata and controls
50 lines (42 loc) · 932 Bytes
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
/*
* SPDX-FileCopyrightText: Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package dql
import (
"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/hypermodeinc/dgraph/v24/lex"
)
func ParseDQL(dqlQuery string) (*api.Request, error) {
var lexer lex.Lexer
lexer.Reset(dqlQuery)
lexer.Run(lexTopLevel)
if err := lexer.ValidateResult(); err != nil {
return nil, err
}
it := lexer.NewIterator()
if !it.Next() {
return nil, it.Errorf("Invalid mutation")
}
item := it.Item()
switch item.Typ {
case itemUpsertBlock:
it.Prev()
return parseUpsertBlock(it)
default:
it.Next()
item = it.Item()
if item.Typ == itemMutationOp {
it.Prev()
it.Prev()
mu, err := parseMutationBlock(it)
if err != nil {
return nil, err
}
return &api.Request{Mutations: []*api.Mutation{mu}}, nil
}
it.Prev()
it.Prev()
return &api.Request{Query: dqlQuery}, nil
}
}