@@ -560,7 +560,10 @@ public function record_create_table( WP_Parser_Node $node ): void {
560560 $ table_name ,
561561 $ column_name ,
562562 $ column_node ,
563- $ column_position
563+ $ column_position ,
564+ function () use ( $ table_collation ) {
565+ return $ table_collation ;
566+ }
564567 );
565568
566569 try {
@@ -866,7 +869,15 @@ private function record_add_column(
866869 array ( self ::SAVED_DATABASE_NAME , $ table_name )
867870 )->fetchColumn ();
868871
869- $ column_data = $ this ->extract_column_data ( $ table_name , $ column_name , $ node , (int ) $ position + 1 );
872+ $ column_data = $ this ->extract_column_data (
873+ $ table_name ,
874+ $ column_name ,
875+ $ node ,
876+ (int ) $ position + 1 ,
877+ function () use ( $ table_is_temporary , $ table_name ) {
878+ return $ this ->get_recorded_table_collation ( $ table_is_temporary , $ table_name );
879+ }
880+ );
870881 try {
871882 $ this ->insert_values (
872883 $ this ->get_table_name ( $ table_is_temporary , 'columns ' ),
@@ -916,7 +927,15 @@ private function record_change_column(
916927 string $ new_column_name ,
917928 WP_Parser_Node $ node
918929 ): void {
919- $ column_data = $ this ->extract_column_data ( $ table_name , $ new_column_name , $ node , 0 );
930+ $ column_data = $ this ->extract_column_data (
931+ $ table_name ,
932+ $ new_column_name ,
933+ $ node ,
934+ 0 ,
935+ function () use ( $ table_is_temporary , $ table_name ) {
936+ return $ this ->get_recorded_table_collation ( $ table_is_temporary , $ table_name );
937+ }
938+ );
920939 unset( $ column_data ['ordinal_position ' ] );
921940 $ this ->update_values (
922941 $ this ->get_table_name ( $ table_is_temporary , 'columns ' ),
@@ -1499,13 +1518,21 @@ private function record_drop_check_constraint(
14991518 /**
15001519 * Analyze "columnDefinition" or "fieldDefinition" AST node and extract column data.
15011520 *
1502- * @param string $table_name The table name.
1503- * @param string $column_name The column name.
1504- * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
1505- * @param int $position The ordinal position of the column in the table.
1506- * @return array Column data for the information schema.
1521+ * @param string $table_name The table name.
1522+ * @param string $column_name The column name.
1523+ * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
1524+ * @param int $position The ordinal position of the column in the table.
1525+ * @param callable $get_table_collation Returns the default collation of the table.
1526+ * Called only when the column inherits it.
1527+ * @return array Column data for the information schema.
15071528 */
1508- private function extract_column_data ( string $ table_name , string $ column_name , WP_Parser_Node $ node , int $ position ): array {
1529+ private function extract_column_data (
1530+ string $ table_name ,
1531+ string $ column_name ,
1532+ WP_Parser_Node $ node ,
1533+ int $ position ,
1534+ callable $ get_table_collation
1535+ ): array {
15091536 list ( $ data_type , $ column_type ) = $ this ->get_column_data_types ( $ node );
15101537
15111538 $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name );
@@ -1514,7 +1541,7 @@ private function extract_column_data( string $table_name, string $column_name, W
15141541 $ extra = $ this ->get_column_extra ( $ node );
15151542 $ comment = $ this ->get_column_comment ( $ node );
15161543
1517- list ( $ charset , $ collation ) = $ this ->get_column_charset_and_collation ( $ node , $ data_type );
1544+ list ( $ charset , $ collation ) = $ this ->get_column_charset_and_collation ( $ node , $ data_type, $ get_table_collation );
15181545 list ( $ char_length , $ octet_length ) = $ this ->get_column_lengths ( $ node , $ data_type , $ charset );
15191546 list ( $ precision , $ scale ) = $ this ->get_column_numeric_attributes ( $ node , $ data_type );
15201547 $ datetime_precision = $ this ->get_column_datetime_precision ( $ node , $ data_type );
@@ -2018,12 +2045,44 @@ private function get_table_engine( WP_Parser_Node $node ): string {
20182045 * @return string The table collation as stored in information schema.
20192046 */
20202047 private function get_table_collation ( WP_Parser_Node $ node ): string {
2021- $ collate_node = $ node ->get_first_descendant_node ( 'collationName ' );
2022- if ( null === $ collate_node ) {
2023- // @TODO: Use default DB collation or DB_CHARSET & DB_COLLATE.
2024- return 'utf8mb4_0900_ai_ci ' ;
2048+ // Only table options apply; column definitions may specify their own collation.
2049+ $ options_node = $ node ->get_first_descendant_node ( 'createTableOptions ' );
2050+
2051+ $ collation_node = $ options_node ? $ options_node ->get_first_descendant_node ( 'defaultCollation ' ) : null ;
2052+ if ( null !== $ collation_node ) {
2053+ return strtolower ( $ this ->get_value ( $ collation_node ->get_first_child_node ( 'collationName ' ) ) );
2054+ }
2055+
2056+ $ charset_node = $ options_node ? $ options_node ->get_first_descendant_node ( 'defaultCharset ' ) : null ;
2057+ if ( null !== $ charset_node ) {
2058+ $ charset = strtolower ( $ this ->get_value ( $ charset_node ->get_first_child_node ( 'charsetName ' ) ) );
2059+ return $ this ->get_charset_default_collation ( 'utf8mb3 ' === $ charset ? 'utf8 ' : $ charset );
20252060 }
2026- return strtolower ( $ this ->get_value ( $ collate_node ) );
2061+
2062+ // @TODO: Use the default collation of the database.
2063+ return 'utf8mb4_0900_ai_ci ' ;
2064+ }
2065+
2066+ /**
2067+ * Get the table collation that is recorded in the information schema.
2068+ *
2069+ * @param bool $table_is_temporary Whether the table is temporary.
2070+ * @param string $table_name The table name.
2071+ * @return string The table collation as stored in information schema.
2072+ */
2073+ private function get_recorded_table_collation ( bool $ table_is_temporary , string $ table_name ): string {
2074+ $ collation = $ this ->connection ->query (
2075+ '
2076+ SELECT table_collation
2077+ FROM ' . $ this ->connection ->quote_identifier ( $ this ->get_table_name ( $ table_is_temporary , 'tables ' ) ) . '
2078+ WHERE table_schema = ?
2079+ AND table_name = ?
2080+ ' ,
2081+ array ( self ::SAVED_DATABASE_NAME , $ table_name )
2082+ )->fetchColumn ();
2083+
2084+ // @TODO: Use the default collation of the database.
2085+ return false === $ collation || null === $ collation ? 'utf8mb4_0900_ai_ci ' : $ collation ;
20272086 }
20282087
20292088 /**
@@ -2443,11 +2502,12 @@ private function get_column_data_types( WP_Parser_Node $node ): array {
24432502 /**
24442503 * Extract column charset and collation from the "columnDefinition" or "fieldDefinition" AST node.
24452504 *
2446- * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2447- * @param string $data_type The column data type as stored in information schema.
2448- * @return array{ string|null, string|null } The column charset and collation as stored in information schema.
2505+ * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2506+ * @param string $data_type The column data type as stored in information schema.
2507+ * @param callable $get_table_collation Returns the default collation of the table.
2508+ * @return array{ string|null, string|null } The column charset and collation as stored in information schema.
24492509 */
2450- private function get_column_charset_and_collation ( WP_Parser_Node $ node , string $ data_type ): array {
2510+ private function get_column_charset_and_collation ( WP_Parser_Node $ node , string $ data_type, callable $ get_table_collation ): array {
24512511 if ( ! (
24522512 'char ' === $ data_type
24532513 || 'varchar ' === $ data_type
@@ -2508,30 +2568,48 @@ private function get_column_charset_and_collation( WP_Parser_Node $node, string
25082568 $ collation = strtolower ( $ this ->get_value ( $ collation_node ) );
25092569 }
25102570
2511- // Defaults.
2512- // @TODO: These are hardcoded now. We should get them from table/DB.
2571+ // When neither charset nor collation is set, the table defaults are used.
25132572 if ( null === $ charset && null === $ collation ) {
2514- $ charset = 'utf8mb4 ' ;
2573+ $ table_collation = $ get_table_collation ();
2574+ $ charset = $ this ->get_collation_charset ( $ table_collation );
2575+ if ( ! $ is_binary ) {
2576+ $ collation = $ table_collation ;
2577+ }
25152578 // @TODO: "BINARY" (seems to change varchar to varbinary).
25162579 // @TODO: "DEFAULT"
25172580 }
25182581
25192582 // If only one of charset/collation is set, the other one is derived.
25202583 if ( null === $ collation ) {
2521- if ( $ is_binary ) {
2522- $ collation = $ charset . '_bin ' ;
2523- } elseif ( isset ( self ::CHARSET_DEFAULT_COLLATION_MAP [ $ charset ] ) ) {
2524- $ collation = self ::CHARSET_DEFAULT_COLLATION_MAP [ $ charset ];
2525- } else {
2526- $ collation = $ charset . '_general_ci ' ;
2527- }
2584+ $ collation = $ is_binary ? $ charset . '_bin ' : $ this ->get_charset_default_collation ( $ charset );
25282585 } elseif ( null === $ charset ) {
2529- $ charset = substr ( $ collation , 0 , strpos ( $ collation, ' _ ' ) );
2586+ $ charset = $ this -> get_collation_charset ( $ collation );
25302587 }
25312588
25322589 return array ( $ charset , $ collation );
25332590 }
25342591
2592+ /**
2593+ * Get the default collation for a given charset.
2594+ *
2595+ * @param string $charset The charset name.
2596+ * @return string The default collation of the charset.
2597+ */
2598+ private function get_charset_default_collation ( string $ charset ): string {
2599+ return self ::CHARSET_DEFAULT_COLLATION_MAP [ $ charset ] ?? $ charset . '_general_ci ' ;
2600+ }
2601+
2602+ /**
2603+ * Get the charset for a given collation.
2604+ *
2605+ * @param string $collation The collation name.
2606+ * @return string The charset of the collation.
2607+ */
2608+ private function get_collation_charset ( string $ collation ): string {
2609+ $ separator_position = strpos ( $ collation , '_ ' );
2610+ return false === $ separator_position ? $ collation : substr ( $ collation , 0 , $ separator_position );
2611+ }
2612+
25352613 /**
25362614 * Extract column length info from the "columnDefinition" or "fieldDefinition" AST node.
25372615 *
0 commit comments