-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Infer omitted column charset from collation #5471
Merged
morozov
merged 1 commit into
doctrine:3.3.x
from
morozov:issues/5338-charset-from-collation
Jul 2, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface CollationMetadataProvider | ||
{ | ||
public function getCollationCharset(string $collation): ?string; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; | ||
|
||
use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; | ||
|
||
use function array_key_exists; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CachingCollationMetadataProvider implements CollationMetadataProvider | ||
{ | ||
/** @var CollationMetadataProvider */ | ||
private $collationMetadataProvider; | ||
|
||
/** @var array<string,?string> */ | ||
private $cache = []; | ||
|
||
public function __construct(CollationMetadataProvider $collationMetadataProvider) | ||
{ | ||
$this->collationMetadataProvider = $collationMetadataProvider; | ||
} | ||
|
||
public function getCollationCharset(string $collation): ?string | ||
{ | ||
if (array_key_exists($collation, $this->cache)) { | ||
return $this->cache[$collation]; | ||
} | ||
|
||
return $this->cache[$collation] = $this->collationMetadataProvider->getCollationCharset($collation); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Platforms/MySQL/CollationMetadataProvider/ConnectionCollationMetadataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception; | ||
use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ConnectionCollationMetadataProvider implements CollationMetadataProvider | ||
{ | ||
/** @var Connection */ | ||
private $connection; | ||
|
||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function getCollationCharset(string $collation): ?string | ||
{ | ||
$charset = $this->connection->fetchOne( | ||
<<<'SQL' | ||
SELECT CHARACTER_SET_NAME | ||
FROM information_schema.COLLATIONS | ||
WHERE COLLATION_NAME = ?; | ||
SQL | ||
, | ||
[$collation] | ||
); | ||
|
||
if ($charset !== false) { | ||
return $charset; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Platforms\MySQL\CollationMetadataProvider; | ||
|
||
use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; | ||
use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider\CachingCollationMetadataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CachingCollationMetadataProviderTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider charsetAndCollationProvider | ||
*/ | ||
public function testCharsetCaching(string $collation, ?string $charset): void | ||
{ | ||
$underlyingProvider = $this->createMock(CollationMetadataProvider::class); | ||
$underlyingProvider->expects(self::once()) | ||
->method('getCollationCharset') | ||
->with($collation) | ||
->willReturn($charset); | ||
|
||
$cachingProvider = new CachingCollationMetadataProvider($underlyingProvider); | ||
self::assertSame($charset, $cachingProvider->getCollationCharset($collation)); | ||
self::assertSame($charset, $cachingProvider->getCollationCharset($collation)); | ||
} | ||
|
||
/** | ||
* @return iterable<string,array{string,?string}> | ||
*/ | ||
public static function charsetAndCollationProvider(): iterable | ||
{ | ||
yield 'found' => ['utf8mb4_unicode_ci', 'utf8mb4']; | ||
yield 'not found' => ['utf8mb5_unicode_ci', null]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does
null
mean here? "This collation has no charset"? "We don't know about this collation"? Both? If both, should the second case result in an exception?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"This collation has no charset" is impossible, so it's "We don't know about this collation".
It doesn't have to. Should
strpos()
throw an exception if the substring is not found? The calling code handlesnull
by not updating options, so if the collation is invalid, the DDL will eventually fail once it hits the database.If we decide to throw an exception here, how do we unit-test the comparator without the database connection? Right now, we supply it with a provider that always returns
null
, which is fine because no unit tests rely on this feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's clearer, thanks for the explanation.