From 28261236b1036768d933d48f9bd2848a060d0b3a Mon Sep 17 00:00:00 2001 From: zoilomora Date: Fri, 20 Nov 2020 23:16:47 +0100 Subject: [PATCH] Change line break to LF --- README.md | 122 ++++----- .../DBAL/Driver/MicrosoftAccess/Statement.php | 240 +++++++++--------- .../Driver/MicrosoftAccess/StatementTest.php | 92 +++---- 3 files changed, 227 insertions(+), 227 deletions(-) diff --git a/README.md b/README.md index 10ac270..2c37fea 100644 --- a/README.md +++ b/README.md @@ -1,61 +1,61 @@ -# Doctrine DBAL for Microsoft Access -An implementation of the [doctrine/dbal](https://github.com/doctrine/dbal) library to support **Microsoft Access databases** in **Microsoft OS**. - -There are some functionalities that are not supported by Microsoft Access in a PDO-based connection. For these functionalities the implementation uses a direct connection through ODBC. - -## OS Requirements -- Microsoft Access Database Engine Redistributable ([2010](https://www.microsoft.com/download/details.aspx?id=13255) or [2016](https://www.microsoft.com/download/details.aspx?id=54920)). -- Register a **DSN** in **ODBC Data Source Administrator** (odbcad32.exe). - -## Installation - -1) Install via [composer](https://getcomposer.org/) - - ```shell script - composer require zoilomora/doctrine-dbal-msaccess - ``` - -### Register a **DSN** -We don't need to reinvent the wheel, on the internet there are hundreds of tutorials on how to set up a DSN for Microsoft Access. -I leave you a [video](https://www.youtube.com/watch?v=biSjA8ms_Wk) that I think explains it perfectly. - -Once the DSN is configured we will have to configure the connection in the following way: - -```php -$connection = \Doctrine\DBAL\DriverManager::getConnection( - [ - 'driverClass' => \ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Driver::class, - 'dsn' => 'name of the created dsn', - ] -); -``` - -## Discovered problems - -### Character encoding problems -The default character encoding in Access databases is [Windows-1252](https://en.wikipedia.org/wiki/Windows-1252). -If you want to convert the data to UTF-8, a simple solution would be: - -```php -$field = \mb_convert_encoding($field, 'UTF-8', 'Windows-1252'); -``` - -If you want all the data to be encoded automatically to UTF-8 (with the performance reduction that it may imply) -configure the driver as follows: - -```php -$connection = \Doctrine\DBAL\DriverManager::getConnection( - [ - 'driverClass' => \ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Driver::class, - 'dsn' => 'name of the created dsn', - 'driverOptions' => [ - 'charset' => 'UTF-8', - ], - ] -); -``` - -## License -Licensed under the [MIT license](http://opensource.org/licenses/MIT) - -Read [LICENSE](LICENSE) for more information +# Doctrine DBAL for Microsoft Access +An implementation of the [doctrine/dbal](https://github.com/doctrine/dbal) library to support **Microsoft Access databases** in **Microsoft OS**. + +There are some functionalities that are not supported by Microsoft Access in a PDO-based connection. For these functionalities the implementation uses a direct connection through ODBC. + +## OS Requirements +- Microsoft Access Database Engine Redistributable ([2010](https://www.microsoft.com/download/details.aspx?id=13255) or [2016](https://www.microsoft.com/download/details.aspx?id=54920)). +- Register a **DSN** in **ODBC Data Source Administrator** (odbcad32.exe). + +## Installation + +1) Install via [composer](https://getcomposer.org/) + + ```shell script + composer require zoilomora/doctrine-dbal-msaccess + ``` + +### Register a **DSN** +We don't need to reinvent the wheel, on the internet there are hundreds of tutorials on how to set up a DSN for Microsoft Access. +I leave you a [video](https://www.youtube.com/watch?v=biSjA8ms_Wk) that I think explains it perfectly. + +Once the DSN is configured we will have to configure the connection in the following way: + +```php +$connection = \Doctrine\DBAL\DriverManager::getConnection( + [ + 'driverClass' => \ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Driver::class, + 'dsn' => 'name of the created dsn', + ] +); +``` + +## Discovered problems + +### Character encoding problems +The default character encoding in Access databases is [Windows-1252](https://en.wikipedia.org/wiki/Windows-1252). +If you want to convert the data to UTF-8, a simple solution would be: + +```php +$field = \mb_convert_encoding($field, 'UTF-8', 'Windows-1252'); +``` + +If you want all the data to be encoded automatically to UTF-8 (with the performance reduction that it may imply) +configure the driver as follows: + +```php +$connection = \Doctrine\DBAL\DriverManager::getConnection( + [ + 'driverClass' => \ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Driver::class, + 'dsn' => 'name of the created dsn', + 'driverOptions' => [ + 'charset' => 'UTF-8', + ], + ] +); +``` + +## License +Licensed under the [MIT license](http://opensource.org/licenses/MIT) + +Read [LICENSE](LICENSE) for more information diff --git a/src/Doctrine/DBAL/Driver/MicrosoftAccess/Statement.php b/src/Doctrine/DBAL/Driver/MicrosoftAccess/Statement.php index 3aad5ca..094a38c 100644 --- a/src/Doctrine/DBAL/Driver/MicrosoftAccess/Statement.php +++ b/src/Doctrine/DBAL/Driver/MicrosoftAccess/Statement.php @@ -1,120 +1,120 @@ -charset = $charset; - } - - public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) - { - switch ($type) { - case ParameterType::LARGE_OBJECT: - case ParameterType::BINARY: - if ($driverOptions === null) { - $driverOptions = \PDO::SQLSRV_ENCODING_BINARY; - } - - break; - - case ParameterType::ASCII: - $type = ParameterType::STRING; - $length = 0; - $driverOptions = \PDO::SQLSRV_ENCODING_SYSTEM; - break; - } - - return parent::bindParam($param, $variable, $type, $length, $driverOptions); - } - - public function bindValue($param, $value, $type = ParameterType::STRING) - { - return $this->bindParam($param, $value, $type); - } - - public function fetchOne() - { - return $this->convertStringEncoding( - parent::fetchOne() - ); - } - - public function fetchNumeric() - { - return $this->convertArrayEncoding( - parent::fetchNumeric() - ); - } - - public function fetchAssociative() - { - return $this->convertArrayEncoding( - parent::fetchAssociative() - ); - } - - public function fetchAllNumeric(): array - { - return $this->convertCollectionEncoding( - parent::fetchAllNumeric() - ); - } - - public function fetchFirstColumn(): array - { - return $this->convertArrayEncoding( - parent::fetchFirstColumn() - ); - } - - public function fetchAllAssociative(): array - { - return $this->convertCollectionEncoding( - parent::fetchAllAssociative() - ); - } - - private function convertCollectionEncoding(array $items): array - { - \array_walk( - $items, - function (&$item) { - $item = $this->convertArrayEncoding($item); - } - ); - - return $items; - } - - private function convertArrayEncoding(array $items): array - { - foreach ($items as $key => $value) { - $items[$key] = $this->convertStringEncoding($items[$key]); - } - - return $items; - } - - private function convertStringEncoding(?string $value): ?string - { - if (null === $this->charset) { - return $value; - } - - if (null === $value) { - return null; - } - - return \mb_convert_encoding($value, $this->charset, self::FROM_ENCODING); - } -} +charset = $charset; + } + + public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) + { + switch ($type) { + case ParameterType::LARGE_OBJECT: + case ParameterType::BINARY: + if ($driverOptions === null) { + $driverOptions = \PDO::SQLSRV_ENCODING_BINARY; + } + + break; + + case ParameterType::ASCII: + $type = ParameterType::STRING; + $length = 0; + $driverOptions = \PDO::SQLSRV_ENCODING_SYSTEM; + break; + } + + return parent::bindParam($param, $variable, $type, $length, $driverOptions); + } + + public function bindValue($param, $value, $type = ParameterType::STRING) + { + return $this->bindParam($param, $value, $type); + } + + public function fetchOne() + { + return $this->convertStringEncoding( + parent::fetchOne() + ); + } + + public function fetchNumeric() + { + return $this->convertArrayEncoding( + parent::fetchNumeric() + ); + } + + public function fetchAssociative() + { + return $this->convertArrayEncoding( + parent::fetchAssociative() + ); + } + + public function fetchAllNumeric(): array + { + return $this->convertCollectionEncoding( + parent::fetchAllNumeric() + ); + } + + public function fetchFirstColumn(): array + { + return $this->convertArrayEncoding( + parent::fetchFirstColumn() + ); + } + + public function fetchAllAssociative(): array + { + return $this->convertCollectionEncoding( + parent::fetchAllAssociative() + ); + } + + private function convertCollectionEncoding(array $items): array + { + \array_walk( + $items, + function (&$item) { + $item = $this->convertArrayEncoding($item); + } + ); + + return $items; + } + + private function convertArrayEncoding(array $items): array + { + foreach ($items as $key => $value) { + $items[$key] = $this->convertStringEncoding($items[$key]); + } + + return $items; + } + + private function convertStringEncoding(?string $value): ?string + { + if (null === $this->charset) { + return $value; + } + + if (null === $value) { + return null; + } + + return \mb_convert_encoding($value, $this->charset, self::FROM_ENCODING); + } +} diff --git a/tests/Doctrine/DBAL/Driver/MicrosoftAccess/StatementTest.php b/tests/Doctrine/DBAL/Driver/MicrosoftAccess/StatementTest.php index 2076547..66a4242 100644 --- a/tests/Doctrine/DBAL/Driver/MicrosoftAccess/StatementTest.php +++ b/tests/Doctrine/DBAL/Driver/MicrosoftAccess/StatementTest.php @@ -1,46 +1,46 @@ - 'UTF-8', - ]; - } - - /** - * @test - * @dataProvider connections - */ - public function given_a_french_database_when_has_driver_options_then_it_appears_in_utf8(Connection $connection) - { - $result = $connection - ->createQueryBuilder() - ->select('note') - ->from(self::TABLE_NAME) - ->execute(); - - $item = $result->fetchAllAssociative()[0]; - - $this->assertSame('Matériel Prêt un mail est envoyé', $item['note']); - } -} + 'UTF-8', + ]; + } + + /** + * @test + * @dataProvider connections + */ + public function given_a_french_database_when_has_driver_options_then_it_appears_in_utf8(Connection $connection) + { + $result = $connection + ->createQueryBuilder() + ->select('note') + ->from(self::TABLE_NAME) + ->execute(); + + $item = $result->fetchAllAssociative()[0]; + + $this->assertSame('Matériel Prêt un mail est envoyé', $item['note']); + } +}