diff --git a/src/PHPSQLParser/builders/FromBuilder.php b/src/PHPSQLParser/builders/FromBuilder.php index 1679ac97..be14fab9 100644 --- a/src/PHPSQLParser/builders/FromBuilder.php +++ b/src/PHPSQLParser/builders/FromBuilder.php @@ -40,7 +40,9 @@ */ namespace PHPSQLParser\builders; + use PHPSQLParser\exceptions\UnableToCreateSQLException; +use PHPSQLParser\utils\ExpressionType; /** * This class implements the builder for the [FROM] part. You can overwrite @@ -93,12 +95,20 @@ public function build(array $parsed) { } } else { + $numberOfComments = 0; + foreach ($parsed as $k => $v) { + $k -= $numberOfComments; $len = strlen($sql); $sql .= $this->buildTable($v, $k); $sql .= $this->buildTableExpression($v, $k); $sql .= $this->buildSubquery($v, $k); + if ($this->isComment($v)) { + $sql .= $this->buildComment($v); + $numberOfComments++; + } + if ($len == strlen($sql)) { throw new UnableToCreateSQLException('FROM', $k, $v, 'expr_type'); } @@ -106,5 +116,15 @@ public function build(array $parsed) { } return "FROM " . $sql; } + + protected function isComment($parsed) { + return $parsed['expr_type'] === ExpressionType::COMMENT; + } + + protected function buildComment($parsed) { + if (!$this->isComment($parsed)) { + return ''; + } + return $parsed['value'] . ' '; + } } -?> diff --git a/tests/cases/creator/FromBuilderTest.php b/tests/cases/creator/FromBuilderTest.php new file mode 100644 index 00000000..82681f15 --- /dev/null +++ b/tests/cases/creator/FromBuilderTest.php @@ -0,0 +1,69 @@ + + * @copyright 2010-2014 Justin Swanhart and André Rothe + * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) + * @version SVN: $Id$ + * + */ + +namespace PHPSQLParser\Test\Creator; + +use PHPSQLParser\PHPSQLParser; +use PHPSQLParser\PHPSQLCreator; +use PHPUnit\Framework\TestCase; + +/** + * This class implements the builder for the [FROM] part. You can overwrite + * all functions to achieve another handling. + * + * @author Christian Stoller + */ +class FromBuilderTest extends TestCase +{ + public function testComment() + { + $sql = "SELECT * FROM car /* test comment */ WHERE color = 'black'"; + $parser = new PHPSQLParser($sql); + $creator = new PHPSQLCreator($parser->parsed); + $created = $creator->created; + + // Comment and table name are interchanged, but this can be tolerated. + // Otherwise the parsing would be more complex. + $expected = "SELECT * FROM /* test comment */ car WHERE color = 'black'"; + + $this->assertSame($expected, $created, 'a comment in the from clause'); + } +}