PHP-SQL-Parser
PHP-SQL-Parser copied to clipboard
Position issue
Wrong position of "Order" (Guess if it content was repeated in From) in some cases.
<?php
require_once ROOT_FOLDER.'/Plugins/SQLParser411m/vendor/autoload.php';
$parser = new PHPSQLParser\PHPSQLParser(
"select 1
from travordersview
left join t1 on t1.i=travordersview.ID
left join t2 on t2.id=5
order by travordersview.ID", true);
echo "Order pos: ".$parser->parsed["ORDER"][0]["position"]."<BR>";
$parser = new PHPSQLParser\PHPSQLParser(
"select 1
from travordersview
left join t1 on t1.i=travordersview.ID
left join t2 on t2.id=5
order by travordersview.IM", true);
echo "Order pos: ".$parser->parsed["ORDER"][0]["position"]."<BR>";
?>
Result:
Order pos: 52 (Wrong - must be 105 too)
Order pos: 105
Solved by replacing
// move the current pos after the keyword
// SELECT, WHERE, INSERT etc.
if (PHPSQLParserConstants::getInstance()->isReserved($key)) {
$charPos = stripos($sql, $key, $charPos);
$charPos += strlen($key);
}
with
// move the current pos after the keyword
// SELECT, WHERE, INSERT etc.
if (PHPSQLParserConstants::getInstance()->isReserved($key)) {
$spaces = array(" ","\r","\t","\n");
$l = strlen($key);
do {
$charPos = stripos($sql, $key, $charPos);
$charPos += $l;
} while ($charPos !== FALSE && (!in_array($sql[$charPos], $spaces)
|| ($charPos > $l && !in_array($sql[$charPos-$l-1], $spaces))) );
}
at file positions/PositionCalculator.php, function lookForBaseExpression. Not sure it will work with subqueries...