forked from manhcuongbk56/cypher-go-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.go
124 lines (105 loc) · 3.2 KB
/
operator.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
package cypher
type Operator struct {
representation string
operatorType OperatorType
key string
notNil bool
err error
}
func OperatorCreate(representation string, operatorType OperatorType) Operator {
o := Operator{
representation: representation,
operatorType: operatorType,
notNil: true,
}
o.key = getAddress(&o)
return o
}
func OperatorError(err error) Operator {
return Operator{err: err}
}
func (o Operator) GetError() error {
return o.err
}
func (o Operator) isNotNil() bool {
return o.notNil
}
func (o Operator) accept(visitor *CypherRenderer) {
visitor.enter(o)
visitor.leave(o)
}
func (o Operator) enter(renderer *CypherRenderer) {
operatorType := o.operatorType
if operatorType == LABEL {
return
}
if operatorType != PREFIX && o != EXPONENTIATION {
renderer.append(" ")
}
renderer.append(o.representation)
if operatorType != POSTFIX && o != EXPONENTIATION {
renderer.append(" ")
}
}
func (o Operator) leave(renderer *CypherRenderer) {
}
func (o Operator) getKey() string {
return o.key
}
func (o Operator) isUnary() bool {
return o.operatorType != BINARY
}
var ADDITION = createBinaryOperator("+")
var SUBTRACTION = createBinaryOperator("-")
var MULTIPLICATION = createBinaryOperator("*")
var DIVISION = createBinaryOperator("/")
var MODULO_DIVISION = createBinaryOperator("%")
var EXPONENTIATION = createBinaryOperator("^")
var EQUALITY = createBinaryOperator("=")
var INEQUALITY = createBinaryOperator("<>")
var LESS_THAN = createBinaryOperator("<")
var GREATER_THAN = createBinaryOperator(">")
var LESS_THAN_OR_EQUAL_TO = createBinaryOperator("<=")
var GREATER_THAN_OR_EQUAL_TO = createBinaryOperator(">=")
var STARTS_WITH = createBinaryOperator("STARTS WITH")
var ENDS_WITH = createBinaryOperator("ENDS WITH")
var CONTAINS = createBinaryOperator("CONTAINS")
var AND = createBinaryOperator("AND")
var OR = createBinaryOperator("OR")
var XOR = createBinaryOperator("XOR")
var CONCAT = createBinaryOperator("+")
var MATCHES = createBinaryOperator("=~")
var IN = createBinaryOperator("IN")
var ASSIGMENT = createBinaryOperator("=")
var PIPE = createBinaryOperator("|")
var IS_NULL = createPostfixOperator("IS NULL")
var IS_NOT_NULL = createPostfixOperator("IS NOT NULL")
var NOT = createPrefixOperator("NOT")
var SET = createPropertyOperator("=")
var GET = createPropertyOperator(".")
var MUTATE = createPropertyOperator("+=")
var SET_LABEL = createLabelOperator("")
var REMOVE_LABEL = createLabelOperator("")
func createBinaryOperator(representation string) Operator {
return OperatorCreate(representation, BINARY)
}
func createPropertyOperator(representation string) Operator {
return OperatorCreate(representation, PROPERTY)
}
func createLabelOperator(representation string) Operator {
return OperatorCreate(representation, LABEL)
}
func createPrefixOperator(representation string) Operator {
return OperatorCreate(representation, PREFIX)
}
func createPostfixOperator(representation string) Operator {
return OperatorCreate(representation, POSTFIX)
}
type OperatorType string
const (
BINARY OperatorType = "binary"
PREFIX = "prefix"
POSTFIX = "postfix"
PROPERTY = "property"
LABEL = "label"
)