Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore comment option #330

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/PHPSQLParser/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ final class Options
*/
const ANSI_QUOTES = 'ansi_quotes';


/**
* @const string
*/
const IGNORE_COMMENT = 'ignore_comment';

/**
* Options constructor.
*
Expand All @@ -54,4 +60,12 @@ public function getANSIQuotes()
{
return (isset($this->options[self::ANSI_QUOTES]) && $this->options[self::ANSI_QUOTES]);
}

/**
* @return bool
*/
public function getIgnoreComment()
{
return (isset($this->options[self::IGNORE_COMMENT]) && $this->options[self::IGNORE_COMMENT]);
}
}
5 changes: 4 additions & 1 deletion src/PHPSQLParser/processors/ExpressionListProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public function process($tokens) {


if ($this->isCommentToken($v)) {
$resultList[] = parent::processComment($v);
if(!$this->options->getIgnoreComment()) {
$resultList[] = parent::processComment($v);
}

continue;
}

Expand Down
5 changes: 4 additions & 1 deletion src/PHPSQLParser/processors/FromProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ public function process($tokens) {
}

if ($this->isCommentToken($token)) {
$expr[] = parent::processComment($token);
if(!$this->options->getIgnoreComment()) {
$expr[] = parent::processComment($token);
}

continue;
}

Expand Down
7 changes: 5 additions & 2 deletions src/PHPSQLParser/processors/InsertProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ public function process($tokenList, $token_category = 'INSERT') {
}
foreach ($token as &$value) {
if ($this->isCommentToken($value)) {
$comments[] = parent::processComment($value);
$value = '';
if(!$this->options->getIgnoreComment()) {
$comments[] = parent::processComment($value);
}

$value = '';
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/PHPSQLParser/processors/LimitProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ public function process($tokens) {

foreach ($tokens as &$token) {
if ($this->isCommentToken($token)) {
$comments[] = parent::processComment($token);
$token = '';
if(!$this->options->getIgnoreComment()) {
$comments[] = parent::processComment($token);
}

$token = '';
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/PHPSQLParser/processors/OrderByProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ public function process($tokens, $select = array()) {

default:
if ($this->isCommentToken($token)) {
$out[] = parent::processComment($token);
if(!$this->options->getIgnoreComment()) {
$out[] = parent::processComment($token);
}

break;
}

Expand Down
10 changes: 6 additions & 4 deletions src/PHPSQLParser/processors/SelectProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
namespace PHPSQLParser\processors;

/**
*
*
* This class processes the SELECT statements.
*
*
* @author arothe
*
*
*/
class SelectProcessor extends SelectExpressionProcessor {

Expand All @@ -51,7 +51,9 @@ public function process($tokens) {
$expressionList[] = $expression;
$expression = "";
} else if ($this->isCommentToken($token)) {
$expressionList[] = parent::processComment($token);
if(!$this->options->getIgnoreComment()) {
$expressionList[] = parent::processComment($token);
}
} else {
switch (strtoupper($token)) {

Expand Down
5 changes: 4 additions & 1 deletion src/PHPSQLParser/processors/ValuesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public function process($tokens) {

foreach ($tokens['VALUES'] as $k => $v) {
if ($this->isCommentToken($v)) {
$parsed[] = parent::processComment($v);
if(!$this->options->getIgnoreComment()) {
$parsed[] = parent::processComment($v);
}

continue;
}

Expand Down
127 changes: 127 additions & 0 deletions tests/cases/parser/ignoreCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace PHPSQLParser\Test\Parser;

use PHPSQLParser\PHPSQLParser;

class IgnoreCommentsTest extends \PHPUnit_Framework_TestCase
{
public function testComments1()
{
$sqlWithComment = 'SELECT a, -- inline comment in SELECT section
b
FROM test';
$sqlWithoutComment = 'SELECT a,
b
FROM test';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in SELECT section');
}

public function testComments2()
{
$sqlWithComment = 'SELECT a, /*
multi line
comment
*/
b
FROM test';
$sqlWithoutComment = 'SELECT a,
b
FROM test';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'multi line comment');
}

public function testComments3()
{
$sqlWithComment = 'SELECT a
FROM test -- inline comment in FROM section';
$sqlWithoutComment = 'SELECT a
FROM test';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in FROM section');
}

public function testComments4()
{
$sqlWithComment = 'SELECT a
FROM test
WHERE id = 3 -- inline comment in WHERE section
AND b > 4';
$sqlWithoutComment = 'SELECT a
FROM test
WHERE id = 3
AND b > 4';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in WHERE section');
}

public function testComments5()
{
$sqlWithComment = 'SELECT a
FROM test
LIMIT -- inline comment in LIMIT section
10';
$sqlWithoutComment = 'SELECT a
FROM test
LIMIT
10';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in LIMIT section');
}

public function testComments6()
{
$sqlWithComment = 'SELECT a
FROM test
ORDER BY -- inline comment in ORDER BY section
a DESC';
$sqlWithoutComment = 'SELECT a
FROM test
ORDER BY
a DESC';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in ORDER BY section');
}

public function testComments7()
{
$sqlWithComment = 'INSERT INTO a (id) -- inline comment in INSERT section
VALUES (1)';
$sqlWithoutComment = 'INSERT INTO a (id)
VALUES (1)';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in INSERT section');
}

public function testComments8()
{
$sqlWithComment = 'INSERT INTO a (id)
VALUES (1) -- inline comment in VALUES section';
$sqlWithoutComment = 'INSERT INTO a (id)
VALUES (1)';

$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in VALUES section');
}

public function testComments9()
{
$sqlWithComment = 'INSERT INTO a (id) -- inline comment in INSERT section;
SELECT id -- inline comment in SELECT section
FROM x';
$sqlWithoutComment = 'INSERT INTO a (id)
SELECT id
FROM x';
$this->commonAssert($sqlWithComment, $sqlWithoutComment, 'inline comment in SELECT section');
}

private function commonAssert($sqlWithComment, $sqlWithoutComment, $assertMessage)
{
$withComment = (new PHPSQLParser($sqlWithComment, false, ['ignore_comment' => true]))->parsed;
$withoutComment = (new PHPSQLParser($sqlWithoutComment, false, ['ignore_comment' => false]))->parsed;
$this->assertEquals($withComment, $withoutComment, $assertMessage);
}
}

?>