Skip to content

Commit

Permalink
Fixed SqlParser for Triggers that use delimiter statement of mysql.
Browse files Browse the repository at this point in the history
This was the latest version of v1.x (copy &pasted now) and has changed back then without a comment.
  • Loading branch information
marcj committed Dec 2, 2014
1 parent 0dab987 commit 887bfed
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/Propel/Generator/Util/SqlParser.php
Expand Up @@ -21,6 +21,7 @@
class SqlParser
{
protected $delimiter = ';';
protected $delimiterLength = 1;

protected $sql = '';
protected $len = 0;
Expand Down Expand Up @@ -206,9 +207,9 @@ public function getNextStatement()
$isInString = false;
$stringQuotes = '';
$parsedString = '';
while ($this->pos < $this->len) {
$char = $this->sql[$this->pos];

$lowercaseString = ''; // helper variable for performance sake
while ($this->pos <= $this->len) {
$char = isset($this->sql[$this->pos]) ? $this->sql[$this->pos] : '';
// check flags for strings or escaper
switch ($char) {
case "\\":
Expand All @@ -226,21 +227,49 @@ public function getNextStatement()
}
break;
}

$this->pos++;

if ($char !== "\\") {
$isAfterBackslash = false;
}

// check for end of statement
if (!$isInString && $char == $this->delimiter) {
return trim($parsedString);
if (!$isInString) {
if (false !== strpos($lowercaseString, 'delimiter ')) {
// remove DELIMITER from string because it's a command-line keyword only
$parsedString = trim(str_ireplace('delimiter ', '', $parsedString));
// set new delimiter
$this->delimiter = $char;
// append other delimiter characters if any
while (isset($this->sql[$this->pos]) && $this->sql[$this->pos] != "\n") {
$this->delimiter .= $this->sql[$this->pos++]; // increase position
}
$this->delimiter = trim($this->delimiter);
// store delimiter length for better performance
$this->delimiterLength = strlen($this->delimiter);
// delimiter has changed so return current sql if any
if ($parsedString) {
return $parsedString;
} else {
// reset helper variable
$lowercaseString = '';
continue;
}
}
// get next characters if we have multiple characters in delimiter
$nextChars = '';
for ($i = 0; $i < $this->delimiterLength - 1; $i++) {
if (!isset($this->sql[$this->pos + $i])) break;
$nextChars .= $this->sql[$this->pos + $i];
}
// check for end of statement
if ($char.$nextChars == $this->delimiter) {
$this->pos += $i; // increase position
return trim($parsedString);
}
// avoid using strtolower on the whole parsed string every time new character is added
// there is also no point in adding characters which are in the string
$lowercaseString .= strtolower($char);
}

$parsedString .= $char;
}

return trim($parsedString);
}

Expand Down

0 comments on commit 887bfed

Please sign in to comment.