Skip to content

Commit

Permalink
Dropped support for fetching objects and non-zero column
Browse files Browse the repository at this point in the history
1. `FetchMode::STANDARD_OBJECT` and `FetchMode::CUSTOM_OBJECT` are no longer supported.
2. `FetchMode::COLUMN` with a non-zero index is no longer supported.
3. Incompatible change in the `Connection::fetchColumn()` signature where the 3rd argument is now `$types`, not `$columnIndex`.
  • Loading branch information
morozov committed Apr 9, 2018
1 parent c78e0e5 commit 8870f08
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 539 deletions.
16 changes: 6 additions & 10 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,8 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode)
{
if (count($args) > 0) {
throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
}

$this->defaultFetchMode = $fetchMode;

return true;
Expand All @@ -85,7 +81,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch($fetchMode = null)
{
if (! isset($this->data[$this->num])) {
return false;
Expand Down Expand Up @@ -116,10 +112,10 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null)
{
$rows = [];
while ($row = $this->fetch($fetchMode, ...$args)) {
while ($row = $this->fetch($fetchMode)) {
$rows[] = $row;
}

Expand All @@ -129,11 +125,11 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);

// TODO: verify that return false is the correct behavior
return $row[$columnIndex] ?? false;
return $row[0] ?? false;
}
}
13 changes: 6 additions & 7 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode)
{
$this->defaultFetchMode = $fetchMode;

Expand All @@ -133,7 +133,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch($fetchMode = null)
{
if ($this->data === null) {
$this->data = [];
Expand Down Expand Up @@ -173,10 +173,10 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null)
{
$rows = [];
while ($row = $this->fetch($fetchMode, ...$args)) {
while ($row = $this->fetch($fetchMode)) {
$rows[] = $row;
}

Expand All @@ -186,12 +186,11 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);

// TODO: verify that return false is the correct behavior
return $row[$columnIndex] ?? false;
return $row[0] ?? false;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,16 +576,15 @@ public function fetchArray($statement, array $params = [], array $types = [])
*
* @param string $statement The SQL query to be executed.
* @param array $params The prepared statement params.
* @param int $column The 0-indexed column number to retrieve.
* @param array $types The query parameter types.
*
* @return mixed|bool False is returned if no rows are found.
*
* @throws \Doctrine\DBAL\DBALException
*/
public function fetchColumn($statement, array $params = [], $column = 0, array $types = [])
public function fetchColumn($statement, array $params = [], array $types = [])
{
return $this->executeQuery($statement, $params, $types)->fetchColumn($column);
return $this->executeQuery($statement, $params, $types)->fetchColumn();
}

/**
Expand Down
117 changes: 5 additions & 112 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ class DB2Statement implements \IteratorAggregate, Statement
*/
private $_bindParam = [];

/**
* @var string Name of the default class to instantiate when fetching class instances.
*/
private $defaultFetchClass = '\stdClass';

/**
* @var string Constructor arguments for the default class to instantiate when fetching class instances.
*/
private $defaultFetchClassCtorArgs = [];

/**
* @var int
*/
Expand Down Expand Up @@ -195,18 +185,10 @@ public function execute($params = null)
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode)
{
$this->_defaultFetchMode = $fetchMode;

if (isset($args[0])) {
$this->defaultFetchClass = $args[0];
}

if (isset($args[1])) {
$this->defaultFetchClassCtorArgs = (array) $args[2];
}

return true;
}

Expand All @@ -221,7 +203,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand All @@ -240,29 +222,9 @@ public function fetch($fetchMode = null, ...$args)
case FetchMode::ASSOCIATIVE:
return db2_fetch_assoc($this->_stmt);

case FetchMode::CUSTOM_OBJECT:
$className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs;

if (count($args) > 0) {
$className = $args[0];
$ctorArgs = $args[1] ?? [];
}

$result = db2_fetch_object($this->_stmt);

if ($result instanceof \stdClass) {
$result = $this->castObject($result, $className, $ctorArgs);
}

return $result;

case FetchMode::NUMERIC:
return db2_fetch_array($this->_stmt);

case FetchMode::STANDARD_OBJECT:
return db2_fetch_object($this->_stmt);

default:
throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.');
}
Expand All @@ -271,16 +233,11 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null)
{
$rows = [];

switch ($fetchMode) {
case FetchMode::CUSTOM_OBJECT:
while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
$rows[] = $row;
}
break;
case FetchMode::COLUMN:
while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row;
Expand All @@ -298,15 +255,15 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);

