diff options
author | Deven Bansod <devenbansod.bits@gmail.com> | 2016-11-03 13:56:04 +0530 |
---|---|---|
committer | Deven Bansod <devenbansod.bits@gmail.com> | 2016-11-03 13:56:04 +0530 |
commit | 7895b262cbb715511cc98ce44bc6504d9ae8ac88 (patch) | |
tree | a0d1dd194359f7febd0b62f353e32e7c61cdaff9 /src/Utils/Query.php | |
parent | 94e550e4a9b359332bab257a5f4aa6dfb8e48ea7 (diff) | |
download | sql-parser-7895b262cbb715511cc98ce44bc6504d9ae8ac88.zip sql-parser-7895b262cbb715511cc98ce44bc6504d9ae8ac88.tar.gz sql-parser-7895b262cbb715511cc98ce44bc6504d9ae8ac88.tar.bz2 |
Validate order of clauses in the parsing of statements
Fix #22
Signed-off-by: Deven Bansod <devenbansod.bits@gmail.com>
Diffstat (limited to 'src/Utils/Query.php')
-rw-r--r-- | src/Utils/Query.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/Utils/Query.php b/src/Utils/Query.php index 2e0982a..c3167ff 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -783,4 +783,66 @@ class Query return array(trim($statement), $query, $delimiter); } + + /** + * Gets a starting offset of a specific clause. + * + * @param Statement $statement The parsed query that has to be modified. + * @param TokensList $list The list of tokens. + * @param string $clause The clause to be returned. + * + * @return int + */ + public static function getClauseStartOffset($statement, $list, $clause) + { + + /** + * The index of the current clause. + * + * @var int $currIdx + */ + $currIdx = 0; + + /** + * The count of brackets. + * We keep track of them so we won't insert the clause in a subquery. + * + * @var int $brackets + */ + $brackets = 0; + + /** + * The clauses of this type of statement and their index. + * + * @var array $clauses + */ + $clauses = array_flip(array_keys($statement->getClauses())); + + for ($i = $statement->first; $i <= $statement->last; ++$i) { + $token = $list->tokens[$i]; + + if ($token->type === Token::TYPE_COMMENT) { + continue; + } + + if ($token->type === Token::TYPE_OPERATOR) { + if ($token->value === '(') { + ++$brackets; + } elseif ($token->value === ')') { + --$brackets; + } + } + + if ($brackets == 0) { + if (($token->type === Token::TYPE_KEYWORD) + && (isset($clauses[$token->value])) + && ($clause === $token->value) + ) { + return $i; + } + } + } + + return -1; + } } |