-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.go
120 lines (113 loc) · 3.3 KB
/
statement.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
120
// Copyright (c) 2022 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pageql
import (
"encoding/json"
"fmt"
"strings"
)
type Statement struct {
Expression *Expression `parser:"@@" json:"expressions,omitempty"`
Limit *Value `parser:"( 'LIMIT' @@ )?" json:"limit,omitempty"`
Offset *Value `parser:"( 'OFFSET' @@ )?" json:"offset,omitempty"`
OrderBy *string `parser:"( 'ORDER' 'BY' '.' @Ident )?" json:"order-by,omitempty"`
SortDir *string `parser:"( @'ASC' | @'DSC' | @'DESC' )?" json:"sort-dir,omitempty"`
ContextKeys []string `parser:"" json:"context-keys,omitempty"`
rendered bool `parser:""`
}
func (s *Statement) Render() (out *Statement) {
out = new(Statement)
if s.Expression != nil {
out.Expression = s.Expression.Render()
}
if s.Limit != nil {
limit := *s.Limit
out.Limit = &limit
}
if s.Offset != nil {
offset := *s.Offset
out.Offset = &offset
}
if s.OrderBy != nil {
orderBy := *s.OrderBy
out.OrderBy = &orderBy
}
if s.SortDir != nil {
sortDir := *s.SortDir
out.SortDir = &sortDir
}
out.ContextKeys = append(out.ContextKeys, s.ContextKeys...)
out.rendered = true
return
}
func (s *Statement) String() (query string) {
if s.rendered {
return
}
var compile func(expr *Expression)
compile = func(expr *Expression) {
switch {
case expr.Operation != nil:
var right string
switch {
case expr.Operation.Right.ContextKey != nil:
right = "." + *expr.Operation.Right.ContextKey
case expr.Operation.Right.String != nil:
right = *expr.Operation.Right.String
case expr.Operation.Right.Regexp != nil:
right = "m" + *expr.Operation.Right.Regexp
case expr.Operation.Right.Int != nil:
right = fmt.Sprintf("%v", *expr.Operation.Right.Int)
case expr.Operation.Right.Float != nil:
right = fmt.Sprintf("%v", *expr.Operation.Right.Float)
case expr.Operation.Right.Bool != nil:
right = fmt.Sprintf("%v", *expr.Operation.Right.Bool)
case expr.Operation.Right.Nil != nil:
right = "nil"
}
query += fmt.Sprintf("(.%s %s %s)", *expr.Operation.Left, expr.Operation.Type, right)
case expr.Condition != nil:
query += "("
compile(expr.Condition.Left)
query += " " + strings.ToUpper(expr.Condition.Type) + " "
compile(expr.Condition.Right)
query += ")"
}
}
compile(s.Expression)
apply := func(v string) {
if query != "" {
query += " "
}
query += v
}
if s.Limit != nil {
apply(fmt.Sprintf("LIMIT %d", *s.Limit))
}
if s.Offset != nil {
apply(fmt.Sprintf("OFFSET %d", *s.Offset))
}
if s.OrderBy != nil {
apply(fmt.Sprintf("ORDER BY .%s", *s.OrderBy))
}
if s.SortDir != nil {
apply(strings.ToUpper(*s.SortDir))
}
return
}
func (s *Statement) Stringify() (out string) {
b, _ := json.MarshalIndent(s.Render(), "", " ")
out = string(b)
return
}