[DBAL-3079] Added type hints to query-related method parameters and return values

parent b888c048
......@@ -190,7 +190,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
*
* @return int The number of rows.
*/
public function rowCount()
public function rowCount() : int
{
return $this->statement->rowCount();
}
......
......@@ -24,7 +24,6 @@ use Throwable;
use function array_key_exists;
use function array_merge;
use function assert;
use function func_get_args;
use function implode;
use function is_int;
use function is_string;
......@@ -846,18 +845,16 @@ class Connection implements DriverConnection
/**
* Prepares an SQL statement.
*
* @param string $statement The SQL statement to prepare.
*
* @return DriverStatement The prepared statement.
* @param string $sql The SQL statement to prepare.
*
* @throws DBALException
*/
public function prepare($statement)
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($statement, $this);
$stmt = new Statement($sql, $this);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}
$stmt->setFetchMode($this->defaultFetchMode);
......@@ -880,7 +877,7 @@ class Connection implements DriverConnection
*
* @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);
......@@ -924,11 +921,9 @@ class Connection implements DriverConnection
* @param int[]|string[] $types The types the previous parameters are in.
* @param QueryCacheProfile $qcp The query cache profile.
*
* @return ResultStatement
*
* @throws CacheException
*/
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) {
......@@ -987,7 +982,7 @@ class Connection implements DriverConnection
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$this->connect();
......@@ -1017,11 +1012,9 @@ class Connection implements DriverConnection
* @param mixed[] $params The query parameters.
* @param int[]|string[] $types The parameter types.
*
* @return int The number of affected rows.
*
* @throws DBALException
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$this->connect();
......@@ -1053,15 +1046,9 @@ class Connection implements DriverConnection
}
/**
* Executes an SQL statement and return the number of affected rows.
*
* @param string $statement
*
* @return int The number of affected rows.
*
* @throws DBALException
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$this->connect();
......@@ -1470,13 +1457,13 @@ class Connection implements DriverConnection
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
* raw PDOStatement instances.
*
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
* @param DriverStatement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
*
* @return void
*/
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))) {
......
......@@ -7,12 +7,13 @@ use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use InvalidArgumentException;
use function array_rand;
use function count;
use function func_get_args;
/**
* Master-Slave Connection
......@@ -218,7 +219,7 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$this->connect('master');
......@@ -301,7 +302,7 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$this->connect('master');
......@@ -341,7 +342,7 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$this->connect('master');
......@@ -358,10 +359,10 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function prepare($statement)
public function prepare(string $sql) : Statement
{
$this->connect('master');
return parent::prepare($statement);
return parent::prepare($sql);
}
}
......@@ -15,21 +15,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.
......@@ -44,11 +38,9 @@ interface Connection
/**
* 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.
......
......@@ -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 stdClass;
use const DB2_AUTOCOMMIT_OFF;
......@@ -22,7 +24,6 @@ use function db2_prepare;
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 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function prepare($sql)
public function prepare(string $sql) : DriverStatement
{
$stmt = @db2_prepare($this->conn, $sql);
if (! $stmt) {
......@@ -86,7 +87,7 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$stmt = $this->prepare($sql);
$stmt->execute();
......@@ -111,7 +112,7 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$stmt = @db2_exec($this->conn, $statement);
......
......@@ -356,7 +356,7 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return @db2_num_rows($this->stmt) ? : 0;
}
......
......@@ -4,7 +4,9 @@ namespace Doctrine\DBAL\Driver\Mysqli;
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 mysqli;
use const MYSQLI_INIT_COMMAND;
......@@ -15,7 +17,6 @@ use const MYSQLI_READ_DEFAULT_GROUP;
use const MYSQLI_SERVER_PUBLIC_KEY;
use function defined;
use function floor;
use function func_get_args;
use function in_array;
use function ini_get;
use function mysqli_errno;
......@@ -126,15 +127,15 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
/**
* {@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();
......@@ -153,7 +154,7 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
if ($this->conn->query($statement) === false) {
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
......
......@@ -420,7 +420,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
if ($this->_columnNames === false) {
return $this->_stmt->affected_rows;
......
......@@ -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 UnexpectedValueException;
use const OCI_COMMIT_ON_SUCCESS;
......@@ -12,7 +14,6 @@ use const OCI_NO_AUTO_COMMIT;
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;
......@@ -95,15 +96,15 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
/**
* {@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();
......@@ -127,7 +128,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$stmt = $this->prepare($statement);
$stmt->execute();
......
......@@ -502,7 +502,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return oci_num_rows($this->_sth);
}
......
......@@ -36,7 +36,7 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
try {
return $this->connection->exec($statement);
......@@ -56,11 +56,11 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
/**
* {@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 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
try {
return $this->createStatement(
......
......@@ -137,7 +137,7 @@ class PDOStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return $this->stmt->rowCount();
}
......
......@@ -3,10 +3,11 @@
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 assert;
use function func_get_args;
use function is_float;
use function is_int;
use function is_resource;
......@@ -112,7 +113,7 @@ class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
if (sasql_real_query($this->connection, $statement) === false) {
throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
......@@ -148,15 +149,15 @@ class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
/**
* {@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();
......
......@@ -291,7 +291,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return sasql_stmt_affected_rows($this->stmt);
}
......
......@@ -3,7 +3,9 @@
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 is_float;
......@@ -71,7 +73,7 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritDoc}
*/
public function prepare($sql)
public function prepare(string $sql) : DriverStatement
{
return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
}
......@@ -79,7 +81,7 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$stmt = $this->prepare($sql);
$stmt->execute();
......@@ -104,7 +106,7 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$stmt = sqlsrv_query($this->conn, $statement);
......
......@@ -407,7 +407,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return sqlsrv_rows_affected($this->stmt);
}
......
......@@ -100,5 +100,5 @@ interface Statement extends ResultStatement
*
* @return int The number of rows.
*/
public function rowCount();
public function rowCount() : int;
}
......@@ -5,6 +5,8 @@ namespace Doctrine\DBAL\Portability;
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 PDO;
use const CASE_LOWER;
use const CASE_UPPER;
......@@ -93,7 +95,7 @@ class Connection extends \Doctrine\DBAL\Connection
/**
* {@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);
......@@ -104,9 +106,9 @@ class Connection extends \Doctrine\DBAL\Connection
/**
* {@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;
......@@ -115,7 +117,7 @@ class Connection extends \Doctrine\DBAL\Connection
/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$this->connect();
......
......@@ -221,7 +221,7 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return $this->stmt->rowCount();
}
......
......@@ -250,7 +250,7 @@ class Statement implements IteratorAggregate, DriverStatement
*
* @return int The number of affected rows.
*/
public function rowCount()
public function rowCount() : int
{
return $this->stmt->rowCount();
}
......
......@@ -27,7 +27,7 @@ class StatementTest extends DbalFunctionalTestCase
public function testFailureToPrepareResultsInException()
{
// use the driver connection directly to avoid having exception wrapped
$stmt = $this->connection->getWrappedConnection()->prepare(null);
$stmt = $this->connection->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
......
......@@ -6,6 +6,7 @@ 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\Logging\SQLLogger;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Statement;
......@@ -23,26 +24,23 @@ class StatementTest extends DbalTestCase
private $configuration;
/** @var PDOStatement */
private $pdoStatement;
private $driverStatement;
protected function setUp()
{
$this->pdoStatement = $this->getMockBuilder(PDOStatement::class)
->setMethods(['execute', 'bindParam', 'bindValue'])
->getMock();
$platform = new MockPlatform();
$driverConnection = $this->createMock(DriverConnection::class);
$driverConnection->expects($this->any())
->method('prepare')
->will($this->returnValue($this->pdoStatement));
$driver = $this->createMock(Driver::class);
$constructorArgs = [
['platform' => $platform],
$driver,
];
$this->conn = $this->getMockBuilder(Connection::class)
->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')
......@@ -146,7 +144,7 @@ class StatementTest extends DbalTestCase
$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')));
......
<?php
namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\ParameterType;
class DriverConnectionMock implements Connection
{
public function prepare($prepareString)
{
}
public function query(string $sql)
{
}
public function quote($input, $type = ParameterType::STRING)
{
}
public function exec($statement)
{
}
public function lastInsertId($name = null)
{
}
public function beginTransaction()
{
}
public function commit()
{
}
public function rollBack()
{
}
public function errorCode()
{
}
public function errorInfo()
{
}
}
......@@ -3,6 +3,7 @@
namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
......@@ -21,7 +22,7 @@ class DriverMock implements Driver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
return new DriverConnectionMock();
throw new DBALException('Not implemented');
}
public function getDatabasePlatform()
......
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