From e7ee98c1f6ce2265bbeba22d9ca034011b66e404 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 1 Apr 2018 19:53:57 -0700 Subject: [PATCH 1/3] [DBAL-3079] Reworked the usage of PDO inPDOConnection from inheritance to composition --- lib/Doctrine/DBAL/Connection.php | 16 ++-- .../Connections/MasterSlaveConnection.php | 8 +- lib/Doctrine/DBAL/Driver/Connection.php | 9 ++- .../DBAL/Driver/IBMDB2/DB2Connection.php | 4 +- .../DBAL/Driver/Mysqli/MysqliConnection.php | 4 +- .../DBAL/Driver/OCI8/OCI8Connection.php | 5 +- lib/Doctrine/DBAL/Driver/PDOConnection.php | 77 +++++++++++++++---- lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php | 8 +- lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php | 6 +- .../SQLAnywhere/SQLAnywhereConnection.php | 6 +- .../DBAL/Driver/SQLSrv/SQLSrvConnection.php | 4 +- lib/Doctrine/DBAL/Portability/Connection.php | 10 +-- .../Tests/DBAL/Driver/PDOPgSql/DriverTest.php | 16 ++-- .../Functional/Driver/PDOConnectionTest.php | 4 +- .../DBAL/Functional/Ticket/DBAL630Test.php | 16 +++- .../Tests/Mocks/DriverConnectionMock.php | 5 +- 16 files changed, 122 insertions(+), 76 deletions(-) diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 44082eecd44..91b4dbe61f9 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -993,25 +993,19 @@ public function project($query, array $params, Closure $function) } /** - * Executes an SQL statement, returning a result set as a Statement object. - * - * @return \Doctrine\DBAL\Driver\Statement - * - * @throws \Doctrine\DBAL\DBALException + * {@inheritDoc} */ - public function query() + public function query(string $sql) { $this->connect(); - $args = func_get_args(); - $logger = $this->_config->getSQLLogger(); - $logger->startQuery($args[0]); + $logger->startQuery($sql); try { - $statement = $this->_conn->query(...$args); + $statement = $this->_conn->query($sql); } catch (Exception $ex) { - throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]); + throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql); } $statement->setFetchMode($this->defaultFetchMode); diff --git a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php index ac12b6814d9..adce5eabb53 100644 --- a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php +++ b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php @@ -345,16 +345,14 @@ public function rollbackSavepoint($savepoint) /** * {@inheritDoc} */ - public function query() + public function query(string $sql) { $this->connect('master'); - $args = func_get_args(); - $logger = $this->getConfiguration()->getSQLLogger(); - $logger->startQuery($args[0]); + $logger->startQuery($sql); - $statement = $this->_conn->query(...$args); + $statement = $this->_conn->query($sql); $logger->stopQuery(); diff --git a/lib/Doctrine/DBAL/Driver/Connection.php b/lib/Doctrine/DBAL/Driver/Connection.php index f5a100f5339..eac0cd0ebfd 100644 --- a/lib/Doctrine/DBAL/Driver/Connection.php +++ b/lib/Doctrine/DBAL/Driver/Connection.php @@ -2,6 +2,7 @@ namespace Doctrine\DBAL\Driver; +use Doctrine\DBAL\DBALException; use Doctrine\DBAL\ParameterType; /** @@ -19,16 +20,18 @@ interface Connection * * @param string $prepareString * - * @return \Doctrine\DBAL\Driver\Statement + * @return Statement */ public function prepare($prepareString); /** * Executes an SQL statement, returning a result set as a Statement object. * - * @return \Doctrine\DBAL\Driver\Statement + * @return Statement + * + * @throws DBALException */ - public function query(); + public function query(string $sql); /** * Quotes a string for use in a query. diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php index f3d32cb327f..aec0fffd858 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php @@ -86,10 +86,8 @@ public function prepare($sql) /** * {@inheritdoc} */ - public function query() + public function query(string $sql) { - $args = func_get_args(); - $sql = $args[0]; $stmt = $this->prepare($sql); $stmt->execute(); diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php index d39b44ffbe2..9d92b564ab7 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php @@ -129,10 +129,8 @@ public function prepare($prepareString) /** * {@inheritdoc} */ - public function query() + public function query(string $sql) { - $args = func_get_args(); - $sql = $args[0]; $stmt = $this->prepare($sql); $stmt->execute(); diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php index edd37c585ff..38a20413e56 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php @@ -108,11 +108,8 @@ public function prepare($prepareString) /** * {@inheritdoc} */ - public function query() + public function query(string $sql) { - $args = func_get_args(); - $sql = $args[0]; - //$fetchMode = $args[1]; $stmt = $this->prepare($sql); $stmt->execute(); diff --git a/lib/Doctrine/DBAL/Driver/PDOConnection.php b/lib/Doctrine/DBAL/Driver/PDOConnection.php index 19cc18f406d..94fb34d9b87 100644 --- a/lib/Doctrine/DBAL/Driver/PDOConnection.php +++ b/lib/Doctrine/DBAL/Driver/PDOConnection.php @@ -4,17 +4,17 @@ use Doctrine\DBAL\ParameterType; use PDO; -use function count; -use function func_get_args; /** * PDO implementation of the Connection interface. - * Used by all PDO-based drivers. * - * @since 2.0 + * Used by all PDO-based drivers. */ -class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection +class PDOConnection implements Connection, ServerInfoAwareConnection { + /** @var PDO */ + private $connection; + /** * @param string $dsn * @param string|null $user @@ -26,8 +26,8 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection public function __construct($dsn, $user = null, $password = null, array $options = null) { try { - parent::__construct($dsn, $user, $password, $options); - $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->connection = new PDO($dsn, $user, $password, $options); + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -39,7 +39,7 @@ public function __construct($dsn, $user = null, $password = null, array $options public function exec($statement) { try { - return parent::exec($statement); + return $this->connection->exec($statement); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -50,17 +50,17 @@ public function exec($statement) */ public function getServerVersion() { - return PDO::getAttribute(PDO::ATTR_SERVER_VERSION); + return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); } /** * {@inheritdoc} */ - public function prepare($prepareString, $driverOptions = []) + public function prepare($prepareString) { try { return $this->createStatement( - parent::prepare($prepareString, $driverOptions) + $this->connection->prepare($prepareString) ); } catch (\PDOException $exception) { throw new PDOException($exception); @@ -70,13 +70,11 @@ public function prepare($prepareString, $driverOptions = []) /** * {@inheritdoc} */ - public function query() + public function query(string $sql) { - $args = func_get_args(); - try { return $this->createStatement( - parent::query(...$args) + $this->connection->query($sql) ); } catch (\PDOException $exception) { throw new PDOException($exception); @@ -88,7 +86,7 @@ public function query() */ public function quote($input, $type = ParameterType::STRING) { - return parent::quote($input, $type); + return $this->connection->quote($input, $type); } /** @@ -96,7 +94,7 @@ public function quote($input, $type = ParameterType::STRING) */ public function lastInsertId($name = null) { - return parent::lastInsertId($name); + return $this->connection->lastInsertId($name); } /** @@ -117,4 +115,49 @@ protected function createStatement(\PDOStatement $stmt) : PDOStatement { return new PDOStatement($stmt); } + + /** + * {@inheritDoc} + */ + public function beginTransaction() + { + return $this->connection->beginTransaction(); + } + + /** + * {@inheritDoc} + */ + public function commit() + { + return $this->connection->commit(); + } + + /** + * {@inheritDoc} + */ + public function rollBack() + { + return $this->connection->rollBack(); + } + + /** + * {@inheritDoc} + */ + public function errorCode() + { + return $this->connection->errorCode(); + } + + /** + * {@inheritDoc} + */ + public function errorInfo() + { + return $this->connection->errorInfo(); + } + + public function getWrappedConnection() : PDO + { + return $this->connection; + } } diff --git a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php index b63fc3aa46e..5b6d0b0288f 100644 --- a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php @@ -22,7 +22,7 @@ class Driver extends AbstractPostgreSQLDriver public function connect(array $params, $username = null, $password = null, array $driverOptions = []) { try { - $pdo = new PDOConnection( + $connection = new PDOConnection( $this->_constructPdoDsn($params), $username, $password, @@ -34,7 +34,7 @@ public function connect(array $params, $username = null, $password = null, array || true === $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] ) ) { - $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true); + $connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true); } /* defining client_encoding via SET NAMES to avoid inconsistent DSN support @@ -42,10 +42,10 @@ public function connect(array $params, $username = null, $password = null, array * - passing client_encoding via the 'options' param breaks pgbouncer support */ if (isset($params['charset'])) { - $pdo->query('SET NAMES \''.$params['charset'].'\''); + $connection->query('SET NAMES \'' . $params['charset'] . '\''); } - return $pdo; + return $connection; } catch (PDOException $e) { throw DBALException::driverException($this, $e); } diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php index f19045c3145..83fa6a19025 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php @@ -36,7 +36,7 @@ public function connect(array $params, $username = null, $password = null, array } try { - $pdo = new PDOConnection( + $connection = new PDOConnection( $this->_constructPdoDsn($params), $username, $password, @@ -46,11 +46,13 @@ public function connect(array $params, $username = null, $password = null, array throw DBALException::driverException($this, $ex); } + $pdo = $connection->getWrappedConnection(); + foreach ($this->_userDefinedFunctions as $fn => $data) { $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); } - return $pdo; + return $connection; } /** diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php index 7b4e84ed5c4..77660ffc575 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php @@ -158,11 +158,9 @@ public function prepare($prepareString) /** * {@inheritdoc} */ - public function query() + public function query(string $sql) { - $args = func_get_args(); - $stmt = $this->prepare($args[0]); - + $stmt = $this->prepare($sql); $stmt->execute(); return $stmt; diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php index 78216ed837c..557c7807a49 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php @@ -87,10 +87,8 @@ public function prepare($sql) /** * {@inheritDoc} */ - public function query() + public function query(string $sql) { - $args = func_get_args(); - $sql = $args[0]; $stmt = $this->prepare($sql); $stmt->execute(); diff --git a/lib/Doctrine/DBAL/Portability/Connection.php b/lib/Doctrine/DBAL/Portability/Connection.php index d0aac151c26..7bbfaa28352 100644 --- a/lib/Doctrine/DBAL/Portability/Connection.php +++ b/lib/Doctrine/DBAL/Portability/Connection.php @@ -4,9 +4,9 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\ColumnCase; +use Doctrine\DBAL\Driver\PDOConnection; use const CASE_LOWER; use const CASE_UPPER; -use function func_get_args; /** * Portability wrapper for a Connection. @@ -69,9 +69,9 @@ public function connect() } if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) { - if ($this->_conn instanceof \Doctrine\DBAL\Driver\PDOConnection) { + if ($this->_conn instanceof PDOConnection) { // make use of c-level support for case handling - $this->_conn->setAttribute(\PDO::ATTR_CASE, $params['fetch_case']); + $this->_conn->getWrappedConnection()->setAttribute(\PDO::ATTR_CASE, $params['fetch_case']); } else { $this->case = ($params['fetch_case'] === ColumnCase::LOWER) ? CASE_LOWER : CASE_UPPER; } @@ -122,11 +122,11 @@ public function prepare($statement) /** * {@inheritdoc} */ - public function query() + public function query(string $sql) { $this->connect(); - $stmt = $this->_conn->query(...func_get_args()); + $stmt = $this->_conn->query($sql); $stmt = new Statement($stmt, $this); $stmt->setFetchMode($this->defaultFetchMode); diff --git a/tests/Doctrine/Tests/DBAL/Driver/PDOPgSql/DriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/PDOPgSql/DriverTest.php index 6fd187e3d28..45e9c530a11 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/PDOPgSql/DriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/PDOPgSql/DriverTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Driver\PDOPgSql; +use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOPgSql\Driver; use Doctrine\Tests\DBAL\Driver\AbstractPostgreSQLDriverTest; use PDO; @@ -31,10 +32,10 @@ public function testConnectionDisablesPreparesOnPhp56() $GLOBALS['db_password'] ); - self::assertInstanceOf('Doctrine\DBAL\Driver\PDOConnection', $connection); + self::assertInstanceOf(PDOConnection::class, $connection); try { - self::assertTrue($connection->getAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES)); + self::assertTrue($connection->getWrappedConnection()->getAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES)); } catch (PDOException $ignored) { /** @link https://bugs.php.net/bug.php?id=68371 */ $this->markTestIncomplete('See https://bugs.php.net/bug.php?id=68371'); @@ -58,10 +59,13 @@ public function testConnectionDoesNotDisablePreparesOnPhp56WhenAttributeDefined( array(PDO::PGSQL_ATTR_DISABLE_PREPARES => false) ); - self::assertInstanceOf('Doctrine\DBAL\Driver\PDOConnection', $connection); + self::assertInstanceOf(PDOConnection::class, $connection); try { - self::assertNotSame(true, $connection->getAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES)); + self::assertNotSame( + true, + $connection->getWrappedConnection()->getAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES) + ); } catch (PDOException $ignored) { /** @link https://bugs.php.net/bug.php?id=68371 */ $this->markTestIncomplete('See https://bugs.php.net/bug.php?id=68371'); @@ -85,10 +89,10 @@ public function testConnectionDisablePreparesOnPhp56WhenDisablePreparesIsExplici array(PDO::PGSQL_ATTR_DISABLE_PREPARES => true) ); - self::assertInstanceOf('Doctrine\DBAL\Driver\PDOConnection', $connection); + self::assertInstanceOf(PDOConnection::class, $connection); try { - self::assertTrue($connection->getAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES)); + self::assertTrue($connection->getWrappedConnection()->getAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES)); } catch (PDOException $ignored) { /** @link https://bugs.php.net/bug.php?id=68371 */ $this->markTestIncomplete('See https://bugs.php.net/bug.php?id=68371'); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOConnectionTest.php index 29f18f8c78f..359e81b70d9 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOConnectionTest.php @@ -72,7 +72,9 @@ public function testThrowsWrappedExceptionOnPrepare() // Emulated prepared statements have to be disabled for this test // so that PDO actually communicates with the database server to check the query. - $this->driverConnection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + $this->driverConnection + ->getWrappedConnection() + ->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); $this->driverConnection->prepare('foo'); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php b/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php index 70210133b1f..8588a16ba6e 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php @@ -38,7 +38,9 @@ protected function setUp() protected function tearDown() { if ($this->running) { - $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + $this->_conn->getWrappedConnection() + ->getWrappedConnection() + ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } parent::tearDown(); @@ -72,7 +74,9 @@ public function testBooleanConversionBoolParamRealPrepares() public function testBooleanConversionBoolParamEmulatedPrepares() { - $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); + $this->_conn->getWrappedConnection() + ->getWrappedConnection() + ->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $platform = $this->_conn->getDatabasePlatform(); @@ -96,7 +100,9 @@ public function testBooleanConversionNullParamEmulatedPrepares( $statementValue, $databaseConvertedValue ) { - $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); + $this->_conn->getWrappedConnection() + ->getWrappedConnection() + ->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $platform = $this->_conn->getDatabasePlatform(); @@ -120,7 +126,9 @@ public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInB $statementValue, $databaseConvertedValue ) { - $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); + $this->_conn->getWrappedConnection() + ->getWrappedConnection() + ->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $platform = $this->_conn->getDatabasePlatform(); diff --git a/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php b/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php index 760dbff40cf..74aed7005d7 100644 --- a/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php +++ b/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php @@ -7,7 +7,10 @@ class DriverConnectionMock implements \Doctrine\DBAL\Driver\Connection { public function prepare($prepareString) {} - public function query() {} + + public function query(string $sql) + { + } public function quote($input, $type = ParameterType::STRING) { From 2aa555455318a974e23e3b480484a83ffa47d9ac Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Tue, 10 Apr 2018 23:58:28 -0700 Subject: [PATCH 2/3] [DBAL-3079] Added type hints to query-related method parameters and return values --- .../DBAL/Cache/ResultCacheStatement.php | 2 +- lib/Doctrine/DBAL/Connection.php | 80 +++++++------------ .../Connections/MasterSlaveConnection.php | 22 +++-- lib/Doctrine/DBAL/Driver/Connection.php | 16 +--- .../DBAL/Driver/IBMDB2/DB2Connection.php | 9 ++- .../DBAL/Driver/IBMDB2/DB2Statement.php | 4 +- .../DBAL/Driver/Mysqli/MysqliConnection.php | 13 +-- .../DBAL/Driver/Mysqli/MysqliStatement.php | 2 +- .../DBAL/Driver/OCI8/OCI8Connection.php | 11 +-- .../DBAL/Driver/OCI8/OCI8Statement.php | 2 +- lib/Doctrine/DBAL/Driver/PDOConnection.php | 8 +- lib/Doctrine/DBAL/Driver/PDOStatement.php | 2 +- .../SQLAnywhere/SQLAnywhereConnection.php | 11 +-- .../SQLAnywhere/SQLAnywhereStatement.php | 5 +- .../DBAL/Driver/SQLSrv/SQLSrvConnection.php | 9 ++- .../DBAL/Driver/SQLSrv/SQLSrvStatement.php | 3 +- lib/Doctrine/DBAL/Driver/Statement.php | 2 +- lib/Doctrine/DBAL/Portability/Connection.php | 10 ++- lib/Doctrine/DBAL/Portability/Statement.php | 2 +- lib/Doctrine/DBAL/Statement.php | 2 +- .../Driver/SQLSrv/StatementTest.php | 2 +- tests/Doctrine/Tests/DBAL/StatementTest.php | 44 +++++----- .../Tests/Mocks/DriverConnectionMock.php | 26 ------ tests/Doctrine/Tests/Mocks/DriverMock.php | 3 +- 24 files changed, 119 insertions(+), 171 deletions(-) delete mode 100644 tests/Doctrine/Tests/Mocks/DriverConnectionMock.php diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index f1de248d460..cd39805a25d 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -205,7 +205,7 @@ public function fetchColumn($columnIndex = 0) * * @return int The number of rows. */ - public function rowCount() + public function rowCount() : int { return $this->statement->rowCount(); } diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 91b4dbe61f9..eaf5e404a4b 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -2,7 +2,9 @@ namespace Doctrine\DBAL; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Exception\InvalidArgumentException; use Closure; use Exception; @@ -17,7 +19,6 @@ use Throwable; use function array_key_exists; use function array_merge; -use function func_get_args; use function implode; use function is_int; use function is_string; @@ -856,18 +857,16 @@ public function fetchAll($sql, array $params = [], $types = []) /** * Prepares an SQL statement. * - * @param string $statement The SQL statement to prepare. + * @param string $sql The SQL statement to prepare. * - * @return \Doctrine\DBAL\Statement The prepared statement. - * - * @throws \Doctrine\DBAL\DBALException + * @throws DBALException */ - public function prepare($statement) + public function prepare(string $sql) : DriverStatement { try { - $stmt = new Statement($statement, $this); + $stmt = new Statement($sql, $this); } catch (Exception $ex) { - throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement); + throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql); } $stmt->setFetchMode($this->defaultFetchMode); @@ -881,16 +880,14 @@ public function prepare($statement) * If the query is parametrized, a prepared statement is used. * If an SQLLogger is configured, the execution is logged. * - * @param string $query The SQL query to execute. - * @param array $params The parameters to bind to the query, if any. - * @param array $types The types the previous parameters are in. - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. - * - * @return \Doctrine\DBAL\Driver\Statement The executed statement. + * @param string $query The SQL query to execute. + * @param mixed[] $params The parameters to bind to the query, if any. + * @param (int|string|Type)[] $types The types the previous parameters are in. + * @param QueryCacheProfile|null $qcp The query cache profile, optional. * - * @throws \Doctrine\DBAL\DBALException + * @throws DBALException */ - public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) + public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement { if ($qcp !== null) { return $this->executeCacheQuery($query, $params, $types, $qcp); @@ -929,16 +926,14 @@ public function executeQuery($query, array $params = [], $types = [], QueryCache /** * Executes a caching query. * - * @param string $query The SQL query to execute. - * @param array $params The parameters to bind to the query, if any. - * @param array $types The types the previous parameters are in. - * @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile. - * - * @return \Doctrine\DBAL\Driver\ResultStatement + * @param string $query The SQL query to execute. + * @param mixed[] $params The parameters to bind to the query, if any. + * @param (int|string)|Type[] $types The types the previous parameters are in. + * @param QueryCacheProfile $qcp The query cache profile. * - * @throws \Doctrine\DBAL\Cache\CacheException + * @throws DBALException */ - public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) + public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement { $resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl(); if ( ! $resultCache) { @@ -995,7 +990,7 @@ public function project($query, array $params, Closure $function) /** * {@inheritDoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $this->connect(); @@ -1021,15 +1016,13 @@ public function query(string $sql) * * This method supports PDO binding types as well as DBAL mapping types. * - * @param string $query The SQL query. - * @param array $params The query parameters. - * @param array $types The parameter types. - * - * @return int The number of affected rows. + * @param string $query The SQL query. + * @param mixed[] $params The query parameters. + * @param (int|string|Type)[] $types The parameter types. * - * @throws \Doctrine\DBAL\DBALException + * @throws DBALException */ - public function executeUpdate($query, array $params = [], array $types = []) + public function executeUpdate(string $query, array $params = [], array $types = []) : int { $this->connect(); @@ -1061,15 +1054,9 @@ public function executeUpdate($query, array $params = [], array $types = []) } /** - * Executes an SQL statement and return the number of affected rows. - * - * @param string $statement - * - * @return int The number of affected rows. - * - * @throws \Doctrine\DBAL\DBALException + * {@inheritDoc} */ - public function exec($statement) + public function exec(string $statement) : int { $this->connect(); @@ -1473,16 +1460,11 @@ public function convertToPHPValue($value, $type) * Binds a set of parameters, some or all of which are typed with a PDO binding type * or DBAL mapping type, to a given statement. * - * @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to. - * @param array $params The map/list of named/positional parameters. - * @param array $types The parameter types (PDO binding types or DBAL mapping types). - * - * @return void - * - * @internal Duck-typing used on the $stmt parameter to support driver statements as well as - * raw PDOStatement instances. + * @param DriverStatement $stmt The statement to bind the values to. + * @param mixed[] $params The map/list of named/positional parameters. + * @param (int|string|Type)[] $types The parameter types. */ - private function _bindTypedValues($stmt, array $params, array $types) + private function _bindTypedValues(DriverStatement $stmt, array $params, array $types) : void { // Check whether parameters are positional or named. Mixing is not allowed, just like in PDO. if (is_int(key($params))) { diff --git a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php index adce5eabb53..fe5e5516efa 100644 --- a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php +++ b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php @@ -2,15 +2,16 @@ namespace Doctrine\DBAL\Connections; +use Doctrine\Common\EventManager; +use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; -use Doctrine\DBAL\Configuration; -use Doctrine\Common\EventManager; +use Doctrine\DBAL\Driver\ResultStatement; +use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Event\ConnectionEventArgs; use Doctrine\DBAL\Events; use function array_rand; use function count; -use function func_get_args; /** * Master-Slave Connection @@ -86,10 +87,7 @@ class MasterSlaveConnection extends Connection /** * Creates Master Slave Connection. * - * @param array $params - * @param \Doctrine\DBAL\Driver $driver - * @param \Doctrine\DBAL\Configuration|null $config - * @param \Doctrine\Common\EventManager|null $eventManager + * @param mixed[] $params * * @throws \InvalidArgumentException */ @@ -222,7 +220,7 @@ protected function chooseConnectionConfiguration($connectionName, $params) /** * {@inheritDoc} */ - public function executeUpdate($query, array $params = [], array $types = []) + public function executeUpdate(string $query, array $params = [], array $types = []) : int { $this->connect('master'); @@ -305,7 +303,7 @@ public function insert($tableName, array $data, array $types = []) /** * {@inheritDoc} */ - public function exec($statement) + public function exec(string $statement) : int { $this->connect('master'); @@ -345,7 +343,7 @@ public function rollbackSavepoint($savepoint) /** * {@inheritDoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $this->connect('master'); @@ -362,10 +360,10 @@ public function query(string $sql) /** * {@inheritDoc} */ - public function prepare($statement) + public function prepare(string $sql) : Statement { $this->connect('master'); - return parent::prepare($statement); + return parent::prepare($sql); } } diff --git a/lib/Doctrine/DBAL/Driver/Connection.php b/lib/Doctrine/DBAL/Driver/Connection.php index eac0cd0ebfd..4c133369988 100644 --- a/lib/Doctrine/DBAL/Driver/Connection.php +++ b/lib/Doctrine/DBAL/Driver/Connection.php @@ -17,21 +17,15 @@ interface Connection { /** * Prepares a statement for execution and returns a Statement object. - * - * @param string $prepareString - * - * @return Statement */ - public function prepare($prepareString); + public function prepare(string $sql) : Statement; /** * Executes an SQL statement, returning a result set as a Statement object. * - * @return Statement - * * @throws DBALException */ - public function query(string $sql); + public function query(string $sql) : ResultStatement; /** * Quotes a string for use in a query. @@ -46,11 +40,9 @@ public function quote($input, $type = ParameterType::STRING); /** * Executes an SQL statement and return the number of affected rows. * - * @param string $statement - * - * @return int + * @throws DBALException */ - public function exec($statement); + public function exec(string $statement) : int; /** * Returns the ID of the last inserted row or sequence value. diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php index aec0fffd858..e122748dc50 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php @@ -3,7 +3,9 @@ namespace Doctrine\DBAL\Driver\IBMDB2; use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\ParameterType; use const DB2_AUTOCOMMIT_OFF; use const DB2_AUTOCOMMIT_ON; @@ -21,7 +23,6 @@ use function db2_rollback; use function db2_server_info; use function db2_stmt_errormsg; -use function func_get_args; class DB2Connection implements Connection, ServerInfoAwareConnection { @@ -73,7 +74,7 @@ public function requiresQueryForServerVersion() /** * {@inheritdoc} */ - public function prepare($sql) + public function prepare(string $sql) : DriverStatement { $stmt = @db2_prepare($this->_conn, $sql); if ( ! $stmt) { @@ -86,7 +87,7 @@ public function prepare($sql) /** * {@inheritdoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $stmt = $this->prepare($sql); $stmt->execute(); @@ -111,7 +112,7 @@ public function quote($input, $type = ParameterType::STRING) /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) : int { $stmt = @db2_exec($this->_conn, $statement); diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 54b71d622b9..231a5609895 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -22,8 +22,6 @@ use function db2_num_rows; use function db2_stmt_error; use function db2_stmt_errormsg; -use function func_get_args; -use function func_num_args; use function gettype; use function is_object; use function is_string; @@ -312,7 +310,7 @@ public function fetchColumn($columnIndex = 0) /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { return (@db2_num_rows($this->_stmt)) ? : 0; } diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php index 9d92b564ab7..2383483a876 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php @@ -2,13 +2,14 @@ namespace Doctrine\DBAL\Driver\Mysqli; -use Doctrine\DBAL\Driver\Connection as Connection; +use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\PingableConnection; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\ParameterType; use function defined; use function floor; -use function func_get_args; use function in_array; use function ini_get; use function mysqli_errno; @@ -121,15 +122,15 @@ public function requiresQueryForServerVersion() /** * {@inheritdoc} */ - public function prepare($prepareString) + public function prepare(string $sql) : DriverStatement { - return new MysqliStatement($this->_conn, $prepareString); + return new MysqliStatement($this->_conn, $sql); } /** * {@inheritdoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $stmt = $this->prepare($sql); $stmt->execute(); @@ -148,7 +149,7 @@ public function quote($input, $type = ParameterType::STRING) /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) : int { if (false === $this->_conn->query($statement)) { throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno); diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index 54f401d15b7..dd87d3207ed 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -372,7 +372,7 @@ public function closeCursor() /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { if (false === $this->_columnNames) { return $this->_stmt->affected_rows; diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php index 38a20413e56..07cf3070795 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php @@ -3,7 +3,9 @@ namespace Doctrine\DBAL\Driver\OCI8; use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\ParameterType; use const OCI_COMMIT_ON_SUCCESS; use const OCI_DEFAULT; @@ -11,7 +13,6 @@ use function addcslashes; use function define; use function defined; -use function func_get_args; use function is_float; use function is_int; use function oci_commit; @@ -100,15 +101,15 @@ public function requiresQueryForServerVersion() /** * {@inheritdoc} */ - public function prepare($prepareString) + public function prepare(string $sql) : DriverStatement { - return new OCI8Statement($this->dbh, $prepareString, $this); + return new OCI8Statement($this->dbh, $sql, $this); } /** * {@inheritdoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $stmt = $this->prepare($sql); $stmt->execute(); @@ -132,7 +133,7 @@ public function quote($value, $type = ParameterType::STRING) /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) : int { $stmt = $this->prepare($statement); $stmt->execute(); diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index a20d43312e1..a70da6fb94b 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -491,7 +491,7 @@ public function fetchColumn($columnIndex = 0) /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { return oci_num_rows($this->_sth); } diff --git a/lib/Doctrine/DBAL/Driver/PDOConnection.php b/lib/Doctrine/DBAL/Driver/PDOConnection.php index 94fb34d9b87..ea66dd3649d 100644 --- a/lib/Doctrine/DBAL/Driver/PDOConnection.php +++ b/lib/Doctrine/DBAL/Driver/PDOConnection.php @@ -36,7 +36,7 @@ public function __construct($dsn, $user = null, $password = null, array $options /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) : int { try { return $this->connection->exec($statement); @@ -56,11 +56,11 @@ public function getServerVersion() /** * {@inheritdoc} */ - public function prepare($prepareString) + public function prepare(string $sql) : Statement { try { return $this->createStatement( - $this->connection->prepare($prepareString) + $this->connection->prepare($sql) ); } catch (\PDOException $exception) { throw new PDOException($exception); @@ -70,7 +70,7 @@ public function prepare($prepareString) /** * {@inheritdoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { try { return $this->createStatement( diff --git a/lib/Doctrine/DBAL/Driver/PDOStatement.php b/lib/Doctrine/DBAL/Driver/PDOStatement.php index ece7cca94be..271824bd9a7 100644 --- a/lib/Doctrine/DBAL/Driver/PDOStatement.php +++ b/lib/Doctrine/DBAL/Driver/PDOStatement.php @@ -146,7 +146,7 @@ public function execute($params = null) /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { return $this->stmt->rowCount(); } diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php index 77660ffc575..84438fb2aef 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php @@ -3,9 +3,10 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere; use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\ParameterType; -use function func_get_args; use function is_float; use function is_int; use function is_resource; @@ -118,7 +119,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) : int { if (false === sasql_real_query($this->connection, $statement)) { throw SQLAnywhereException::fromSQLAnywhereError($this->connection); @@ -150,15 +151,15 @@ public function lastInsertId($name = null) /** * {@inheritdoc} */ - public function prepare($prepareString) + public function prepare(string $sql) : DriverStatement { - return new SQLAnywhereStatement($this->connection, $prepareString); + return new SQLAnywhereStatement($this->connection, $sql); } /** * {@inheritdoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $stmt = $this->prepare($sql); $stmt->execute(); diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php index 03b7f4fa01c..e0c5f82dcc3 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php @@ -9,9 +9,6 @@ use IteratorAggregate; use const SASQL_BOTH; use function array_key_exists; -use function call_user_func_array; -use function func_get_args; -use function func_num_args; use function gettype; use function is_array; use function is_numeric; @@ -306,7 +303,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { return sasql_stmt_affected_rows($this->stmt); } diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php index 557c7807a49..860f0d22cdc 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php @@ -3,10 +3,11 @@ namespace Doctrine\DBAL\Driver\SQLSrv; use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\ParameterType; use const SQLSRV_ERR_ERRORS; -use function func_get_args; use function is_float; use function is_int; use function sprintf; @@ -79,7 +80,7 @@ public function requiresQueryForServerVersion() /** * {@inheritDoc} */ - public function prepare($sql) + public function prepare(string $sql) : DriverStatement { return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId); } @@ -87,7 +88,7 @@ public function prepare($sql) /** * {@inheritDoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $stmt = $this->prepare($sql); $stmt->execute(); @@ -113,7 +114,7 @@ public function quote($value, $type = ParameterType::STRING) /** * {@inheritDoc} */ - public function exec($statement) + public function exec(string $statement) : int { $stmt = sqlsrv_query($this->conn, $statement); diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php index 6056a76a497..77242a2495e 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php @@ -15,7 +15,6 @@ use const SQLSRV_PARAM_IN; use function array_key_exists; use function count; -use function func_get_args; use function in_array; use function is_numeric; use function sqlsrv_errors; @@ -397,7 +396,7 @@ public function fetchColumn($columnIndex = 0) /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { return sqlsrv_rows_affected($this->stmt); } diff --git a/lib/Doctrine/DBAL/Driver/Statement.php b/lib/Doctrine/DBAL/Driver/Statement.php index 7d65b11357b..2530df52dad 100644 --- a/lib/Doctrine/DBAL/Driver/Statement.php +++ b/lib/Doctrine/DBAL/Driver/Statement.php @@ -108,5 +108,5 @@ public function execute($params = null); * * @return int The number of rows. */ - public function rowCount(); + public function rowCount() : int; } diff --git a/lib/Doctrine/DBAL/Portability/Connection.php b/lib/Doctrine/DBAL/Portability/Connection.php index 7bbfaa28352..d5921d85871 100644 --- a/lib/Doctrine/DBAL/Portability/Connection.php +++ b/lib/Doctrine/DBAL/Portability/Connection.php @@ -5,6 +5,8 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\ColumnCase; use Doctrine\DBAL\Driver\PDOConnection; +use Doctrine\DBAL\Driver\ResultStatement; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use const CASE_LOWER; use const CASE_UPPER; @@ -100,7 +102,7 @@ public function getFetchCase() /** * {@inheritdoc} */ - public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) + public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement { $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this); $stmt->setFetchMode($this->defaultFetchMode); @@ -111,9 +113,9 @@ public function executeQuery($query, array $params = [], $types = [], QueryCache /** * {@inheritdoc} */ - public function prepare($statement) + public function prepare(string $sql) : DriverStatement { - $stmt = new Statement(parent::prepare($statement), $this); + $stmt = new Statement(parent::prepare($sql), $this); $stmt->setFetchMode($this->defaultFetchMode); return $stmt; @@ -122,7 +124,7 @@ public function prepare($statement) /** * {@inheritdoc} */ - public function query(string $sql) + public function query(string $sql) : ResultStatement { $this->connect(); diff --git a/lib/Doctrine/DBAL/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index f926760a56b..5a908b8db84 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -234,7 +234,7 @@ public function fetchColumn($columnIndex = 0) /** * {@inheritdoc} */ - public function rowCount() + public function rowCount() : int { return $this->stmt->rowCount(); } diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 4ab76b26db8..550faafd286 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -256,7 +256,7 @@ public function fetchColumn($columnIndex = 0) * * @return int The number of affected rows. */ - public function rowCount() + public function rowCount() : int { return $this->stmt->rowCount(); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLSrv/StatementTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLSrv/StatementTest.php index 6c9ea327a62..637b8539b5a 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLSrv/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/SQLSrv/StatementTest.php @@ -25,7 +25,7 @@ protected function setUp() public function testFailureToPrepareResultsInException() { // use the driver connection directly to avoid having exception wrapped - $stmt = $this->_conn->getWrappedConnection()->prepare(null); + $stmt = $this->_conn->getWrappedConnection()->prepare(''); // it's impossible to prepare the statement without bound variables for SQL Server, // so the preparation happens before the first execution when variables are already in place diff --git a/tests/Doctrine/Tests/DBAL/StatementTest.php b/tests/Doctrine/Tests/DBAL/StatementTest.php index fa22536f2d4..ff19217917d 100644 --- a/tests/Doctrine/Tests/DBAL/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/StatementTest.php @@ -2,9 +2,15 @@ namespace Doctrine\Tests\DBAL; +use Doctrine\DBAL\Configuration; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Driver\Connection as DriverConnection; +use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Statement; use Doctrine\DBAL\Logging\SQLLogger; +use Doctrine\Tests\DBAL\Mocks\MockPlatform; class StatementTest extends \Doctrine\Tests\DbalTestCase { @@ -23,34 +29,29 @@ class StatementTest extends \Doctrine\Tests\DbalTestCase /** * @var \PDOStatement */ - private $pdoStatement; + private $driverStatement; protected function setUp() { - $this->pdoStatement = $this->getMockBuilder('\PDOStatement') - ->setMethods(array('execute', 'bindParam', 'bindValue')) - ->getMock(); - $platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); - $driverConnection = $this->createMock('\Doctrine\DBAL\Driver\Connection'); - $driverConnection->expects($this->any()) - ->method('prepare') - ->will($this->returnValue($this->pdoStatement)); - - $driver = $this->createMock('\Doctrine\DBAL\Driver'); - $constructorArgs = array( - array( - 'platform' => $platform - ), - $driver - ); - $this->conn = $this->getMockBuilder('\Doctrine\DBAL\Connection') - ->setConstructorArgs($constructorArgs) + $this->driverStatement = $this->createMock(DriverStatement::class); + + $driverConnection = $this->createConfiguredMock(DriverConnection::class, [ + 'prepare' => $this->driverStatement, + ]); + + $driver = $this->createMock(Driver::class); + + $this->conn = $this->getMockBuilder(Connection::class) + ->setConstructorArgs([ + ['platform' => new MockPlatform()], + $driver, + ]) ->getMock(); $this->conn->expects($this->atLeastOnce()) ->method('getWrappedConnection') ->will($this->returnValue($driverConnection)); - $this->configuration = $this->createMock('\Doctrine\DBAL\Configuration'); + $this->configuration = $this->createMock(Configuration::class); $this->conn->expects($this->any()) ->method('getConfiguration') ->will($this->returnValue($this->configuration)); @@ -58,7 +59,6 @@ protected function setUp() $this->conn->expects($this->any()) ->method('getDriver') ->will($this->returnValue($driver)); - } public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound() @@ -149,7 +149,7 @@ public function testExecuteCallsLoggerStopQueryOnException() $logger->expects($this->once()) ->method('stopQuery'); - $this->pdoStatement->expects($this->once()) + $this->driverStatement->expects($this->once()) ->method('execute') ->will($this->throwException(new \Exception("Mock test exception"))); diff --git a/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php b/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php deleted file mode 100644 index 74aed7005d7..00000000000 --- a/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php +++ /dev/null @@ -1,26 +0,0 @@ - Date: Wed, 11 Apr 2018 20:27:31 -0700 Subject: [PATCH 3/3] [DBAL-3079] Updated upgrade documentation --- UPGRADE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/UPGRADE.md b/UPGRADE.md index f44f8e47555..17ddd8832d9 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -61,7 +61,8 @@ The Doctrine\DBAL\Version class is no longer available: please refrain from chec ## BC BREAK: the PDO symbols are no longer part of the DBAL API 1. The support of `PDO::PARAM_*`, `PDO::FETCH_*`, `PDO::CASE_*` and `PDO::PARAM_INPUT_OUTPUT` constants in the DBAL API is removed. -2. `\Doctrine\DBAL\Driver\PDOStatement` does not extend `\PDOStatement` anymore. +2. `\Doctrine\DBAL\Driver\PDOConnection` does not extend `\PDO` anymore. Please use `\Doctrine\DBAL\Driver\PDOConnection::getWrappedConnection()` to access the underlying `PDO` object. +3. `\Doctrine\DBAL\Driver\PDOStatement` does not extend `\PDOStatement` anymore. Before: