@@ -358,16 +358,41 @@ class WP_SQLite_Information_Schema_Builder {
358358 */
359359 private $ connection ;
360360
361+ /**
362+ * Emulated MySQL version as an integer.
363+ *
364+ * @var int
365+ */
366+ private $ mysql_version ;
367+
361368 /**
362369 * Constructor.
363370 *
364371 * @param string $reserved_prefix An identifier prefix for internal database objects.
365372 * @param WP_SQLite_Connection $connection An instance of the SQLite connection.
373+ * @param int $mysql_version Optional. The emulated MySQL version as an integer. Default 80038.
366374 */
367- public function __construct ( string $ reserved_prefix , WP_SQLite_Connection $ connection ) {
375+ public function __construct (
376+ string $ reserved_prefix ,
377+ WP_SQLite_Connection $ connection ,
378+ int $ mysql_version = WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION
379+ ) {
368380 $ this ->connection = $ connection ;
369381 $ this ->table_prefix = $ reserved_prefix . 'mysql_information_schema_ ' ;
370382 $ this ->temporary_table_prefix = $ reserved_prefix . 'mysql_information_schema_tmp_ ' ;
383+ $ this ->mysql_version = $ mysql_version ;
384+ }
385+
386+ /**
387+ * Check whether inline REFERENCES clauses are enforced for foreign keys.
388+ *
389+ * In MySQL, inline REFERENCES clauses are parsed but ignored prior to MySQL 9.0.
390+ * Starting with MySQL 9.0, inline REFERENCES clauses define foreign key constraints.
391+ *
392+ * @return bool True if inline REFERENCES clauses are enforced, false if ignored.
393+ */
394+ private function supports_inline_references (): bool {
395+ return $ this ->mysql_version >= 90000 ;
371396 }
372397
373398 /**
@@ -591,23 +616,26 @@ public function record_create_table( WP_Parser_Node $node ): void {
591616 }
592617
593618 // Extract inline column constraints and indexes.
594- $ index_data = $ this ->extract_column_statistics_data (
619+ $ index_data = $ this ->extract_column_statistics_data (
595620 $ table_name ,
596621 $ column_name ,
597622 $ column_node ,
598623 'YES ' === $ column_data ['is_nullable ' ]
599624 );
600- $ constraint_data = $ this ->extract_table_constraint_data (
625+ $ constraint_data = $ this ->extract_table_constraint_data (
601626 $ column_node ,
602627 $ table_name ,
603628 $ index_data ['index_name ' ] ?? null
604629 );
605- $ key_column_usage_data = $ this ->extract_key_column_usage_data (
630+ $ referential_constraint_data = $ this ->supports_inline_references ()
631+ ? $ this ->extract_referential_constraint_data ( $ column_node , $ table_name )
632+ : null ;
633+ $ key_column_usage_data = $ this ->extract_key_column_usage_data (
606634 $ column_node ,
607635 $ table_name ,
608636 $ index_data ['index_name ' ] ?? null
609637 );
610- $ check_constraint_data = $ this ->extract_check_constraint_data (
638+ $ check_constraint_data = $ this ->extract_check_constraint_data (
611639 $ column_node ,
612640 $ table_name
613641 );
@@ -625,6 +653,12 @@ public function record_create_table( WP_Parser_Node $node ): void {
625653 $ constraint_data
626654 );
627655 }
656+ if ( null !== $ referential_constraint_data ) {
657+ $ this ->insert_values (
658+ $ this ->get_table_name ( $ table_is_temporary , 'referential_constraints ' ),
659+ $ referential_constraint_data
660+ );
661+ }
628662 foreach ( $ key_column_usage_data as $ key_column_usage_item ) {
629663 $ this ->insert_values (
630664 $ this ->get_table_name ( $ table_is_temporary , 'key_column_usage ' ),
@@ -1882,7 +1916,7 @@ public function extract_table_constraint_data(
18821916 * @return array|null The referential constraint data as stored in information schema.
18831917 */
18841918 private function extract_referential_constraint_data ( WP_Parser_Node $ node , string $ table_name ): ?array {
1885- if ( 'tableConstraintDef ' !== $ node ->rule_name ) {
1919+ if ( 'tableConstraintDef ' !== $ node ->rule_name && ! $ this -> supports_inline_references () ) {
18861920 return null ;
18871921 }
18881922
@@ -1978,7 +2012,7 @@ private function extract_key_column_usage_data(
19782012 ): array {
19792013 $ is_primary = $ node ->get_first_descendant_token ( WP_MySQL_Lexer::PRIMARY_SYMBOL );
19802014 $ is_unique = $ node ->get_first_descendant_token ( WP_MySQL_Lexer::UNIQUE_SYMBOL );
1981- $ references = 'tableConstraintDef ' === $ node ->rule_name ? $ node ->get_first_descendant_node ( 'references ' ) : null ;
2015+ $ references = ( 'tableConstraintDef ' === $ node ->rule_name || $ this -> supports_inline_references () ) ? $ node ->get_first_descendant_node ( 'references ' ) : null ;
19822016 if ( null === $ references && ! $ is_primary && ! $ is_unique ) {
19832017 return array ();
19842018 }
@@ -2922,7 +2956,7 @@ public function get_table_constraint_name( WP_Parser_Node $node, string $table_n
29222956 return $ this ->get_value ( $ name_node ->get_first_child_node ( 'identifier ' ) );
29232957 }
29242958
2925- $ foreign_key = 'tableConstraintDef ' === $ node ->rule_name ? $ node ->get_first_descendant_node ( 'references ' ) : null ;
2959+ $ foreign_key = ( 'tableConstraintDef ' === $ node ->rule_name || $ this -> supports_inline_references () ) ? $ node ->get_first_descendant_node ( 'references ' ) : null ;
29262960 $ check_constraint = $ node ->get_first_descendant_node ( 'checkConstraint ' );
29272961
29282962 // FOREIGN KEY and CHECK constraints without a name get a generated name.
@@ -2980,7 +3014,7 @@ private function get_table_constraint_type( WP_Parser_Node $node ): ?string {
29803014 if ( $ node ->get_first_descendant_token ( WP_MySQL_Lexer::UNIQUE_SYMBOL ) ) {
29813015 return 'UNIQUE ' ;
29823016 }
2983- if ( 'tableConstraintDef ' === $ node ->rule_name && $ node ->get_first_descendant_node ( 'references ' ) ) {
3017+ if ( ( 'tableConstraintDef ' === $ node ->rule_name || $ this -> supports_inline_references () ) && $ node ->get_first_descendant_node ( 'references ' ) ) {
29843018 return 'FOREIGN KEY ' ;
29853019 }
29863020 if ( $ node ->get_first_descendant_node ( 'checkConstraint ' ) ) {
0 commit comments