Unverified Commit c30aac35 authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3560 from morozov/driver-types

Enforced parameter and return value types in Driver classes
parents 84e365da 406dc01e
# Upgrade to 3.0 # Upgrade to 3.0
## BC BREAK: Changes in the `Doctrine\DBAL\Driver` API
1. The `$username` and `$password` arguments of `::connect()` are no longer nullable. Use an empty string to indicate empty username or password.
2. The return value of `::getDatabase()` has been documented as nullable since some of the drivers allow establishing a connection without selecting a database.
## BC BREAK: `Doctrine\DBAL\Driver::getName()` removed ## BC BREAK: `Doctrine\DBAL\Driver::getName()` removed
The `Doctrine\DBAL\Driver::getName()` has been removed. The `Doctrine\DBAL\Driver::getName()` has been removed.
......
...@@ -330,8 +330,8 @@ class Connection implements DriverConnection ...@@ -330,8 +330,8 @@ class Connection implements DriverConnection
} }
$driverOptions = $this->params['driverOptions'] ?? []; $driverOptions = $this->params['driverOptions'] ?? [];
$user = $this->params['user'] ?? null; $user = $this->params['user'] ?? '';
$password = $this->params['password'] ?? null; $password = $this->params['password'] ?? '';
$this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions); $this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions);
$this->isConnected = true; $this->isConnected = true;
......
...@@ -192,8 +192,8 @@ class MasterSlaveConnection extends Connection ...@@ -192,8 +192,8 @@ class MasterSlaveConnection extends Connection
$connectionParams = $this->chooseConnectionConfiguration($connectionName, $params); $connectionParams = $this->chooseConnectionConfiguration($connectionName, $params);
$user = $connectionParams['user'] ?? null; $user = $connectionParams['user'] ?? '';
$password = $connectionParams['password'] ?? null; $password = $connectionParams['password'] ?? '';
return $this->_driver->connect($connectionParams, $user, $password, $driverOptions); return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
} }
......
...@@ -4,6 +4,7 @@ declare(strict_types=1); ...@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\AbstractSchemaManager;
...@@ -16,16 +17,19 @@ interface Driver ...@@ -16,16 +17,19 @@ interface Driver
/** /**
* Attempts to create a connection with the database. * Attempts to create a connection with the database.
* *
* The usage of NULL to indicate empty username or password is deprecated. Use an empty string instead. * @param mixed[] $params All connection parameters passed by the user.
* @param string $username The username to use when connecting.
* @param string $password The password to use when connecting.
* @param mixed[] $driverOptions The driver options to use when connecting.
* *
* @param mixed[] $params All connection parameters passed by the user. * @return DriverConnection The database connection.
* @param string|null $username The username to use when connecting.
* @param string|null $password The password to use when connecting.
* @param mixed[] $driverOptions The driver options to use when connecting.
*
* @return \Doctrine\DBAL\Driver\Connection The database connection.
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []); public function connect(
array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : DriverConnection;
/** /**
* Gets the DatabasePlatform instance that provides all the metadata about * Gets the DatabasePlatform instance that provides all the metadata about
...@@ -33,20 +37,18 @@ interface Driver ...@@ -33,20 +37,18 @@ interface Driver
* *
* @return AbstractPlatform The database platform. * @return AbstractPlatform The database platform.
*/ */
public function getDatabasePlatform(); public function getDatabasePlatform() : AbstractPlatform;
/** /**
* Gets the SchemaManager that can be used to inspect and change the underlying * Gets the SchemaManager that can be used to inspect and change the underlying
* database schema of the platform this driver connects to. * database schema of the platform this driver connects to.
*
* @return AbstractSchemaManager
*/ */
public function getSchemaManager(Connection $conn); public function getSchemaManager(Connection $conn) : AbstractSchemaManager;
/** /**
* Gets the name of the database connected to for this driver. * Gets the name of the database connected to for this driver.
* *
* @return string The name of the database. * @return string The name of the database or NULL if no database is currently selected.
*/ */
public function getDatabase(Connection $conn); public function getDatabase(Connection $conn) : ?string;
} }
...@@ -6,7 +6,9 @@ namespace Doctrine\DBAL\Driver; ...@@ -6,7 +6,9 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\DB2SchemaManager; use Doctrine\DBAL\Schema\DB2SchemaManager;
/** /**
...@@ -17,7 +19,7 @@ abstract class AbstractDB2Driver implements Driver ...@@ -17,7 +19,7 @@ abstract class AbstractDB2Driver implements Driver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -27,7 +29,7 @@ abstract class AbstractDB2Driver implements Driver ...@@ -27,7 +29,7 @@ abstract class AbstractDB2Driver implements Driver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new DB2Platform(); return new DB2Platform();
} }
...@@ -35,7 +37,7 @@ abstract class AbstractDB2Driver implements Driver ...@@ -35,7 +37,7 @@ abstract class AbstractDB2Driver implements Driver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new DB2SchemaManager($conn); return new DB2SchemaManager($conn);
} }
......
...@@ -7,12 +7,16 @@ namespace Doctrine\DBAL\Driver; ...@@ -7,12 +7,16 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion; use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySQL80Platform; use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\MySqlSchemaManager; use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
use function preg_match; use function preg_match;
...@@ -30,7 +34,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -30,7 +34,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
*/ */
public function convertException($message, DriverException $exception) public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{ {
switch ($exception->getCode()) { switch ($exception->getCode()) {
case 1213: case 1213:
...@@ -106,7 +110,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -106,7 +110,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
return new Exception\NotNullConstraintViolationException($message, $exception); return new Exception\NotNullConstraintViolationException($message, $exception);
} }
return new Exception\DriverException($message, $exception); return new DriverException($message, $exception);
} }
/** /**
...@@ -114,7 +118,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -114,7 +118,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
* *
* @throws DBALException * @throws DBALException
*/ */
public function createDatabasePlatformForVersion($version) public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
{ {
$mariadb = stripos($version, 'mariadb') !== false; $mariadb = stripos($version, 'mariadb') !== false;
if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) { if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
...@@ -192,7 +196,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -192,7 +196,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -204,7 +208,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -204,7 +208,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
* *
* @return MySqlPlatform * @return MySqlPlatform
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new MySqlPlatform(); return new MySqlPlatform();
} }
...@@ -214,7 +218,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -214,7 +218,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
* *
* @return MySqlSchemaManager * @return MySqlSchemaManager
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new MySqlSchemaManager($conn); return new MySqlSchemaManager($conn);
} }
......
...@@ -7,8 +7,12 @@ namespace Doctrine\DBAL\Driver; ...@@ -7,8 +7,12 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractOracleDriver\EasyConnectString; use Doctrine\DBAL\Driver\AbstractOracleDriver\EasyConnectString;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\OracleSchemaManager; use Doctrine\DBAL\Schema\OracleSchemaManager;
/** /**
...@@ -19,7 +23,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver ...@@ -19,7 +23,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function convertException($message, DriverException $exception) public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{ {
switch ($exception->getCode()) { switch ($exception->getCode()) {
case 1: case 1:
...@@ -56,13 +60,13 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver ...@@ -56,13 +60,13 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
return new Exception\ForeignKeyConstraintViolationException($message, $exception); return new Exception\ForeignKeyConstraintViolationException($message, $exception);
} }
return new Exception\DriverException($message, $exception); return new DriverException($message, $exception);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -72,7 +76,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver ...@@ -72,7 +76,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new OraclePlatform(); return new OraclePlatform();
} }
...@@ -80,7 +84,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver ...@@ -80,7 +84,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new OracleSchemaManager($conn); return new OracleSchemaManager($conn);
} }
...@@ -89,10 +93,8 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver ...@@ -89,10 +93,8 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
* Returns an appropriate Easy Connect String for the given parameters. * Returns an appropriate Easy Connect String for the given parameters.
* *
* @param mixed[] $params The connection parameters to return the Easy Connect String for. * @param mixed[] $params The connection parameters to return the Easy Connect String for.
*
* @return string
*/ */
protected function getEasyConnectString(array $params) protected function getEasyConnectString(array $params) : string
{ {
return (string) EasyConnectString::fromConnectionParameters($params); return (string) EasyConnectString::fromConnectionParameters($params);
} }
......
...@@ -6,11 +6,15 @@ namespace Doctrine\DBAL\Driver; ...@@ -6,11 +6,15 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion; use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform; use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform; use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager; use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
use function preg_match; use function preg_match;
...@@ -27,7 +31,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -27,7 +31,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
* *
* @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html * @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
*/ */
public function convertException($message, DriverException $exception) public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{ {
switch ($exception->getSQLState()) { switch ($exception->getSQLState()) {
case '40001': case '40001':
...@@ -73,13 +77,13 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -73,13 +77,13 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
return new Exception\ConnectionException($message, $exception); return new Exception\ConnectionException($message, $exception);
} }
return new Exception\DriverException($message, $exception); return new DriverException($message, $exception);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createDatabasePlatformForVersion($version) public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
{ {
if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
throw InvalidPlatformVersion::new( throw InvalidPlatformVersion::new(
...@@ -106,7 +110,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -106,7 +110,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -116,7 +120,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -116,7 +120,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new PostgreSqlPlatform(); return new PostgreSqlPlatform();
} }
...@@ -124,7 +128,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -124,7 +128,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new PostgreSqlSchemaManager($conn); return new PostgreSqlSchemaManager($conn);
} }
......
...@@ -6,9 +6,13 @@ namespace Doctrine\DBAL\Driver; ...@@ -6,9 +6,13 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion; use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform; use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager; use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
use function preg_match; use function preg_match;
...@@ -23,7 +27,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -23,7 +27,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
* *
* @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
*/ */
public function convertException($message, DriverException $exception) public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{ {
switch ($exception->getCode()) { switch ($exception->getCode()) {
case -306: case -306:
...@@ -60,13 +64,13 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -60,13 +64,13 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
return new Exception\TableNotFoundException($message, $exception); return new Exception\TableNotFoundException($message, $exception);
} }
return new Exception\DriverException($message, $exception); return new DriverException($message, $exception);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createDatabasePlatformForVersion($version) public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
{ {
if (! preg_match( if (! preg_match(
'/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/', '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
...@@ -88,7 +92,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -88,7 +92,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -98,7 +102,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -98,7 +102,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new SQLAnywherePlatform(); return new SQLAnywherePlatform();
} }
...@@ -106,7 +110,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -106,7 +110,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new SQLAnywhereSchemaManager($conn); return new SQLAnywhereSchemaManager($conn);
} }
......
...@@ -6,9 +6,11 @@ namespace Doctrine\DBAL\Driver; ...@@ -6,9 +6,11 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion; use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Schema\SQLServerSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
use function preg_match; use function preg_match;
...@@ -22,7 +24,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr ...@@ -22,7 +24,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createDatabasePlatformForVersion($version) public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
{ {
if (! preg_match( if (! preg_match(
'/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/', '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
...@@ -52,7 +54,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr ...@@ -52,7 +54,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -62,7 +64,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr ...@@ -62,7 +64,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new SQLServerPlatform(); return new SQLServerPlatform();
} }
...@@ -70,7 +72,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr ...@@ -70,7 +72,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new SQLServerSchemaManager($conn); return new SQLServerSchemaManager($conn);
} }
......
...@@ -6,8 +6,12 @@ namespace Doctrine\DBAL\Driver; ...@@ -6,8 +6,12 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SqliteSchemaManager; use Doctrine\DBAL\Schema\SqliteSchemaManager;
use function strpos; use function strpos;
...@@ -21,7 +25,7 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver ...@@ -21,7 +25,7 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
* *
* @link http://www.sqlite.org/c3ref/c_abort.html * @link http://www.sqlite.org/c3ref/c_abort.html
*/ */
public function convertException($message, DriverException $exception) public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{ {
if (strpos($exception->getMessage(), 'database is locked') !== false) { if (strpos($exception->getMessage(), 'database is locked') !== false) {
return new Exception\LockWaitTimeoutException($message, $exception); return new Exception\LockWaitTimeoutException($message, $exception);
...@@ -69,13 +73,13 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver ...@@ -69,13 +73,13 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
return new Exception\ConnectionException($message, $exception); return new Exception\ConnectionException($message, $exception);
} }
return new Exception\DriverException($message, $exception); return new DriverException($message, $exception);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabase(Connection $conn) public function getDatabase(Connection $conn) : ?string
{ {
$params = $conn->getParams(); $params = $conn->getParams();
...@@ -85,7 +89,7 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver ...@@ -85,7 +89,7 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDatabasePlatform() public function getDatabasePlatform() : AbstractPlatform
{ {
return new SqlitePlatform(); return new SqlitePlatform();
} }
...@@ -93,7 +97,7 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver ...@@ -93,7 +97,7 @@ abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSchemaManager(Connection $conn) public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{ {
return new SqliteSchemaManager($conn); return new SqliteSchemaManager($conn);
} }
......
...@@ -4,6 +4,9 @@ declare(strict_types=1); ...@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception\DriverException;
/** /**
* Contract for a driver that is capable of converting DBAL driver exceptions into standardized DBAL driver exceptions. * Contract for a driver that is capable of converting DBAL driver exceptions into standardized DBAL driver exceptions.
*/ */
...@@ -15,10 +18,10 @@ interface ExceptionConverterDriver ...@@ -15,10 +18,10 @@ interface ExceptionConverterDriver
* It evaluates the vendor specific error code and SQLSTATE and transforms * It evaluates the vendor specific error code and SQLSTATE and transforms
* it into a unified {@link Doctrine\DBAL\Exception\DriverException} subclass. * it into a unified {@link Doctrine\DBAL\Exception\DriverException} subclass.
* *
* @param string $message The DBAL exception message to use. * @param string $message The DBAL exception message to use.
* @param DriverException $exception The DBAL driver exception to convert. * @param DriverExceptionInterface $exception The DBAL driver exception to convert.
* *
* @return \Doctrine\DBAL\Exception\DriverException An instance of one of the DriverException subclasses. * @return DriverException An instance of one of the DriverException subclasses.
*/ */
public function convertException($message, DriverException $exception); public function convertException(string $message, DriverExceptionInterface $exception) : DriverException;
} }
...@@ -5,6 +5,11 @@ declare(strict_types=1); ...@@ -5,6 +5,11 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2; namespace Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\AbstractDB2Driver; use Doctrine\DBAL\Driver\AbstractDB2Driver;
use Doctrine\DBAL\Driver\Connection;
use function array_keys;
use function array_map;
use function implode;
use function sprintf;
/** /**
* IBM DB2 Driver. * IBM DB2 Driver.
...@@ -14,28 +19,42 @@ class DB2Driver extends AbstractDB2Driver ...@@ -14,28 +19,42 @@ class DB2Driver extends AbstractDB2Driver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
if (! isset($params['protocol'])) { string $username = '',
$params['protocol'] = 'TCPIP'; string $password = '',
} array $driverOptions = []
) : Connection {
if ($params['host'] !== 'localhost' && $params['host'] !== '127.0.0.1') { if ($params['host'] !== 'localhost' && $params['host'] !== '127.0.0.1') {
// if the host isn't localhost, use extended connection params // if the host isn't localhost, use extended connection params
$params['dbname'] = 'DRIVER={IBM DB2 ODBC DRIVER}' . $params['dbname'] = $this->buildConnectionString($params, $username, $password);
';DATABASE=' . $params['dbname'] .
';HOSTNAME=' . $params['host'] . $username = $password = '';
';PROTOCOL=' . $params['protocol'] . }
';UID=' . $username .
';PWD=' . $password . ';'; return new DB2Connection($params, $username, $password, $driverOptions);
if (isset($params['port'])) { }
$params['dbname'] .= 'PORT=' . $params['port'];
} /**
* @param string[] $params
$username = null; */
$password = null; private function buildConnectionString(array $params, string $username, string $password) : string
{
$connectionParams = [
'DRIVER' => '{IBM DB2 ODBC DRIVER}',
'DATABASE' => $params['dbname'],
'HOSTNAME' => $params['host'],
'PROTOCOL' => $params['protocol'] ?? 'TCPIP',
'UID' => $username,
'PWD' => $password,
];
if (isset($params['port'])) {
$connectionParams['PORT'] = $params['port'];
} }
return new DB2Connection($params, (string) $username, (string) $password, $driverOptions); return implode(';', array_map(static function (string $key, string $value) : string {
return sprintf('%s=%s', $key, $value);
}, array_keys($connectionParams), $connectionParams));
} }
} }
...@@ -6,16 +6,21 @@ namespace Doctrine\DBAL\Driver\Mysqli; ...@@ -6,16 +6,21 @@ namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\Connection;
class Driver extends AbstractMySQLDriver class Driver extends AbstractMySQLDriver
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
try { try {
return new MysqliConnection($params, (string) $username, (string) $password, $driverOptions); return new MysqliConnection($params, $username, $password, $driverOptions);
} catch (MysqliException $e) { } catch (MysqliException $e) {
throw DBALException::driverException($this, $e); throw DBALException::driverException($this, $e);
} }
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\OCI8; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\Connection;
use const OCI_DEFAULT; use const OCI_DEFAULT;
/** /**
...@@ -16,12 +17,16 @@ class Driver extends AbstractOracleDriver ...@@ -16,12 +17,16 @@ class Driver extends AbstractOracleDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
try { try {
return new OCI8Connection( return new OCI8Connection(
(string) $username, $username,
(string) $password, $password,
$this->_constructDsn($params), $this->_constructDsn($params),
$params['charset'] ?? '', $params['charset'] ?? '',
$params['sessionMode'] ?? OCI_DEFAULT, $params['sessionMode'] ?? OCI_DEFAULT,
...@@ -39,7 +44,7 @@ class Driver extends AbstractOracleDriver ...@@ -39,7 +44,7 @@ class Driver extends AbstractOracleDriver
* *
* @return string The DSN. * @return string The DSN.
*/ */
protected function _constructDsn(array $params) protected function _constructDsn(array $params) : string
{ {
return $this->getEasyConnectString($params); return $this->getEasyConnectString($params);
} }
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOMySql; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOMySql;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use PDO; use PDO;
...@@ -18,8 +19,12 @@ class Driver extends AbstractMySQLDriver ...@@ -18,8 +19,12 @@ class Driver extends AbstractMySQLDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
...@@ -45,7 +50,7 @@ class Driver extends AbstractMySQLDriver ...@@ -45,7 +50,7 @@ class Driver extends AbstractMySQLDriver
* *
* @return string The DSN. * @return string The DSN.
*/ */
protected function constructPdoDsn(array $params) protected function constructPdoDsn(array $params) : string
{ {
$dsn = 'mysql:'; $dsn = 'mysql:';
if (isset($params['host']) && $params['host'] !== '') { if (isset($params['host']) && $params['host'] !== '') {
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOOracle; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOOracle;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use PDO; use PDO;
...@@ -23,8 +24,12 @@ class Driver extends AbstractOracleDriver ...@@ -23,8 +24,12 @@ class Driver extends AbstractOracleDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
...@@ -48,7 +53,7 @@ class Driver extends AbstractOracleDriver ...@@ -48,7 +53,7 @@ class Driver extends AbstractOracleDriver
* *
* @return string The DSN. * @return string The DSN.
*/ */
private function constructPdoDsn(array $params) private function constructPdoDsn(array $params) : string
{ {
$dsn = 'oci:dbname=' . $this->getEasyConnectString($params); $dsn = 'oci:dbname=' . $this->getEasyConnectString($params);
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOPgSql; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOPgSql;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use PDO; use PDO;
...@@ -19,8 +20,12 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -19,8 +20,12 @@ class Driver extends AbstractPostgreSQLDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
...@@ -62,7 +67,7 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -62,7 +67,7 @@ class Driver extends AbstractPostgreSQLDriver
* *
* @return string The DSN. * @return string The DSN.
*/ */
private function _constructPdoDsn(array $params) private function _constructPdoDsn(array $params) : string
{ {
$dsn = 'pgsql:'; $dsn = 'pgsql:';
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOSqlite; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Driver\PDOSqlite;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractSQLiteDriver; use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform;
...@@ -26,8 +27,12 @@ class Driver extends AbstractSQLiteDriver ...@@ -26,8 +27,12 @@ class Driver extends AbstractSQLiteDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
if (isset($driverOptions['userDefinedFunctions'])) { if (isset($driverOptions['userDefinedFunctions'])) {
$this->_userDefinedFunctions = array_merge( $this->_userDefinedFunctions = array_merge(
$this->_userDefinedFunctions, $this->_userDefinedFunctions,
...@@ -63,7 +68,7 @@ class Driver extends AbstractSQLiteDriver ...@@ -63,7 +68,7 @@ class Driver extends AbstractSQLiteDriver
* *
* @return string The DSN. * @return string The DSN.
*/ */
protected function _constructPdoDsn(array $params) protected function _constructPdoDsn(array $params) : string
{ {
$dsn = 'sqlite:'; $dsn = 'sqlite:';
if (isset($params['path'])) { if (isset($params['path'])) {
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\PDOSqlsrv; namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use PDO; use PDO;
use function is_int; use function is_int;
use function sprintf; use function sprintf;
...@@ -17,8 +18,12 @@ class Driver extends AbstractSQLServerDriver ...@@ -17,8 +18,12 @@ class Driver extends AbstractSQLServerDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : DriverConnection {
$pdoOptions = $dsnOptions = []; $pdoOptions = $dsnOptions = [];
foreach ($driverOptions as $option => $value) { foreach ($driverOptions as $option => $value) {
...@@ -49,7 +54,7 @@ class Driver extends AbstractSQLServerDriver ...@@ -49,7 +54,7 @@ class Driver extends AbstractSQLServerDriver
* *
* @return string The DSN. * @return string The DSN.
*/ */
private function _constructPdoDsn(array $params, array $connectionOptions) private function _constructPdoDsn(array $params, array $connectionOptions) : string
{ {
$dsn = 'sqlsrv:server='; $dsn = 'sqlsrv:server=';
......
...@@ -6,9 +6,12 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere; ...@@ -6,9 +6,12 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver; use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver;
use Doctrine\DBAL\Driver\Connection;
use function array_keys; use function array_keys;
use function array_map; use function array_map;
use function array_merge;
use function implode; use function implode;
use function sprintf;
/** /**
* A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension. * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension.
...@@ -20,19 +23,15 @@ class Driver extends AbstractSQLAnywhereDriver ...@@ -20,19 +23,15 @@ class Driver extends AbstractSQLAnywhereDriver
* *
* @throws DBALException If there was a problem establishing the connection. * @throws DBALException If there was a problem establishing the connection.
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
try { try {
return new SQLAnywhereConnection( return new SQLAnywhereConnection(
$this->buildDsn( $this->buildDsn($params, $username, $password, $driverOptions),
$params['host'] ?? null,
$params['port'] ?? null,
$params['server'] ?? null,
$params['dbname'] ?? null,
$username,
$password,
$driverOptions
),
$params['persistent'] ?? false $params['persistent'] ?? false
); );
} catch (SQLAnywhereException $e) { } catch (SQLAnywhereException $e) {
...@@ -43,37 +42,40 @@ class Driver extends AbstractSQLAnywhereDriver ...@@ -43,37 +42,40 @@ class Driver extends AbstractSQLAnywhereDriver
/** /**
* Build the connection string for given connection parameters and driver options. * Build the connection string for given connection parameters and driver options.
* *
* @param string $host Host address to connect to. * @param mixed[] $params DBAL connection parameters
* @param int $port Port to use for the connection (default to SQL Anywhere standard port 2638).
* @param string $server Database server name on the host to connect to.
* SQL Anywhere allows multiple database server instances on the same host,
* therefore specifying the server instance name to use is mandatory.
* @param string $dbname Name of the database on the server instance to connect to.
* @param string $username User name to use for connection authentication. * @param string $username User name to use for connection authentication.
* @param string $password Password to use for connection authentication. * @param string $password Password to use for connection authentication.
* @param mixed[] $driverOptions Additional parameters to use for the connection. * @param mixed[] $driverOptions Additional parameters to use for the connection.
*
* @return string
*/ */
private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = []) private function buildDsn(array $params, string $username, string $password, array $driverOptions = []) : string
{ {
$host = $host ?: 'localhost'; $connectionParams = [];
$port = $port ?: 2638;
if (isset($params['host'])) {
$host = $params['host'];
if (! empty($server)) { if (isset($params['port'])) {
$server = ';ServerName=' . $server; $host .= sprintf(':%d', $params['port']);
}
$connectionParams['HOST'] = $host;
} }
return 'HOST=' . $host . ':' . $port . if (isset($params['server'])) {
$server . $connectionParams['ServerName'] = $params['server'];
';DBN=' . $dbname . }
';UID=' . $username .
';PWD=' . $password . if (isset($params['dbname'])) {
';' . implode( $connectionParams['DBN'] = $params['dbname'];
';', }
array_map(static function ($key, $value) {
return $key . '=' . $value; $connectionParams['UID'] = $username;
}, array_keys($driverOptions), $driverOptions) $connectionParams['PWD'] = $password;
);
$connectionParams = array_merge($connectionParams, $driverOptions);
return implode(';', array_map(static function (string $key, string $value) : string {
return sprintf('%s=%s', $key, $value);
}, array_keys($connectionParams), $connectionParams));
} }
} }
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\SQLSrv; namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\Connection;
/** /**
* Driver for ext/sqlsrv. * Driver for ext/sqlsrv.
...@@ -14,8 +15,12 @@ class Driver extends AbstractSQLServerDriver ...@@ -14,8 +15,12 @@ class Driver extends AbstractSQLServerDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(
{ array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : Connection {
if (! isset($params['host'])) { if (! isset($params['host'])) {
throw new SQLSrvException('Missing "host" in configuration for sqlsrv driver.'); throw new SQLSrvException('Missing "host" in configuration for sqlsrv driver.');
} }
...@@ -33,11 +38,11 @@ class Driver extends AbstractSQLServerDriver ...@@ -33,11 +38,11 @@ class Driver extends AbstractSQLServerDriver
$driverOptions['CharacterSet'] = $params['charset']; $driverOptions['CharacterSet'] = $params['charset'];
} }
if ($username !== null) { if ($username !== '') {
$driverOptions['UID'] = $username; $driverOptions['UID'] = $username;
} }
if ($password !== null) { if ($password !== '') {
$driverOptions['PWD'] = $password; $driverOptions['PWD'] = $password;
} }
......
...@@ -218,8 +218,8 @@ class PoolingShardConnection extends Connection ...@@ -218,8 +218,8 @@ class PoolingShardConnection extends Connection
$connectionParams = $this->connectionParameters[$shardId]; $connectionParams = $this->connectionParameters[$shardId];
$user = $connectionParams['user'] ?? null; $user = $connectionParams['user'] ?? '';
$password = $connectionParams['password'] ?? null; $password = $connectionParams['password'] ?? '';
return $this->_driver->connect($connectionParams, $user, $password, $driverOptions); return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
} }
......
...@@ -22,9 +22,7 @@ interface VersionAwarePlatformDriver ...@@ -22,9 +22,7 @@ interface VersionAwarePlatformDriver
* @param string $version The platform/server version string to evaluate. This should be given in the notation * @param string $version The platform/server version string to evaluate. This should be given in the notation
* the underlying database vendor uses. * the underlying database vendor uses.
* *
* @return AbstractPlatform
*
* @throws DBALException If the given version string could not be evaluated. * @throws DBALException If the given version string could not be evaluated.
*/ */
public function createDatabasePlatformForVersion($version); public function createDatabasePlatformForVersion(string $version) : AbstractPlatform;
} }
...@@ -695,8 +695,8 @@ class ConnectionTest extends DbalTestCase ...@@ -695,8 +695,8 @@ class ConnectionTest extends DbalTestCase
/** @var Driver|VersionAwarePlatformDriver|MockObject $driverMock */ /** @var Driver|VersionAwarePlatformDriver|MockObject $driverMock */
$driverMock = $this->createMock([Driver::class, VersionAwarePlatformDriver::class]); $driverMock = $this->createMock([Driver::class, VersionAwarePlatformDriver::class]);
/** @var ServerInfoAwareConnection|MockObject $driverConnectionMock */ /** @var DriverConnection|ServerInfoAwareConnection|MockObject $driverConnectionMock */
$driverConnectionMock = $this->createMock(ServerInfoAwareConnection::class); $driverConnectionMock = $this->createMock([DriverConnection::class, ServerInfoAwareConnection::class]);
/** @var AbstractPlatform|MockObject $platformMock */ /** @var AbstractPlatform|MockObject $platformMock */
$platformMock = $this->getMockForAbstractClass(AbstractPlatform::class); $platformMock = $this->getMockForAbstractClass(AbstractPlatform::class);
......
...@@ -33,8 +33,8 @@ abstract class AbstractDriverTest extends DbalFunctionalTestCase ...@@ -33,8 +33,8 @@ abstract class AbstractDriverTest extends DbalFunctionalTestCase
$params = $this->connection->getParams(); $params = $this->connection->getParams();
unset($params['dbname']); unset($params['dbname']);
$user = $params['user'] ?? null; $user = $params['user'] ?? '';
$password = $params['password'] ?? null; $password = $params['password'] ?? '';
$connection = $this->driver->connect($params, $user, $password); $connection = $this->driver->connect($params, $user, $password);
......
...@@ -79,8 +79,8 @@ class DriverTest extends AbstractDriverTest ...@@ -79,8 +79,8 @@ class DriverTest extends AbstractDriverTest
$parameters = $this->connection->getParams(); $parameters = $this->connection->getParams();
$parameters['application_name'] = 'doctrine'; $parameters['application_name'] = 'doctrine';
$user = $parameters['user'] ?? null; $user = $parameters['user'] ?? '';
$password = $parameters['password'] ?? null; $password = $parameters['password'] ?? '';
$connection = $this->driver->connect($parameters, $user, $password); $connection = $this->driver->connect($parameters, $user, $password);
......
...@@ -117,8 +117,8 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase ...@@ -117,8 +117,8 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
$params['dbname'] = 'test_drop_database'; $params['dbname'] = 'test_drop_database';
} }
$user = $params['user'] ?? null; $user = $params['user'] ?? '';
$password = $params['password'] ?? null; $password = $params['password'] ?? '';
$connection = $this->connection->getDriver()->connect($params, $user, $password); $connection = $this->connection->getDriver()->connect($params, $user, $password);
......
...@@ -51,8 +51,8 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -51,8 +51,8 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
$params = $this->connection->getParams(); $params = $this->connection->getParams();
$params['dbname'] = 'test_drop_database'; $params['dbname'] = 'test_drop_database';
$user = $params['user'] ?? null; $user = $params['user'] ?? '';
$password = $params['password'] ?? null; $password = $params['password'] ?? '';
$connection = $this->connection->getDriver()->connect($params, $user, $password); $connection = $this->connection->getDriver()->connect($params, $user, $password);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment