Skip to content

Commit

Permalink
Fixed some errors and add new tests
Browse files Browse the repository at this point in the history
Signed-off-by: Deven Bansod <devenbansod.bits@gmail.com>
  • Loading branch information
devenbansod committed Sep 26, 2016
1 parent 0fb198f commit 98679cb
Show file tree
Hide file tree
Showing 33 changed files with 87 additions and 46 deletions.
6 changes: 1 addition & 5 deletions src/Components/Limit.php
Expand Up @@ -127,10 +127,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
*/
public static function build($component, array $options = array())
{
if (count($component->offset) === 0) {
return (string) $component->rowCount;
} else {
return $component->offset . ', ' . $component->rowCount;
}
return $component->offset . ', ' . $component->rowCount;
}
}
50 changes: 20 additions & 30 deletions src/Statements/DeleteStatement.php
Expand Up @@ -180,6 +180,13 @@ public function parse(Parser $parser, TokensList $list)
*/
$state = 0;

/**
* If the query is multi-table or not
*
* @var bool $multiTable
*/
$multiTable = false;

for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
Expand All @@ -193,11 +200,6 @@ public function parse(Parser $parser, TokensList $list)
break;
}

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}

if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD
&& $token->value !== 'FROM'
Expand Down Expand Up @@ -226,9 +228,6 @@ public function parse(Parser $parser, TokensList $list)
++$list->idx; // Skip 'FROM'
$this->from = ExpressionArray::parse($parser, $list);
$state = 2;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error(__('Unexpected keyword.'), $token);
break;
} else {
$parser->error(__('Unexpected token.'), $token);
break;
Expand All @@ -237,9 +236,11 @@ public function parse(Parser $parser, TokensList $list)
if ($token->type === Token::TYPE_KEYWORD
&& $token->value === 'USING'
) {
++$list->idx; // Skip 'FROM'
++$list->idx; // Skip 'USING'
$this->using = ExpressionArray::parse($parser, $list);
$state = 3;

$multiTable = true;
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->value === 'WHERE'
) {
Expand All @@ -261,9 +262,6 @@ public function parse(Parser $parser, TokensList $list)
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error(__('Unexpected keyword.'), $token);
break;
} else {
$parser->error(__('Unexpected token.'), $token);
break;
}
} elseif ($state === 3) {
if ($token->type === Token::TYPE_KEYWORD
Expand All @@ -272,18 +270,6 @@ public function parse(Parser $parser, TokensList $list)
++$list->idx; // Skip 'WHERE'
$this->where = Condition::parse($parser, $list);
$state = 4;
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->value === 'ORDER BY'
) {
++$list->idx; // Skip 'ORDER BY'
$this->order = OrderKeyword::parse($parser, $list);
$state = 5;
} elseif ($token->type === Token::TYPE_KEYWORD
&& $token->value === 'LIMIT'
) {
++$list->idx; // Skip 'LIMIT'
$this->limit = Limit::parse($parser, $list);
$state = 6;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error(__('Unexpected keyword.'), $token);
break;
Expand All @@ -292,6 +278,16 @@ public function parse(Parser $parser, TokensList $list)
break;
}
} elseif ($state === 4) {
if ($multiTable === true
&& $token->type === Token::TYPE_KEYWORD
) {
$parser->error(
__('This type of clause is not valid in Multi-table queries.'),
$token
);
break;
}

if ($token->type === Token::TYPE_KEYWORD
&& $token->value === 'ORDER BY'
) {
Expand All @@ -307,9 +303,6 @@ public function parse(Parser $parser, TokensList $list)
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error(__('Unexpected keyword.'), $token);
break;
} else {
$parser->error(__('Unexpected token.'), $token);
break;
}
} elseif ($state === 5) {
if ($token->type === Token::TYPE_KEYWORD
Expand All @@ -321,9 +314,6 @@ public function parse(Parser $parser, TokensList $list)
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error(__('Unexpected keyword.'), $token);
break;
} else {
$parser->error(__('Unexpected token.'), $token);
break;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Builder/DeleteStatementTest.php
Expand Up @@ -78,5 +78,13 @@ public function testBuilderMultiTable()
$stmt = $parser->statements[0];

$this->assertEquals($query, $stmt->build());

/* Assertion 3 */
$query = 'DELETE QUICK FROM table1, table2.* USING table1 AS `t1`, table2 AS `t2` WHERE 1=1';

$parser = new Parser($query);
$stmt = $parser->statements[0];

$this->assertEquals($query, $stmt->build());
}
}
2 changes: 1 addition & 1 deletion tests/Components/LimitTest.php
Expand Up @@ -10,7 +10,7 @@
class LimitTest extends TestCase
{

public function testBuild()
public function testBuildWithoutOffset()
{
$component = new Limit(1);
$this->assertEquals(Limit::build($component), '0, 1');
Expand Down
13 changes: 13 additions & 0 deletions tests/Parser/DeleteStatementTest.php
Expand Up @@ -28,6 +28,19 @@ public function testDeleteProvider()
array('parser/parseDelete8'),
array('parser/parseDelete9'),
array('parser/parseDelete10'),
array('parser/parseDelete11'),
array('parser/parseDelete12'),
array('parser/parseDeleteErr1'),
array('parser/parseDeleteErr2'),
array('parser/parseDeleteErr3'),
array('parser/parseDeleteErr4'),
array('parser/parseDeleteErr5'),
array('parser/parseDeleteErr6'),
array('parser/parseDeleteErr7'),
array('parser/parseDeleteErr8'),
array('parser/parseDeleteErr9'),
array('parser/parseDeleteErr10'),
array('parser/parseDeleteErr11'),
);
}
}
4 changes: 3 additions & 1 deletion tests/data/parser/parseDelete.in
@@ -1,7 +1,9 @@
DELETE LOW_PRIORITY
/* */
FROM
`test`.users
WHERE
`id`<3 AND (username="Dan" or username="Paul")
ORDER BY
id;
id
;

0 comments on commit 98679cb

Please sign in to comment.