1 | <?php |
||
24 | class SqlToken extends Object implements \ArrayAccess |
||
25 | { |
||
26 | const TYPE_CODE = 0; |
||
27 | const TYPE_STATEMENT = 1; |
||
28 | const TYPE_TOKEN = 2; |
||
29 | const TYPE_PARENTHESIS = 3; |
||
30 | const TYPE_KEYWORD = 4; |
||
31 | const TYPE_OPERATOR = 5; |
||
32 | const TYPE_IDENTIFIER = 6; |
||
33 | const TYPE_STRING_LITERAL = 7; |
||
34 | |||
35 | /** |
||
36 | * @var int token type. It has to be one of the following constants: |
||
37 | * |
||
38 | * - [[TYPE_CODE]] |
||
39 | * - [[TYPE_STATEMENT]] |
||
40 | * - [[TYPE_TOKEN]] |
||
41 | * - [[TYPE_PARENTHESIS]] |
||
42 | * - [[TYPE_KEYWORD]] |
||
43 | * - [[TYPE_OPERATOR]] |
||
44 | * - [[TYPE_IDENTIFIER]] |
||
45 | * - [[TYPE_STRING_LITERAL]] |
||
46 | */ |
||
47 | public $type = self::TYPE_TOKEN; |
||
48 | /** |
||
49 | * @var string|null token content. |
||
50 | */ |
||
51 | public $content; |
||
52 | /** |
||
53 | * @var int original SQL token start position. |
||
54 | */ |
||
55 | public $startOffset; |
||
56 | /** |
||
57 | * @var int original SQL token end position. |
||
58 | */ |
||
59 | public $endOffset; |
||
60 | /** |
||
61 | * @var SqlToken parent token. |
||
62 | */ |
||
63 | public $parent; |
||
64 | |||
65 | /** |
||
66 | * @var SqlToken[] token children. |
||
67 | */ |
||
68 | private $_children = []; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Returns the SQL code representing the token. |
||
73 | * @return string SQL code. |
||
74 | */ |
||
75 | public function __toString() |
||
79 | |||
80 | /** |
||
81 | * Returns whether there is a child token at the specified offset. |
||
82 | * This method is required by the SPL [[\ArrayAccess]] interface. |
||
83 | * It is implicitly called when you use something like `isset($token[$offset])`. |
||
84 | * @param int $offset child token offset. |
||
85 | * @return bool whether the token exists. |
||
86 | */ |
||
87 | 12 | public function offsetExists($offset) |
|
91 | |||
92 | /** |
||
93 | * Returns a child token at the specified offset. |
||
94 | * This method is required by the SPL [[\ArrayAccess]] interface. |
||
95 | * It is implicitly called when you use something like `$child = $token[$offset];`. |
||
96 | * @param int $offset child token offset. |
||
97 | * @return SqlToken|null the child token at the specified offset, `null` if there's no token. |
||
98 | */ |
||
99 | 13 | public function offsetGet($offset) |
|
104 | |||
105 | /** |
||
106 | * Adds a child token to the token. |
||
107 | * This method is required by the SPL [[\ArrayAccess]] interface. |
||
108 | * It is implicitly called when you use something like `$token[$offset] = $child;`. |
||
109 | * @param int|null $offset child token offset. |
||
110 | * @param SqlToken $token token to be added. |
||
111 | */ |
||
112 | 13 | public function offsetSet($offset, $token) |
|
122 | |||
123 | /** |
||
124 | * Removes a child token at the specified offset. |
||
125 | * This method is required by the SPL [[\ArrayAccess]] interface. |
||
126 | * It is implicitly called when you use something like `unset($token[$offset])`. |
||
127 | * @param int $offset child token offset. |
||
128 | */ |
||
129 | 1 | public function offsetUnset($offset) |
|
137 | |||
138 | /** |
||
139 | * Returns child tokens. |
||
140 | * @return SqlToken[] child tokens. |
||
141 | */ |
||
142 | 12 | public function getChildren() |
|
146 | |||
147 | /** |
||
148 | * Sets a list of child tokens. |
||
149 | * @param SqlToken[] $children child tokens. |
||
150 | */ |
||
151 | public function setChildren($children) |
||
160 | |||
161 | /** |
||
162 | * Returns whether the token represents a collection of tokens. |
||
163 | * @return bool whether the token represents a collection of tokens. |
||
164 | */ |
||
165 | 13 | public function getIsCollection() |
|
173 | |||
174 | /** |
||
175 | * Returns whether the token represents a collection of tokens and has non-zero number of children. |
||
176 | * @return bool whether the token has children. |
||
177 | */ |
||
178 | 13 | public function getHasChildren() |
|
182 | |||
183 | /** |
||
184 | * Returns the SQL code representing the token. |
||
185 | * @return string SQL code. |
||
186 | */ |
||
187 | 3 | public function getSql() |
|
195 | |||
196 | /** |
||
197 | * Returns whether this token (including its children) matches the specified "pattern" SQL code. |
||
198 | * |
||
199 | * Usage Example: |
||
200 | * |
||
201 | * ```php |
||
202 | * $patternToken = (new \yii\db\sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize(); |
||
203 | * if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) { |
||
204 | * // ... |
||
205 | * } |
||
206 | * ``` |
||
207 | * |
||
208 | * @param SqlToken $patternToken tokenized SQL code to match against. In addition to normal SQL, the |
||
209 | * `any` keyword is supported which will match any number of keywords, identifiers, whitespaces. |
||
210 | * @param int $offset token children offset to start lookup with. |
||
211 | * @param int|null $firstMatchIndex token children offset where a successful match begins. |
||
212 | * @param int|null $lastMatchIndex token children offset where a successful match ends. |
||
213 | * @return bool whether this token matches the pattern SQL code. |
||
214 | */ |
||
215 | 12 | public function matches(SqlToken $patternToken, $offset = 0, &$firstMatchIndex = null, &$lastMatchIndex = null) |
|
224 | |||
225 | /** |
||
226 | * Tests the given token to match the specified pattern token. |
||
227 | * @param SqlToken $patternToken |
||
228 | * @param SqlToken $token |
||
229 | * @param int $offset |
||
230 | * @param int|null $firstMatchIndex |
||
231 | * @param int|null $lastMatchIndex |
||
232 | * @return bool |
||
233 | */ |
||
234 | 12 | private function tokensMatch(SqlToken $patternToken, SqlToken $token, $offset = 0, &$firstMatchIndex = null, &$lastMatchIndex = null) |
|
281 | |||
282 | /** |
||
283 | * Returns an absolute offset in the children array. |
||
284 | * @param int $offset |
||
285 | * @return int |
||
286 | */ |
||
287 | 13 | private function calculateOffset($offset) |
|
295 | |||
296 | /** |
||
297 | * Updates token SQL code start and end offsets based on its children. |
||
298 | */ |
||
299 | 13 | private function updateCollectionOffsets() |
|
309 | } |
||
310 |