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
21 changes: 21 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 @@ -5360,6 +5360,12 @@ 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 'SUBSTRING_INDEX':
$argument_nodes = isset( $nodes[1] ) ? $nodes[1]->get_child_nodes() : array();
if ( isset( $argument_nodes[0] ) && $this->contains_binary_token( $argument_nodes[0] ) ) {
return sprintf( '_helper_substr_index_binary(%s)', implode( ', ', $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 +5394,21 @@ private function translate_function_call( WP_Parser_Node $node ): string {
}
}

/**
* Check whether an expression contains the MySQL BINARY operator or type.
*
* @param WP_Parser_Node $node The expression to inspect.
* @return bool Whether the expression contains a BINARY token.
*/
private function contains_binary_token( WP_Parser_Node $node ): bool {
foreach ( $node->get_descendants() as $descendant ) {
if ( $descendant instanceof WP_Parser_Token && WP_MySQL_Lexer::BINARY_SYMBOL === $descendant->id ) {
return true;
}
}
return false;
}

/**
* Get the configured MySQL version in dotted notation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public static function register_for( $pdo ): self {
'utc_timestamp' => 'utc_timestamp',
'version' => 'version',
'reverse' => 'reverse',
'substring_index' => 'substring_index',

// Internal helper functions.
'_helper_substr_index_binary' => 'substring_index_binary',
'_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern',
);

Expand Down Expand Up @@ -927,6 +929,115 @@ public function reverse( $str ) {
return strrev( $str );
}

/**
* Method to emulate MySQL SUBSTRING_INDEX() function.
*
* Returns the substring of a string before a given number of
* occurrences of a delimiter. A positive count searches from the left
* and a negative count searches from the right. A count of zero, an
* empty delimiter, or fewer delimiters than requested (in the searched
* direction) yields defined fallbacks matching MySQL. Any NULL input
* yields NULL.
*
* @param string|null $str The string to search.
* @param string|null $delim The delimiter to search for.
* @param mixed $count How many delimiters to skip; negative counts from the right.
*
* @return string|null The substring, or NULL on NULL input.
*/
public function substring_index( $str, $delim, $count ) {
return $this->substring_index_with_binary_semantics( $str, $delim, $count, false );
}

/**
* Internal callback for SUBSTRING_INDEX() when its first argument is binary.
*
* @param string|null $str The string to search.
* @param string|null $delim The delimiter to search for.
* @param mixed $count How many delimiters to skip; negative counts from the right.
*
* @return string|null The substring, or NULL on NULL input.
*/
public function substring_index_binary( $str, $delim, $count ) {
return $this->substring_index_with_binary_semantics( $str, $delim, $count, true );
}

/**
* Find a SUBSTRING_INDEX() result using the selected collation semantics.
*
* @param string|null $str The string to search.
* @param string|null $delim The delimiter to search for.
* @param mixed $count How many delimiters to skip; negative counts from the right.
* @param bool $binary Whether the first argument uses binary semantics.
*
* @return string|null The substring, or NULL on NULL input.
*/
private function substring_index_with_binary_semantics( $str, $delim, $count, bool $binary ): ?string {
if ( null === $str || null === $delim || null === $count ) {
return null;
}
$str = (string) $str;
$delim = (string) $delim;
$count = (int) round( (float) $count );
if ( '' === $delim || 0 === $count ) {
return '';
}
$delim_len = strlen( $delim );
if ( $count > 0 ) {
$offset = 0;
for ( $i = 0; $i < $count; $i++ ) {
$pos = strpos( $str, $delim, $offset );
if ( false === $pos ) {
return $str;
}
$offset = $pos + $delim_len;
}
return substr( $str, 0, $offset - $delim_len );
}

if ( $binary ) {
$length = strlen( $str );
$end = $length;
$count = -$count;
for ( $i = 0; $i < $count; $i++ ) {
if ( $end < $delim_len ) {
return $str;
}
$offset = $end - $delim_len - $length;
$pos = strrpos( $str, $delim, $offset );
if ( false === $pos ) {
return $str;
}
if ( $i === $count - 1 ) {
return substr( $str, $pos + $delim_len );
}
$end = $pos;
}
return $str;
}

$delimiter_count = 0;
$offset = 0;
while ( false !== ( $pos = strpos( $str, $delim, $offset ) ) ) {
++$delimiter_count;
$offset = $pos + $delim_len;
}
$count += $delimiter_count + 1;
if ( $count <= 0 ) {
return $str;
}

$offset = 0;
for ( $i = 0; $i < $count; $i++ ) {
$pos = strpos( $str, $delim, $offset );
if ( false === $pos ) {
return $str;
}
$offset = $pos + $delim_len;
}
return substr( $str, $offset );
}

/**
* A helper to convert a LIKE pattern to a GLOB pattern for "LIKE BINARY" support.

Expand Down
51 changes: 51 additions & 0 deletions packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,57 @@ public function testSubstringSimple() {
$this->assertEquals( 0, count( $actual ) );
}

public function testSubstringIndex() {
$query = <<<'QUERY'
SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2) AS two_from_left,
SUBSTRING_INDEX('www.mysql.com', '.', -2) AS two_from_right,
SUBSTRING_INDEX('www.mysql.com', '.', 7) AS beyond_last,
SUBSTRING_INDEX('nosuchdelim', '.', 3) AS no_match,
SUBSTRING_INDEX('a--b--c', '--', -1) AS multi_char,
SUBSTRING_INDEX('a.b.c', '.', 1.6) AS fractional_positive,
SUBSTRING_INDEX('a.b.c', '.', -1.6) AS fractional_negative,
SUBSTRING_INDEX('aaaaa', 'aa', -2) AS overlapping_multibyte,
SUBSTRING_INDEX(BINARY 'aaaaa', 'aa', -2) AS overlapping_binary,
SUBSTRING_INDEX(NULL, '.', 1) AS null_string,
SUBSTRING_INDEX('a.b', NULL, 1) AS null_delimiter,
SUBSTRING_INDEX('a.b', '.', NULL) AS null_count,
SUBSTRING_INDEX('a.b', '.', 0) AS zero_count,
SUBSTRING_INDEX('a.b', '', 1) AS empty_delimiter
QUERY;

$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 'www.mysql', $actual[0]->two_from_left );
$this->assertEquals( 'mysql.com', $actual[0]->two_from_right );
$this->assertEquals( 'www.mysql.com', $actual[0]->beyond_last );
$this->assertEquals( 'nosuchdelim', $actual[0]->no_match );
$this->assertEquals( 'c', $actual[0]->multi_char );
$this->assertSame( 'a.b', $actual[0]->fractional_positive );
$this->assertSame( 'b.c', $actual[0]->fractional_negative );
$this->assertSame( 'aaa', $actual[0]->overlapping_multibyte );
$this->assertSame( 'aa', $actual[0]->overlapping_binary );
$this->assertNull( $actual[0]->null_string );
$this->assertNull( $actual[0]->null_delimiter );
$this->assertNull( $actual[0]->null_count );
$this->assertSame( '', $actual[0]->zero_count );
$this->assertSame( '', $actual[0]->empty_delimiter );
}

public function testSubstringIndexDomainExtraction() {
$this->assertQuery(
"INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('_substring_index_probe', 'https://example.com/a?x=1', 'no');"
);
$query = <<<'QUERY'
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(option_value, '/', 3), '://', -1), '/', 1) AS domain
FROM wp_options WHERE option_name = '_substring_index_probe'
QUERY;

$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 1, count( $actual ) );
$this->assertEquals( 'example.com', $actual[0]->domain );
}

public function testCharLengthComplex() {
$query = <<<'QUERY'
SELECT option_name,
Expand Down
Loading