diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php index f7a49423f..c7a95aa5d 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php @@ -943,7 +943,12 @@ private function record_add_column( throw $e; } - $index_data = $this->extract_column_statistics_data( $table_name, $column_name, $node, true ); + $index_data = $this->extract_column_statistics_data( + $table_name, + $column_name, + $node, + 'YES' === $column_data['is_nullable'] + ); if ( null !== $index_data ) { $this->insert_values( $this->get_table_name( $table_is_temporary, 'statistics' ), @@ -962,6 +967,21 @@ private function record_add_column( $constraint_data ); } + + if ( null !== $index_data ) { + $key_column_usage_data = $this->extract_key_column_usage_data( + $node, + $table_name, + $index_data['index_name'], + $column_name + ); + foreach ( $key_column_usage_data as $key_column_usage_item ) { + $this->insert_values( + $this->get_table_name( $table_is_temporary, 'key_column_usage' ), + $key_column_usage_item + ); + } + } } /** @@ -1723,7 +1743,7 @@ private function extract_column_statistics_data( bool $nullable ): ?array { // Handle inline PRIMARY KEY and UNIQUE constraints. - $has_inline_primary_key = null !== $node->get_first_descendant_token( WP_MySQL_Lexer::KEY_SYMBOL ); + $has_inline_primary_key = $this->has_inline_primary_key( $node ); $has_inline_unique_key = null !== $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ); if ( $has_inline_primary_key || $has_inline_unique_key ) { $index_name = $has_inline_primary_key ? 'PRIMARY' : $column_name; @@ -1997,17 +2017,20 @@ private function extract_referential_constraint_data( WP_Parser_Node $node, stri } /** - * Extract key column usage data from the "tableConstraintDef" AST node. + * Extract key column usage data from the "tableConstraintDef", "columnDefinition", + * or "fieldDefinition" AST node. * - * @param WP_Parser_Node $node The "tableConstraintDef" AST node. + * @param WP_Parser_Node $node The "tableConstraintDef", "columnDefinition", or "fieldDefinition" AST node. * @param string $table_name The table name. * @param string $index_name The index name, when the constraint uses an index. + * @param string $column_name The column name, required for the "fieldDefinition" AST node. * @return array The key column usage data as stored in information schema. */ private function extract_key_column_usage_data( WP_Parser_Node $node, string $table_name, - ?string $index_name = null + ?string $index_name = null, + ?string $column_name = null ): array { $is_primary = $node->get_first_descendant_token( WP_MySQL_Lexer::PRIMARY_SYMBOL ); $is_unique = $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ); @@ -2036,30 +2059,32 @@ private function extract_key_column_usage_data( // Constraint name. $name = $index_name ?? $this->get_table_constraint_name( $node, $table_name ); - // Key parts. - if ( 'columnDefinition' === $node->rule_name ) { - $identifiers = $node + // Key column names. + if ( 'fieldDefinition' === $node->rule_name ) { + // The "fieldDefinition" node doesn't include the column name. + $key_column_names = array( $column_name ); + } elseif ( 'columnDefinition' === $node->rule_name ) { + $identifiers = $node ->get_first_descendant_node( 'fieldIdentifier' ) ->get_descendant_nodes( 'identifier' ); - $key_parts = array( end( $identifiers ) ); + $key_column_names = array( $this->get_value( end( $identifiers ) ) ); } else { - $key_parts = array(); + $key_column_names = array(); foreach ( $node->get_descendant_nodes( 'keyPart' ) as $key_part ) { - $key_parts[] = $key_part->get_first_child_node( 'identifier' ); + $key_column_names[] = $this->get_value( $key_part->get_first_child_node( 'identifier' ) ); } } $rows = array(); - foreach ( $key_parts as $i => $key_part ) { - $column_name = $this->get_value( $key_part ); - $position = $i + 1; + foreach ( $key_column_names as $i => $key_column_name ) { + $position = $i + 1; $rows[] = array( 'constraint_schema' => self::SAVED_DATABASE_NAME, 'constraint_name' => $name, 'table_schema' => self::SAVED_DATABASE_NAME, 'table_name' => $table_name, - 'column_name' => $column_name, + 'column_name' => $key_column_name, 'ordinal_position' => $position, 'position_in_unique_constraint' => $references ? $position : null, 'referenced_table_schema' => $referenced_table_schema, @@ -2479,7 +2504,10 @@ private function get_column_nullable( WP_Parser_Node $node ): string { foreach ( $node->get_descendant_nodes( 'columnAttribute' ) as $attr ) { // PRIMARY KEY columns are always NOT NULL. - if ( $attr->has_child_token( WP_MySQL_Lexer::KEY_SYMBOL ) ) { + if ( + $attr->has_child_token( WP_MySQL_Lexer::KEY_SYMBOL ) + && ! $attr->has_child_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ) + ) { return 'NO'; } @@ -2502,9 +2530,7 @@ private function get_column_nullable( WP_Parser_Node $node ): string { */ private function get_column_key( WP_Parser_Node $node ): string { // 1. PRI: Column is a primary key or its any component. - if ( - null !== $node->get_first_descendant_token( WP_MySQL_Lexer::KEY_SYMBOL ) - ) { + if ( $this->has_inline_primary_key( $node ) ) { return 'PRI'; } @@ -2527,6 +2553,32 @@ private function get_column_key( WP_Parser_Node $node ): string { return ''; } + /** + * Check if the "columnDefinition" or "fieldDefinition" AST node declares a primary key. + * + * In a column definition, both "PRIMARY KEY" and "KEY" declare a primary key, + * while "UNIQUE KEY" declares a unique key. Therefore, the presence of a "KEY" + * token alone is not sufficient, and each column attribute must be checked. + * + * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node. + * @return bool Whether the column definition declares a primary key. + */ + private function has_inline_primary_key( WP_Parser_Node $node ): bool { + $attributes = array_merge( + $node->get_descendant_nodes( 'columnAttribute' ), + $node->get_descendant_nodes( 'gcolAttribute' ) + ); + foreach ( $attributes as $attr ) { + if ( + $attr->has_child_token( WP_MySQL_Lexer::KEY_SYMBOL ) + && ! $attr->has_child_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ) + ) { + return true; + } + } + return false; + } + /** * Extract column extra from the "columnDefinition" or "fieldDefinition" AST node. * diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 15e90b261..1c2d66749 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -2456,6 +2456,218 @@ public function testAlterTableAddColumnListErrors(): void { ); } + /** + * @dataProvider getInlineKeyQueries + */ + public function testInlineKeys( array $queries ): void { + foreach ( $queries as $query ) { + $this->assertQuery( $query ); + } + + // SHOW CREATE TABLE + $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `id` int DEFAULT NULL,', + ' `a` int NOT NULL,', + ' `b` int DEFAULT NULL,', + ' `c` int NOT NULL,', + ' PRIMARY KEY (`a`),', + ' UNIQUE KEY `b` (`b`),', + ' UNIQUE KEY `c` (`c`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $this->last_result[0]->{'Create Table'} + ); + + // INFORMATION_SCHEMA.COLUMNS + $this->assertQuery( + "SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_KEY + FROM information_schema.columns + WHERE table_schema = DATABASE() AND table_name = 't' + ORDER BY ORDINAL_POSITION" + ); + $this->assertEquals( + array( + (object) array( + 'COLUMN_NAME' => 'id', + 'IS_NULLABLE' => 'YES', + 'COLUMN_KEY' => '', + ), + (object) array( + 'COLUMN_NAME' => 'a', + 'IS_NULLABLE' => 'NO', + 'COLUMN_KEY' => 'PRI', + ), + (object) array( + 'COLUMN_NAME' => 'b', + 'IS_NULLABLE' => 'YES', + 'COLUMN_KEY' => 'UNI', + ), + (object) array( + 'COLUMN_NAME' => 'c', + 'IS_NULLABLE' => 'NO', + 'COLUMN_KEY' => 'UNI', + ), + ), + $this->last_result + ); + + // INFORMATION_SCHEMA.STATISTICS + $this->assertQuery( + "SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, NULLABLE + FROM information_schema.statistics + WHERE table_schema = DATABASE() AND table_name = 't' + ORDER BY INDEX_NAME" + ); + $this->assertEquals( + array( + (object) array( + 'INDEX_NAME' => 'b', + 'NON_UNIQUE' => '0', + 'COLUMN_NAME' => 'b', + 'NULLABLE' => 'YES', + ), + (object) array( + 'INDEX_NAME' => 'c', + 'NON_UNIQUE' => '0', + 'COLUMN_NAME' => 'c', + 'NULLABLE' => '', + ), + (object) array( + 'INDEX_NAME' => 'PRIMARY', + 'NON_UNIQUE' => '0', + 'COLUMN_NAME' => 'a', + 'NULLABLE' => '', + ), + ), + $this->last_result + ); + + // INFORMATION_SCHEMA.TABLE_CONSTRAINTS + $this->assertQuery( + "SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE + FROM information_schema.table_constraints + WHERE table_schema = DATABASE() AND table_name = 't' + ORDER BY CONSTRAINT_NAME" + ); + $this->assertEquals( + array( + (object) array( + 'CONSTRAINT_NAME' => 'b', + 'CONSTRAINT_TYPE' => 'UNIQUE', + ), + (object) array( + 'CONSTRAINT_NAME' => 'c', + 'CONSTRAINT_TYPE' => 'UNIQUE', + ), + (object) array( + 'CONSTRAINT_NAME' => 'PRIMARY', + 'CONSTRAINT_TYPE' => 'PRIMARY KEY', + ), + ), + $this->last_result + ); + + // INFORMATION_SCHEMA.KEY_COLUMN_USAGE + $this->assertQuery( + "SELECT CONSTRAINT_NAME, COLUMN_NAME, ORDINAL_POSITION + FROM information_schema.key_column_usage + WHERE table_schema = DATABASE() AND table_name = 't' + ORDER BY CONSTRAINT_NAME" + ); + $this->assertEquals( + array( + (object) array( + 'CONSTRAINT_NAME' => 'b', + 'COLUMN_NAME' => 'b', + 'ORDINAL_POSITION' => '1', + ), + (object) array( + 'CONSTRAINT_NAME' => 'c', + 'COLUMN_NAME' => 'c', + 'ORDINAL_POSITION' => '1', + ), + (object) array( + 'CONSTRAINT_NAME' => 'PRIMARY', + 'COLUMN_NAME' => 'a', + 'ORDINAL_POSITION' => '1', + ), + ), + $this->last_result + ); + + // Key enforcement. The nullable unique key accepts multiple NULLs. + $this->assertQuery( 'INSERT INTO t (id, a, b, c) VALUES (1, 1, NULL, 1), (2, 2, NULL, 2), (3, 3, 3, 3)' ); + $this->assertQueryError( + 'INSERT INTO t (id, a, b, c) VALUES (4, 1, 4, 4)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.a' + ); + $this->assertQueryError( + 'INSERT INTO t (id, a, b, c) VALUES (4, 4, 3, 4)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.b' + ); + $this->assertQueryError( + 'INSERT INTO t (id, a, b, c) VALUES (4, 4, 4, 3)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.c' + ); + $this->assertQueryError( + 'INSERT INTO t (id, a, b, c) VALUES (4, NULL, 4, 4)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t.a' + ); + } + + public static function getInlineKeyQueries(): array { + return array( + 'CREATE TABLE' => array( + array( 'CREATE TABLE t (id INT, a INT PRIMARY KEY, b INT UNIQUE KEY, c INT NOT NULL UNIQUE)' ), + ), + 'ALTER TABLE ADD' => array( + array( + 'CREATE TABLE t (id INT)', + 'ALTER TABLE t ADD a INT PRIMARY KEY', + 'ALTER TABLE t ADD b INT UNIQUE KEY', + 'ALTER TABLE t ADD COLUMN c INT NOT NULL UNIQUE', + ), + ), + 'ALTER TABLE ADD with a column list' => array( + array( + 'CREATE TABLE t (id INT)', + 'ALTER TABLE t ADD (a INT PRIMARY KEY, b INT UNIQUE KEY, c INT NOT NULL UNIQUE)', + ), + ), + ); + } + + public function testInlineUniqueKeyIsNotPrimaryKey(): void { + $this->assertQuery( 'CREATE TABLE t (a INT UNIQUE KEY)' ); + + $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `a` int DEFAULT NULL,', + ' UNIQUE KEY `a` (`a`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $this->last_result[0]->{'Create Table'} + ); + + // The column is nullable, and the unique key accepts multiple NULLs. + $this->assertQuery( 'INSERT INTO t (a) VALUES (NULL), (NULL), (1)' ); + $this->assertQueryError( + 'INSERT INTO t (a) VALUES (1)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.a' + ); + } + public function testColumnWithOnUpdate() { // CREATE TABLE with ON UPDATE $this->assertQuery(