Skip to content

Commit

Permalink
Statement objects should throw exception in exceptional cases
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jul 25, 2018
1 parent a598800 commit daff186
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 311 deletions.
10 changes: 4 additions & 6 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,29 @@ public function __construct(array $data)
/**
* {@inheritdoc}
*/
public function closeCursor()
public function closeCursor() : void
{
unset ($this->data);
}

/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
return $this->columnCount;
}

/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode, ...$args) : void
{
if (count($args) > 0) {
throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
}

$this->defaultFetchMode = $fetchMode;

return true;
}

/**
Expand Down Expand Up @@ -116,7 +114,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null, ...$args) : array
{
$rows = [];
while ($row = $this->fetch($fetchMode, ...$args)) {
Expand Down
10 changes: 4 additions & 6 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $rea
/**
* {@inheritdoc}
*/
public function closeCursor()
public function closeCursor() : void
{
$this->statement->closeCursor();
if ($this->emptied && $this->data !== null) {
Expand All @@ -105,19 +105,17 @@ public function closeCursor()
/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
return $this->statement->columnCount();
}

/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode, ...$args) : void
{
$this->defaultFetchMode = $fetchMode;

return true;
}

/**
Expand Down Expand Up @@ -173,7 +171,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null, ...$args) : array
{
$rows = [];
while ($row = $this->fetch($fetchMode, ...$args)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Doctrine\DBAL\Driver\IBMDB2;

class DB2Exception extends \Exception
use Doctrine\DBAL\Driver\AbstractDriverException;

class DB2Exception extends AbstractDriverException
{
}
40 changes: 13 additions & 27 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public function __construct($stmt)
/**
* {@inheritdoc}
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
public function bindValue($param, $value, $type = ParameterType::STRING) : void
{
return $this->bindParam($param, $value, $type);
$this->bindParam($param, $value, $type);
}

/**
* {@inheritdoc}
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void
{
$this->_bindParam[$column] =& $variable;

Expand All @@ -105,34 +105,28 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
throw new DB2Exception(db2_stmt_errormsg());
}

return true;
}

/**
* {@inheritdoc}
*/
public function closeCursor()
public function closeCursor() : void
{
if ( ! $this->_stmt) {
return false;
return;
}

$this->_bindParam = [];

if (!db2_free_result($this->_stmt)) {
return false;
}
db2_free_result($this->_stmt);

$this->result = false;

return true;
}

/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
if ( ! $this->_stmt) {
return false;
Expand Down Expand Up @@ -163,10 +157,10 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function execute($params = null)
public function execute($params = null) : void
{
if ( ! $this->_stmt) {
return false;
return;
}

if ($params === null) {
Expand All @@ -179,21 +173,17 @@ public function execute($params = null)
}
}

$retval = db2_execute($this->_stmt, $params);

if ($retval === false) {
if (! db2_execute($this->_stmt, $params)) {
throw new DB2Exception(db2_stmt_errormsg());
}

$this->result = true;

return $retval;
}

/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode, ...$args) : void
{
$this->_defaultFetchMode = $fetchMode;

Expand All @@ -204,8 +194,6 @@ public function setFetchMode($fetchMode, ...$args)
if (isset($args[1])) {
$this->defaultFetchClassCtorArgs = (array) $args[2];
}

return true;
}

/**
Expand All @@ -221,10 +209,8 @@ public function getIterator()
*/
public function fetch($fetchMode = null, ...$args)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if (!$this->result) {
return false;
throw new DB2Exception('The statement does not contain a result to be fetched');
}

$fetchMode = $fetchMode ?: $this->_defaultFetchMode;
Expand Down Expand Up @@ -269,7 +255,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null, ...$args) : array
{
$rows = [];

Expand Down
49 changes: 19 additions & 30 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ class MysqliStatement implements \IteratorAggregate, Statement

/**
* @param \mysqli $conn
* @param string $prepareString
* @param string $sql
*
* @throws \Doctrine\DBAL\Driver\Mysqli\MysqliException
* @throws MysqliException
*/
public function __construct(\mysqli $conn, $prepareString)
public function __construct(\mysqli $conn, string $sql)
{
$this->_conn = $conn;
$this->_stmt = $conn->prepare($prepareString);
$this->_stmt = $conn->prepare($sql);

if (false === $this->_stmt) {
throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno);
}
Expand All @@ -104,50 +105,46 @@ public function __construct(\mysqli $conn, $prepareString)
/**
* {@inheritdoc}
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void
{
if (null === $type) {
$type = 's';
} else {
if (isset(self::$_paramTypeMap[$type])) {
$type = self::$_paramTypeMap[$type];
} else {
if (! isset(self::$_paramTypeMap[$type])) {
throw new MysqliException("Unknown type: '{$type}'");
}

$type = self::$_paramTypeMap[$type];
}

$this->_bindedValues[$column] =& $variable;
$this->types[$column - 1] = $type;

return true;
}

/**
* {@inheritdoc}
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
public function bindValue($param, $value, $type = ParameterType::STRING) : void
{
if (null === $type) {
$type = 's';
} else {
if (isset(self::$_paramTypeMap[$type])) {
$type = self::$_paramTypeMap[$type];
} else {
if (! isset(self::$_paramTypeMap[$type])) {
throw new MysqliException("Unknown type: '{$type}'");
}

$type = self::$_paramTypeMap[$type];
}

$this->_values[$param] = $value;
$this->_bindedValues[$param] =& $this->_values[$param];
$this->types[$param - 1] = $type;

return true;
}

/**
* {@inheritdoc}
*/
public function execute($params = null)
public function execute($params = null) : void
{
if (null !== $this->_bindedValues) {
if (null !== $params) {
Expand Down Expand Up @@ -210,8 +207,6 @@ public function execute($params = null)
}

$this->result = true;

return true;
}

/**
Expand Down Expand Up @@ -258,10 +253,8 @@ private function _fetch()
*/
public function fetch($fetchMode = null, ...$args)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
if (!$this->result) {
return false;
throw new MysqliException('The statement does not contain a result to be fetched');
}

$fetchMode = $fetchMode ?: $this->_defaultFetchMode;
Expand Down Expand Up @@ -310,7 +303,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null, ...$args) : array
{
$fetchMode = $fetchMode ?: $this->_defaultFetchMode;

Expand Down Expand Up @@ -362,12 +355,10 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function closeCursor()
public function closeCursor() : void
{
$this->_stmt->free_result();
$this->result = false;

return true;
}

/**
Expand All @@ -385,19 +376,17 @@ public function rowCount() : int
/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
return $this->_stmt->field_count;
}

/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode, ...$args) : void
{
$this->_defaultFetchMode = $fetchMode;

return true;
}

/**
Expand Down
Loading

0 comments on commit daff186

Please sign in to comment.