Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4602,6 +4602,9 @@ private function translate_token( WP_MySQL_Token $token ): ?string {
return $value;
case WP_MySQL_Lexer::AUTO_INCREMENT_SYMBOL:
return 'AUTOINCREMENT';
case WP_MySQL_Lexer::NULL_SAFE_EQUAL_OPERATOR:
// The MySQL null-safe equality "<=>" is "IS" in SQLite.
return 'IS';
case WP_MySQL_Lexer::BINARY_SYMBOL:
/*
* "BINARY expr" is translated in "translate_simple_expr_body()".
Expand Down Expand Up @@ -5360,6 +5363,15 @@ private function translate_function_call( WP_Parser_Node $node ): string {
return sprintf( 'CAST(STRFTIME(%s, %s) AS FLOAT)', $format, $date );
}
return sprintf( 'STRFTIME(%s, %s)', $format, $date );
case 'JSON_UNQUOTE':
$argument_nodes = isset( $nodes[1] ) ? $nodes[1]->get_child_nodes() : array();
if ( 1 === count( $argument_nodes ) ) {
$json_extract_args = $this->get_direct_function_call_arguments( $argument_nodes[0], 'JSON_EXTRACT' );
if ( is_array( $json_extract_args ) && count( $json_extract_args ) >= 2 ) {
return sprintf( '_helper_json_unquote_extract(%s)', implode( ', ', $json_extract_args ) );
}
}
return $this->translate_sequence( $node->get_children() );
case 'CHAR_LENGTH':
// @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters.
return 'LENGTH(' . $args[0] . ')';
Expand Down Expand Up @@ -5388,6 +5400,39 @@ private function translate_function_call( WP_Parser_Node $node ): string {
}
}

/**
* Get the arguments to a function call when it is the entire expression.
*
* @param WP_Parser_Node $expression The expression to inspect.
* @param string $function_name The expected function name.
* @return array|null The translated arguments, or NULL when the expression differs.
*/
private function get_direct_function_call_arguments( WP_Parser_Node $expression, string $function_name ): ?array {
$node = $expression;
while ( 'functionCall' !== $node->rule_name ) {
$children = $node->get_children();
if ( 1 !== count( $children ) || ! $children[0] instanceof WP_Parser_Node ) {
return null;
}
$node = $children[0];
}

$function_nodes = $node->get_child_nodes();
if ( ! isset( $function_nodes[0] ) || ! isset( $function_nodes[1] ) ) {
return null;
}
$name = strtoupper( $this->unquote_sqlite_identifier( $this->translate( $function_nodes[0] ) ) );
if ( $function_name !== $name ) {
return null;
}

$args = array();
foreach ( $function_nodes[1]->get_child_nodes() as $argument ) {
$args[] = $this->translate( $argument );
}
return $args;
}

/**
* Get the configured MySQL version in dotted notation.
*
Expand Down
Loading
Loading