Skip to content

Commit 12ae705

Browse files
authored
Support composite TRADITIONAL SQL mode (#509)
## Summary **Make `TRADITIONAL` enable its component SQL modes.** Previously, `SET sql_mode = 'TRADITIONAL'` stored only the composite flag, leaving the driver's existing strict and zero-date checks disabled. It now expands the mode for both named and numeric assignments and retains `TRADITIONAL` in `@@sql_mode`. The expansion follows the emulated MySQL version, including `NO_AUTO_CREATE_USER` before its removal in MySQL 8.0.11. Regression coverage checks version differences, canonical mode ordering, combination with `ANSI`, reassignment, clearing modes, and rejection of invalid dates and missing required values. This PR does not implement the deprecated MySQL 5.7 composite modes, such as `ORACLE`, `MSSQL`, and `POSTGRESQL`. ## Why Applications should get the same existing checks whether they select `TRADITIONAL` or list its component modes individually. This change uses the driver's current implementations of those modes; their existing emulation limits still apply. [MySQL documents `TRADITIONAL` as a combination mode.](https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sql-mode-combo) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * The `TRADITIONAL` SQL mode now applies its expected strict validation rules, including rejecting invalid or zero dates and missing or `NULL` values in `NOT NULL` columns. * `TRADITIONAL` mode assignments now expand consistently across supported assignment formats, including combined mode lists and numeric bitmaps. * Mode expansion respects MySQL version differences, including whether `NO_AUTO_CREATE_USER` is available. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent d0a5d8c commit 12ae705

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6314,6 +6314,20 @@ private function set_sql_modes( $modes ): void {
63146314
| self::SQL_MODES['ONLY_FULL_GROUP_BY'];
63156315
}
63166316

6317+
if ( ( $sql_modes & self::SQL_MODES['TRADITIONAL'] ) !== 0 ) {
6318+
$sql_modes |= self::SQL_MODES['STRICT_TRANS_TABLES']
6319+
| self::SQL_MODES['STRICT_ALL_TABLES']
6320+
| self::SQL_MODES['NO_ZERO_IN_DATE']
6321+
| self::SQL_MODES['NO_ZERO_DATE']
6322+
| self::SQL_MODES['ERROR_FOR_DIVISION_BY_ZERO']
6323+
| self::SQL_MODES['NO_ENGINE_SUBSTITUTION'];
6324+
6325+
// TRADITIONAL included NO_AUTO_CREATE_USER until its removal in MySQL 8.0.11.
6326+
if ( ! $this->is_sql_mode_removed( 'NO_AUTO_CREATE_USER' ) ) {
6327+
$sql_modes |= self::SQL_MODES['NO_AUTO_CREATE_USER'];
6328+
}
6329+
}
6330+
63176331
$this->active_sql_modes = $sql_modes;
63186332
}
63196333

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class WP_MySQL_On_SQLite_Tests extends TestCase {
1212
private const SQL_MODE_NOT_USED = 1 << 4;
1313
private const SQL_MODE_POSTGRESQL = 1 << 8;
1414
private const SQL_MODE_ORACLE = 1 << 9;
15+
private const SQL_MODE_ANSI = 1 << 18;
1516
private const SQL_MODE_NO_BACKSLASH_ESCAPES = 1 << 20;
17+
private const SQL_MODE_TRADITIONAL = 1 << 27;
1618
private const SQL_MODE_TIME_TRUNCATE_FRACTIONAL = 1 << 32;
1719
private const UNKNOWN_SQL_MODE_BIT = 1 << 33;
1820

@@ -3338,6 +3340,134 @@ public function testCompositeAnsiModeExpandsAlongsideOtherModes() {
33383340
);
33393341
}
33403342

