Skip to content

Commit a1d28f0

Browse files
committed
feat(sqlite): sqlite-string-fns
1 parent 70bdaf3 commit a1d28f0

4 files changed

Lines changed: 1340 additions & 42 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,140 @@ 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::POSITION_SYMBOL:
5229+
// POSITION(substr IN str) is LOCATE() with swapped arguments.
5230+
$nodes = $node->get_child_nodes();
5231+
return sprintf(
5232+
'locate(%s, %s)',
5233+
$this->translate( $nodes[0] ),
5234+
$this->translate( $nodes[1] )
5235+
);
5236+
case WP_MySQL_Lexer::INSERT_SYMBOL:
5237+
// The string function INSERT(). Emitted under a different
5238+
// name because SQLite will not accept INSERT as a function.
5239+
$nodes = $node->get_child_nodes();
5240+
return sprintf(
5241+
'insert_str(%s, %s, %s, %s)',
5242+
$this->translate( $nodes[0] ),
5243+
$this->translate( $nodes[1] ),
5244+
$this->translate( $nodes[2] ),
5245+
$this->translate( $nodes[3] )
5246+
);
5247+
case WP_MySQL_Lexer::EXTRACT_SYMBOL:
5248+
$nodes = $node->get_child_nodes();
5249+
return $this->translate_extract_function( $nodes[0], $nodes[1] );
52255250
default:
52265251
return $this->translate_sequence( $node->get_children() );
52275252
}
52285253
}
52295254

