Skip to content

Commit

Permalink
Use DatabaseName VO for db names in Table\Partition
Browse files Browse the repository at this point in the history
- Adds DatabaseName support for DatabaseInterface::selectDb method

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 22, 2021
1 parent c2b1896 commit 5faf950
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 39 deletions.
54 changes: 48 additions & 6 deletions libraries/classes/Controllers/Table/PartitionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ public function check(): void
return;
}

[$rows, $query] = $this->model->check($this->db, $this->table, $partitionName);
try {
[$rows, $query] = $this->model->check(new DatabaseName($this->db), $this->table, $partitionName);
} catch (Throwable $e) {
$message = Message::error($e->getMessage());
$this->response->addHTML($message->getDisplay());

return;
}

$message = Generator::getMessage(
__('Your SQL query has been executed successfully.'),
Expand All @@ -92,7 +99,14 @@ public function drop(): void
return;
}

[$result, $query] = $this->model->drop($this->db, $this->table, $partitionName);
try {
[$result, $query] = $this->model->drop(new DatabaseName($this->db), $this->table, $partitionName);
} catch (Throwable $e) {
$message = Message::error($e->getMessage());
$this->response->addHTML($message->getDisplay());

return;
}

if ($result) {
$message = Generator::getMessage(
Expand Down Expand Up @@ -122,7 +136,14 @@ public function optimize(): void
return;
}

[$rows, $query] = $this->model->optimize($this->db, $this->table, $partitionName);
try {
[$rows, $query] = $this->model->optimize(new DatabaseName($this->db), $this->table, $partitionName);
} catch (Throwable $e) {
$message = Message::error($e->getMessage());
$this->response->addHTML($message->getDisplay());

return;
}

$message = Generator::getMessage(
__('Your SQL query has been executed successfully.'),
Expand All @@ -145,7 +166,14 @@ public function rebuild(): void
return;
}

[$result, $query] = $this->model->rebuild($this->db, $this->table, $partitionName);
try {
[$result, $query] = $this->model->rebuild(new DatabaseName($this->db), $this->table, $partitionName);
} catch (Throwable $e) {
$message = Message::error($e->getMessage());
$this->response->addHTML($message->getDisplay());

return;
}

if ($result) {
$message = Generator::getMessage(
Expand Down Expand Up @@ -175,7 +203,14 @@ public function repair(): void
return;
}

[$rows, $query] = $this->model->repair($this->db, $this->table, $partitionName);
try {
[$rows, $query] = $this->model->repair(new DatabaseName($this->db), $this->table, $partitionName);
} catch (Throwable $e) {
$message = Message::error($e->getMessage());
$this->response->addHTML($message->getDisplay());

return;
}

$message = Generator::getMessage(
__('Your SQL query has been executed successfully.'),
Expand All @@ -198,7 +233,14 @@ public function truncate(): void
return;
}

[$result, $query] = $this->model->truncate($this->db, $this->table, $partitionName);
try {
[$result, $query] = $this->model->truncate(new DatabaseName($this->db), $this->table, $partitionName);
} catch (Throwable $e) {
$message = Message::error($e->getMessage());
$this->response->addHTML($message->getDisplay());

return;
}

if ($result) {
$message = Generator::getMessage(
Expand Down
7 changes: 4 additions & 3 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use mysqli_result;
use PhpMyAdmin\Database\DatabaseList;
use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Dbal\DbalInterface;
use PhpMyAdmin\Dbal\DbiExtension;
use PhpMyAdmin\Dbal\DbiMysqli;
Expand Down Expand Up @@ -1984,10 +1985,10 @@ public function connect(int $mode, ?array $server = null, ?int $target = null)
/**
* selects given database
*
* @param string $dbname database name to select
* @param int $link link type
* @param string|DatabaseName $dbname database name to select
* @param int $link link type
*/
public function selectDb(string $dbname, $link = self::CONNECT_USER): bool
public function selectDb($dbname, $link = self::CONNECT_USER): bool
{
if (! isset($this->links[$link])) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions libraries/classes/Dbal/DatabaseName.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpMyAdmin\Dbal;

use InvalidArgumentException;
use Webmozart\Assert\Assert;

/** @psalm-immutable */
Expand All @@ -21,6 +22,7 @@ final class DatabaseName
*/
private $name;

/** @throws InvalidArgumentException */
public function __construct(string $name)
{
Assert::stringNotEmpty($name);
Expand Down
6 changes: 3 additions & 3 deletions libraries/classes/Dbal/DbalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,10 @@ public function connect(int $mode, ?array $server = null, ?int $target = null);
/**
* selects given database
*
* @param string $dbname database name to select
* @param int $link link type
* @param string|DatabaseName $dbname database name to select
* @param int $link link type
*/
public function selectDb(string $dbname, $link = DatabaseInterface::CONNECT_USER): bool;
public function selectDb($dbname, $link = DatabaseInterface::CONNECT_USER): bool;

/**
* returns array of rows with associative and numeric keys from $result
Expand Down
4 changes: 2 additions & 2 deletions libraries/classes/Dbal/DbiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function connect(
/**
* selects given database
*
* @param string $dbname database name to select
* @param object $link connection object
* @param string|DatabaseName $dbname database name to select
* @param object $link connection object
*
* @return bool
*/
Expand Down
6 changes: 3 additions & 3 deletions libraries/classes/Dbal/DbiMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ public function connect($user, $password, array $server)
/**
* selects given database
*
* @param string $databaseName database name to select
* @param mysqli $mysqli the mysqli object
* @param string|DatabaseName $databaseName database name to select
* @param mysqli $mysqli the mysqli object
*
* @return bool
*/
public function selectDb($databaseName, $mysqli)
{
return $mysqli->select_db($databaseName);
return $mysqli->select_db((string) $databaseName);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions libraries/classes/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@ public function executeQueryAndGetQueryResponse(
}

$GLOBALS['reload'] = $this->hasCurrentDbChanged($db);
$this->dbi->selectDb($db);
$this->dbi->selectDb($db ?? '');

[
$result,
Expand Down Expand Up @@ -1851,7 +1851,7 @@ public function executeQueryAndGetQueryResponse(
$htmlOutput = $this->getQueryResponseForResultsReturned(
$result ?? null,
$analyzedSqlResults,
$db,
$db ?? '',
$table,
$sqlData ?? null,
$displayResultsObject,
Expand Down
14 changes: 7 additions & 7 deletions libraries/classes/Table/Partition.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function analyze(DatabaseName $db, string $table, string $partition): arr
Util::backquote($partition)
);

$this->dbi->selectDb($db->getName());
$this->dbi->selectDb($db);
$result = $this->dbi->fetchResult($query);

$rows = [];
Expand All @@ -39,7 +39,7 @@ public function analyze(DatabaseName $db, string $table, string $partition): arr
return [$rows, $query];
}

public function check(string $db, string $table, string $partition): array
public function check(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s CHECK PARTITION %s;',
Expand All @@ -58,7 +58,7 @@ public function check(string $db, string $table, string $partition): array
return [$rows, $query];
}

public function drop(string $db, string $table, string $partition): array
public function drop(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s DROP PARTITION %s;',
Expand All @@ -72,7 +72,7 @@ public function drop(string $db, string $table, string $partition): array
return [(bool) $result, $query];
}

public function optimize(string $db, string $table, string $partition): array
public function optimize(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s OPTIMIZE PARTITION %s;',
Expand All @@ -91,7 +91,7 @@ public function optimize(string $db, string $table, string $partition): array
return [$rows, $query];
}

public function rebuild(string $db, string $table, string $partition): array
public function rebuild(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s REBUILD PARTITION %s;',
Expand All @@ -105,7 +105,7 @@ public function rebuild(string $db, string $table, string $partition): array
return [(bool) $result, $query];
}

public function repair(string $db, string $table, string $partition): array
public function repair(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s REPAIR PARTITION %s;',
Expand All @@ -124,7 +124,7 @@ public function repair(string $db, string $table, string $partition): array
return [$rows, $query];
}

public function truncate(string $db, string $table, string $partition): array
public function truncate(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s TRUNCATE PARTITION %s;',
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1810,11 +1810,6 @@ parameters:
count: 1
path: libraries/classes/Sql.php

-
message: "#^Parameter \\#1 \\$dbname of method PhpMyAdmin\\\\DatabaseInterface\\:\\:selectDb\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: libraries/classes/Sql.php

-
message: "#^Parameter \\#1 \\$messageToShow of method PhpMyAdmin\\\\Sql\\:\\:getMessageForNoRowsReturned\\(\\) expects string, string\\|null given\\.$#"
count: 1
Expand Down Expand Up @@ -1865,11 +1860,6 @@ parameters:
count: 1
path: libraries/classes/Sql.php

-
message: "#^Parameter \\#3 \\$db of method PhpMyAdmin\\\\Sql\\:\\:getQueryResponseForResultsReturned\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: libraries/classes/Sql.php

-
message: "#^Parameter \\#3 \\$isModifyLink of static method PhpMyAdmin\\\\Html\\\\Generator\\:\\:mysqlDie\\(\\) expects bool, string given\\.$#"
count: 1
Expand Down
7 changes: 4 additions & 3 deletions test/classes/Stubs/DbiDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace PhpMyAdmin\Tests\Stubs;

use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Dbal\DbiExtension;
use PhpMyAdmin\FieldMetadata;

Expand Down Expand Up @@ -67,14 +68,14 @@ public function connect(
/**
* selects given database
*
* @param string $dbname name of db to select
* @param object $link mysql link resource
* @param string|DatabaseName $dbname name of db to select
* @param object $link mysql link resource
*
* @return bool
*/
public function selectDb($dbname, $link)
{
$GLOBALS['dummy_db'] = $dbname;
$GLOBALS['dummy_db'] = (string) $dbname;

return true;
}
Expand Down

0 comments on commit 5faf950

Please sign in to comment.