Skip to content
Draft
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
120 changes: 108 additions & 12 deletions packages/mysql-on-sqlite/src/sqlite/class-wp-pdo-mysql-on-sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3837,12 +3837,17 @@ private function translate( $node ): ?string {
return $this->translate_sequence( $node->get_children() );
case 'simpleExprBody':
return $this->translate_simple_expr_body( $node );
case 'predicate':
$operations = $node->get_first_child_node( 'predicateOperations' );
$token = null === $operations ? null : $operations->get_first_child_token();
if ( null !== $token && WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) {
return $this->translate_regexp_predicate( $node, $operations );
}
return $this->translate_sequence( $node->get_children() );
case 'predicateOperations':
$token = $node->get_first_child_token();
if ( WP_MySQL_Lexer::LIKE_SYMBOL === $token->id ) {
return $this->translate_like( $node );
} elseif ( WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) {
return $this->translate_regexp_functions( $node );
}
return $this->translate_sequence( $node->get_children() );
case 'runtimeFunctionCall':
Expand Down Expand Up @@ -4478,13 +4483,16 @@ private function translate_like( WP_Parser_Node $node ): string {
/**
* Translate MySQL REGEXP expression to SQLite.
*
* @param WP_Parser_Node $node The "predicateOperations" AST node.
* @param WP_Parser_Node $node The "predicate" AST node.
* @param WP_Parser_Node $operations The "predicateOperations" AST node.
* @return string The translated value.
* @throws WP_SQLite_Driver_Exception When the translation fails.
*/
private function translate_regexp_functions( WP_Parser_Node $node ): string {
$tokens = $node->get_descendant_tokens();
private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Node $operations ): string {
$tokens = $operations->get_descendant_tokens();
$is_binary = isset( $tokens[1] ) && WP_MySQL_Lexer::BINARY_SYMBOL === $tokens[1]->id;
$subject = $this->translate_regexp_string_arg( $node->get_first_child_node() );
$pattern = $this->translate_regexp_string_arg( $operations->get_first_child_node() );

/*
* If the query says REGEXP BINARY, the comparison is byte-by-byte
Expand All @@ -4500,9 +4508,11 @@ private function translate_regexp_functions( WP_Parser_Node $node ): string {
* regular expressions anyway.
*/
if ( true === $is_binary ) {
return 'REGEXP CHAR(0) || ' . $this->translate( $node->get_first_child_node() );
$pattern = 'CHAR(0) || ' . $pattern;
}
return 'REGEXP ' . $this->translate( $node->get_first_child_node() );

$operator = null === $node->get_first_child_node( 'notRule' ) ? 'REGEXP' : 'NOT REGEXP';
return "$subject $operator $pattern";
}

/**
Expand Down Expand Up @@ -4570,11 +4580,10 @@ private function translate_function_call( WP_Parser_Node $node ): string {
$this->unquote_sqlite_identifier( $this->translate( $nodes[0] ) )
);

$args = array();
if ( isset( $nodes[1] ) ) {
foreach ( $nodes[1]->get_child_nodes() as $child ) {
$args[] = $this->translate( $child );
}
$arg_nodes = isset( $nodes[1] ) ? $nodes[1]->get_child_nodes() : array();
$args = array();
foreach ( $arg_nodes as $child ) {
$args[] = $this->translate( $child );
}

switch ( $name ) {
Expand Down Expand Up @@ -4668,11 +4677,98 @@ private function translate_function_call( WP_Parser_Node $node ): string {
substr( $version, 3, 2 )
);
return $this->quote_sqlite_value( $value );
case 'REGEXP_LIKE':
case 'REGEXP_REPLACE':
case 'REGEXP_SUBSTR':
case 'REGEXP_INSTR':
return sprintf(
'%s(%s)',
$name,
implode( ', ', $this->translate_regexp_function_args( $name, $arg_nodes ) )
);
default:
return $this->translate_sequence( $node->get_children() );
}
}

/**
* Translate REGEXP function arguments while preserving numeric literal text.
*
* @param string $name Function name.
* @param array $arg_nodes Function argument nodes.
*
* @return array Translated arguments.
*/
private function translate_regexp_function_args( string $name, array $arg_nodes ): array {
$string_arg_indexes = array(
'REGEXP_LIKE' => array( 0, 1, 2 ),
'REGEXP_REPLACE' => array( 0, 1, 2, 5 ),
'REGEXP_SUBSTR' => array( 0, 1, 4 ),
'REGEXP_INSTR' => array( 0, 1, 5 ),
);

$args = array();
foreach ( $arg_nodes as $index => $arg_node ) {
if ( in_array( $index, $string_arg_indexes[ $name ], true ) ) {
$args[] = $this->translate_regexp_string_arg( $arg_node );
} else {
$args[] = $this->translate( $arg_node );
}
}
return $args;
}

/**
* Translate a REGEXP string-domain argument while preserving numeric literal text.
*
* @param WP_Parser_Node $node Function argument or operator operand.
*
* @return string Translated argument.
*/
private function translate_regexp_string_arg( WP_Parser_Node $node ): string {
$numeric_literal = $this->get_regexp_numeric_literal_string( $node );
return null === $numeric_literal
? $this->translate( $node )
: $this->quote_sqlite_value( $numeric_literal );
}

/**
* Read an exact decimal or floating-point literal from an expression node.
*
* @param WP_Parser_Node $node Expression node.
*
* @return string|null Literal text, or NULL for a non-literal expression.
*/
private function get_regexp_numeric_literal_string( WP_Parser_Node $node ): ?string {
$number = null;
$sign = '';
foreach ( $node->get_descendant_tokens() as $token ) {
$value = $token->get_value();
if ( '(' === $value || ')' === $value ) {
continue;
}
if ( WP_MySQL_Lexer::BINARY_SYMBOL === $token->id ) {
continue;
}
if ( ( '+' === $value || '-' === $value ) && null === $number && '' === $sign ) {
$sign = '-' === $value ? '-' : '';
continue;
}
if (
null === $number
&& (
WP_MySQL_Lexer::DECIMAL_NUMBER === $token->id
|| WP_MySQL_Lexer::FLOAT_NUMBER === $token->id
)
) {
$number = $value;
continue;
}
return null;
}
return null === $number ? null : $sign . $number;
}

/**
* Translate a MySQL datetime literal to SQLite.
*
Expand Down
Loading
Loading