@@ -1069,7 +1069,7 @@ private function record_alter_column_default(
10691069 ): void {
10701070 $ columns_table = $ this ->get_table_name ( $ table_is_temporary , 'columns ' );
10711071 $ column = $ this ->connection ->query (
1072- 'SELECT DATA_TYPE, DATETIME_PRECISION, IS_NULLABLE, EXTRA FROM ' . $ this ->connection ->quote_identifier ( $ columns_table ) . '
1072+ 'SELECT DATA_TYPE, NUMERIC_PRECISION, DATETIME_PRECISION, IS_NULLABLE, EXTRA FROM ' . $ this ->connection ->quote_identifier ( $ columns_table ) . '
10731073 WHERE table_schema = ? AND table_name = ? AND column_name = ? ' ,
10741074 array ( self ::SAVED_DATABASE_NAME , $ table_name , $ column_name )
10751075 )->fetch ( PDO ::FETCH_ASSOC );
@@ -1094,9 +1094,10 @@ private function record_alter_column_default(
10941094 $ expression = $ node ->get_first_child_node ( 'exprWithParentheses ' );
10951095 $ literal = $ expression ? $ this ->get_parenthesized_default_literal ( $ expression ) : null ;
10961096 $ datetime_precision = null === $ column ['DATETIME_PRECISION ' ] ? null : (int ) $ column ['DATETIME_PRECISION ' ];
1097+ $ numeric_precision = null === $ column ['NUMERIC_PRECISION ' ] ? null : (int ) $ column ['NUMERIC_PRECISION ' ];
10971098 $ default = $ literal
1098- ? $ this ->get_literal_default ( $ literal , $ column ['DATA_TYPE ' ], $ column_name , $ datetime_precision )
1099- : $ this ->get_default_value ( $ node , $ column ['DATA_TYPE ' ], $ column_name , $ datetime_precision );
1099+ ? $ this ->get_literal_default ( $ literal , $ column ['DATA_TYPE ' ], $ column_name , $ datetime_precision, $ numeric_precision )
1100+ : $ this ->get_default_value ( $ node , $ column ['DATA_TYPE ' ], $ column_name , $ datetime_precision, $ numeric_precision );
11001101 if ( null === $ default && 'NO ' === $ column ['IS_NULLABLE ' ] ) {
11011102 throw WP_SQLite_Information_Schema_Exception ::invalid_default_value ( $ column_name );
11021103 }
@@ -1669,16 +1670,21 @@ private function record_drop_check_constraint(
16691670 */
16701671 private function extract_column_data ( string $ table_name , string $ column_name , WP_Parser_Node $ node , int $ position ): array {
16711672 list ( $ data_type , $ column_type ) = $ this ->get_column_data_types ( $ node );
1673+ list ( $ precision , $ scale ) = $ this ->get_column_numeric_attributes ( $ node , $ data_type );
16721674
1673- $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name );
1675+ // Like MySQL, check the width of a BIT column before its default value.
1676+ if ( 'bit ' === $ data_type ) {
1677+ $ this ->validate_bit_width ( $ column_name , $ precision );
1678+ }
1679+
1680+ $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name , $ precision );
16741681 $ nullable = $ this ->get_column_nullable ( $ node );
16751682 $ key = $ this ->get_column_key ( $ node );
16761683 $ extra = $ this ->get_column_extra ( $ node );
16771684 $ comment = $ this ->get_column_comment ( $ node );
16781685
16791686 list ( $ charset , $ collation ) = $ this ->get_column_charset_and_collation ( $ node , $ data_type );
16801687 list ( $ char_length , $ octet_length ) = $ this ->get_column_lengths ( $ node , $ data_type , $ charset );
1681- list ( $ precision , $ scale ) = $ this ->get_column_numeric_attributes ( $ node , $ data_type );
16821688 $ datetime_precision = $ this ->get_column_datetime_precision ( $ node , $ data_type );
16831689 $ generation_expression = $ this ->get_column_generation_expression ( $ node );
16841690
@@ -2212,12 +2218,13 @@ private function get_table_comment( WP_Parser_Node $node ): string {
22122218 /**
22132219 * Extract column default value from the "columnDefinition" or "fieldDefinition" AST node.
22142220 *
2215- * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2216- * @param string $data_type The column data type as stored in information schema.
2217- * @param string $column_name The column name.
2218- * @return string|null The column default as stored in information schema.
2221+ * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2222+ * @param string $data_type The column data type as stored in information schema.
2223+ * @param string $column_name The column name.
2224+ * @param int|null $numeric_precision The column numeric precision. For BIT, the width in bits.
2225+ * @return string|null The column default as stored in information schema.
22192226 */
2220- private function get_column_default ( WP_Parser_Node $ node , string $ data_type , string $ column_name ): ?string {
2227+ private function get_column_default ( WP_Parser_Node $ node , string $ data_type , string $ column_name, ? int $ numeric_precision ): ?string {
22212228 $ default_attr = null ;
22222229 foreach ( $ node ->get_descendant_nodes ( 'columnAttribute ' ) as $ attr ) {
22232230 if ( $ attr ->has_child_token ( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2229,7 +2236,7 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
22292236 return null ;
22302237 }
22312238 $ datetime_precision = $ this ->get_column_datetime_precision ( $ node , $ data_type );
2232- return $ this ->get_default_value ( $ default_attr , $ data_type , $ column_name , $ datetime_precision );
2239+ return $ this ->get_default_value ( $ default_attr , $ data_type , $ column_name , $ datetime_precision, $ numeric_precision );
22332240 }
22342241
22352242 /**
@@ -2239,9 +2246,10 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
22392246 * @param string $data_type The column data type as stored in information schema.
22402247 * @param string $column_name The column name.
22412248 * @param int|null $datetime_precision The column fractional seconds precision.
2249+ * @param int|null $numeric_precision The column numeric precision. For BIT, the width in bits.
22422250 * @return string|null The default value as stored in information schema.
22432251 */
2244- private function get_default_value ( WP_Parser_Node $ node , string $ data_type , string $ column_name , ?int $ datetime_precision ): ?string {
2252+ private function get_default_value ( WP_Parser_Node $ node , string $ data_type , string $ column_name , ?int $ datetime_precision, ? int $ numeric_precision ): ?string {
22452253 /*
22462254 * [GRAMMAR]
22472255 * columnAttribute:
@@ -2271,10 +2279,10 @@ private function get_default_value( WP_Parser_Node $node, string $data_type, str
22712279 // A signed number, such as "-5", has no "literal" child node.
22722280 // Like MySQL, drop the unary plus, e.g., "+5" becomes "5".
22732281 $ number = ltrim ( $ this ->get_value ( $ signed_literal ), '+ ' );
2274- $ default = $ this ->normalize_literal_default ( $ number , $ data_type , $ column_name );
2282+ $ default = $ this ->normalize_literal_default ( $ number , $ data_type , $ column_name, $ numeric_precision );
22752283 return $ this ->get_temporal_zero_value ( $ data_type , $ default , $ datetime_precision ) ?? $ default ;
22762284 }
2277- return $ this ->get_literal_default ( $ literal , $ data_type , $ column_name , $ datetime_precision );
2285+ return $ this ->get_literal_default ( $ literal , $ data_type , $ column_name , $ datetime_precision, $ numeric_precision );
22782286 }
22792287
22802288 // DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2293,9 +2301,10 @@ private function get_default_value( WP_Parser_Node $node, string $data_type, str
22932301 * @param string $data_type The column data type as stored in information schema.
22942302 * @param string $column_name The column name.
22952303 * @param int|null $datetime_precision The column fractional seconds precision.
2304+ * @param int|null $numeric_precision The column numeric precision. For BIT, the width in bits.
22962305 * @return string|null The default value as stored in information schema.
22972306 */
2298- private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type , string $ column_name , ?int $ datetime_precision ): ?string {
2307+ private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type , string $ column_name , ?int $ datetime_precision, ? int $ numeric_precision ): ?string {
22992308 // DEFAULT NULL
23002309 if ( $ literal ->has_child_node ( 'nullLiteral ' ) ) {
23012310 return null ;
@@ -2311,7 +2320,7 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
23112320 $ bool_value = $ bool_literal ->has_child_token ( WP_MySQL_Lexer::TRUE_SYMBOL ) ? '1 ' : '0 ' ;
23122321 $ default = 'bit ' === $ data_type ? "b' {$ bool_value }' " : $ bool_value ;
23132322 } else {
2314- $ default = $ this ->normalize_literal_default ( $ this ->get_value ( $ literal ), $ data_type , $ column_name );
2323+ $ default = $ this ->normalize_literal_default ( $ this ->get_value ( $ literal ), $ data_type , $ column_name, $ numeric_precision );
23152324 }
23162325
23172326 /*
@@ -2328,19 +2337,22 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
23282337 /**
23292338 * Normalize a literal default value for the column data type.
23302339 *
2331- * @param string $default_value The literal default value.
2332- * @param string $data_type The column data type as stored in information schema.
2333- * @param string $column_name The column name.
2334- * @return string The default value as stored in information schema.
2340+ * @param string $default_value The literal default value.
2341+ * @param string $data_type The column data type as stored in information schema.
2342+ * @param string $column_name The column name.
2343+ * @param int|null $numeric_precision The column numeric precision. For BIT, the width in bits.
2344+ * @return string The default value as stored in information schema.
23352345 */
2336- private function normalize_literal_default ( string $ default_value , string $ data_type , string $ column_name ): string {
2346+ private function normalize_literal_default ( string $ default_value , string $ data_type , string $ column_name, ? int $ numeric_precision ): string {
23372347 if ( 'bit ' === $ data_type ) {
23382348 /*
23392349 * @TODO: Validate and normalize defaults from their AST and the full
23402350 * column definition before storing them in the information schema.
23412351 */
23422352 $ bit_default = $ this ->get_bit_default ( $ default_value );
2343- if ( null === $ bit_default ) {
2353+
2354+ // The value must fit the column width. The "b''" wrapper takes 3 bytes.
2355+ if ( null === $ bit_default || strlen ( $ bit_default ) - 3 > $ numeric_precision ) {
23442356 throw WP_SQLite_Information_Schema_Exception ::invalid_default_value ( $ column_name );
23452357 }
23462358 return $ bit_default ;
@@ -2439,6 +2451,19 @@ private function get_bit_default( string $default_value ): ?string {
24392451 return "b'0' " ;
24402452 }
24412453
2454+ // Hex literal, e.g. x'05' or 0x05. Convert it to a bit literal digit by
2455+ // digit, as a value of 64 bits doesn't fit into an integer.
2456+ if (
2457+ preg_match ( "/\Ax'([0-9a-f]*)'\z/ " , $ value , $ matches )
2458+ || preg_match ( '/\A0x([0-9a-f]+)\z/ ' , $ value , $ matches )
2459+ ) {
2460+ $ value = "b' " ;
2461+ foreach ( str_split ( $ matches [1 ] ) as $ digit ) {
2462+ $ value .= sprintf ( '%04b ' , hexdec ( $ digit ) );
2463+ }
2464+ $ value .= "' " ;
2465+ }
2466+
24422467 // Bit literal, e.g. b'101' or 0b101.
24432468 if (
24442469 preg_match ( "/\Ab'([01]*)'\z/ " , $ value , $ matches )
@@ -2448,14 +2473,6 @@ private function get_bit_default( string $default_value ): ?string {
24482473 return "b' " . ( '' === $ bits ? '0 ' : $ bits ) . "' " ;
24492474 }
24502475
2451- // Hex literal, e.g. x'05' or 0x05.
2452- if (
2453- preg_match ( "/\Ax'([0-9a-f]*)'\z/ " , $ value , $ matches )
2454- || preg_match ( '/\A0x([0-9a-f]+)\z/ ' , $ value , $ matches )
2455- ) {
2456- return "b' " . decbin ( hexdec ( $ matches [1 ] ) ) . "' " ;
2457- }
2458-
24592476 // Non-negative decimal, e.g. 5, or a numeric string literal such as '0'.
24602477 if ( is_numeric ( $ value ) && $ value >= 0 ) {
24612478 return "b' " . decbin ( (int ) $ value ) . "' " ;
@@ -2913,7 +2930,9 @@ private function get_column_numeric_attributes( WP_Parser_Node $node, string $da
29132930
29142931 // For bit columns, we need to check the precision.
29152932 if ( 'bit ' === $ data_type ) {
2916- $ field_length = $ node ->get_first_descendant_node ( 'fieldLength ' );
2933+ // Search only the data type. An expression in the column definition
2934+ // can contain a length as well, e.g. "CAST(... AS CHAR(10))".
2935+ $ field_length = $ node ->get_first_descendant_node ( 'dataType ' )->get_first_descendant_node ( 'fieldLength ' );
29172936 if ( null === $ field_length ) {
29182937 return array ( 1 , null );
29192938 }
@@ -2948,6 +2967,28 @@ private function get_column_numeric_attributes( WP_Parser_Node $node, string $da
29482967 return array ( null , null );
29492968 }
29502969
2970+ /**
2971+ * Check that the width of a BIT column is 1 to 64 bits.
2972+ *
2973+ * @param string $column_name The column name.
2974+ * @param int $width The column width in bits.
2975+ * @throws WP_SQLite_Information_Schema_Exception When the width is not valid.
2976+ */
2977+ private function validate_bit_width ( string $ column_name , int $ width ): void {
2978+ if ( 0 === $ width ) {
2979+ throw WP_SQLite_Information_Schema_Exception ::invalid_column_size ( $ column_name );
2980+ }
2981+
2982+ if ( $ width > 64 ) {
2983+ // MySQL reports a different limit when the width doesn't fit in 32 bits.
2984+ $ max_width = 64 ;
2985+ if ( $ width > 4294967295 ) {
2986+ $ max_width = 4294967295 ;
2987+ }
2988+ throw WP_SQLite_Information_Schema_Exception ::display_width_out_of_range ( $ column_name , $ max_width );
2989+ }
2990+ }
2991+
29512992 /**
29522993 * Extract column date/time precision from the "columnDefinition" or "fieldDefinition" AST node.
29532994 *
0 commit comments