Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing SQL translations #1

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
49 changes: 47 additions & 2 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class WP_SQLite_Translator {
*
* @var array reference to the PHP object
*/
private $results = null;
public $results = null;

/**
* Class variable to check if there is an error.
Expand Down Expand Up @@ -871,12 +871,45 @@ private function execute_mysql_query( $query ) {
}
}

private function get_first_table_name()
{
// Use a regular expression to match the table name following 'CREATE TABLE'
if (preg_match('/CREATE TABLE\s+`([^`]+)`/i', $this->mysql_query, $matches)) {
return $matches[1];
}
throw new Exception( 'Not able to extract the first table name from the query ' . $this->mysql_query );
}

private function get_second_table_name()
{
// Use a regular expression to match the table name following 'LIKE'
if (preg_match('/LIKE\s+`([^`]+)`/i', $this->mysql_query, $matches)) {
return $matches[1];
}
throw new Exception( 'Not able to extract the second table name from the query ' . $this->mysql_query );
}

private function execute_copy_table()
{
$table = $this->get_first_table_name();
$like_table = $this->get_second_table_name();

$this->execute_sqlite_query("CREATE TABLE \"{$table}\" AS SELECT * FROM \"{$like_table}\"");
}

/**
* Executes a MySQL CREATE TABLE query in SQLite.
*
* @throws Exception If the query is not supported.
*/
private function execute_create_table() {

// If the query contains LIKE, handle it differently because this query intents a coping process.
if (strpos($this->mysql_query, 'LIKE') !== false) {
$this->execute_copy_table();
return;
}

$table = $this->parse_create_table();

$definitions = array();
Expand Down Expand Up @@ -3546,7 +3579,9 @@ private function execute_show() {
// [LIKE 'pattern' | WHERE expr]
if ( 'LIKE' === $database_expression->token ) {
$pattern = $this->rewriter->consume()->value;
} elseif ( 'WHERE' === $database_expression->token ) {
} elseif ( null === $database_expression->token ) {
// continue
} elseif ( 'WHERE' === $database_expression->token ) {
// @TODO Support me please.
} elseif ( ';' !== $database_expression->token ) {
throw new Exception( 'Syntax error: Unexpected token ' . $database_expression->token . ' in query ' . $this->mysql_query );
Expand Down Expand Up @@ -3626,6 +3661,16 @@ private function execute_show() {
$this->results = true;
return;

case 'GRANTS':
$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;

default:
throw new Exception( 'Unknown show type: ' . $what );
}
Expand Down