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

Fix creating wrapped connection twice #1087

Merged
merged 1 commit into from Dec 17, 2019
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
31 changes: 25 additions & 6 deletions ConnectionFactory.php
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use function is_subclass_of;

class ConnectionFactory
{
Expand Down Expand Up @@ -42,22 +43,40 @@ public function createConnection(array $params, Configuration $config = null, Ev
$this->initializeTypes();
}

$connection = DriverManager::getConnection($params, $config, $eventManager);

if (! isset($params['pdo']) && ! isset($params['charset'])) {
$params = $connection->getParams();
$params['charset'] = 'utf8';
$driver = $connection->getDriver();
$wrapperClass = null;
if (isset($params['wrapperClass'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of inlining this logic here, that's what's DriverManager for. Other places using it are still having old behaviour and in case we update DriverManager, it can break ConnectionFactory, since it unsets wrapperClass option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this should be ported to DriverManager, but fixing this here was the faster choice for Symfony users. Once this is fixed in DBAL 2.9, we can remove it here and bump the doctrine/dbal dependency.

if (! is_subclass_of($params['wrapperClass'], Connection::class)) {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}

$wrapperClass = $params['wrapperClass'];
$params['wrapperClass'] = null;
}

$connection = DriverManager::getConnection($params, $config, $eventManager);
$params = $connection->getParams();
$driver = $connection->getDriver();

if ($driver instanceof AbstractMySQLDriver) {
$params['charset'] = 'utf8mb4';

if (! isset($params['defaultTableOptions']['collate'])) {
$params['defaultTableOptions']['collate'] = 'utf8mb4_unicode_ci';
}
} else {
$params['charset'] = 'utf8';
}

if ($wrapperClass !== null) {
$params['wrapperClass'] = $wrapperClass;
} else {
$wrapperClass = Connection::class;
}

$connection = new $connection($params, $driver, $connection->getConfiguration(), $connection->getEventManager());
$connection = new $wrapperClass($params, $driver, $config, $eventManager);
} else {
$connection = DriverManager::getConnection($params, $config, $eventManager);
}

if (! empty($mappingTypes)) {
Expand Down
23 changes: 21 additions & 2 deletions Tests/ConnectionFactoryTest.php
Expand Up @@ -41,11 +41,17 @@ public function testContainer()
public function testDefaultCharset()
{
$factory = new ConnectionFactory([]);
$params = ['driverClass' => FakeDriver::class];
$params = [
'driverClass' => FakeDriver::class,
'wrapperClass' => FakeConnection::class,
];

$connection = $factory->createConnection($params);
$creationCount = FakeConnection::$creationCount;
$connection = $factory->createConnection($params);

$this->assertInstanceof(FakeConnection::class, $connection);
$this->assertSame('utf8', $connection->getParams()['charset']);
$this->assertSame(1 + $creationCount, FakeConnection::$creationCount);
}

public function testDefaultCharsetMySql()
Expand Down Expand Up @@ -120,3 +126,16 @@ public function getDatabase(Connection $conn)
return 'fake_db';
}
}

class FakeConnection extends Connection
{
/** @var int */
public static $creationCount = 0;

public function __construct(array $params, FakeDriver $driver, ?Configuration $config = null, ?EventManager $eventManager = null)
{
++self::$creationCount;

parent::__construct($params, $driver, $config, $eventManager);
}
}