@@ -5185,6 +5185,9 @@ private function translate_regexp_functions( WP_Parser_Node $node ): string {
51855185 private function translate_runtime_function_call ( WP_Parser_Node $ node ): string {
51865186 $ child = $ node ->get_first_child ();
51875187 if ( $ child instanceof WP_Parser_Node ) {
5188+ if ( 'trimFunction ' === $ child ->rule_name ) {
5189+ return $ this ->translate_trim_function ( $ child );
5190+ }
51885191 return $ this ->translate ( $ child );
51895192 }
51905193
@@ -5222,11 +5225,152 @@ private function translate_runtime_function_call( WP_Parser_Node $node ): string
52225225 $ this ->translate ( $ nodes [0 ] ),
52235226 $ this ->translate ( $ nodes [1 ] )
52245227 );
5228+ case WP_MySQL_Lexer::RIGHT_SYMBOL :
5229+ /*
5230+ * RIGHT is parsed as a keyword, and it is a join keyword in
5231+ * newer SQLite versions, where it cannot be called as a
5232+ * function. Route it to a UDF with a non-reserved name.
5233+ */
5234+ $ nodes = $ node ->get_child_nodes ();
5235+ return sprintf (
5236+ 'right_str(%s, %s) ' ,
5237+ $ this ->translate ( $ nodes [0 ] ),
5238+ $ this ->translate ( $ nodes [1 ] )
5239+ );
5240+ case WP_MySQL_Lexer::POSITION_SYMBOL :
5241+ // POSITION(substr IN str) is LOCATE() with swapped arguments.
5242+ $ nodes = $ node ->get_child_nodes ();
5243+ return sprintf (
5244+ 'locate(%s, %s) ' ,
5245+ $ this ->translate ( $ nodes [0 ] ),
5246+ $ this ->translate ( $ nodes [1 ] )
5247+ );
5248+ case WP_MySQL_Lexer::INSERT_SYMBOL :
5249+ // The string function INSERT(). Emitted under a different
5250+ // name because SQLite will not accept INSERT as a function.
5251+ $ nodes = $ node ->get_child_nodes ();
5252+ return sprintf (
5253+ 'insert_str(%s, %s, %s, %s) ' ,
5254+ $ this ->translate ( $ nodes [0 ] ),
5255+ $ this ->translate ( $ nodes [1 ] ),
5256+ $ this ->translate ( $ nodes [2 ] ),
5257+ $ this ->translate ( $ nodes [3 ] )
5258+ );
5259+ case WP_MySQL_Lexer::EXTRACT_SYMBOL :
5260+ $ nodes = $ node ->get_child_nodes ();
5261+ return $ this ->translate_extract_function ( $ nodes [0 ], $ nodes [1 ] );
52255262 default :
52265263 return $ this ->translate_sequence ( $ node ->get_children () );
52275264 }
52285265 }
52295266
5267+ /**
5268+ * Translate a MySQL EXTRACT(part FROM date) call to SQLite.
5269+ *
5270+ * @param WP_Parser_Node $part_node The "interval" AST node holding the unit.
5271+ * @param WP_Parser_Node $date_node The date expression AST node.
5272+ * @return string The translated value.
5273+ * @throws WP_MySQL_On_SQLite_Exception When the translation fails.
5274+ */
5275+ private function translate_extract_function ( WP_Parser_Node $ part_node , WP_Parser_Node $ date_node ): string {
5276+ $ unit_token = $ part_node ->get_first_descendant_token ();
5277+ $ date = $ this ->translate ( $ date_node );
5278+
5279+ switch ( $ unit_token ->id ) {
5280+ case WP_MySQL_Lexer::YEAR_SYMBOL :
5281+ return sprintf ( 'year(%s) ' , $ date );
5282+ case WP_MySQL_Lexer::QUARTER_SYMBOL :
5283+ return sprintf (
5284+ 'CAST(((CAST(STRFTIME(%s, %s) AS INTEGER) + 2) / 3) AS INTEGER) ' ,
5285+ $ this ->quote_sqlite_value ( '%m ' ),
5286+ $ date
5287+ );
5288+ case WP_MySQL_Lexer::MONTH_SYMBOL :
5289+ return sprintf ( 'month(%s) ' , $ date );
5290+ case WP_MySQL_Lexer::WEEK_SYMBOL :
5291+ return sprintf ( 'week(%s, 0) ' , $ date );
5292+ case WP_MySQL_Lexer::DAY_SYMBOL :
5293+ return sprintf ( 'day(%s) ' , $ date );
5294+ case WP_MySQL_Lexer::HOUR_SYMBOL :
5295+ return sprintf ( 'hour(%s) ' , $ date );
5296+ case WP_MySQL_Lexer::MINUTE_SYMBOL :
5297+ return sprintf ( 'minute(%s) ' , $ date );
5298+ case WP_MySQL_Lexer::SECOND_SYMBOL :
5299+ return sprintf ( 'second(%s) ' , $ date );
5300+ case WP_MySQL_Lexer::MICROSECOND_SYMBOL :
5301+ return sprintf (
5302+ 'CASE WHEN (%1$s) IS NULL THEN NULL WHEN INSTR((%1$s), %2$s) > 0 THEN CAST(SUBSTR((%1$s) || %3$s, INSTR((%1$s), %2$s) + 1, 6) AS INTEGER) ELSE 0 END ' ,
5303+ $ date ,
5304+ $ this ->quote_sqlite_value ( '. ' ),
5305+ $ this ->quote_sqlite_value ( '000000 ' )
5306+ );
5307+ default :
5308+ throw $ this ->new_not_supported_exception (
5309+ sprintf ( 'EXTRACT unit: %s ' , $ unit_token ->get_value () )
5310+ );
5311+ }
5312+ }
5313+
5314+ /**
5315+ * Translate a MySQL TRIM() call to SQLite.
5316+ *
5317+ * Handles the extended TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)
5318+ * syntax. The plain TRIM(str) and TRIM(remstr FROM str) forms are covered
5319+ * by the same mapping.
5320+ *
5321+ * @param WP_Parser_Node $node The "trimFunction" AST node.
5322+ * @return string The translated value.
5323+ * @throws WP_MySQL_On_SQLite_Exception When the translation fails.
5324+ */
5325+ private function translate_trim_function ( WP_Parser_Node $ node ): string {
5326+ $ spec = null ;
5327+ $ exprs = array ();
5328+ foreach ( $ node ->get_children () as $ child ) {
5329+ if ( $ child instanceof WP_MySQL_Token ) {
5330+ if ( WP_MySQL_Lexer::LEADING_SYMBOL === $ child ->id ) {
5331+ $ spec = 'LEADING ' ;
5332+ } elseif ( WP_MySQL_Lexer::TRAILING_SYMBOL === $ child ->id ) {
5333+ $ spec = 'TRAILING ' ;
5334+ } elseif ( WP_MySQL_Lexer::BOTH_SYMBOL === $ child ->id ) {
5335+ $ spec = 'BOTH ' ;
5336+ }
5337+ continue ;
5338+ }
5339+ if ( $ child instanceof WP_Parser_Node ) {
5340+ $ exprs [] = $ this ->translate ( $ child );
5341+ }
5342+ }
5343+
5344+ if ( 0 === count ( $ exprs ) || 2 < count ( $ exprs ) ) {
5345+ throw $ this ->new_invalid_input_exception ();
5346+ }
5347+
5348+ if ( 2 === count ( $ exprs ) ) {
5349+ $ str = $ exprs [1 ];
5350+ $ remstr = $ exprs [0 ];
5351+ } else {
5352+ $ str = $ exprs [0 ];
5353+ $ remstr = null ;
5354+ }
5355+
5356+ if ( 'LEADING ' === $ spec ) {
5357+ if ( null === $ remstr ) {
5358+ return sprintf ( 'LTRIM(%s) ' , $ str );
5359+ }
5360+ return sprintf ( 'LTRIM(%s, %s) ' , $ str , $ remstr );
5361+ }
5362+ if ( 'TRAILING ' === $ spec ) {
5363+ if ( null === $ remstr ) {
5364+ return sprintf ( 'RTRIM(%s) ' , $ str );
5365+ }
5366+ return sprintf ( 'RTRIM(%s, %s) ' , $ str , $ remstr );
5367+ }
5368+ if ( null === $ remstr ) {
5369+ return sprintf ( 'TRIM(%s) ' , $ str );
5370+ }
5371+ return sprintf ( 'TRIM(%s, %s) ' , $ str , $ remstr );
5372+ }
5373+
52305374 /**
52315375 * Translate MySQL date arithmetic to a SQLite DATE() or DATETIME() call.
52325376 *
@@ -5367,6 +5511,29 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53675511 case 'CHAR_LENGTH ' :
53685512 // @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters.
53695513 return 'LENGTH( ' . $ args [0 ] . ') ' ;
5514+ case 'LCASE ' :
5515+ if ( 1 === count ( $ args ) ) {
5516+ return 'LOWER( ' . $ args [0 ] . ') ' ;
5517+ }
5518+ return $ this ->translate_sequence ( $ node ->get_children () );
5519+ case 'UCASE ' :
5520+ if ( 1 === count ( $ args ) ) {
5521+ return 'UPPER( ' . $ args [0 ] . ') ' ;
5522+ }
5523+ return $ this ->translate_sequence ( $ node ->get_children () );
5524+ case 'TRUNCATE ' :
5525+ /*
5526+ * SQLite's TRUNC() takes a single argument, and POW() is only
5527+ * available when SQLite is built with math functions, which
5528+ * older supported builds are not. Delegate to a UDF instead.
5529+ */
5530+ if ( 1 === count ( $ args ) ) {
5531+ $ args [1 ] = '0 ' ;
5532+ }
5533+ if ( 2 === count ( $ args ) ) {
5534+ return sprintf ( 'truncate(%s, %s) ' , $ args [0 ], $ args [1 ] );
5535+ }
5536+ return $ this ->translate_sequence ( $ node ->get_children () );
53705537 case 'CONCAT ' :
53715538 return '( ' . implode ( ' || ' , $ args ) . ') ' ;
53725539 case 'FOUND_ROWS ' :
0 commit comments