Skip to content
Merged
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
16 changes: 16 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ public function testSelectFromDual() {
$this->assertEquals( 1, $result[0]->output );
}

public function testSelectIndexHintForce() {
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" );
$result = $this->assertQuery(
'SELECT 1 as output FROM _options FORCE INDEX (PRIMARY, post_parent) WHERE 1=1'
);
$this->assertEquals( 1, $result[0]->output );
}

public function testSelectIndexHintUseGroup() {
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" );
$result = $this->assertQuery(
'SELECT 1 as output FROM _options USE KEY FOR GROUP BY (PRIMARY, post_parent) WHERE 1=1'
);
$this->assertEquals( 1, $result[0]->output );
}

public function testLeftFunction1Char() {
$result = $this->assertQuery(
'SELECT LEFT("abc", 1) as output'
Expand Down
69 changes: 69 additions & 0 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,10 @@ private function execute_select() {
continue;
}

if ( $this->skip_index_hint() ) {
continue;
}

$this->rewriter->consume();
}
$this->rewriter->consume_all();
Expand Down Expand Up @@ -1476,6 +1480,71 @@ private function execute_select() {
}
}

/**
* Ignores the FORCE INDEX clause
*
*
* USE {INDEX|KEY}
* [FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
* | {IGNORE|FORCE} {INDEX|KEY}
* [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
* @see https://dev.mysql.com/doc/refman/8.3/en/index-hints.html
* @return bool
*/
private function skip_index_hint() {
$force = $this->rewriter->peek();
if ( ! $force || ! $force->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_RESERVED,
array( 'USE', 'FORCE', 'IGNORE' )
) ) {
return false;
}

$index = $this->rewriter->peek_nth( 2 );
if ( ! $index || ! $index->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_RESERVED,
array( 'INDEX', 'KEY' )
) ) {
return false;
}

$this->rewriter->skip(); // USE, FORCE, IGNORE
$this->rewriter->skip(); // INDEX, KEY

$maybe_for = $this->rewriter->peek();
if ( $maybe_for && $maybe_for->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_RESERVED,
array( 'FOR' )
) ) {
$this->rewriter->skip(); // FOR

$token = $this->rewriter->peek();
if ( $token && $token->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_RESERVED,
array( 'JOIN', 'ORDER', 'GROUP' )
) ) {
$this->rewriter->skip(); // JOIN, ORDER, GROUP
if ( 'BY' === strtoupper( $this->rewriter->peek()->value ) ) {
$this->rewriter->skip(); // BY
}
}
}

// Skip everything until the closing parenthesis.
$this->rewriter->skip(
array(
'type' => WP_SQLite_Token::TYPE_OPERATOR,
'value' => ')',
)
);

return true;
}

/**
* Executes a TRUNCATE statement.
*/
Expand Down