@@ -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,66 @@ 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+ $ value_sql = sprintf ( '(%s) ' , $ args [1 ] );
5428+ if ( 'SUBDATE ' === $ name ) {
5429+ $ value_sql = '- ' . $ value_sql ;
5430+ }
5431+ $ modifiers_sql = sprintf ( "%s || ' DAY' " , $ value_sql );
5432+ return sprintf (
5433+ 'CASE WHEN LENGTH(%1$s) = 10 THEN DATE(%1$s, %2$s) ELSE DATETIME(%1$s, %2$s) END ' ,
5434+ $ args [0 ],
5435+ $ modifiers_sql
5436+ );
5437+ }
5438+ return $ this ->translate_sequence ( $ node ->get_children () );
5439+ case 'TIMESTAMPADD ' :
5440+ case 'TIMESTAMPDIFF ' :
5441+ /*
5442+ * The unit arrives as a bare identifier (e.g. SECOND) which
5443+ * would otherwise translate to a quoted column reference.
5444+ * Pass it to the UDF as a string literal instead. An already
5445+ * quoted unit such as TIMESTAMPADD('SECOND', ...) is left as is.
5446+ */
5447+ if ( isset ( $ nodes [1 ] ) ) {
5448+ $ unit_args = $ nodes [1 ]->get_child_nodes ();
5449+ if ( 3 === count ( $ unit_args ) ) {
5450+ return sprintf (
5451+ '%s(%s, %s, %s) ' ,
5452+ $ name ,
5453+ $ this ->translate_keyword_arg ( $ unit_args [0 ] ),
5454+ $ this ->translate ( $ unit_args [1 ] ),
5455+ $ this ->translate ( $ unit_args [2 ] )
5456+ );
5457+ }
5458+ }
5459+ return $ this ->translate_sequence ( $ node ->get_children () );
5460+ case 'GET_FORMAT ' :
5461+ /*
5462+ * The first argument is a keyword (DATE, TIME, or DATETIME)
5463+ * which would otherwise translate to a quoted column
5464+ * reference. Pass bare keywords to the UDF as strings.
5465+ */
5466+ if ( isset ( $ nodes [1 ] ) ) {
5467+ $ format_args = $ nodes [1 ]->get_child_nodes ();
5468+ if ( 2 === count ( $ format_args ) ) {
5469+ return sprintf (
5470+ 'GET_FORMAT(%s, %s) ' ,
5471+ $ this ->translate_keyword_arg ( $ format_args [0 ] ),
5472+ $ this ->translate_keyword_arg ( $ format_args [1 ] )
5473+ );
5474+ }
5475+ }
5476+ return $ this ->translate_sequence ( $ node ->get_children () );
53705477 case 'CONCAT ' :
53715478 return '( ' . implode ( ' || ' , $ args ) . ') ' ;
53725479 case 'FOUND_ROWS ' :
@@ -5392,6 +5499,31 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53925499 }
53935500 }
53945501
5502+ /**
5503+ * Translate a bare keyword argument to a string literal.
5504+ *
5505+ * Some functions take bare keywords where an expression would normally
5506+ * go, e.g. the unit in TIMESTAMPADD(SECOND, ...) or the type in
5507+ * GET_FORMAT(DATE, ...). The parser represents those as column
5508+ * references, which would translate to quoted identifiers and fail in
5509+ * SQLite. Pass them to the UDF as string literals instead. Arguments
5510+ * that are not bare keywords are translated normally, so already quoted
5511+ * values such as TIMESTAMPADD('SECOND', ...) keep working.
5512+ *
5513+ * @param WP_Parser_Node $node The argument AST node.
5514+ * @return string The translated value.
5515+ */
5516+ private function translate_keyword_arg ( WP_Parser_Node $ node ): string {
5517+ $ column_ref = $ node ->get_first_descendant_node ( 'columnRef ' );
5518+ if ( null !== $ column_ref ) {
5519+ $ token = $ column_ref ->get_first_descendant_token ();
5520+ if ( null !== $ token ) {
5521+ return $ this ->quote_sqlite_value ( strtoupper ( $ token ->get_value () ) );
5522+ }
5523+ }
5524+ return $ this ->translate ( $ node );
5525+ }
5526+
53955527 /**
53965528 * Get the configured MySQL version in dotted notation.
53975529 *
0 commit comments