@@ -1507,7 +1507,7 @@ private function record_drop_check_constraint(
15071507 private function extract_column_data ( string $ table_name , string $ column_name , WP_Parser_Node $ node , int $ position ): array {
15081508 list ( $ data_type , $ column_type ) = $ this ->get_column_data_types ( $ node );
15091509
1510- $ default = $ this ->get_column_default ( $ node , $ data_type );
1510+ $ default = $ this ->get_column_default ( $ node , $ data_type, $ column_name );
15111511 $ nullable = $ this ->get_column_nullable ( $ node );
15121512 $ key = $ this ->get_column_key ( $ node );
15131513 $ extra = $ this ->get_column_extra ( $ node );
@@ -2043,11 +2043,12 @@ private function get_table_comment( WP_Parser_Node $node ): string {
20432043 /**
20442044 * Extract column default value from the "columnDefinition" or "fieldDefinition" AST node.
20452045 *
2046- * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2047- * @param string $data_type The column data type as stored in information schema.
2048- * @return string|null The column default as stored in information schema.
2046+ * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2047+ * @param string $data_type The column data type as stored in information schema.
2048+ * @param string $column_name The column name.
2049+ * @return string|null The column default as stored in information schema.
20492050 */
2050- private function get_column_default ( WP_Parser_Node $ node , string $ data_type ): ?string {
2051+ private function get_column_default ( WP_Parser_Node $ node , string $ data_type, string $ column_name ): ?string {
20512052 $ default_attr = null ;
20522053 foreach ( $ node ->get_descendant_nodes ( 'columnAttribute ' ) as $ attr ) {
20532054 if ( $ attr ->has_child_token ( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2081,7 +2082,7 @@ private function get_column_default( WP_Parser_Node $node, string $data_type ):
20812082 // A signed number, such as "-5", has no "literal" child node.
20822083 return $ this ->get_value ( $ signed_literal );
20832084 }
2084- return $ this ->get_literal_default ( $ literal , $ data_type );
2085+ return $ this ->get_literal_default ( $ literal , $ data_type, $ column_name );
20852086 }
20862087
20872088 // DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2096,11 +2097,12 @@ private function get_column_default( WP_Parser_Node $node, string $data_type ):
20962097 /**
20972098 * Extract and normalize a literal default value.
20982099 *
2099- * @param WP_Parser_Node $literal The "literal" AST node.
2100- * @param string $data_type The column data type as stored in information schema.
2101- * @return string|null The default value as stored in information schema.
2100+ * @param WP_Parser_Node $literal The "literal" AST node.
2101+ * @param string $data_type The column data type as stored in information schema.
2102+ * @param string $column_name The column name.
2103+ * @return string|null The default value as stored in information schema.
21022104 */
2103- private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type ): ?string {
2105+ private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type, string $ column_name ): ?string {
21042106 // DEFAULT NULL
21052107 if ( $ literal ->has_child_node ( 'nullLiteral ' ) ) {
21062108 return null ;
@@ -2116,7 +2118,15 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21162118 $ default = $ this ->get_value ( $ literal );
21172119
21182120 if ( 'bit ' === $ data_type ) {
2119- return $ this ->get_bit_default ( $ default ) ?? $ default ;
2121+ /*
2122+ * @TODO: Validate and normalize defaults from their AST and the full
2123+ * column definition before storing them in the information schema.
2124+ */
2125+ $ bit_default = $ this ->get_bit_default ( $ default );
2126+ if ( null === $ bit_default ) {
2127+ throw WP_SQLite_Information_Schema_Exception ::invalid_default_value ( $ column_name );
2128+ }
2129+ return $ bit_default ;
21202130 }
21212131
21222132 /*
@@ -2143,14 +2153,20 @@ private function get_bit_default( string $default_value ): ?string {
21432153 $ value = strtolower ( $ default_value );
21442154
21452155 // Bit literal, e.g. b'101' or 0b101.
2146- if ( str_starts_with ( $ value , "b' " ) || str_starts_with ( $ value , '0b ' ) ) {
2147- $ bits = ltrim ( rtrim ( substr ( $ value , 2 ), "' " ), '0 ' );
2156+ if (
2157+ preg_match ( "/\Ab'([01]*)'\z/ " , $ value , $ matches )
2158+ || preg_match ( '/\A0b([01]+)\z/ ' , $ value , $ matches )
2159+ ) {
2160+ $ bits = ltrim ( $ matches [1 ], '0 ' );
21482161 return "b' " . ( '' === $ bits ? '0 ' : $ bits ) . "' " ;
21492162 }
21502163
21512164 // Hex literal, e.g. x'05' or 0x05.
2152- if ( str_starts_with ( $ value , "x' " ) || str_starts_with ( $ value , '0x ' ) ) {
2153- return "b' " . decbin ( hexdec ( rtrim ( substr ( $ value , 2 ), "' " ) ) ) . "' " ;
2165+ if (
2166+ preg_match ( "/\Ax'([0-9a-f]*)'\z/ " , $ value , $ matches )
2167+ || preg_match ( '/\A0x([0-9a-f]+)\z/ ' , $ value , $ matches )
2168+ ) {
2169+ return "b' " . decbin ( hexdec ( $ matches [1 ] ) ) . "' " ;
21542170 }
21552171
21562172 // Decimal, e.g. 5, or a numeric string literal such as '0'.
0 commit comments