Skip to content

Commit 35d8dcf

Browse files
committed
Move the check about missing assignment in SET operation to focus UPDATE statements only.
1 parent 6880861 commit 35d8dcf

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/Components/SetOperation.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
139139
$parser->error('Unexpected token.', $commaLastSeenAt);
140140
}
141141

142-
// We got a SET operation without any assignment
143-
if ($ret === []) {
144-
$parser->error('Missing assignment in SET operation.', $list->tokens[$list->idx]);
145-
}
146-
147142
return $ret;
148143
}
149144

src/Statements/UpdateStatement.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
use PhpMyAdmin\SqlParser\Components\Limit;
1111
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
1212
use PhpMyAdmin\SqlParser\Components\SetOperation;
13+
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
14+
use PhpMyAdmin\SqlParser\Parser;
1315
use PhpMyAdmin\SqlParser\Statement;
16+
use PhpMyAdmin\SqlParser\Token;
17+
use PhpMyAdmin\SqlParser\TokensList;
1418

1519
/**
1620
* `UPDATE` statement.
@@ -135,4 +139,23 @@ class UpdateStatement extends Statement
135139
* @var JoinKeyword[]|null
136140
*/
137141
public $join;
142+
143+
/**
144+
* Function called after the token was processed.
145+
* In the update statement, this is used to check that at least one assignment has been set to throw an error if a
146+
* query like `UPDATE acme SET WHERE 1;` is parsed.
147+
*
148+
* @throws ParserException
149+
*/
150+
public function after(Parser $parser, TokensList $list, Token $token)
151+
{
152+
/** @psalm-var string $tokenValue */
153+
$tokenValue = $token->value;
154+
// Ensure we finished to parse the "SET" token, and if yes, ensure that assignments are defined.
155+
if ($this->set !== [] || (Parser::$KEYWORD_PARSERS[$tokenValue]['field'] ?? null) !== 'set') {
156+
return;
157+
}
158+
159+
$parser->error('Missing assignment in SET operation.', $list->tokens[$list->idx]);
160+
}
138161
}

tests/data/parser/parseUpdate3.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@
243243
"@type": "@12"
244244
},
245245
0
246+
],
247+
[
248+
"Missing assignment in SET operation.",
249+
{
250+
"@type": "@11"
251+
},
252+
0
246253
]
247254
]
248255
}

0 commit comments

Comments
 (0)