Skip to content

Commit

Permalink
Move the check about missing assignment in SET operation to focus UPD…
Browse files Browse the repository at this point in the history
…ATE statements only.
  • Loading branch information
niconoe- committed Aug 27, 2024
1 parent 6880861 commit 35d8dcf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/Components/SetOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$parser->error('Unexpected token.', $commaLastSeenAt);
}

// We got a SET operation without any assignment
if ($ret === []) {
$parser->error('Missing assignment in SET operation.', $list->tokens[$list->idx]);
}

return $ret;
}

Expand Down
23 changes: 23 additions & 0 deletions src/Statements/UpdateStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
use PhpMyAdmin\SqlParser\Components\Limit;
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Components\SetOperation;
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;

/**
* `UPDATE` statement.
Expand Down Expand Up @@ -135,4 +139,23 @@ class UpdateStatement extends Statement
* @var JoinKeyword[]|null
*/
public $join;

/**
* Function called after the token was processed.
* In the update statement, this is used to check that at least one assignment has been set to throw an error if a
* query like `UPDATE acme SET WHERE 1;` is parsed.
*
* @throws ParserException
*/
public function after(Parser $parser, TokensList $list, Token $token)
{
/** @psalm-var string $tokenValue */
$tokenValue = $token->value;
// Ensure we finished to parse the "SET" token, and if yes, ensure that assignments are defined.
if ($this->set !== [] || (Parser::$KEYWORD_PARSERS[$tokenValue]['field'] ?? null) !== 'set') {
return;
}

$parser->error('Missing assignment in SET operation.', $list->tokens[$list->idx]);
}
}
7 changes: 7 additions & 0 deletions tests/data/parser/parseUpdate3.out
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@
"@type": "@12"
},
0
],
[
"Missing assignment in SET operation.",
{
"@type": "@11"
},
0
]
]
}
Expand Down

0 comments on commit 35d8dcf

Please sign in to comment.