-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve MySQL charset and collation comparison
- Loading branch information
Showing
12 changed files
with
238 additions
and
26 deletions.
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 CharsetMetadataProvider | ||
{ | ||
public function getDefaultCharsetCollation(string $charset): ?string; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Platforms/MySQL/CharsetMetadataProvider/CachingCharsetMetadataProvider.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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
use Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
use function array_key_exists; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CachingCharsetMetadataProvider implements CharsetMetadataProvider | ||
{ | ||
private CharsetMetadataProvider $charsetMetadataProvider; | ||
|
||
/** @var array<string,?string> */ | ||
private array $cache = []; | ||
|
||
public function __construct(CharsetMetadataProvider $charsetMetadataProvider) | ||
{ | ||
$this->charsetMetadataProvider = $charsetMetadataProvider; | ||
} | ||
|
||
public function getDefaultCharsetCollation(string $charset): ?string | ||
{ | ||
if (array_key_exists($charset, $this->cache)) { | ||
return $this->cache[$charset]; | ||
} | ||
|
||
return $this->cache[$charset] = $this->charsetMetadataProvider->getDefaultCharsetCollation($charset); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Platforms/MySQL/CharsetMetadataProvider/ConnectionCharsetMetadataProvider.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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception; | ||
use Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ConnectionCharsetMetadataProvider implements CharsetMetadataProvider | ||
{ | ||
private Connection $connection; | ||
|
||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function getDefaultCharsetCollation(string $charset): ?string | ||
{ | ||
$collation = $this->connection->fetchOne( | ||
<<<'SQL' | ||
SELECT DEFAULT_COLLATE_NAME | ||
FROM information_schema.CHARACTER_SETS | ||
WHERE CHARACTER_SET_NAME = ?; | ||
SQL | ||
, | ||
[$charset] | ||
); | ||
|
||
if ($collation !== false) { | ||
return $collation; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DefaultTableOptions | ||
{ | ||
private string $charset; | ||
|
||
private string $collation; | ||
|
||
public function __construct(string $charset, string $collation) | ||
{ | ||
$this->charset = $charset; | ||
$this->collation = $collation; | ||
} | ||
|
||
public function getCharset(): string | ||
{ | ||
return $this->charset; | ||
} | ||
|
||
public function getCollation(): string | ||
{ | ||
return $this->collation; | ||
} | ||
} |
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
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
Oops, something went wrong.