Skip to content

Commit

Permalink
Merge pull request #16910 from mauriciofauth/database-name-vo
Browse files Browse the repository at this point in the history
Add DatabaseName value object
  • Loading branch information
MauricioFauth committed May 21, 2021
2 parents 4cc034e + 4d09e19 commit b1f7cba
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -58,6 +58,7 @@
"symfony/polyfill-ctype": "^1.17.0",
"symfony/polyfill-mbstring": "^1.17.0",
"twig/twig": "^3.0.1",
"webmozart/assert": "^1.10",
"williamdes/mariadb-mysql-kbs": "^1.2"
},
"conflict": {
Expand Down
12 changes: 11 additions & 1 deletion libraries/classes/Controllers/Table/PartitionController.php
Expand Up @@ -4,10 +4,13 @@

namespace PhpMyAdmin\Controllers\Table;

use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use PhpMyAdmin\Table\Partition;
use PhpMyAdmin\Template;
use Throwable;

use function strlen;

Expand Down Expand Up @@ -36,7 +39,14 @@ public function analyze(): void
return;
}

[$rows, $query] = $this->model->analyze($this->db, $this->table, $partitionName);
try {
[$rows, $query] = $this->model->analyze(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 Down
41 changes: 41 additions & 0 deletions libraries/classes/Dbal/DatabaseName.php
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Dbal;

use Webmozart\Assert\Assert;

/** @psalm-immutable */
final class DatabaseName
{
/**
* @see https://dev.mysql.com/doc/refman/en/identifier-length.html
* @see https://mariadb.com/kb/en/identifier-names/#maximum-length
*/
private const MAX_LENGTH = 64;

/**
* @var string
* @psalm-var non-empty-string
*/
private $name;

public function __construct(string $name)
{
Assert::stringNotEmpty($name);
Assert::maxLength($name, self::MAX_LENGTH);
Assert::notEndsWith($name, ' ');
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}

public function __toString(): string
{
return $this->name;
}
}
5 changes: 3 additions & 2 deletions libraries/classes/Table/Partition.php
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\Table;

use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Util;

use function sprintf;
Expand All @@ -19,15 +20,15 @@ public function __construct(DatabaseInterface $dbi)
$this->dbi = $dbi;
}

public function analyze(string $db, string $table, string $partition): array
public function analyze(DatabaseName $db, string $table, string $partition): array
{
$query = sprintf(
'ALTER TABLE %s ANALYZE PARTITION %s;',
Util::backquote($table),
Util::backquote($partition)
);

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

$rows = [];
Expand Down
45 changes: 45 additions & 0 deletions test/classes/Dbal/DatabaseNameTest.php
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Dbal;

use InvalidArgumentException;
use PhpMyAdmin\Dbal\DatabaseName;
use PHPUnit\Framework\TestCase;

use function str_repeat;

class DatabaseNameTest extends TestCase
{
public function testEmptyName(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected a different value than "".');
new DatabaseName('');
}

public function testNameWithTrailingWhitespace(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected a value not to end with " ". Got: "a "');
new DatabaseName('a ');
}

public function testLongName(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Expected a value to contain at most 64 characters. Got: '
. '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'
);
new DatabaseName(str_repeat('a', 65));
}

public function testValidName(): void
{
$name = new DatabaseName('name');
$this->assertEquals('name', $name->getName());
$this->assertEquals('name', (string) $name);
}
}

0 comments on commit b1f7cba

Please sign in to comment.