@@ -4723,6 +4723,22 @@ private function translate_pure_identifier( WP_Parser_Node $node ): string {
47234723 );
47244724 }
47254725
4726+ /*
4727+ * MySQL accepts CURRENT_DATE and CURRENT_TIME as bare keywords with
4728+ * the same meaning as the parenthesized forms. SQLite understands
4729+ * the same keywords, so pass them through unquoted. Only intercept
4730+ * unquoted identifiers; an explicitly quoted `current_date` is still
4731+ * a regular column reference.
4732+ */
4733+ if ( WP_MySQL_Lexer::BACK_TICK_QUOTED_ID !== $ token ->id ) {
4734+ if ( 0 === strcasecmp ( $ value , 'CURRENT_DATE ' ) ) {
4735+ return 'CURRENT_DATE ' ;
4736+ }
4737+ if ( 0 === strcasecmp ( $ value , 'CURRENT_TIME ' ) ) {
4738+ return 'CURRENT_TIME ' ;
4739+ }
4740+ }
4741+
47264742 return '` ' . str_replace ( '` ' , '`` ' , $ value ) . '` ' ;
47274743 }
47284744
@@ -5210,11 +5226,42 @@ private function translate_runtime_function_call( WP_Parser_Node $node ): string
52105226 return 'CURRENT_TIMESTAMP ' ;
52115227 case WP_MySQL_Lexer::DATE_ADD_SYMBOL :
52125228 case WP_MySQL_Lexer::DATE_SUB_SYMBOL :
5229+ case WP_MySQL_Lexer::ADDDATE_SYMBOL :
5230+ case WP_MySQL_Lexer::SUBDATE_SYMBOL :
52135231 $ nodes = $ node ->get_child_nodes ();
52145232 return $ this ->translate_interval_arithmetic (
52155233 $ this ->translate ( $ nodes [0 ] ),
5216- array ( array ( WP_MySQL_Lexer::DATE_SUB_SYMBOL === $ child ->id , $ nodes [1 ], $ nodes [2 ] ) )
5234+ array (
5235+ array (
5236+ (
5237+ WP_MySQL_Lexer::DATE_SUB_SYMBOL === $ child ->id ||
5238+ WP_MySQL_Lexer::SUBDATE_SYMBOL === $ child ->id
5239+ ),
5240+ $ nodes [1 ],
5241+ $ nodes [2 ],
5242+ ),
5243+ )
52175244 );
5245+ case WP_MySQL_Lexer::CURDATE_SYMBOL :
5246+ /*
5247+ * The lexer normalizes CURRENT_DATE to the CURDATE token but
5248+ * keeps the original spelling in the token value. SQLite
5249+ * accepts the bare CURRENT_DATE keyword with UTC semantics.
5250+ */
5251+ if ( 0 === strcasecmp ( $ child ->get_value (), 'CURRENT_DATE ' ) ) {
5252+ return 'CURRENT_DATE ' ;
5253+ }
5254+ return $ this ->translate_sequence ( $ node ->get_children () );
5255+ case WP_MySQL_Lexer::CURTIME_SYMBOL :
5256+ /*
5257+ * The lexer normalizes CURRENT_TIME to the CURTIME token but
5258+ * keeps the original spelling in the token value. SQLite
5259+ * accepts the bare CURRENT_TIME keyword with UTC semantics.
5260+ */
5261+ if ( 0 === strcasecmp ( $ child ->get_value (), 'CURRENT_TIME ' ) ) {
5262+ return 'CURRENT_TIME ' ;
5263+ }
5264+ return $ this ->translate_sequence ( $ node ->get_children () );
52185265 case WP_MySQL_Lexer::LEFT_SYMBOL :
52195266 $ nodes = $ node ->get_child_nodes ();
52205267 return sprintf (
@@ -5367,6 +5414,71 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53675414 case 'CHAR_LENGTH ' :
53685415 // @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters.
53695416 return 'LENGTH( ' . $ args [0 ] . ') ' ;
5417+ case 'ADDDATE ' :
5418+ case 'SUBDATE ' :
5419+ /*
5420+ * The two-argument form adds or subtracts a number of days:
5421+ * ADDDATE(date, days) is DATE_ADD(date, INTERVAL days DAY).
5422+ * The three-argument INTERVAL form parses as a
5423+ * runtimeFunctionCall and is handled in
5424+ * translate_runtime_function_call().
5425+ */
5426+ if ( 2 === count ( $ args ) ) {
5427+ /*
5428+ * MySQL treats the two-argument day count as an integer,
5429+ * so a fractional value must not become a partial-day
5430+ * time modifier.
5431+ */
5432+ $ value_sql = sprintf ( 'CAST((%s) AS INTEGER) ' , $ args [1 ] );
5433+ if ( 'SUBDATE ' === $ name ) {
5434+ $ value_sql = '- ' . $ value_sql ;
5435+ }
5436+ $ modifiers_sql = sprintf ( "%s || ' DAY' " , $ value_sql );
5437+ return sprintf (
5438+ 'CASE WHEN LENGTH(%1$s) = 10 THEN DATE(%1$s, %2$s) ELSE DATETIME(%1$s, %2$s) END ' ,
5439+ $ args [0 ],
5440+ $ modifiers_sql
5441+ );
5442+ }
5443+ return $ this ->translate_sequence ( $ node ->get_children () );
5444+ case 'TIMESTAMPADD ' :
5445+ case 'TIMESTAMPDIFF ' :
5446+ /*
5447+ * The unit arrives as a bare identifier (e.g. SECOND) which
5448+ * would otherwise translate to a quoted column reference.
5449+ * Pass it to the UDF as a string literal instead. An already
5450+ * quoted unit such as TIMESTAMPADD('SECOND', ...) is left as is.
5451+ */
5452+ if ( isset ( $ nodes [1 ] ) ) {
5453+ $ unit_args = $ nodes [1 ]->get_child_nodes ();
5454+ if ( 3 === count ( $ unit_args ) ) {
5455+ return sprintf (
5456+ '%s(%s, %s, %s) ' ,
5457+ $ name ,
5458+ $ this ->translate_keyword_arg ( $ unit_args [0 ] ),
5459+ $ this ->translate ( $ unit_args [1 ] ),
5460+ $ this ->translate ( $ unit_args [2 ] )
5461+ );
5462+ }
5463+ }
5464+ return $ this ->translate_sequence ( $ node ->get_children () );
5465+ case 'GET_FORMAT ' :
5466+ /*
5467+ * The first argument is a keyword (DATE, TIME, or DATETIME)
5468+ * which would otherwise translate to a quoted column
5469+ * reference. Pass bare keywords to the UDF as strings.
5470+ */
5471+ if ( isset ( $ nodes [1 ] ) ) {
5472+ $ format_args = $ nodes [1 ]->get_child_nodes ();
5473+ if ( 2 === count ( $ format_args ) ) {
5474+ return sprintf (
5475+ 'GET_FORMAT(%s, %s) ' ,
5476+ $ this ->translate_keyword_arg ( $ format_args [0 ] ),
5477+ $ this ->translate_keyword_arg ( $ format_args [1 ] )
5478+ );
5479+ }
5480+ }
5481+ return $ this ->translate_sequence ( $ node ->get_children () );
53705482 case 'CONCAT ' :
53715483 return '( ' . implode ( ' || ' , $ args ) . ') ' ;
53725484 case 'FOUND_ROWS ' :
@@ -5392,6 +5504,31 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53925504 }
53935505 }
53945506
5507+ /**
5508+ * Translate a bare keyword argument to a string literal.
5509+ *
5510+ * Some functions take bare keywords where an expression would normally
5511+ * go, e.g. the unit in TIMESTAMPADD(SECOND, ...) or the type in
5512+ * GET_FORMAT(DATE, ...). The parser represents those as column
5513+ * references, which would translate to quoted identifiers and fail in
5514+ * SQLite. Pass them to the UDF as string literals instead. Arguments
5515+ * that are not bare keywords are translated normally, so already quoted
5516+ * values such as TIMESTAMPADD('SECOND', ...) keep working.
5517+ *
5518+ * @param WP_Parser_Node $node The argument AST node.
5519+ * @return string The translated value.
5520+ */
5521+ private function translate_keyword_arg ( WP_Parser_Node $ node ): string {
5522+ $ column_ref = $ node ->get_first_descendant_node ( 'columnRef ' );
5523+ if ( null !== $ column_ref ) {
5524+ $ token = $ column_ref ->get_first_descendant_token ();
5525+ if ( null !== $ token ) {
5526+ return $ this ->quote_sqlite_value ( strtoupper ( $ token ->get_value () ) );
5527+ }
5528+ }
5529+ return $ this ->translate ( $ node );
5530+ }
5531+
53955532 /**
53965533 * Get the configured MySQL version in dotted notation.
53975534 *
0 commit comments