Skip to content

Commit

Permalink
Merge pull request #7 from mpk-software/bugfix/driver-options
Browse files Browse the repository at this point in the history
Allow passing the DSN by using `doctine.dbal.options.dsn` for symfony projects
  • Loading branch information
zoilomora authored Nov 23, 2020
2 parents b5d4cbe + ce04c3d commit 95492eb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Once the DSN is configured we will have to configure the connection in the follo
$connection = \Doctrine\DBAL\DriverManager::getConnection(
[
'driverClass' => \ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Driver::class,
'dsn' => 'name of the created dsn',
'driverOptions' => [
'dsn' => 'name of the created dsn',
],
]
);
```
Expand All @@ -47,8 +49,8 @@ configure the driver as follows:
$connection = \Doctrine\DBAL\DriverManager::getConnection(
[
'driverClass' => \ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\Driver::class,
'dsn' => 'name of the created dsn',
'driverOptions' => [
'dsn' => 'name of the created dsn',
'charset' => 'UTF-8',
],
]
Expand Down
67 changes: 43 additions & 24 deletions src/Doctrine/DBAL/Driver/MicrosoftAccess/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

namespace ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use PDOException;
use ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\PDO\Connection as PDOConnection;
use ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess\ODBC\Connection as ODBCConnection;
use ZoiloMora\Doctrine\DBAL\Platforms\MicrosoftAccessPlatform;
Expand All @@ -21,19 +23,19 @@ public function connect(
$password = null,
array $driverOptions = []
): \Doctrine\DBAL\Driver\Connection {
$this->assertRequiredParameters($params);
$this->assertRequiredParameters($driverOptions);

try {
$conn = new PDOConnection(
$this->constructPdoDsn($params),
$this->constructPdoDsn($driverOptions),
$username,
$password,
$driverOptions,
);
$this->odbcConnection = new ODBCConnection(
$this->constructOdbcDsn($params),
$this->constructOdbcDsn($driverOptions),
);
} catch (\PDOException $e) {
} catch (PDOException $e) {
throw Exception::driverException($this, $e);
}

Expand All @@ -45,7 +47,7 @@ public function getName(): string
return 'pdo_msaccess';
}

public function getDatabase(\Doctrine\DBAL\Connection $conn): string
public function getDatabase(Connection $conn): string
{
return 'unknown';
}
Expand All @@ -55,37 +57,54 @@ public function getDatabasePlatform(): AbstractPlatform
return new MicrosoftAccessPlatform();
}

public function getSchemaManager(\Doctrine\DBAL\Connection $conn): AbstractSchemaManager
public function getSchemaManager(Connection $conn): AbstractSchemaManager
{
return new MicrosoftAccessSchemaManager($conn, $this->odbcConnection);
}

private function assertRequiredParameters(array $params): void
/**
* @param array $driverOptions
* @throws \Exception
*/
private function assertRequiredParameters(array $driverOptions): void
{
if (false === \array_key_exists('dsn', $params)) {
throw new \Exception("The parameter 'dsn' is mandatory");
$dsn = $this->getDsn($driverOptions);

if ($dsn === null) {
throw new Exception\InvalidArgumentException("The driver option 'dsn' is mandatory");
}
}

protected function constructPdoDsn(array $params): string
/**
* Get the DSN for the PDO connection.
*
* @param array $driverOptions
* @return string
*/
protected function constructPdoDsn(array $driverOptions): string
{
$dsn = 'odbc:';

if (isset($params['dsn']) && '' !== $params['dsn']) {
return $dsn . $params['dsn'];
}

return $dsn;
return 'odbc:' . $this->getDsn($driverOptions);
}

protected function constructOdbcDsn(array $params): string
/**
* Get the DSN for the ODBC connection.
*
* @param array $driverOptions
* @return string
*/
protected function constructOdbcDsn(array $driverOptions): string
{
$dsn = '';

if (isset($params['dsn']) && '' !== $params['dsn']) {
return $dsn . $params['dsn'];
}
return $this->getDsn($driverOptions);
}

return $dsn;
/**
* Get the DSN by the driver's parameters and options
*
* @param array $driverOptions
* @return string|null
*/
private function getDsn(array $driverOptions): ?string
{
return $driverOptions['dsn'] ?? null;
}
}

0 comments on commit 95492eb

Please sign in to comment.