Skip to content

Commit

Permalink
Merge pull request #11 from zoilomora/bugfix/replace-internal-classes
Browse files Browse the repository at this point in the history
Extend class from origin
  • Loading branch information
zoilomora authored Nov 26, 2020
2 parents 02c858b + 645a0e3 commit 7ca8b7d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 35 deletions.
93 changes: 71 additions & 22 deletions src/Doctrine/DBAL/Driver/MicrosoftAccess/PDO/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@

namespace ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\PDO;

use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\ParameterType;
use PDO;
use PDOStatement;
use ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Statement;

final class Connection extends PDOConnection
final class Connection extends PDO implements ConnectionInterface, ServerInfoAwareConnection
{
private ?bool $transactionsSupport = null;
private ?string $charsetToEncoding = null;

public function __construct($dsn, $user = null, $password = null, $options = null)
/**
* {@inheritdoc}
*/
public function __construct($dsn, $user = null, $password = null, ?array $options = null)
{
parent::__construct($dsn, $user, $password, $options);

$this->setAttribute(
\PDO::ATTR_STATEMENT_CLASS,
[
Statement::class,
[
true === \array_key_exists('charset', $options)
? $options['charset']
: null,
],
],
);
parent::__construct($dsn, (string)$user, (string)$password, (array)$options);

$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class, []]);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$this->charsetToEncoding = \array_key_exists('charset', $options)
? $options['charset']
: null;
}

public function lastInsertId($name = null): string
public function getServerVersion(): string
{
return '0';
return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
}

public function quote($value, $type = \PDO::PARAM_STR)
/**
* {@inheritdoc}
*/
public function quote($value, $type = ParameterType::STRING)
{
$val = parent::quote($value, $type);

Expand All @@ -44,27 +50,70 @@ public function quote($value, $type = \PDO::PARAM_STR)
return $val;
}

public function beginTransaction()
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null): string
{
return '0';
}

public function requiresQueryForServerVersion(): bool
{
return false;
}

public function beginTransaction(): bool
{
return true === $this->transactionsSupported()
? parent::beginTransaction()
: $this->exec('BEGIN TRANSACTION');
}

public function commit()
public function commit(): bool
{
return true === $this->transactionsSupported()
? parent::commit()
: $this->exec('COMMIT TRANSACTION');
}

public function rollback()
public function rollback(): bool
{
return true === $this->transactionsSupported()
? parent::rollback()
: $this->exec('ROLLBACK TRANSACTION');
}

/**
* {@inheritdoc}
*/
public function query(...$args): PDOStatement
{
$statement = parent::query(...$args);

\assert($statement instanceof Statement);
$statement->setCharsetToEncoding($this->charsetToEncoding);

return $statement;
}

/**
* {@inheritdoc}
*/
public function prepare($statement, $options = null)
{
if (null === $options) {
$options = [];
}

$statement = parent::prepare($statement, $options);

\assert($statement instanceof Statement);
$statement->setCharsetToEncoding($this->charsetToEncoding);

return $statement;
}

private function transactionsSupported(): bool
{
if (null !== $this->transactionsSupport) {
Expand Down
17 changes: 6 additions & 11 deletions src/Doctrine/DBAL/Driver/MicrosoftAccess/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@

final class Statement extends \Doctrine\DBAL\Driver\PDO\Statement
{
private const FROM_ENCODING = 'Windows-1252';
private const CHARSET_FROM_ENCODING = 'Windows-1252';

private ?string $charset;

protected function __construct(?string $charset = null)
{
$this->charset = $charset;
}
private ?string $charsetToEncoding = null;

public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
{
Expand All @@ -37,9 +32,9 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
return parent::bindParam($param, $variable, $type, $length, $driverOptions);
}

public function bindValue($param, $value, $type = ParameterType::STRING)
public function setCharsetToEncoding(?string $charset): void
{
return $this->bindParam($param, $value, $type);
$this->charsetToEncoding = $charset;
}

public function fetchOne()
Expand Down Expand Up @@ -107,14 +102,14 @@ private function convertArrayEncoding(array $items): array

private function convertStringEncoding(?string $value): ?string
{
if (null === $this->charset) {
if (null === $this->charsetToEncoding) {
return $value;
}

if (null === $value) {
return null;
}

return \mb_convert_encoding($value, $this->charset, self::FROM_ENCODING);
return \mb_convert_encoding($value, $this->charsetToEncoding, self::CHARSET_FROM_ENCODING);
}
}
5 changes: 3 additions & 2 deletions tests/Doctrine/DBAL/Platforms/MicrosoftAccessPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class MicrosoftAccessPlatformTest extends BaseTest
* @test
* @dataProvider connections
*/
public function given_an_connection_when_get_the_first_two_rows_then_returns_array_with_two_rows(Connection $connection)
{
public function given_an_connection_when_get_the_first_two_rows_then_returns_array_with_two_rows(
Connection $connection
) {
$result = $connection->createQueryBuilder()
->select('*')
->from(self::TABLE_NAME)
Expand Down

0 comments on commit 7ca8b7d

Please sign in to comment.