3343+
/**
3344+
* @dataProvider compositeTraditionalModeValues
3345+
*/
3346+
public function testCompositeTraditionalModeExpandsToComponentModes(
3347+
int $mysql_version,
3348+
string $value,
3349+
string $expected_modes
3350+
): void {
3351+
$this->engine = new WP_MySQL_On_SQLite(
3352+
'mysql-on-sqlite:dbname=wp',
3353+
null,
3354+
null,
3355+
array(
3356+
'sqlite_pdo' => $this->sqlite,
3357+
'mysql_version' => $mysql_version,
3358+
)
3359+
);
3360+
3361+
$this->assertQuery( "SET sql_mode = 'ANSI'" );
3362+
$this->assertQuery( "SET sql_mode = $value" );
3363+
$this->assertQuery( 'SELECT @@sql_mode AS mode' );
3364+
$this->assertSame( $expected_modes, $this->last_result[0]->mode );
3365+
foreach ( explode( ',', $expected_modes ) as $mode ) {
3366+
$this->assertTrue( $this->engine->is_sql_mode_active( $mode ), $mode );
3367+
}
3368+
$this->assertFalse( $this->engine->is_sql_mode_active( 'ANSI' ) );
3369+
$this->assertFalse( $this->engine->is_sql_mode_active( 'ONLY_FULL_GROUP_BY' ) );
3370+
3371+
// Reassigning the expanded value must preserve the same modes.
3372+
$this->assertQuery( 'SET sql_mode = @@sql_mode' );
3373+
$this->assertQuery( 'SELECT @@sql_mode AS mode' );
3374+
$this->assertSame( $expected_modes, $this->last_result[0]->mode );
3375+
3376+
$this->assertQuery( "SET sql_mode = ''" );
3377+
$this->assertQuery( 'SELECT @@sql_mode AS mode' );
3378+
$this->assertSame( '', $this->last_result[0]->mode );
3379+
foreach ( explode( ',', $expected_modes ) as $mode ) {
3380+
$this->assertFalse( $this->engine->is_sql_mode_active( $mode ), $mode );
3381+
}
3382+
}
3383+
3384+
public static function compositeTraditionalModeValues(): array {
3385+
$mysql_80_modes = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION';
3386+
$mysql_57_modes = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
3387+
$versions = array(
3388+
50744 => $mysql_57_modes,
3389+
80010 => $mysql_57_modes,
3390+
80011 => $mysql_80_modes,
3391+
80400 => $mysql_80_modes,
3392+
);
3393+
$values = array(
3394+
'name' => "'TRADITIONAL'",
3395+
'identifier' => 'TRADITIONAL',
3396+
'lowercase and overlap' => "'traditional,STRICT_ALL_TABLES,TRADITIONAL'",
3397+
'bitmap' => (string) self::SQL_MODE_TRADITIONAL,
3398+
);
3399+
$cases = array();
3400+
foreach ( $versions as $version => $expected_modes ) {
3401+
foreach ( $values as $name => $value ) {
3402+
$cases[ "$version $name" ] = array( $version, $value, $expected_modes );
3403+
}
3404+
}
3405+
return $cases;
3406+
}
3407+
3408+
public function testCompositeTraditionalModeExpandsAlongsideAnsi(): void {
3409+
$values = array(
3410+
"'TRADITIONAL,ANSI,STRICT_ALL_TABLES'",
3411+
self::SQL_MODE_TRADITIONAL | self::SQL_MODE_ANSI,
3412+
);
3413+
foreach ( $values as $value ) {
3414+
$this->assertQuery( "SET sql_mode = $value" );
3415+
$this->assertQuery( 'SELECT @@sql_mode AS mode' );
3416+
$this->assertSame(
3417+
'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION',
3418+
$this->last_result[0]->mode
3419+
);
3420+
}
3421+
}
3422+
3423+
/**
3424+
* @dataProvider compositeTraditionalModeInvalidDates
3425+
*/
3426+
public function testCompositeTraditionalModeRejectsInvalidDates( string $value ): void {
3427+
$this->assertQuery( "SET sql_mode = 'TRADITIONAL'" );
3428+
$this->assertQuery( "INSERT INTO _dates (option_value) VALUES ('2020-01-15 00:00:00')" );
3429+
$this->assertQueryError(
3430+
"INSERT INTO _dates (option_value) VALUES ('$value')",
3431+
"Incorrect datetime value: '$value'"
3432+
);
3433+
$this->assertQueryError(
3434+
"UPDATE _dates SET option_value = '$value'",
3435+
"Incorrect datetime value: '$value'"
3436+
);
3437+
$this->assertQuery( 'SELECT option_value FROM _dates' );
3438+
$this->assertEquals( array( (object) array( 'option_value' => '2020-01-15 00:00:00' ) ), $this->last_result );
3439+
}
3440+
3441+
public static function compositeTraditionalModeInvalidDates(): array {
3442+
return array(
3443+
'zero date' => array( '0000-00-00 00:00:00' ),
3444+
'zero month' => array( '2020-00-15 00:00:00' ),
3445+
'zero day' => array( '2020-01-00 00:00:00' ),
3446+
'invalid date' => array( 'not-a-date' ),
3447+
);
3448+
}
3449+
3450+
public function testCompositeTraditionalModeEnablesStrictMode(): void {
3451+
$this->assertQuery( 'SET sql_mode = ' . self::SQL_MODE_TRADITIONAL );
3452+
$this->assertQuery( 'CREATE TABLE t (id INT, value TEXT NOT NULL)' );
3453+
$this->assertQueryError(
3454+
'INSERT INTO t (id) VALUES (1)',
3455+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t.value'
3456+
);
3457+
$this->assertQuery( "INSERT INTO t (id, value) VALUES (1, 'initial-value')" );
3458+
$this->assertQueryError(
3459+
'UPDATE t SET value = NULL',
3460+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t.value'
3461+
);
3462+
$this->assertQuery( 'SELECT value FROM t' );
3463+
$this->assertSame( 'initial-value', $this->last_result[0]->value );
3464+
3465+
$this->assertQuery( "SET sql_mode = ''" );
3466+
$this->assertQuery( 'UPDATE t SET value = NULL' );
3467+
$this->assertQuery( 'SELECT value FROM t' );
3468+
$this->assertSame( '', $this->last_result[0]->value );
3469+
}
3470+
33413471
public function testCaseInsensitiveSelect() {
33423472
$this->assertQuery(
33433473
"CREATE TABLE _tmp_table (

0 commit comments

Comments
 (0)