@@ -772,6 +772,116 @@ public function testShowCreateTableWithDefaultValues(): void {
772772 );
773773 }
774774
775+ /**
776+ * @dataProvider data_binary_column_defaults
777+ */
778+ public function testBinaryColumnDefaults(
779+ string $column_type,
780+ string $default_value,
781+ string $expected_create_default,
782+ string $expected_information_schema_default,
783+ string $expected_value
784+ ): void {
785+ $this->assertQuery( "CREATE TABLE t (id INT PRIMARY KEY, v $column_type NOT NULL DEFAULT $default_value)" );
786+
787+ $this->assertQuery( 'SHOW CREATE TABLE t' );
788+ $this->assertStringContainsString(
789+ "`v` $column_type NOT NULL DEFAULT $expected_create_default,",
790+ $this->last_result[0]->{'Create Table'}
791+ );
792+
793+ $this->assertQuery( "SELECT COLUMN_DEFAULT FROM information_schema.columns WHERE table_name = 't' AND column_name = 'v'" );
794+ $this->assertSame( $expected_information_schema_default, $this->last_result[0]->COLUMN_DEFAULT );
795+
796+ $this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
797+ $this->assertQuery( "INSERT INTO t VALUES (2, 'z')" );
798+ $this->assertQuery( 'UPDATE t SET v = DEFAULT WHERE id = 2' );
799+ $this->assertQuery( 'SELECT HEX(v) AS v FROM t ORDER BY id' );
800+ $this->assertSame(
801+ array( $expected_value, $expected_value ),
802+ array_column( $this->last_result, 'v' )
803+ );
804+ }
805+
806+ public static function data_binary_column_defaults(): array {
807+ return array(
808+ 'hex number' => array( 'varbinary(4)', '0xDEADBEEF', '0xDEADBEEF', '0xDEADBEEF', 'DEADBEEF' ),
809+ 'hex string' => array( 'varbinary(4)', "x'deadbeef'", '0xDEADBEEF', '0xDEADBEEF', 'DEADBEEF' ),
810+ 'uppercase string' => array( 'varbinary(4)', "X'deadbeef'", '0xDEADBEEF', '0xDEADBEEF', 'DEADBEEF' ),
811+ 'odd-length hex' => array( 'varbinary(4)', '0xa', "'\\n'", '0x0A', '0A' ),
812+ 'bit string' => array( 'varbinary(4)', "b'01000001'", "'A'", '0x41', '41' ),
813+ 'bit number' => array( 'varbinary(4)', '0b01000001', "'A'", '0x41', '41' ),
814+ 'zero bytes' => array( 'binary(2)', "x'0000'", "'\\0\\0'", '0x0000', '0000' ),
815+ 'escaped zero bytes' => array( 'binary(2)', "'\\0\\0'", "'\\0\\0'", '0x0000', '0000' ),
816+ 'text' => array( 'varbinary(4)', "'F'", "'F'", '0x46', '46' ),
817+ 'quoted text' => array( 'varbinary(4)', "'it''s'", "'it''s'", '0x69742773', '69742773' ),
818+ 'empty text' => array( 'varbinary(4)', "''", "''", '', '' ),
819+ 'text like a number' => array( 'varbinary(4)', "'0x41'", "'0x41'", '0x30783431', '30783431' ),
820+ 'text like a string' => array( 'varbinary(8)', "'x''41'''", "'x''41'''", '0x7827343127', '7827343127' ),
821+ 'number' => array( 'varbinary(4)', '5', "'5'", '0x35', '35' ),
822+ 'negative number' => array( 'varbinary(4)', '-5', "'-5'", '0x2D35', '2D35' ),
823+ 'true' => array( 'varbinary(4)', 'TRUE', "'1'", '0x31', '31' ),
824+ 'false' => array( 'varbinary(4)', 'FALSE', "'0'", '0x30', '30' ),
825+ );
826+ }
827+
828+ public function testBinaryColumnSetDefault(): void {
829+ $this->assertQuery( "CREATE TABLE t (id INT PRIMARY KEY, v VARBINARY(4) NOT NULL DEFAULT 'a')" );
830+
831+ $this->assertQuery( 'ALTER TABLE t ALTER COLUMN v SET DEFAULT 0xDEADBEEF' );
832+
833+ $this->assertQuery( 'SHOW CREATE TABLE t' );
834+ $this->assertStringContainsString(
835+ '`v` varbinary(4) NOT NULL DEFAULT 0xDEADBEEF,',
836+ $this->last_result[0]->{'Create Table'}
837+ );
838+ $this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
839+ $this->assertQuery( 'SELECT HEX(v) AS v FROM t' );
840+ $this->assertSame( 'DEADBEEF', $this->last_result[0]->v );
841+ }
842+
843+ /**
844+ * Older versions stored most BINARY and VARBINARY defaults as written.
845+ *
846+ * @dataProvider data_binary_column_defaults_from_older_versions
847+ */
848+ public function testBinaryColumnDefaultsFromOlderVersions(
849+ string $stored_default,
850+ string $expected_create_default,
851+ string $expected_value
852+ ): void {
853+ $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, v VARBINARY(16) NOT NULL)' );
854+ $statement = $this->sqlite->prepare(
855+ "UPDATE _wp_sqlite_mysql_information_schema_columns SET COLUMN_DEFAULT = ? WHERE TABLE_NAME = 't' AND COLUMN_NAME = 'v'"
856+ );
857+ $statement->execute( array( $stored_default ) );
858+
859+ $this->assertQuery( 'SHOW CREATE TABLE t' );
860+ $this->assertStringContainsString(
861+ "`v` varbinary(16) NOT NULL DEFAULT $expected_create_default,",
862+ $this->last_result[0]->{'Create Table'}
863+ );
864+
865+ // A table rebuild writes the column default to SQLite as bytes.
866+ $this->assertQuery( 'ALTER TABLE t MODIFY COLUMN id BIGINT' );
867+ $this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
868+ $this->assertQuery( 'SELECT HEX(v) AS v FROM t' );
869+ $this->assertSame( $expected_value, $this->last_result[0]->v );
870+ }
871+
872+ public static function data_binary_column_defaults_from_older_versions(): array {
873+ return array(
874+ 'hex string' => array( "x'00000000000000000000000000000000'", "'\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0'", '00000000000000000000000000000000' ),
875+ 'uppercase hex string' => array( "X'DEADBEEF'", '0xDEADBEEF', 'DEADBEEF' ),
876+ 'hex number' => array( '0xdeadbeef', '0xDEADBEEF', 'DEADBEEF' ),
877+ 'bit string' => array( "b'01000001'", "'A'", '41' ),
878+ 'bit number' => array( '0b01000001', "'A'", '41' ),
879+ 'zero bytes' => array( "\0\0", "'\\0\\0'", '0000' ),
880+ 'text' => array( 'abcd', "'abcd'", '61626364' ),
881+ 'empty text' => array( '', "''", '' ),
882+ );
883+ }
884+
775885 public function testShowCreateTableWithBitDefaultValues(): void {
776886 $this->assertQuery(
777887 "CREATE TABLE _tmp_bit_defaults (
0 commit comments