Skip to content

Commit 6e792cb

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 b406707 commit 6e792cb

5 files changed

Lines changed: 108 additions & 4 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,16 @@ 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+
* The composite ANSI mode also implies REAL_AS_FLOAT and
2218+
* ONLY_FULL_GROUP_BY, which do not affect the lexer.
2219+
*
2220+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi
2221+
*/
2222+
$this->sql_modes |= self::SQL_MODE_PIPES_AS_CONCAT
2223+
| self::SQL_MODE_IGNORE_SPACE
2224+
| self::SQL_MODE_ANSI_QUOTES;
22152225
}
22162226
}
22172227
}

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,7 +3635,7 @@ private function set_sql_modes( $value, bool $is_default = false ): void {
36353635
throw $this->new_invalid_sql_mode_exception( $value );
36363636
}
36373637

3638-
$this->active_sql_modes = $value;
3638+
$this->active_sql_modes = $this->expand_sql_modes( $value );
36393639
return;
36403640
}
36413641

@@ -3644,7 +3644,9 @@ private function set_sql_modes( $value, bool $is_default = false ): void {
36443644
}
36453645

36463646
if ( $is_default ) {
3647-
$this->active_sql_modes = $this->get_sql_modes_bitmap( self::DEFAULT_SQL_MODES );
3647+
$this->active_sql_modes = $this->expand_sql_modes(
3648+
$this->get_sql_modes_bitmap( self::DEFAULT_SQL_MODES )
3649+
);
36483650
return;
36493651
}
36503652

@@ -3655,8 +3657,8 @@ private function set_sql_modes( $value, bool $is_default = false ): void {
36553657
);
36563658
}
36573659

3658-
$this->active_sql_modes = $this->get_sql_modes_bitmap(
3659-
explode( ',', $value )
3660+
$this->active_sql_modes = $this->expand_sql_modes(
3661+
$this->get_sql_modes_bitmap( explode( ',', $value ) )
36603662
);
36613663
}
36623664

@@ -3752,6 +3754,34 @@ private function new_unsupported_sql_mode_exception( int $sql_modes ): WP_SQLite
37523754
);
37533755
}
37543756

3757+
/**
3758+
* Expand composite SQL modes while retaining the composite modes themselves.
3759+
*
3760+
* Some MySQL SQL modes are shorthands that enable a set of other modes.
3761+
* MySQL retains each composite mode when sql_mode is set, enables its
3762+
* component modes, and serializes the result in bitmask order. We do the same
3763+
* so that "@@sql_mode" and individual mode checks reflect both, even though
3764+
* not all resulting modes are respected by the emulation yet.
3765+
*
3766+
* See:
3767+
* https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sql-mode-combo
3768+
* https://github.com/mysql/mysql-server/blob/8.4/sql/sys_vars.cc
3769+
*
3770+
* @param int $sql_modes SQL mode bitmask to expand.
3771+
* @return int The expanded SQL mode bitmask.
3772+
*/
3773+
private function expand_sql_modes( int $sql_modes ): int {
3774+
if ( ( $sql_modes & self::SQL_MODES['ANSI'] ) !== 0 ) {
3775+
$sql_modes |= self::SQL_MODES['REAL_AS_FLOAT']
3776+
| self::SQL_MODES['PIPES_AS_CONCAT']
3777+
| self::SQL_MODES['ANSI_QUOTES']
3778+
| self::SQL_MODES['IGNORE_SPACE']
3779+
| self::SQL_MODES['ONLY_FULL_GROUP_BY'];
3780+
}
3781+
3782+
return $sql_modes;
3783+
}
3784+
37553785
/**
37563786
* Translate and execute a MySQL SET statement for user variables.
37573787
*

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,6 +3111,45 @@ public function testAnsiQuotesAllowsDoubleQuotedIdentifiersInDdl() {
31113111
$this->assertEquals( 42, $results[0]->{'my col'} );
31123112
}
31133113

3114+
public function testCompositeAnsiModeEnablesAnsiQuotes() {
3115+
$this->assertQuery( "SET sql_mode = 'ANSI'" );
3116+
3117+
$this->assertQuery(
3118+
"INSERT INTO _options (option_name, option_value) VALUES ('alpha', 'one');"
3119+
);
3120+
3121+
// The composite ANSI mode implies ANSI_QUOTES.
3122+
$this->assertQuery( 'SELECT "option_name" AS "name" FROM _options WHERE "option_value" = \'one\';' );
3123+
$results = $this->last_result;
3124+
$this->assertCount( 1, $results );
3125+
$this->assertEquals( 'alpha', $results[0]->name );
3126+
}
3127+
3128+
public function testCompositeAnsiModeExpandsToComponentModes() {
3129+
$this->assertQuery( "SET sql_mode = 'ANSI'" );
3130+
3131+
// The composite ANSI mode is retained alongside its component modes.
3132+
$this->assertTrue( $this->engine->is_sql_mode_active( 'ANSI' ) );
3133+
$this->assertQuery( 'SELECT @@sql_mode AS mode;' );
3134+
$results = $this->last_result;
3135+
$this->assertSame(
3136+
'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI',
3137+
$results[0]->mode
3138+
);
3139+
}
3140+
3141+
public function testCompositeAnsiModeExpandsAlongsideOtherModes() {
3142+
$this->assertQuery( "SET sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_ALL_TABLES,ANSI'" );
3143+
3144+
// The expanded modes are returned in MySQL's canonical bitmask order.
3145+
$this->assertQuery( 'SELECT @@sql_mode AS mode;' );
3146+
$results = $this->last_result;
3147+
$this->assertSame(
3148+
'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI,STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION',
3149+
$results[0]->mode
3150+
);
3151+
}
3152+
31143153
public function testCaseInsensitiveSelect() {
31153154
$this->assertQuery(
31163155
"CREATE TABLE _tmp_table (

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,28 @@ public function test_ansi_quotes_identifier_value_is_unescaped(): void {
455455
$this->assertSame( 'a"b', $token->get_value() );
456456
}
457457

458+
/**
459+
* The composite ANSI SQL mode expands to a set of component modes. We expand
460+
* it to the components that affect the lexer: ANSI_QUOTES, PIPES_AS_CONCAT,
461+
* and IGNORE_SPACE.
462+
*/
463+
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
464+
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
465+
$lexer = new WP_MySQL_Lexer( '"foo"', 80038, array( 'ANSI' ) );
466+
$this->assertTrue( $lexer->next_token() );
467+
$this->assertSame( WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, $lexer->get_token()->id );
468+
469+
// PIPES_AS_CONCAT: "||" is the string concatenation operator.
470+
$lexer = new WP_MySQL_Lexer( '||', 80038, array( 'ANSI' ) );
471+
$this->assertTrue( $lexer->next_token() );
472+
$this->assertSame( WP_MySQL_Lexer::CONCAT_PIPES_SYMBOL, $lexer->get_token()->id );
473+
474+
// IGNORE_SPACE: whitespace is permitted between a function name and "(".
475+
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80038, array( 'ANSI' ) );
476+
$this->assertTrue( $lexer->next_token() );
477+
$this->assertSame( WP_MySQL_Lexer::COUNT_SYMBOL, $lexer->get_token()->id );
478+
}
479+
458480
private function get_token_names( array $token_types ): array {
459481
return array_map(
460482
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)