@@ -659,6 +659,41 @@ public function record_alter_table( WP_Parser_Node $node ): void {
659659 // Check if a temporary table with the given name exists.
660660 $ table_is_temporary = $ this ->temporary_table_exists ( $ table_name );
661661
662+ // Defaults apply to existing columns that are not changed by another action.
663+ $ column_defaults = array ();
664+ $ changed_columns = array ();
665+ foreach ( $ actions as $ action ) {
666+ $ first_token = $ action ->get_first_child_token ();
667+ $ column_ref = $ action ->get_first_child_node ( 'fieldIdentifier ' );
668+ if ( null === $ column_ref ) {
669+ continue ;
670+ }
671+ $ name = $ this ->get_value ( $ column_ref );
672+ $ key = strtolower ( $ name );
673+ if (
674+ WP_MySQL_Lexer::ALTER_SYMBOL === $ first_token ->id
675+ && $ action ->has_child_token ( WP_MySQL_Lexer::DEFAULT_SYMBOL )
676+ ) {
677+ if ( isset ( $ column_defaults [ $ key ] ) ) {
678+ throw WP_SQLite_Information_Schema_Exception ::column_not_found ( $ name , $ table_name );
679+ }
680+ $ column_defaults [ $ key ] = $ action ;
681+ } elseif (
682+ WP_MySQL_Lexer::CHANGE_SYMBOL === $ first_token ->id
683+ || WP_MySQL_Lexer::MODIFY_SYMBOL === $ first_token ->id
684+ || ( WP_MySQL_Lexer::DROP_SYMBOL === $ first_token ->id && ! $ action ->has_child_token ( WP_MySQL_Lexer::FOREIGN_SYMBOL ) )
685+ ) {
686+ $ changed_columns [ $ key ] = true ;
687+ }
688+ }
689+ foreach ( $ column_defaults as $ key => $ action ) {
690+ $ name = $ this ->get_value ( $ action ->get_first_child_node ( 'fieldIdentifier ' ) );
691+ if ( isset ( $ changed_columns [ $ key ] ) ) {
692+ throw WP_SQLite_Information_Schema_Exception ::column_not_found ( $ name , $ table_name );
693+ }
694+ $ this ->record_alter_column_default ( $ table_is_temporary , $ table_name , $ name , $ action );
695+ }
696+
662697 foreach ( $ actions as $ action ) {
663698 $ first_token = $ action ->get_first_child_token ();
664699
@@ -1002,6 +1037,100 @@ private function record_modify_column(
10021037 $ this ->record_change_column ( $ table_is_temporary , $ table_name , $ column_name , $ column_name , $ node );
10031038 }
10041039
1040+ /**
1041+ * Record SET DEFAULT or DROP DEFAULT data in the information schema.
1042+ *
1043+ * @param bool $table_is_temporary Whether the table is temporary.
1044+ * @param string $table_name The table name.
1045+ * @param string $column_name The column name.
1046+ * @param WP_Parser_Node $node The "alterListItem" AST node.
1047+ */
1048+ private function record_alter_column_default (
1049+ bool $ table_is_temporary ,
1050+ string $ table_name ,
1051+ string $ column_name ,
1052+ WP_Parser_Node $ node
1053+ ): void {
1054+ $ columns_table = $ this ->get_table_name ( $ table_is_temporary , 'columns ' );
1055+ $ column = $ this ->connection ->query (
1056+ 'SELECT DATA_TYPE, IS_NULLABLE, EXTRA FROM ' . $ this ->connection ->quote_identifier ( $ columns_table ) . '
1057+ WHERE table_schema = ? AND table_name = ? AND column_name = ? ' ,
1058+ array ( self ::SAVED_DATABASE_NAME , $ table_name , $ column_name )
1059+ )->fetch ( PDO ::FETCH_ASSOC );
1060+ if ( false === $ column ) {
1061+ throw WP_SQLite_Information_Schema_Exception ::column_not_found ( $ column_name , $ table_name );
1062+ }
1063+
1064+ $ default = null ;
1065+ $ extra = trim ( str_replace ( 'DEFAULT_GENERATED ' , '' , $ column ['EXTRA ' ] ) );
1066+ if ( $ node ->has_child_token ( WP_MySQL_Lexer::SET_SYMBOL ) ) {
1067+ $ expression = $ node ->get_first_child_node ( 'exprWithParentheses ' );
1068+ $ literal = $ expression ? $ this ->get_parenthesized_default_literal ( $ expression ) : null ;
1069+ $ default = $ literal
1070+ ? $ this ->get_literal_default ( $ literal , $ column ['DATA_TYPE ' ], $ column_name )
1071+ : $ this ->get_default_value ( $ node , $ column ['DATA_TYPE ' ], $ column_name );
1072+ if ( null === $ default && 'NO ' === $ column ['IS_NULLABLE ' ] ) {
1073+ throw WP_SQLite_Information_Schema_Exception ::invalid_default_value ( $ column_name );
1074+ }
1075+ if ( $ expression && ! $ literal ) {
1076+ if ( str_contains ( $ extra , 'auto_increment ' ) ) {
1077+ throw WP_SQLite_Information_Schema_Exception ::invalid_default_value ( $ column_name );
1078+ }
1079+ $ extra = trim ( 'DEFAULT_GENERATED ' . $ extra );
1080+ }
1081+ }
1082+
1083+ // MySQL accepts literal defaults for AUTO_INCREMENT columns but ignores them.
1084+ if ( str_contains ( $ extra , 'auto_increment ' ) ) {
1085+ $ default = null ;
1086+ }
1087+
1088+ $ this ->update_values (
1089+ $ columns_table ,
1090+ array (
1091+ 'column_default ' => $ default ,
1092+ 'extra ' => $ extra ,
1093+ ),
1094+ array (
1095+ 'table_schema ' => self ::SAVED_DATABASE_NAME ,
1096+ 'table_name ' => $ table_name ,
1097+ 'column_name ' => $ column_name ,
1098+ )
1099+ );
1100+ }
1101+
1102+ /**
1103+ * Find a literal wrapped only in parentheses or unary plus operators.
1104+ *
1105+ * @param WP_Parser_Node $node The default expression.
1106+ * @return WP_Parser_Node|null The literal, or null for other expressions.
1107+ */
1108+ private function get_parenthesized_default_literal ( WP_Parser_Node $ node ): ?WP_Parser_Node {
1109+ while ( 'literal ' !== $ node ->rule_name ) {
1110+ $ children = $ node ->get_children ();
1111+ if (
1112+ 3 === count ( $ children )
1113+ && $ children [0 ] instanceof WP_Parser_Token
1114+ && WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $ children [0 ]->id
1115+ && $ children [2 ] instanceof WP_Parser_Token
1116+ && WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $ children [2 ]->id
1117+ ) {
1118+ $ children = array ( $ children [1 ] );
1119+ } elseif (
1120+ 2 === count ( $ children )
1121+ && $ children [0 ] instanceof WP_Parser_Token
1122+ && WP_MySQL_Lexer::PLUS_OPERATOR === $ children [0 ]->id
1123+ ) {
1124+ $ children = array ( $ children [1 ] );
1125+ }
1126+ if ( 1 !== count ( $ children ) || ! $ children [0 ] instanceof WP_Parser_Node ) {
1127+ return null ;
1128+ }
1129+ $ node = $ children [0 ];
1130+ }
1131+ return $ node ;
1132+ }
1133+
10051134 /**
10061135 * Record DROP COLUMN data in the information schema.
10071136 *
@@ -2079,7 +2208,18 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
20792208 if ( null === $ default_attr ) {
20802209 return null ;
20812210 }
2211+ return $ this ->get_default_value ( $ default_attr , $ data_type , $ column_name );
2212+ }
20822213
2214+ /**
2215+ * Extract a default value from a column attribute or ALTER COLUMN action.
2216+ *
2217+ * @param WP_Parser_Node $node The "columnAttribute" or "alterListItem" AST node.
2218+ * @param string $data_type The column data type as stored in information schema.
2219+ * @param string $column_name The column name.
2220+ * @return string|null The default value as stored in information schema.
2221+ */
2222+ private function get_default_value ( WP_Parser_Node $ node , string $ data_type , string $ column_name ): ?string {
20832223 /*
20842224 * [GRAMMAR]
20852225 * DEFAULT_SYMBOL (
@@ -2090,12 +2230,12 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
20902230 */
20912231
20922232 // DEFAULT NOW()
2093- if ( $ default_attr ->has_child_token ( WP_MySQL_Lexer::NOW_SYMBOL ) ) {
2233+ if ( $ node ->has_child_token ( WP_MySQL_Lexer::NOW_SYMBOL ) ) {
20942234 return 'CURRENT_TIMESTAMP ' ;
20952235 }
20962236
20972237 // DEFAULT signedLiteral
2098- $ signed_literal = $ default_attr ->get_first_child_node ( 'signedLiteral ' );
2238+ $ signed_literal = $ node ->get_first_child_node ( 'signedLiteral ' );
20992239 if ( $ signed_literal ) {
21002240 $ literal = $ signed_literal ->get_first_child_node ( 'literal ' );
21012241 if ( null === $ literal ) {
@@ -2106,7 +2246,7 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21062246 }
21072247
21082248 // DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
2109- $ expr_with_parens = $ default_attr ->get_first_child_node ( 'exprWithParentheses ' );
2249+ $ expr_with_parens = $ node ->get_first_child_node ( 'exprWithParentheses ' );
21102250 if ( $ expr_with_parens ) {
21112251 return $ this ->serialize_mysql_expression ( $ expr_with_parens );
21122252 }
0 commit comments