if (false === $row) {
return false;
}

return $row[$columnIndex] ?? null;
return $row[0] ?? null;
}

/**
Expand All @@ -316,68 +273,4 @@ public function rowCount()
{
return (@db2_num_rows($this->_stmt)) ? : 0;
}

/**
* Casts a stdClass object to the given class name mapping its' properties.
*
* @param \stdClass $sourceObject Object to cast from.
* @param string|object $destinationClass Name of the class or class instance to cast to.
* @param array $ctorArgs Arguments to use for constructing the destination class instance.
*
* @return object
*
* @throws DB2Exception
*/
private function castObject(\stdClass $sourceObject, $destinationClass, array $ctorArgs = [])
{
if ( ! is_string($destinationClass)) {
if ( ! is_object($destinationClass)) {
throw new DB2Exception(sprintf(
'Destination class has to be of type string or object, %s given.', gettype($destinationClass)
));
}
} else {
$destinationClass = new \ReflectionClass($destinationClass);
$destinationClass = $destinationClass->newInstanceArgs($ctorArgs);
}

$sourceReflection = new \ReflectionObject($sourceObject);
$destinationClassReflection = new \ReflectionObject($destinationClass);
/** @var \ReflectionProperty[] $destinationProperties */
$destinationProperties = array_change_key_case($destinationClassReflection->getProperties(), \CASE_LOWER);

foreach ($sourceReflection->getProperties() as $sourceProperty) {
$sourceProperty->setAccessible(true);

$name = $sourceProperty->getName();
$value = $sourceProperty->getValue($sourceObject);

// Try to find a case-matching property.
if ($destinationClassReflection->hasProperty($name)) {
$destinationProperty = $destinationClassReflection->getProperty($name);

$destinationProperty->setAccessible(true);
$destinationProperty->setValue($destinationClass, $value);

continue;
}

$name = strtolower($name);

// Try to find a property without matching case.
// Fallback for the driver returning either all uppercase or all lowercase column names.
if (isset($destinationProperties[$name])) {
$destinationProperty = $destinationProperties[$name];

$destinationProperty->setAccessible(true);
$destinationProperty->setValue($destinationClass, $value);

continue;
}

$destinationClass->$name = $value;
}

return $destinationClass;
}
}
20 changes: 5 additions & 15 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private function _fetch()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down Expand Up @@ -291,16 +291,6 @@ public function fetch($fetchMode = null, ...$args)

return $ret;

case FetchMode::STANDARD_OBJECT:
$assoc = array_combine($this->_columnNames, $values);
$ret = new \stdClass();

foreach ($assoc as $column => $value) {
$ret->$column = $value;
}

return $ret;

default:
throw new MysqliException("Unknown fetch type '{$fetchMode}'");
}
Expand All @@ -309,7 +299,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll($fetchMode = null)
{
$fetchMode = $fetchMode ?: $this->_defaultFetchMode;

Expand All @@ -331,15 +321,15 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn()
{
$row = $this->fetch(FetchMode::NUMERIC);

if (false === $row) {
return false;
}

return $row[$columnIndex] ?? null;
return $row[0] ?? null;
}

/**
Expand Down Expand Up @@ -392,7 +382,7 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args)
public function setFetchMode($fetchMode)
{
$this->_defaultFetchMode = $fetchMode;

Expand Down
Loading

0 comments on commit 8870f08

Please sign in to comment.