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

Handle database with escapable identifiers in name #717

Merged
merged 3 commits into from
May 9, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function connect(): void
*/
public function setConnection(Connection $connection): AdapterInterface
{
$connection->execute(sprintf('USE %s', $this->getOption('database')));
$connection->execute(sprintf('USE %s', $this->quoteTableName($this->getOption('database'))));

return parent::setConnection($connection);
}
Expand Down Expand Up @@ -1242,15 +1242,15 @@ public function createDatabase(string $name, array $options = []): void

if (isset($options['collation'])) {
$this->execute(sprintf(
'CREATE DATABASE `%s` DEFAULT CHARACTER SET `%s` COLLATE `%s`',
$name,
'CREATE DATABASE %s DEFAULT CHARACTER SET `%s` COLLATE `%s`',
$this->quoteTableName($name),
$charset,
$options['collation']
));
} else {
$this->execute(sprintf('CREATE DATABASE `%s` DEFAULT CHARACTER SET `%s`', $name, $charset));
$this->execute(sprintf('CREATE DATABASE %s DEFAULT CHARACTER SET `%s`', $this->quoteTableName($name), $charset));
}
$this->execute(sprintf('USE %s', $name));
$this->execute(sprintf('USE %s', $this->quoteTableName($name)));
}

/**
Expand Down Expand Up @@ -1279,7 +1279,7 @@ public function hasDatabase(string $name): bool
*/
public function dropDatabase(string $name): void
{
$this->execute(sprintf('DROP DATABASE IF EXISTS `%s`', $name));
$this->execute(sprintf('DROP DATABASE IF EXISTS %s', $this->quoteTableName($name)));
$this->createdTables = [];
}

Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public function testSchemaTableIsCreatedWithPrimaryKey()
$this->assertTrue($this->adapter->hasIndex($this->adapter->getSchemaTableName(), ['version']));
}

public function testDatabaseNameWithEscapedCharacter()
{
$this->adapter->dropDatabase($this->config['database'] . '-test');
$this->adapter->createDatabase($this->config['database'] . '-test', ['charset' => 'utf8mb4']);
$this->assertTrue($this->adapter->hasDatabase($this->config['database'] . '-test'));
$this->adapter->dropDatabase($this->config['database'] . '-test');
}

public function testQuoteTableName()
{
$this->assertEquals('`test_table`', $this->adapter->quoteTableName('test_table'));
Expand Down