summaryrefslogtreecommitdiffstats
path: root/src/Utils/Query.php
diff options
context:
space:
mode:
authorDan Ungureanu <udan1107@gmail.com>2015-08-02 22:04:05 +0300
committerDan Ungureanu <udan1107@gmail.com>2015-08-02 22:04:05 +0300
commit9130cca0d17169b4647d39b644cc6483b600623c (patch)
treeaab84450559ba2651f31d92648ff9549f25eed5e /src/Utils/Query.php
parente6a562e3e5bfbbbc03d469e50d65d063cac9ebd4 (diff)
downloadsql-parser-9130cca0d17169b4647d39b644cc6483b600623c.zip
sql-parser-9130cca0d17169b4647d39b644cc6483b600623c.tar.gz
sql-parser-9130cca0d17169b4647d39b644cc6483b600623c.tar.bz2
Added utility to get first full statement from a buffer.
Misc coding style fixes.
Diffstat (limited to 'src/Utils/Query.php')
-rw-r--r--src/Utils/Query.php63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/Utils/Query.php b/src/Utils/Query.php
index c11e858..d8e9500 100644
--- a/src/Utils/Query.php
+++ b/src/Utils/Query.php
@@ -587,6 +587,10 @@ class Query
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;
@@ -708,4 +712,63 @@ class Query
return $ret;
}
+
+ /**
+ * Gets the first full statement in the query.
+ *
+ * @param string $query The query to be analyzed.
+ * @param string $delimiter The delimiter to be used.
+ *
+ * @return array Array containing the first full query, the
+ * remaining part of the query and the last
+ * delimiter.
+ */
+ public static function getFirstStatement($query, $delimiter = null)
+ {
+ $lexer = new Lexer($query, false, $delimiter);
+ $list = $lexer->list;
+
+ /**
+ * Whether a full statement was found.
+ * @var bool
+ */
+ $fullStatement = false;
+
+ /**
+ * The first full statement.
+ * @var string
+ */
+ $statement = '';
+
+ for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) {
+ $token = $list->tokens[$list->idx];
+
+ if ($token->type === Token::TYPE_COMMENT) {
+ continue;
+ }
+
+ $statement .= $token->token;
+
+ if (($token->type === Token::TYPE_DELIMITER) && (!empty($token->value))) {
+ $delimiter = $token->value;
+ $fullStatement = true;
+ break;
+ }
+ }
+
+ // No statement was found so we return the entire query as being the
+ // remaining part.
+ if (!$fullStatement) {
+ return array(null, $query, $delimiter);
+ }
+
+ // At least one query was found so we have to build the rest of the
+ // remaining query.
+ $query = '';
+ for (++$list->idx; $list->idx < $list->count; ++$list->idx) {
+ $query .= $list->tokens[$list->idx]->value;
+ }
+
+ return array(trim($statement), $query, $delimiter);
+ }
}