Skip to content

Commit fd4a1bf

Browse files
committed
Treat END_OF_INPUT as an identifier and report syntax errors with SQLSTATE 42000
In MySQL, "END_OF_INPUT" is the name of an internal grammar terminal, not a keyword: typed input is tokenized as an identifier and can be used as an alias, with or without a trailing semicolon. The lexer mapped it to the EOF token instead, so "SELECT 1 END_OF_INPUT;" was rejected as a syntax error, and without the semicolon the alias was silently lost. Remove the keyword mapping, so the typed word tokenizes as an identifier and the EOF token is only emitted at the end of the input. Lexer and parser failures now also report SQLSTATE "42000" with error 1064, like MySQL reports syntax errors, instead of the generic "HY000" with 1105. Internal assertions keep the generic codes. Follows up on the PR review.
1 parent 581e896 commit fd4a1bf

4 files changed

Lines changed: 67 additions & 13 deletions

File tree

‎packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,12 @@ class WP_MySQL_Lexer {
11371137
'ENCLOSED' => self::ENCLOSED_SYMBOL,
11381138
'ENCRYPTION' => self::ENCRYPTION_SYMBOL,
11391139
'END' => self::END_SYMBOL,
1140-
'END_OF_INPUT' => self::EOF,
1140+
/*
1141+
* "END_OF_INPUT" is the name of the internal end-of-input terminal in
1142+
* the MySQL grammar, but it is not a keyword in MySQL. Typed input is
1143+
* tokenized as an identifier, so that it can be used as an alias,
1144+
* while the EOF token is only emitted at the end of the input.
1145+
*/
11411146
'ENDS' => self::ENDS_SYMBOL,
11421147
'ENGINE' => self::ENGINE_SYMBOL,
11431148
'ENGINES' => self::ENGINES_SYMBOL,

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11181118
$parser->next_query();
11191119
$ast = $parser->get_query_ast();
11201120
if ( null === $ast ) {
1121-
throw $this->new_driver_exception( 'Failed to parse the MySQL query.' );
1121+
throw $this->new_syntax_error_exception( 'Failed to parse the MySQL query.' );
11221122
}
11231123

11241124
if ( $parser->next_query() ) {
@@ -1639,7 +1639,7 @@ public function create_parser( string $query ): WP_MySQL_Parser {
16391639
|| WP_MySQL_Lexer::EOF !== $last_token->id
16401640
|| strlen( $query ) !== $last_token->get_start() + $last_token->get_length()
16411641
) {
1642-
throw $this->new_invalid_input_exception();
1642+
throw $this->new_syntax_error_exception();
16431643
}
16441644

16451645
// Reset the reusable parser with new tokens or create it on first use.
@@ -8191,13 +8191,32 @@ private function new_driver_exception(
81918191
return new WP_MySQL_On_SQLite_Exception( $this, $message, $code, $previous, $error_info );
81928192
}
81938193

8194+
/**
8195+
* Create a new syntax error exception.
8196+
*
8197+
* This exception is used for input that MySQL rejects as a syntax error:
8198+
* malformed queries that the lexer cannot tokenize, or queries that do not
8199+
* match the MySQL grammar. Like MySQL, it reports SQLSTATE "42000" with
8200+
* error code 1064.
8201+
*
8202+
* @param string $message The exception message.
8203+
* @return WP_MySQL_On_SQLite_Exception
8204+
*/
8205+
private function new_syntax_error_exception( string $message = 'MySQL query syntax error.' ): WP_MySQL_On_SQLite_Exception {
8206+
return new WP_MySQL_On_SQLite_Exception(
8207+
$this,
8208+
$message,
8209+
'42000',
8210+
null,
8211+
array( '42000', 1064, $message )
8212+
);
8213+
}
8214+
81948215
/**
81958216
* Create a new invalid input exception.
81968217
*
8197-
* This exception is used to mark input that is invalid according to the
8198-
* MySQL grammar, such as malformed queries that the lexer cannot tokenize.
8199-
* It also marks cases that should never occur according to the grammar,
8200-
* where it serves as an assertion that should never fail.
8218+
* This exception is used to mark cases that should never occur according
8219+
* to the MySQL grammar. It may serve as an assertion that should never fail.
82018220
*
82028221
* @return WP_MySQL_On_SQLite_Exception
82038222
*/

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12002,19 +12002,48 @@ public static function oddLengthQuotedHexadecimalLiterals(): array {
1200212002
* @dataProvider malformedQueries
1200312003
*/
1200412004
public function testMalformedQueryIsRejectedWithASyntaxError( string $query ): void {
12005-
$this->expectException( WP_MySQL_On_SQLite_Exception::class );
12006-
$this->expectExceptionMessage( 'MySQL query syntax error.' );
12007-
$this->assertQuery( $query );
12005+
try {
12006+
$this->query( $query );
12007+
$this->fail( 'An exception was expected, but none was thrown.' );
12008+
} catch ( WP_MySQL_On_SQLite_Exception $e ) {
12009+
// Like MySQL, a syntax error reports SQLSTATE "42000" with error 1064.
12010+
$this->assertSame( 'MySQL query syntax error.', $e->getMessage() );
12011+
$this->assertSame( '42000', $e->getCode() );
12012+
$this->assertSame( array( '42000', 1064, 'MySQL query syntax error.' ), $e->errorInfo );
12013+
}
1200812014
}
1200912015

1201012016
public static function malformedQueries(): array {
1201112017
return array(
12012-
array( "SELECT 'abc" ), // Unterminated string literal.
12013-
array( "SELECT x'ab01xyz'" ), // Quoted hex literal with non-hex digits.
12014-
array( "SELECT 1 END_OF_INPUT 'abc'" ), // Query continues after the "END_OF_INPUT" keyword.
12018+
array( "SELECT 'abc" ), // Unterminated string literal.
12019+
array( "SELECT x'ab01xyz'" ), // Quoted hex literal with non-hex digits.
1201512020
);
1201612021
}
1201712022

12023+
public function testEndOfInputWordIsAnIdentifier(): void {
12024+
// In MySQL, "END_OF_INPUT" is the name of an internal grammar terminal,
12025+
// but not a keyword: typed input is tokenized as an identifier, so it
12026+
// can be used as an alias, with or without a trailing semicolon.
12027+
$this->assertQuery( 'SELECT 1 END_OF_INPUT' );
12028+
$this->assertSame( '1', $this->last_result[0]->{'END_OF_INPUT'} );
12029+
12030+
$this->assertQuery( 'SELECT 1 END_OF_INPUT;' );
12031+
$this->assertSame( '1', $this->last_result[0]->{'END_OF_INPUT'} );
12032+
12033+
$this->assertQuery( 'SELECT 1 AS END_OF_INPUT' );
12034+
$this->assertSame( '1', $this->last_result[0]->{'END_OF_INPUT'} );
12035+
12036+
// Trailing input after the alias is a syntax error.
12037+
try {
12038+
$this->query( "SELECT 1 END_OF_INPUT 'abc'" );
12039+
$this->fail( 'An exception was expected, but none was thrown.' );
12040+
} catch ( WP_MySQL_On_SQLite_Exception $e ) {
12041+
$this->assertSame( 'Failed to parse the MySQL query.', $e->getMessage() );
12042+
$this->assertSame( '42000', $e->getCode() );
12043+
$this->assertSame( array( '42000', 1064, 'Failed to parse the MySQL query.' ), $e->errorInfo );
12044+
}
12045+
}
12046+
1201812047
public function testColumnInfo(): void {
1201912048
$this->assertQuery(
1202012049
'CREATE TABLE t (

‎packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public function data_identifier_or_number(): array {
241241
array( '0xab01xyz', array( WP_MySQL_Lexer::IDENTIFIER, WP_MySQL_Lexer::EOF ) ), // identifier
242242
array( '0x', array( WP_MySQL_Lexer::IDENTIFIER, WP_MySQL_Lexer::EOF ) ), // identifier
243243
array( "x'ab01'", array( WP_MySQL_Lexer::HEX_NUMBER, WP_MySQL_Lexer::EOF ) ),
244+
array( 'END_OF_INPUT', array( WP_MySQL_Lexer::IDENTIFIER, WP_MySQL_Lexer::EOF ) ), // grammar terminal name, not a keyword
244245
array( "x'ab01xyz'", array() ), // invalid input
245246
array( "x''", array( WP_MySQL_Lexer::HEX_NUMBER, WP_MySQL_Lexer::EOF ) ),
246247
array( "x'", array() ), // invalid input

0 commit comments

Comments
 (0)