PHP-SQL-Parser icon indicating copy to clipboard operation
PHP-SQL-Parser copied to clipboard

Position issue

Open Nick74k opened this issue 8 years ago • 1 comments

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

Nick74k avatar Mar 24 '17 12:03 Nick74k

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...

Nick74k avatar Mar 24 '17 14:03 Nick74k