1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SQLParser; |
4
|
|
|
|
5
|
|
|
class ExpressionToken |
6
|
|
|
{ |
7
|
|
|
private $subTree; |
8
|
|
|
private $expression; |
9
|
|
|
private $key; |
10
|
|
|
private $token; |
11
|
|
|
private $tokenType; |
12
|
|
|
private $trim; |
13
|
|
|
private $upper; |
14
|
|
|
|
15
|
|
|
public function __construct($key = '', $token = '') |
16
|
|
|
{ |
17
|
|
|
$this->subTree = false; |
18
|
|
|
$this->expression = ''; |
19
|
|
|
$this->key = $key; |
20
|
|
|
$this->token = $token; |
21
|
|
|
$this->tokenType = false; |
22
|
|
|
$this->trim = trim($token); |
23
|
|
|
$this->upper = strtoupper($this->trim); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
# TODO: we could replace it with a constructor new ExpressionToken(this, "*") |
27
|
|
|
public function addToken($string) |
28
|
|
|
{ |
29
|
|
|
$this->token .= $string; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function isEnclosedWithinParenthesis() |
33
|
|
|
{ |
34
|
|
|
return ($this->upper[0] === '(' && substr($this->upper, -1) === ')'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setSubTree($tree) |
38
|
|
|
{ |
39
|
|
|
$this->subTree = $tree; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getSubTree() |
43
|
|
|
{ |
44
|
|
|
return $this->subTree; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getUpper($idx = false) |
48
|
|
|
{ |
49
|
|
|
return $idx !== false ? $this->upper[$idx] : $this->upper; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getTrim($idx = false) |
53
|
|
|
{ |
54
|
|
|
return $idx !== false ? $this->trim[$idx] : $this->trim; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getToken($idx = false) |
58
|
|
|
{ |
59
|
|
|
return $idx !== false ? $this->token[$idx] : $this->token; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setTokenType($type) |
63
|
|
|
{ |
64
|
|
|
$this->tokenType = $type; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function endsWith($needle) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$length = strlen($needle); |
70
|
|
|
if ($length == 0) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$start = $length * -1; |
75
|
|
|
|
76
|
|
|
return (substr($this->token, $start) === $needle); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function isWhitespaceToken() |
80
|
|
|
{ |
81
|
|
|
return ($this->trim === ''); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isCommaToken() |
85
|
|
|
{ |
86
|
|
|
return ($this->trim === ','); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function isVariableToken() |
90
|
|
|
{ |
91
|
|
|
return $this->upper[0] === '@'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function isSubQueryToken() |
95
|
|
|
{ |
96
|
|
|
return preg_match('/^\\(\\s*SELECT/i', $this->trim); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function isExpression() |
100
|
|
|
{ |
101
|
|
|
return $this->tokenType === ExpressionType::EXPRESSION; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function isBracketExpression() |
105
|
|
|
{ |
106
|
|
|
return $this->tokenType === ExpressionType::BRACKET_EXPRESSION; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function isOperator() |
110
|
|
|
{ |
111
|
|
|
return $this->tokenType === ExpressionType::OPERATOR; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function isInList() |
115
|
|
|
{ |
116
|
|
|
return $this->tokenType === ExpressionType::IN_LIST; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function isFunction() |
120
|
|
|
{ |
121
|
|
|
return $this->tokenType === ExpressionType::SIMPLE_FUNCTION; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function isUnspecified() |
125
|
|
|
{ |
126
|
|
|
return ($this->tokenType === false); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function isAggregateFunction() |
130
|
|
|
{ |
131
|
|
|
return $this->tokenType === ExpressionType::AGGREGATE_FUNCTION; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function isColumnReference() |
135
|
|
|
{ |
136
|
|
|
return $this->tokenType === ExpressionType::COLREF; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function isConstant() |
140
|
|
|
{ |
141
|
|
|
return $this->tokenType === ExpressionType::CONSTANT; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function isSign() |
145
|
|
|
{ |
146
|
|
|
return $this->tokenType === ExpressionType::SIGN; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function isSubQuery() |
150
|
|
|
{ |
151
|
|
|
return $this->tokenType === ExpressionType::SUBQUERY; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function toArray() |
155
|
|
|
{ |
156
|
|
|
return array('expr_type' => $this->tokenType, 'base_expr' => $this->token, 'sub_tree' => $this->subTree); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.