Skip to content

Commit d8fbcfa

Browse files
committed
Expand the composite ANSI SQL mode
MySQL's composite ANSI mode is shorthand for a set of component modes. Expand it when sql_mode is set and store the resulting list, so that "@@sql_mode" and individual mode checks reflect the components: REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, ONLY_FULL_GROUP_BY REAL_AS_FLOAT and ONLY_FULL_GROUP_BY are stored but not yet respected by the emulation. The driver is the source of truth for SQL modes, but the lexer also recognizes the composite ANSI mode directly so it stays correct when used standalone, applying the components that affect tokenization: PIPES_AS_CONCAT, IGNORE_SPACE, and ANSI_QUOTES. See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi
1 parent 7d597a1 commit d8fbcfa

7 files changed

Lines changed: 114 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,17 @@ public function __construct(
22122212
$this->sql_modes |= self::SQL_MODE_NO_BACKSLASH_ESCAPES;
22132213
} elseif ( 'ANSI_QUOTES' === $sql_mode ) {
22142214
$this->sql_modes |= self::SQL_MODE_ANSI_QUOTES;
2215+
} elseif ( 'ANSI' === $sql_mode ) {
2216+
/*
2217+
* Expand the composite ANSI mode into its lexer-relevant components.
2218+
* The ANSI mode also implies REAL_AS_FLOAT and ONLY_FULL_GROUP_BY,
2219+
* which do not affect the lexer.
2220+
*
2221+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi
2222+
*/
2223+
$this->sql_modes |= self::SQL_MODE_PIPES_AS_CONCAT
2224+
| self::SQL_MODE_IGNORE_SPACE
2225+
| self::SQL_MODE_ANSI_QUOTES;
22152226
}
22162227
}
22172228
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5834,6 +5834,23 @@ private function set_sql_modes( $modes ): void {
58345834
);
58355835
}
58365836

5837+
/*
5838+
* MySQL retains composite SQL modes while enabling their component modes.
5839+
* Store both so "@@sql_mode" and individual mode checks match MySQL, even
5840+
* though not all resulting modes are respected by the emulation yet.
5841+
*
5842+
* See:
5843+
* https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sql-mode-combo
5844+
* https://github.com/mysql/mysql-server/blob/8.4/sql/sys_vars.cc
5845+
*/
5846+
if ( ( $sql_modes & self::SQL_MODES['ANSI'] ) !== 0 ) {
5847+
$sql_modes |= self::SQL_MODES['REAL_AS_FLOAT']
5848+
| self::SQL_MODES['PIPES_AS_CONCAT']
5849+
| self::SQL_MODES['ANSI_QUOTES']
5850+
| self::SQL_MODES['IGNORE_SPACE']
5851+
| self::SQL_MODES['ONLY_FULL_GROUP_BY'];
5852+
}
5853+
58375854
$this->active_sql_modes = $sql_modes;
58385855
}
58395856

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,6 +3203,44 @@ public function testAnsiQuotesAllowsDoubleQuotedIdentifiersInDdl() {
32033203
$this->assertEquals( 42, $results[0]->{'my col'} );
32043204
}
32053205

