Skip to content

Commit b6ecaaa

Browse files
youknowriadclaude
andcommitted
Drop charset prefixes from defaults and cast generated binary defaults to BLOB
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f7a950c commit b6ecaaa

6 files changed

Lines changed: 67 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ public function get_name(): string {
6464
*/
6565
public function get_value(): string {
6666
$value = $this->get_bytes();
67+
if ( WP_MySQL_Lexer::NCHAR_TEXT === $this->id ) {
68+
// Remove the national string prefix.
69+
$value = substr( $value, 1 );
70+
}
6771
if (
6872
WP_MySQL_Lexer::SINGLE_QUOTED_TEXT === $this->id
6973
|| WP_MySQL_Lexer::DOUBLE_QUOTED_TEXT === $this->id
74+
|| WP_MySQL_Lexer::NCHAR_TEXT === $this->id
7075
|| WP_MySQL_Lexer::BACK_TICK_QUOTED_ID === $this->id
7176
) {
7277
// Remove bounding quotes.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7409,7 +7409,11 @@ private function get_sqlite_create_table_statement(
74097409
$ast = $this->create_parser( 'SELECT ' . $column['COLUMN_DEFAULT'] )->parse();
74107410
$expr = $ast->get_first_descendant_node( 'selectItem' )->get_first_child_node();
74117411
$default_clause = $this->translate( $expr );
7412-
$query .= sprintf( ' DEFAULT (%s)', $default_clause );
7412+
if ( 'BLOB' === $type ) {
7413+
// STRICT tables reject a TEXT default in a BLOB column.
7414+
$default_clause = sprintf( 'CAST(%s AS BLOB)', $default_clause );
7415+
}
7416+
$query .= sprintf( ' DEFAULT (%s)', $default_clause );
74137417
} else {
74147418
$query .= ' DEFAULT ' . $this->format_sqlite_column_default( $column );
74157419
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,6 +3381,9 @@ private function get_value( WP_Parser_Node $node ): string {
33813381
}
33823382
}
33833383
}
3384+
} elseif ( WP_MySQL_Lexer::UNDERSCORE_CHARSET === $child->id ) {
3385+
// A charset introducer isn't part of the value.
3386+
continue;
33843387
} else {
33853388
$value = $child->get_value();
33863389
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ public static function data_binary_column_defaults(): array {
818818
'empty text' => array( 'varbinary(4)', "''", "''", '', '' ),
819819
'text like a number' => array( 'varbinary(4)', "'0x41'", "'0x41'", '0x30783431', '30783431' ),
820820
'text like a string' => array( 'varbinary(8)', "'x''41'''", "'x''41'''", '0x7827343127', '7827343127' ),
821+
'binary introducer' => array( 'varbinary(4)', "_binary'abc'", "'abc'", '0x616263', '616263' ),
822+
'charset introducer' => array( 'varbinary(4)', "_utf8mb4'abc'", "'abc'", '0x616263', '616263' ),
823+
'hex introducer' => array( 'varbinary(4)', '_binary 0x616263', "'abc'", '0x616263', '616263' ),
824+
'national string' => array( 'varbinary(4)', "N'abc'", "'abc'", '0x616263', '616263' ),
825+
'national quoted' => array( 'varbinary(4)', "n'it''s'", "'it''s'", '0x69742773', '69742773' ),
821826
'number' => array( 'varbinary(4)', '5', "'5'", '0x35', '35' ),
822827
'negative number' => array( 'varbinary(4)', '-5', "'-5'", '0x2D35', '2D35' ),
823828
'true' => array( 'varbinary(4)', 'TRUE', "'1'", '0x31', '31' ),
@@ -840,6 +845,45 @@ public function testBinaryColumnSetDefault(): void {
840845
$this->assertSame( 'DEADBEEF', $this->last_result[0]->v );
841846
}
842847

848+
/**
849+
* @dataProvider data_binary_column_generated_defaults
850+
*/
851+
public function testBinaryColumnGeneratedDefaults( string $column_type, string $default_value, string $expected_value ): void {
852+
$this->assertQuery( "CREATE TABLE t (id INT PRIMARY KEY, v $column_type NOT NULL DEFAULT $default_value)" );
853+
854+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
855+
856+
$this->assertQuery( 'SELECT HEX(v) AS v FROM t' );
857+
$this->assertSame( $expected_value, $this->last_result[0]->v );
858+
}
859+
860+
public static function data_binary_column_generated_defaults(): array {
861+
return array(
862+
'varbinary text' => array( 'varbinary(4)', "('abc')", '616263' ),
863+
'binary text' => array( 'binary(3)', "('abc')", '616263' ),
864+
'blob text' => array( 'blob', "('abc')", '616263' ),
865+
'hex' => array( 'varbinary(4)', '(0x616263)', '616263' ),
866+
'function' => array( 'longblob', "(CONCAT('a', 'bc'))", '616263' ),
867+
'number' => array( 'varbinary(4)', '(1 + 1)', '32' ),
868+
);
869+
}
870+
871+
public function testTextColumnDefaultsWithPrefixes(): void {
872+
$this->assertQuery(
873+
"CREATE TABLE t (
874+
id INT PRIMARY KEY,
875+
a VARCHAR(8) NOT NULL DEFAULT _utf8mb4'abc',
876+
b VARCHAR(8) NOT NULL DEFAULT N'it''s'
877+
)"
878+
);
879+
880+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
881+
882+
$this->assertQuery( 'SELECT a, b FROM t' );
883+
$this->assertSame( 'abc', $this->last_result[0]->a );
884+
$this->assertSame( "it's", $this->last_result[0]->b );
885+
}
886+
843887
/**
844888
* Older versions stored most BINARY and VARBINARY defaults as written.
845889
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ public function get_name(): string {
6262
*/
6363
public function get_value(): string {
6464
$value = $this->get_bytes();
65+
if ( WP_MySQL_Lexer::NCHAR_TEXT === $this->id ) {
66+
// Remove the national string prefix.
67+
$value = substr( $value, 1 );
68+
}
6569
if (
6670
WP_MySQL_Lexer::SINGLE_QUOTED_TEXT === $this->id
6771
|| WP_MySQL_Lexer::DOUBLE_QUOTED_TEXT === $this->id
72+
|| WP_MySQL_Lexer::NCHAR_TEXT === $this->id
6873
|| WP_MySQL_Lexer::BACK_TICK_QUOTED_ID === $this->id
6974
) {
7075
// Remove bounding quotes.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function test_get_value_unquotes_string_literals(): void {
2929
$this->assertSame( "new\nline", self::first_token( "SELECT 'new\\nline'", 'SINGLE_QUOTED_TEXT' )->get_value() );
3030
}
3131

32+
public function test_get_value_unquotes_national_strings(): void {
33+
$this->assertSame( "a'b", self::first_token( "SELECT N'a''b'", 'NCHAR_TEXT' )->get_value() );
34+
$this->assertSame( "new\nline", self::first_token( "SELECT n'new\\nline'", 'NCHAR_TEXT' )->get_value() );
35+
}
36+
3237
public function test_get_value_honors_no_backslash_escapes_mode(): void {
3338
$token = self::first_token( "SELECT 'a\\nb'", 'SINGLE_QUOTED_TEXT', array( 'NO_BACKSLASH_ESCAPES' ) );
3439
$this->assertSame( 'a\\nb', $token->get_value() );

0 commit comments

Comments
 (0)