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

public function testLeftFunction1Char() {
$result = $this->assertQuery(
'SELECT LEFT("abc", 1) as output'
);
$this->assertEquals( "a", $result[0]->output );
}

public function testLeftFunction5Chars() {
$result = $this->assertQuery(
'SELECT LEFT("Lorem ipsum", 5) as output'
);
$this->assertEquals( "Lorem", $result[0]->output );
}

public function testLeftFunctionNullString() {
$result = $this->assertQuery(
'SELECT LEFT(NULL, 5) as output'
);
$this->assertEquals( null, $result[0]->output );
}

public function testLeftFunctionNullLength() {
$result = $this->assertQuery(
'SELECT LEFT("Test", NULL) as output'
);
$this->assertEquals( null, $result[0]->output );
}

public function testInsertSelectFromDual() {
$result = $this->assertQuery(
'INSERT INTO _options (option_name, option_value) SELECT "A", "b" FROM DUAL WHERE ( SELECT NULL FROM DUAL ) IS NULL'
);
$this->assertEquals( 1, $result );
}


public function testCreateTemporaryTable() {
$this->assertQuery(
"CREATE TEMPORARY TABLE _tmp_table (
Expand Down Expand Up @@ -1244,6 +1273,17 @@ public function testAlterTableAddColumnChangesMySQLDataType() {
$fields
);
}
public function testShowGrantsFor() {
$result = $this->assertQuery( 'SHOW GRANTS FOR current_user();' );
$this->assertEquals(
$result,
array(
(object) array(
'Grants for root@localhost' => 'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION'
)
)
);
}

public function testShowIndex() {
$result = $this->assertQuery(
Expand Down
44 changes: 44 additions & 0 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,7 @@ private function translate_expression( $token ) {
|| $this->capture_group_by( $token )
|| $this->translate_ungrouped_having( $token )
|| $this->translate_like_escape( $token )
|| $this->translate_left_function( $token )
);
}

Expand Down Expand Up @@ -2025,6 +2026,41 @@ private function translate_date_add_sub( $token ) {
return true;
}

/**
* Translate the LEFT() function.
*
* > Returns the leftmost len characters from the string str, or NULL if any argument is NULL.
*
* https://dev.mysql.com/doc/refman/8.3/en/string-functions.html#function_left
*
* @param WP_SQLite_Token $token The token to translate.
*
* @return bool
*/
private function translate_left_function( $token ) {
if (
! $token->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_FUNCTION,
array( 'LEFT' )
)
) {
return false;
}

$this->rewriter->skip();
$this->rewriter->add( new WP_SQLite_Token( 'SUBSTRING', WP_SQLite_Token::TYPE_KEYWORD, WP_SQLite_Token::FLAG_KEYWORD_FUNCTION ) );
$this->rewriter->consume(
array(
'type' => WP_SQLite_Token::TYPE_OPERATOR,
'value' => ',',
)
);
$this->rewriter->add( new WP_SQLite_Token( 1, WP_SQLite_Token::TYPE_NUMBER ) );
$this->rewriter->add( new WP_SQLite_Token( ',', WP_SQLite_Token::TYPE_OPERATOR ) );
return true;
}

/**
* Convert function aliases.
*
Expand Down Expand Up @@ -3026,6 +3062,14 @@ private function execute_show() {
$this->results = true;
return;

case 'GRANTS FOR':
$this->set_results_from_fetched_data( array(
(object) array(
'Grants for root@localhost' => 'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION'
)
) );
return;

case 'FULL COLUMNS':
$this->rewriter->consume();
// Fall through.
Expand Down