3206+
public function testCompositeAnsiModeEnablesAnsiQuotes() {
3207+
$this->assertQuery( "SET sql_mode = 'ANSI'" );
3208+
3209+
$this->assertQuery(
3210+
"INSERT INTO _options (option_name, option_value) VALUES ('alpha', 'one');"
3211+
);
3212+
3213+
$this->assertQuery( 'SELECT "option_name" AS "name" FROM _options WHERE "option_value" = \'one\';' );
3214+
$results = $this->last_result;
3215+
$this->assertCount( 1, $results );
3216+
$this->assertEquals( 'alpha', $results[0]->name );
3217+
}
3218+
3219+
public function testCompositeAnsiModeExpandsToComponentModes() {
3220+
$this->assertQuery( "SET sql_mode = 'ANSI'" );
3221+
3222+
// The composite ANSI mode is retained alongside its component modes.
3223+
$this->assertTrue( $this->engine->is_sql_mode_active( 'ANSI' ) );
3224+
$this->assertQuery( 'SELECT @@sql_mode AS mode;' );
3225+
$results = $this->last_result;
3226+
$this->assertSame(
3227+
'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI',
3228+
$results[0]->mode
3229+
);
3230+
}
3231+
3232+
public function testCompositeAnsiModeExpandsAlongsideOtherModes() {
3233+
$this->assertQuery( "SET sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_ALL_TABLES,ANSI'" );
3234+
3235+
// The expanded modes are returned in MySQL's canonical bitmask order.
3236+
$this->assertQuery( 'SELECT @@sql_mode AS mode;' );
3237+
$results = $this->last_result;
3238+
$this->assertSame(
3239+
'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI,STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION',
3240+
$results[0]->mode
3241+
);
3242+
}
3243+
32063244
public function testCaseInsensitiveSelect() {
32073245
$this->assertQuery(
32083246
"CREATE TABLE _tmp_table (

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,23 @@ public function test_ansi_quotes_identifier_value_is_unescaped(): void {
435435
$this->assertSame( 'a"b', $token->get_value() );
436436
}
437437

438+
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
439+
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
440+
$lexer = new WP_MySQL_Lexer( '"foo"', 80038, array( 'ANSI' ) );
441+
$this->assertTrue( $lexer->next_token() );
442+
$this->assertSame( WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, $lexer->get_token()->id );
443+
444+
// PIPES_AS_CONCAT: "||" is the string concatenation operator.
445+
$lexer = new WP_MySQL_Lexer( '||', 80038, array( 'ANSI' ) );
446+
$this->assertTrue( $lexer->next_token() );
447+
$this->assertSame( WP_MySQL_Lexer::CONCAT_PIPES_SYMBOL, $lexer->get_token()->id );
448+
449+
// IGNORE_SPACE: whitespace is permitted between a function name and "(".
450+
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80038, array( 'ANSI' ) );
451+
$this->assertTrue( $lexer->next_token() );
452+
$this->assertSame( WP_MySQL_Lexer::COUNT_SYMBOL, $lexer->get_token()->id );
453+
}
454+
438455
private function get_token_names( array $token_types ): array {
439456
return array_map(
440457
function ( $token_type ) {

‎packages/mysql-parser/src/class-wp-mysql-lexer.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,17 @@ public function __construct(
348348
$this->sql_modes |= self::SQL_MODE_NO_BACKSLASH_ESCAPES;
349349
} elseif ( 'ANSI_QUOTES' === $sql_mode ) {
350350
$this->sql_modes |= self::SQL_MODE_ANSI_QUOTES;
351+
} elseif ( 'ANSI' === $sql_mode ) {
352+
/*
353+
* Expand the composite ANSI mode into its lexer-relevant components.
354+
* The ANSI mode also implies REAL_AS_FLOAT and ONLY_FULL_GROUP_BY,
355+
* which do not affect the lexer.
356+
*
357+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi
358+
*/
359+
$this->sql_modes |= self::SQL_MODE_PIPES_AS_CONCAT
360+
| self::SQL_MODE_IGNORE_SPACE
361+
| self::SQL_MODE_ANSI_QUOTES;
351362
}
352363
}
353364

‎packages/mysql-parser/tests/WP_MySQL_Lexer_Tests.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,23 @@ public function data_ansi_quotes_tokenization(): array {
576576
);
577577
}
578578

579+
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
580+
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
581+
$lexer = new WP_MySQL_Lexer( '"foo"', 80400, array( 'ANSI' ) );
582+
$this->assertTrue( $lexer->next_token() );
583+
$this->assertSame( WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, $lexer->get_token()->id );
584+
585+
// PIPES_AS_CONCAT: "||" is the string concatenation operator.
586+
$lexer = new WP_MySQL_Lexer( '||', 80400, array( 'ANSI' ) );
587+
$this->assertTrue( $lexer->next_token() );
588+
$this->assertSame( WP_MySQL_Lexer::CONCAT_PIPES_SYMBOL, $lexer->get_token()->id );
589+
590+
// IGNORE_SPACE: whitespace is permitted between a function name and "(".
591+
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80400, array( 'ANSI' ) );
592+
$this->assertTrue( $lexer->next_token() );
593+
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['COUNT'], $lexer->get_token()->id );
594+
}
595+
579596
private function get_token_names( array $token_types ): array {
580597
return array_map(
581598
function ( $token_type ) {

‎packages/php-ext-wp-mysql-parser/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ fn sql_modes_mask(sql_modes: &[String]) -> i64 {
139139
"IGNORE_SPACE" => mask |= SQL_MODE_IGNORE_SPACE,
140140
"NO_BACKSLASH_ESCAPES" => mask |= SQL_MODE_NO_BACKSLASH_ESCAPES,
141141
"ANSI_QUOTES" => mask |= SQL_MODE_ANSI_QUOTES,
142+
"ANSI" => {
143+
mask |= SQL_MODE_PIPES_AS_CONCAT | SQL_MODE_IGNORE_SPACE | SQL_MODE_ANSI_QUOTES
144+
}
142145
_ => {}
143146
}
144147
}

0 commit comments

Comments
 (0)