-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexpr.go
180 lines (157 loc) · 4.49 KB
/
expr.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package mql
import (
"fmt"
)
type exprType int
const (
unknownExprType exprType = iota
comparisonExprType
logicalExprType
)
type expr interface {
Type() exprType
String() string
}
// ComparisonOp defines a set of comparison operators
type ComparisonOp string
const (
GreaterThanOp ComparisonOp = ">"
GreaterThanOrEqualOp ComparisonOp = ">="
LessThanOp ComparisonOp = "<"
LessThanOrEqualOp ComparisonOp = "<="
EqualOp ComparisonOp = "="
NotEqualOp ComparisonOp = "!="
ContainsOp ComparisonOp = "%"
)
func newComparisonOp(s string) (ComparisonOp, error) {
const op = "newComparisonOp"
switch ComparisonOp(s) {
case
GreaterThanOp,
GreaterThanOrEqualOp,
LessThanOp,
LessThanOrEqualOp,
EqualOp,
NotEqualOp,
ContainsOp:
return ComparisonOp(s), nil
default:
return "", fmt.Errorf("%s: %w %q", op, ErrInvalidComparisonOp, s)
}
}
type comparisonExpr struct {
column string
comparisonOp ComparisonOp
value *string
}
// Type returns the expr type
func (e *comparisonExpr) Type() exprType {
return comparisonExprType
}
// String returns a string rep of the expr
func (e *comparisonExpr) String() string {
switch e.value {
case nil:
return fmt.Sprintf("(comparisonExpr: %s %s nil)", e.column, e.comparisonOp)
default:
return fmt.Sprintf("(comparisonExpr: %s %s %s)", e.column, e.comparisonOp, *e.value)
}
}
func (e *comparisonExpr) isComplete() bool {
return e.column != "" && e.comparisonOp != "" && e.value != nil
}
// defaultValidateConvert will validate the comparison expr value, and then convert the
// expr to its SQL equivalence.
func defaultValidateConvert(columnName string, comparisonOp ComparisonOp, columnValue *string, validator validator, opt ...Option) (*WhereClause, error) {
const op = "mql.(comparisonExpr).convertToSql"
switch {
case columnName == "":
return nil, fmt.Errorf("%s: %w", op, ErrMissingColumn)
case comparisonOp == "":
return nil, fmt.Errorf("%s: %w", op, ErrMissingComparisonOp)
case isNil(columnValue):
return nil, fmt.Errorf("%s: %w", op, ErrMissingComparisonValue)
case validator.fn == nil:
return nil, fmt.Errorf("%s: missing validator function: %w", op, ErrInvalidParameter)
case validator.typ == "":
return nil, fmt.Errorf("%s: missing validator type: %w", op, ErrInvalidParameter)
}
// everything was validated at the start, so we know this is a valid/complete comparisonExpr
e := &comparisonExpr{
column: columnName,
comparisonOp: comparisonOp,
value: columnValue,
}
v, err := validator.fn(*e.value)
if err != nil {
return nil, fmt.Errorf("%s: %q in %s: %w", op, *e.value, e.String(), ErrInvalidParameter)
}
if validator.typ == "time" {
columnName = fmt.Sprintf("%s::date", columnName)
}
switch e.comparisonOp {
case ContainsOp:
return &WhereClause{
Condition: fmt.Sprintf("%s like ?", columnName),
Args: []any{fmt.Sprintf("%%%s%%", v)},
}, nil
default:
return &WhereClause{
Condition: fmt.Sprintf("%s%s?", columnName, e.comparisonOp),
Args: []any{v},
}, nil
}
}
type logicalOp string
const (
andOp logicalOp = "and"
orOp logicalOp = "or"
)
func newLogicalOp(s string) (logicalOp, error) {
const op = "newLogicalOp"
switch logicalOp(s) {
case andOp, orOp:
return logicalOp(s), nil
default:
return "", fmt.Errorf("%s: %w %q", op, ErrInvalidLogicalOp, s)
}
}
type logicalExpr struct {
leftExpr expr
logicalOp logicalOp
rightExpr expr
}
// Type returns the expr type
func (l *logicalExpr) Type() exprType {
return logicalExprType
}
// String returns a string rep of the expr
func (l *logicalExpr) String() string {
return fmt.Sprintf("(logicalExpr: %s %s %s)", l.leftExpr, l.logicalOp, l.rightExpr)
}
// root will return the root of the expr tree
func root(lExpr *logicalExpr, raw string) (expr, error) {
const op = "mql.root"
switch {
// intentionally not checking raw, since can be an empty string
case lExpr == nil:
return nil, fmt.Errorf("%s: %w (missing expression)", op, ErrInvalidParameter)
}
logicalOp := lExpr.logicalOp
if logicalOp != "" && lExpr.rightExpr == nil {
return nil, fmt.Errorf("%s: %w in: %q", op, ErrMissingRightSideExpr, raw)
}
for lExpr.logicalOp == "" {
switch {
case lExpr.leftExpr == nil:
return nil, fmt.Errorf("%s: %w nil in: %q", op, ErrMissingExpr, raw)
case lExpr.leftExpr.Type() == comparisonExprType:
return lExpr.leftExpr, nil
default:
lExpr = lExpr.leftExpr.(*logicalExpr)
}
}
return lExpr, nil
}