Skip to content

Commit a46b6a4

Browse files
authored
Support SHOW GRANTS FOR
A naive implementation of SHOW GRANTS FOR It returns all the privileges one can have and assumes a root user. https://dev.mysql.com/doc/refman/8.0/en/show-grants.html
2 parents 1b59f5f + a2e4758 commit a46b6a4

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

tests/WP_SQLite_Translator_Tests.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,42 @@ public function testSelectFromDual() {
146146
$this->assertEquals( 1, $result[0]->output );
147147
}
148148

149+
public function testLeftFunction1Char() {
150+
$result = $this->assertQuery(
151+
'SELECT LEFT("abc", 1) as output'
152+
);
153+
$this->assertEquals( "a", $result[0]->output );
154+
}
155+
156+
public function testLeftFunction5Chars() {
157+
$result = $this->assertQuery(
158+
'SELECT LEFT("Lorem ipsum", 5) as output'
159+
);
160+
$this->assertEquals( "Lorem", $result[0]->output );
161+
}
162+
163+
public function testLeftFunctionNullString() {
164+
$result = $this->assertQuery(
165+
'SELECT LEFT(NULL, 5) as output'
166+
);
167+
$this->assertEquals( null, $result[0]->output );
168+
}
169+
170+
public function testLeftFunctionNullLength() {
171+
$result = $this->assertQuery(
172+
'SELECT LEFT("Test", NULL) as output'
173+
);
174+
$this->assertEquals( null, $result[0]->output );
175+
}
176+
149177
public function testInsertSelectFromDual() {
150178
$result = $this->assertQuery(
151179
'INSERT INTO _options (option_name, option_value) SELECT "A", "b" FROM DUAL WHERE ( SELECT NULL FROM DUAL ) IS NULL'
152180
);
153181
$this->assertEquals( 1, $result );
154182
}
155183

184+
156185
public function testCreateTemporaryTable() {
157186
$this->assertQuery(
158187
"CREATE TEMPORARY TABLE _tmp_table (
@@ -1244,6 +1273,17 @@ public function testAlterTableAddColumnChangesMySQLDataType() {
12441273
$fields
12451274
);
12461275
}
1276+
public function testShowGrantsFor() {
1277+
$result = $this->assertQuery( 'SHOW GRANTS FOR current_user();' );
1278+
$this->assertEquals(
1279+
$result,
1280+
array(
1281+
(object) array(
1282+
'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'
1283+
)
1284+
)
1285+
);
1286+
}
12471287

12481288
public function testShowIndex() {
12491289
$result = $this->assertQuery(

wp-includes/sqlite/class-wp-sqlite-translator.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,7 @@ private function translate_expression( $token ) {
18241824
|| $this->capture_group_by( $token )
18251825
|| $this->translate_ungrouped_having( $token )
18261826
|| $this->translate_like_escape( $token )
1827+
|| $this->translate_left_function( $token )
18271828
);
18281829
}
18291830

@@ -2025,6 +2026,41 @@ private function translate_date_add_sub( $token ) {
20252026
return true;
20262027
}
20272028

2029+
/**
2030+
* Translate the LEFT() function.
2031+
*
2032+
* > Returns the leftmost len characters from the string str, or NULL if any argument is NULL.
2033+
*
2034+
* https://dev.mysql.com/doc/refman/8.3/en/string-functions.html#function_left
2035+
*
2036+
* @param WP_SQLite_Token $token The token to translate.
2037+
*
2038+
* @return bool
2039+
*/
2040+
private function translate_left_function( $token ) {
2041+
if (
2042+
! $token->matches(
2043+
WP_SQLite_Token::TYPE_KEYWORD,
2044+
WP_SQLite_Token::FLAG_KEYWORD_FUNCTION,
2045+
array( 'LEFT' )
2046+
)
2047+
) {
2048+
return false;
2049+
}
2050+
2051+
$this->rewriter->skip();
2052+
$this->rewriter->add( new WP_SQLite_Token( 'SUBSTRING', WP_SQLite_Token::TYPE_KEYWORD, WP_SQLite_Token::FLAG_KEYWORD_FUNCTION ) );
2053+
$this->rewriter->consume(
2054+
array(
2055+
'type' => WP_SQLite_Token::TYPE_OPERATOR,
2056+
'value' => ',',
2057+
)
2058+
);
2059+
$this->rewriter->add( new WP_SQLite_Token( 1, WP_SQLite_Token::TYPE_NUMBER ) );
2060+
$this->rewriter->add( new WP_SQLite_Token( ',', WP_SQLite_Token::TYPE_OPERATOR ) );
2061+
return true;
2062+
}
2063+
20282064
/**
20292065
* Convert function aliases.
20302066
*
@@ -3026,6 +3062,14 @@ private function execute_show() {
30263062
$this->results = true;
30273063
return;
30283064

3065+
case 'GRANTS FOR':
3066+
$this->set_results_from_fetched_data( array(
3067+
(object) array(
3068+
'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'
3069+
)
3070+
) );
3071+
return;
3072+
30293073
case 'FULL COLUMNS':
30303074
$this->rewriter->consume();
30313075
// Fall through.

0 commit comments

Comments
 (0)