Skip to content

Commit 7dc4b80

Browse files
committed
Improving Expression to allow is null and is not null column options
Fixes: phpmyadmin/phpmyadmin#17422 Signed-off-by: iifawzi <[email protected]>
1 parent e29ed1e commit 7dc4b80

5 files changed

Lines changed: 1110 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* Fixed differentiating between `ANALYZE` and `EXPLAIN` statements (#386)
1414
* Added "NOT" to the select options (#374)
1515
* Implement the `EXPLAIN` Parser (#389)
16-
* Context: Updated contexts to contain `multipoint` and `multipolygon` data types. (#393)
16+
* Context: Updated contexts to contain `multipoint` and `multipolygon` data types (#393)
17+
* Improving Expression to allow `IS NULL` and `IS NOT NULL` column options (#399)
1718

1819
## [5.5.0] - 2021-12-08
1920

src/Components/Expression.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use function implode;
1515
use function is_array;
16+
use function rtrim;
1617
use function strlen;
1718
use function trim;
1819

@@ -33,7 +34,6 @@ class Expression extends Component
3334
private static $ALLOWED_KEYWORDS = [
3435
'AS' => 1,
3536
'DUAL' => 1,
36-
'NULL' => 1,
3737
'REGEXP' => 1,
3838
'CASE' => 1,
3939
'DIV' => 1,
@@ -44,6 +44,10 @@ class Expression extends Component
4444
'MOD' => 1,
4545

4646
'OVER' => 2,
47+
48+
'IS' => 3,
49+
'NOT NULL' => 4,
50+
'NULL' => 4,
4751
];
4852

4953
/**
@@ -371,6 +375,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =
371375

372376
$ret->alias = $prev[1]->value;
373377
} else {
378+
$currIdx = $list->idx;
379+
--$list->idx;
380+
$beforeToken = $list->getPrevious();
381+
$list->idx = $currIdx;
382+
// columns names tokens are of type NONE, and the columns options
383+
// would start with a token of type KEYWORD, in that case, we want to have a space
384+
// between the tokens.
385+
if (
386+
$ret->expr !== null && $beforeToken &&
387+
$beforeToken->type === Token::TYPE_NONE && $token->type === Token::TYPE_KEYWORD
388+
) {
389+
$ret->expr = rtrim($ret->expr, ' ') . ' ';
390+
}
391+
374392
$ret->expr .= $token->token;
375393
}
376394
} elseif (! $isExpr) {

tests/Builder/SelectStatementTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ public function testBuilderUnion(): void
3535
);
3636
}
3737

38+
public function testBuilderWithIsNull(): void
39+
{
40+
$parser = new Parser('SELECT test3.t1 is not null AS `col1` FROM test3');
41+
$stmt = $parser->statements[0];
42+
43+
$this->assertEquals('SELECT test3.t1 is not null AS `col1` FROM test3', $stmt->build());
44+
45+
$parser = new Parser('SELECT test3.t1 is null AS `col1` FROM test3');
46+
$stmt = $parser->statements[0];
47+
48+
$this->assertEquals('SELECT test3.t1 is null AS `col1` FROM test3', $stmt->build());
49+
}
50+
3851
public function testBuilderAlias(): void
3952
{
4053
$parser = new Parser(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE VIEW `t3` as SELECT `t1` IS NOT NULL AS `is_not_null` FROM `test3`;

0 commit comments

Comments
 (0)