@@ -312,6 +312,17 @@ class WP_SQLite_Information_Schema_Builder {
312312 'utf8mb4 ' => 4 ,
313313 );
314314
315+ /**
316+ * Zero values of temporal data types, without fractional seconds.
317+ */
318+ const TEMPORAL_ZERO_VALUE_MAP = array (
319+ 'date ' => '0000-00-00 ' ,
320+ 'time ' => '00:00:00 ' ,
321+ 'datetime ' => '0000-00-00 00:00:00 ' ,
322+ 'timestamp ' => '0000-00-00 00:00:00 ' ,
323+ 'year ' => '0000 ' ,
324+ );
325+
315326 /**
316327 * A prefix for information schema table names.
317328 *
@@ -425,7 +436,7 @@ public function normalize_temporal_zero_defaults(): void {
425436 $ columns_table = $ this ->connection ->quote_identifier ( $ this ->get_table_name ( false , 'columns ' ) );
426437 $ columns = $ this ->connection ->query (
427438 sprintf (
428- "SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE , COLUMN_DEFAULT
439+ "SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATETIME_PRECISION , COLUMN_DEFAULT
429440 FROM %s
430441 WHERE DATA_TYPE IN ('date', 'time', 'datetime', 'timestamp', 'year')
431442 AND COLUMN_DEFAULT IS NOT NULL " ,
@@ -434,10 +445,10 @@ public function normalize_temporal_zero_defaults(): void {
434445 )->fetchAll ( PDO ::FETCH_ASSOC ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
435446
436447 foreach ( $ columns as $ column ) {
437- $ default = $ this ->get_temporal_zero_default (
438- (string ) $ column ['COLUMN_DEFAULT ' ],
448+ $ default = $ this ->get_temporal_zero_value (
439449 $ column ['DATA_TYPE ' ],
440- $ column ['COLUMN_TYPE ' ]
450+ $ column ['COLUMN_DEFAULT ' ],
451+ null === $ column ['DATETIME_PRECISION ' ] ? null : (int ) $ column ['DATETIME_PRECISION ' ]
441452 );
442453 if ( null === $ default || $ default === $ column ['COLUMN_DEFAULT ' ] ) {
443454 continue ;
@@ -1561,7 +1572,7 @@ private function record_drop_check_constraint(
15611572 private function extract_column_data ( string $ table_name , string $ column_name , WP_Parser_Node $ node , int $ position ): array {
15621573 list ( $ data_type , $ column_type ) = $ this ->get_column_data_types ( $ node );
15631574
1564- $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name, $ column_type );
1575+ $ default = $ this ->get_column_default ( $ node , $ data_type , $ column_name );
15651576 $ nullable = $ this ->get_column_nullable ( $ node );
15661577 $ key = $ this ->get_column_key ( $ node );
15671578 $ extra = $ this ->get_column_extra ( $ node );
@@ -2106,10 +2117,9 @@ private function get_table_comment( WP_Parser_Node $node ): string {
21062117 * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
21072118 * @param string $data_type The column data type as stored in information schema.
21082119 * @param string $column_name The column name.
2109- * @param string $column_type The column type as stored in information schema.
21102120 * @return string|null The column default as stored in information schema.
21112121 */
2112- private function get_column_default ( WP_Parser_Node $ node , string $ data_type , string $ column_name, string $ column_type ): ?string {
2122+ private function get_column_default ( WP_Parser_Node $ node , string $ data_type , string $ column_name ): ?string {
21132123 $ default_attr = null ;
21142124 foreach ( $ node ->get_descendant_nodes ( 'columnAttribute ' ) as $ attr ) {
21152125 if ( $ attr ->has_child_token ( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2141,11 +2151,27 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21412151 $ literal = $ signed_literal ->get_first_child_node ( 'literal ' );
21422152 if ( null === $ literal ) {
21432153 // A signed number, such as "-5", has no "literal" child node.
2144- $ default = $ this ->get_value ( $ signed_literal );
2145- $ zero_default = $ this ->get_temporal_zero_default ( $ default , $ data_type , $ column_type );
2146- return $ zero_default ?? $ default ;
2154+ $ default = $ this ->get_value ( $ signed_literal );
2155+ } else {
2156+ $ default = $ this ->get_literal_default ( $ literal , $ data_type , $ column_name );
2157+ }
2158+
2159+ /*
2160+ * MySQL stores a numeric zero default of a temporal column as a zero
2161+ * value, e.g., "DEFAULT 0" of a DATETIME column as "0000-00-00 00:00:00".
2162+ *
2163+ * A string zero, such as '0', is normalized the same way, as older
2164+ * driver versions stored it the same as a number. MySQL reads it as
2165+ * a zero value of a TIME column, and rejects it for other temporal
2166+ * columns in strict SQL mode. The exception is a YEAR column, where
2167+ * MySQL reads the strings '0' and '00' as the year 2000.
2168+ */
2169+ $ is_string = null !== $ literal && $ literal ->has_child_node ( 'textLiteral ' );
2170+ if ( null === $ default || ( $ is_string && 'year ' === $ data_type ) ) {
2171+ return $ default ;
21472172 }
2148- return $ this ->get_literal_default ( $ literal , $ data_type , $ column_name , $ column_type );
2173+ $ datetime_precision = $ this ->get_column_datetime_precision ( $ node , $ data_type );
2174+ return $ this ->get_temporal_zero_value ( $ data_type , $ default , $ datetime_precision ) ?? $ default ;
21492175 }
21502176
21512177 // DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2163,10 +2189,9 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21632189 * @param WP_Parser_Node $literal The "literal" AST node.
21642190 * @param string $data_type The column data type as stored in information schema.
21652191 * @param string $column_name The column name.
2166- * @param string $column_type The column type as stored in information schema.
21672192 * @return string|null The default value as stored in information schema.
21682193 */
2169- private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type , string $ column_name, string $ column_type ): ?string {
2194+ private function get_literal_default ( WP_Parser_Node $ literal , string $ data_type , string $ column_name ): ?string {
21702195 // DEFAULT NULL
21712196 if ( $ literal ->has_child_node ( 'nullLiteral ' ) ) {
21722197 return null ;
@@ -2181,12 +2206,6 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21812206
21822207 $ default = $ this ->get_value ( $ literal );
21832208
2184- // Numeric zero temporal defaults, such as "DEFAULT 0" of a DATETIME column.
2185- $ zero_default = $ this ->get_temporal_zero_default ( $ default , $ data_type , $ column_type );
2186- if ( null !== $ zero_default ) {
2187- return $ zero_default ;
2188- }
2189-
21902209 if ( 'bit ' === $ data_type ) {
21912210 /*
21922211 * @TODO: Validate and normalize defaults from their AST and the full
@@ -2200,9 +2219,9 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
22002219 }
22012220
22022221 /*
2203- * @TODO: Other non -BIT literal defaults (apart from numeric zero temporal
2204- * defaults handled above) are currently stored verbatim. To match MySQL,
2205- * they should be normalized. For example:
2222+ * @TODO: Non -BIT literal defaults, except for temporal zero values, are
2223+ * currently stored verbatim. To match MySQL, they should be normalized.
2224+ * For example:
22062225 * - BINARY/VARBINARY defaults stored as 0x hex literals.
22072226 * - Hex and bit literals coerced to raw bytes for text columns and to
22082227 * their decimal value for numeric columns.
@@ -2213,42 +2232,29 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
22132232 }
22142233
22152234 /**
2216- * Normalize a numeric zero default of a temporal column to its MySQL zero value .
2235+ * Get the zero value of a temporal column when its default is a numeric zero .
22172236 *
2218- * MySQL stores "DEFAULT 0" of a DATETIME column as "0000-00-00 00:00:00",
2219- * and it rejects the quoted form "DEFAULT '0'" in strict SQL mode. Other
2220- * temporal defaults are not handled here.
2237+ * For example, a "0", "-0.0", or "0e0" default of a DATETIME(3) column has
2238+ * the zero value of "0000-00-00 00:00:00.000".
22212239 *
2222- * @param string $default_value The default value .
2223- * @param string $data_type The column data type as stored in information schema .
2224- * @param string $column_type The column type as stored in information schema .
2225- * @return string|null The zero value, or null when the default is not a numeric zero
2226- * or the column is not a temporal column .
2240+ * @param string $data_type The column data type as stored in information schema .
2241+ * @param string $default_value The column default value of a number or string literal .
2242+ * @param int|null $datetime_precision The column fractional seconds precision .
2243+ * @return string|null The zero value, or null when the column is not temporal
2244+ * or the default is not a numeric zero .
22272245 */
2228- private function get_temporal_zero_default ( string $ default_value , string $ data_type , string $ column_type ): ?string {
2229- if ( ! preg_match ( '/\A[+-]?(?:0+(?:\.0*)?|\.0+)\z/ ' , $ default_value ) ) {
2230- return null ;
2231- }
2246+ private function get_temporal_zero_value ( string $ data_type , string $ default_value , ?int $ datetime_precision ): ?string {
2247+ $ zero_value = self ::TEMPORAL_ZERO_VALUE_MAP [ $ data_type ] ?? null ;
22322248
2233- // Fractional seconds precision, e.g., "datetime(3)".
2234- $ fraction = '' ;
2235- if ( preg_match ( '/\((\d+)\)/ ' , $ column_type , $ matches ) && (int ) $ matches [1 ] > 0 ) {
2236- $ fraction = '. ' . str_repeat ( '0 ' , (int ) $ matches [1 ] );
2249+ // A signed number can be a hex literal, such as "-0x0", which is not numeric.
2250+ if ( null === $ zero_value || ! is_numeric ( $ default_value ) || 0.0 !== (float ) $ default_value ) {
2251+ return null ;
22372252 }
22382253
2239- switch ( $ data_type ) {
2240- case 'date ' :
2241- return '0000-00-00 ' ;
2242- case 'year ' :
2243- return '0000 ' ;
2244- case 'time ' :
2245- return '00:00:00 ' . $ fraction ;
2246- case 'datetime ' :
2247- case 'timestamp ' :
2248- return '0000-00-00 00:00:00 ' . $ fraction ;
2249- default :
2250- return null ;
2254+ if ( $ datetime_precision > 0 ) {
2255+ $ zero_value .= '. ' . str_repeat ( '0 ' , $ datetime_precision );
22512256 }
2257+ return $ zero_value ;
22522258 }
22532259
22542260 /**
@@ -2785,11 +2791,14 @@ private function get_column_numeric_attributes( WP_Parser_Node $node, string $da
27852791 */
27862792 private function get_column_datetime_precision ( WP_Parser_Node $ node , string $ data_type ): ?int {
27872793 if ( 'time ' === $ data_type || 'datetime ' === $ data_type || 'timestamp ' === $ data_type ) {
2788- $ precision = $ node ->get_first_descendant_node ( 'typeDatetimePrecision ' );
2794+ // Expressions in the column definition may have their own precision,
2795+ // e.g., "CAST(... AS DATETIME(6))", so only the data type is checked.
2796+ $ type_node = $ node ->get_first_descendant_node ( 'dataType ' );
2797+ $ precision = $ type_node ->get_first_descendant_node ( 'typeDatetimePrecision ' );
27892798 if ( null === $ precision ) {
27902799 return 0 ;
27912800 } else {
2792- return (int ) $ this -> get_value ( $ precision );
2801+ return (int ) $ precision -> get_first_child_token ( WP_MySQL_Lexer:: INT_NUMBER )-> get_value ( );
27932802 }
27942803 }
27952804 return null ;
0 commit comments