5255+
/**
5256+
* Translate a MySQL EXTRACT(part FROM date) call to SQLite.
5257+
*
5258+
* @param WP_Parser_Node $part_node The "interval" AST node holding the unit.
5259+
* @param WP_Parser_Node $date_node The date expression AST node.
5260+
* @return string The translated value.
5261+
* @throws WP_MySQL_On_SQLite_Exception When the translation fails.
5262+
*/
5263+
private function translate_extract_function( WP_Parser_Node $part_node, WP_Parser_Node $date_node ): string {
5264+
$unit_token = $part_node->get_first_descendant_token();
5265+
$date = $this->translate( $date_node );
5266+
5267+
switch ( $unit_token->id ) {
5268+
case WP_MySQL_Lexer::YEAR_SYMBOL:
5269+
return sprintf( 'year(%s)', $date );
5270+
case WP_MySQL_Lexer::QUARTER_SYMBOL:
5271+
return sprintf(
5272+
'CAST(((CAST(STRFTIME(%s, %s) AS INTEGER) + 2) / 3) AS INTEGER)',
5273+
$this->quote_sqlite_value( '%m' ),
5274+
$date
5275+
);
5276+
case WP_MySQL_Lexer::MONTH_SYMBOL:
5277+
return sprintf( 'month(%s)', $date );
5278+
case WP_MySQL_Lexer::WEEK_SYMBOL:
5279+
return sprintf( 'week(%s, 0)', $date );
5280+
case WP_MySQL_Lexer::DAY_SYMBOL:
5281+
return sprintf( 'day(%s)', $date );
5282+
case WP_MySQL_Lexer::HOUR_SYMBOL:
5283+
return sprintf( 'hour(%s)', $date );
5284+
case WP_MySQL_Lexer::MINUTE_SYMBOL:
5285+
return sprintf( 'minute(%s)', $date );
5286+
case WP_MySQL_Lexer::SECOND_SYMBOL:
5287+
return sprintf( 'second(%s)', $date );
5288+
case WP_MySQL_Lexer::MICROSECOND_SYMBOL:
5289+
return sprintf(
5290+
'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',
5291+
$date,
5292+
$this->quote_sqlite_value( '.' ),
5293+
$this->quote_sqlite_value( '000000' )
5294+
);
5295+
default:
5296+
throw $this->new_not_supported_exception(
5297+
sprintf( 'EXTRACT unit: %s', $unit_token->get_value() )
5298+
);
5299+
}
5300+
}
5301+
5302+
/**
5303+
* Translate a MySQL TRIM() call to SQLite.
5304+
*
5305+
* Handles the extended TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)
5306+
* syntax. The plain TRIM(str) and TRIM(remstr FROM str) forms are covered
5307+
* by the same mapping.
5308+
*
5309+
* @param WP_Parser_Node $node The "trimFunction" AST node.
5310+
* @return string The translated value.
5311+
* @throws WP_MySQL_On_SQLite_Exception When the translation fails.
5312+
*/
5313+
private function translate_trim_function( WP_Parser_Node $node ): string {
5314+
$spec = null;
5315+
$exprs = array();
5316+
foreach ( $node->get_children() as $child ) {
5317+
if ( $child instanceof WP_MySQL_Token ) {
5318+
if ( WP_MySQL_Lexer::LEADING_SYMBOL === $child->id ) {
5319+
$spec = 'LEADING';
5320+
} elseif ( WP_MySQL_Lexer::TRAILING_SYMBOL === $child->id ) {
5321+
$spec = 'TRAILING';
5322+
} elseif ( WP_MySQL_Lexer::BOTH_SYMBOL === $child->id ) {
5323+
$spec = 'BOTH';
5324+
}
5325+
continue;
5326+
}
5327+
if ( $child instanceof WP_Parser_Node ) {
5328+
$exprs[] = $this->translate( $child );
5329+
}
5330+
}
5331+
5332+
if ( 0 === count( $exprs ) || 2 < count( $exprs ) ) {
5333+
throw $this->new_invalid_input_exception();
5334+
}
5335+
5336+
if ( 2 === count( $exprs ) ) {
5337+
$str = $exprs[1];
5338+
$remstr = $exprs[0];
5339+
} else {
5340+
$str = $exprs[0];
5341+
$remstr = null;
5342+
}
5343+
5344+
if ( 'LEADING' === $spec ) {
5345+
if ( null === $remstr ) {
5346+
return sprintf( 'LTRIM(%s)', $str );
5347+
}
5348+
return sprintf( 'LTRIM(%s, %s)', $str, $remstr );
5349+
}
5350+
if ( 'TRAILING' === $spec ) {
5351+
if ( null === $remstr ) {
5352+
return sprintf( 'RTRIM(%s)', $str );
5353+
}
5354+
return sprintf( 'RTRIM(%s, %s)', $str, $remstr );
5355+
}
5356+
if ( null === $remstr ) {
5357+
return sprintf( 'TRIM(%s)', $str );
5358+
}
5359+
return sprintf( 'TRIM(%s, %s)', $str, $remstr );
5360+
}
5361+
52305362
/**
52315363
* Translate MySQL date arithmetic to a SQLite DATE() or DATETIME() call.
52325364
*
@@ -5367,6 +5499,31 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53675499
case 'CHAR_LENGTH':
53685500
// @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters.
53695501
return 'LENGTH(' . $args[0] . ')';
5502+
case 'LCASE':
5503+
if ( 1 === count( $args ) ) {
5504+
return 'LOWER(' . $args[0] . ')';
5505+
}
5506+
return $this->translate_sequence( $node->get_children() );
5507+
case 'UCASE':
5508+
if ( 1 === count( $args ) ) {
5509+
return 'UPPER(' . $args[0] . ')';
5510+
}
5511+
return $this->translate_sequence( $node->get_children() );
5512+
case 'TRUNCATE':
5513+
// SQLite TRUNC() takes a single argument, so emulate the
5514+
// decimals argument with a power-of-ten shift. CAST() in
5515+
// SQLite truncates toward zero, like MySQL TRUNCATE().
5516+
if ( 1 === count( $args ) ) {
5517+
$args[1] = '0';
5518+
}
5519+
if ( 2 === count( $args ) ) {
5520+
return sprintf(
5521+
'CASE WHEN (%2$s) >= 0 THEN CAST((%1$s) * POW(10, (%2$s)) AS INTEGER) / POW(10, (%2$s)) ELSE CAST((%1$s) / POW(10, -(%2$s)) AS INTEGER) * POW(10, -(%2$s)) END',
5522+
$args[0],
5523+
$args[1]
5524+
);
5525+
}
5526+
return $this->translate_sequence( $node->get_children() );
53705527
case 'CONCAT':
53715528
return '(' . implode( ' || ', $args ) . ')';
53725529
case 'FOUND_ROWS':

0 commit comments

Comments
 (0)