Skip to content

Commit

Permalink
Remove default fetch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed May 9, 2020
1 parent cc7bc1e commit fd0c14e
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 238 deletions.
15 changes: 3 additions & 12 deletions src/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ final class ArrayStatement implements IteratorAggregate, ResultStatement
/** @var int */
private $num = 0;

/** @var int */
private $defaultFetchMode = FetchMode::MIXED;

/**
* @param mixed[] $data
*/
Expand Down Expand Up @@ -57,11 +54,6 @@ public function rowCount() : int
return count($this->data);
}

public function setFetchMode(int $fetchMode) : void
{
$this->defaultFetchMode = $fetchMode;
}

/**
* {@inheritdoc}
*/
Expand All @@ -75,14 +67,13 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch(?int $fetchMode = null)
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
{
if (! isset($this->data[$this->num])) {
return false;
}

$row = $this->data[$this->num++];
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
$row = $this->data[$this->num++];

if ($fetchMode === FetchMode::ASSOCIATIVE) {
return $row;
Expand All @@ -108,7 +99,7 @@ public function fetch(?int $fetchMode = null)
/**
* {@inheritdoc}
*/
public function fetchAll(?int $fetchMode = null) : array
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
$rows = [];
while ($row = $this->fetch($fetchMode)) {
Expand Down
14 changes: 2 additions & 12 deletions src/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ final class ResultCacheStatement implements IteratorAggregate, ResultStatement
/** @var mixed[] */
private $data;

/** @var int */
private $defaultFetchMode = FetchMode::MIXED;

public function __construct(ResultStatement $stmt, Cache $resultCache, string $cacheKey, string $realKey, int $lifetime)
{
$this->statement = $stmt;
Expand Down Expand Up @@ -90,11 +87,6 @@ public function columnCount() : int
return $this->statement->columnCount();
}

public function setFetchMode(int $fetchMode) : void
{
$this->defaultFetchMode = $fetchMode;
}

/**
* {@inheritdoc}
*/
Expand All @@ -108,7 +100,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch(?int $fetchMode = null)
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
{
if ($this->data === null) {
$this->data = [];
Expand All @@ -119,8 +111,6 @@ public function fetch(?int $fetchMode = null)
if ($row !== false) {
$this->data[] = $row;

$fetchMode = $fetchMode ?? $this->defaultFetchMode;

if ($fetchMode === FetchMode::ASSOCIATIVE) {
return $row;
}
Expand Down Expand Up @@ -148,7 +138,7 @@ public function fetch(?int $fetchMode = null)
/**
* {@inheritdoc}
*/
public function fetchAll(?int $fetchMode = null) : array
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
$data = $this->statement->fetchAll($fetchMode);

Expand Down
23 changes: 1 addition & 22 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ class Connection implements DriverConnection
*/
private $isRollbackOnly = false;

/** @var int */
protected $defaultFetchMode = FetchMode::ASSOCIATIVE;

/**
* Initializes a new instance of the Connection class.
*
Expand Down Expand Up @@ -446,14 +443,6 @@ public function setAutoCommit(bool $autoCommit) : void
$this->commitAll();
}

/**
* Sets the fetch mode.
*/
public function setFetchMode(int $fetchMode) : void
{
$this->defaultFetchMode = $fetchMode;
}

/**
* Prepares and executes an SQL query and returns the first row of the result
* as an associative array.
Expand Down Expand Up @@ -760,14 +749,10 @@ public function fetchAll(string $query, array $params = [], array $types = []) :
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($sql, $this);
return new Statement($sql, $this);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
}

/**
Expand Down Expand Up @@ -818,8 +803,6 @@ public function executeQuery(
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
}

$stmt->setFetchMode($this->defaultFetchMode);

$logger->stopQuery();

return $stmt;
Expand Down Expand Up @@ -864,8 +847,6 @@ public function executeCacheQuery(string $query, array $params, array $types, Qu
$stmt = new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
}

$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
}

Expand All @@ -882,8 +863,6 @@ public function query(string $sql) : ResultStatement
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$statement->setFetchMode($this->defaultFetchMode);

$logger->stopQuery();

return $statement;
Expand Down
2 changes: 0 additions & 2 deletions src/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ public function query(string $sql) : ResultStatement

$statement = $this->_conn->query($sql);

$statement->setFetchMode($this->defaultFetchMode);

$logger->stopQuery();

return $statement;
Expand Down
13 changes: 2 additions & 11 deletions src/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ final class DB2Statement implements IteratorAggregate, Statement
*/
private $lobs = [];

/** @var int */
private $defaultFetchMode = FetchMode::MIXED;

/**
* Indicates whether the statement is in the state when fetching results is possible
*
Expand Down Expand Up @@ -171,11 +168,6 @@ public function execute(?array $params = null) : void
$this->result = true;
}

public function setFetchMode(int $fetchMode) : void
{
$this->defaultFetchMode = $fetchMode;
}

/**
* {@inheritdoc}
*/
Expand All @@ -187,15 +179,14 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch(?int $fetchMode = null)
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
{
// 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;
}

$fetchMode = $fetchMode ?? $this->defaultFetchMode;
switch ($fetchMode) {
case FetchMode::COLUMN:
return $this->fetchColumn();
Expand All @@ -217,7 +208,7 @@ public function fetch(?int $fetchMode = null)
/**
* {@inheritdoc}
*/
public function fetchAll(?int $fetchMode = null) : array
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
$rows = [];

Expand Down
16 changes: 2 additions & 14 deletions src/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ final class MysqliStatement implements IteratorAggregate, Statement
*/
private $values = [];

/** @var int */
private $defaultFetchMode = FetchMode::MIXED;

/**
* Indicates whether the statement is in the state when fetching results is possible
*
Expand Down Expand Up @@ -326,16 +323,14 @@ private function _fetch()
/**
* {@inheritdoc}
*/
public function fetch(?int $fetchMode = null)
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
{
// 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;
}

$fetchMode = $fetchMode ?? $this->defaultFetchMode;

if ($fetchMode === FetchMode::COLUMN) {
return $this->fetchColumn();
}
Expand Down Expand Up @@ -372,10 +367,8 @@ public function fetch(?int $fetchMode = null)
/**
* {@inheritdoc}
*/
public function fetchAll(?int $fetchMode = null) : array
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
$fetchMode = $fetchMode ?? $this->defaultFetchMode;

$rows = [];

if ($fetchMode === FetchMode::COLUMN) {
Expand Down Expand Up @@ -425,11 +418,6 @@ public function columnCount() : int
return $this->stmt->field_count;
}

public function setFetchMode(int $fetchMode) : void
{
$this->defaultFetchMode = $fetchMode;
}

/**
* {@inheritdoc}
*/
Expand Down
17 changes: 3 additions & 14 deletions src/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use const OCI_D_LOB;
use const OCI_FETCHSTATEMENT_BY_COLUMN;
use const OCI_FETCHSTATEMENT_BY_ROW;
use const OCI_NO_AUTO_COMMIT;
use const OCI_NUM;
use const OCI_RETURN_LOBS;
use const OCI_RETURN_NULLS;
Expand Down Expand Up @@ -60,9 +61,6 @@ final class OCI8Statement implements IteratorAggregate, Statement
FetchMode::COLUMN => OCI_NUM,
];

/** @var int */
protected $_defaultFetchMode = FetchMode::MIXED;

/** @var string[] */
protected $_paramMap = [];

Expand Down Expand Up @@ -219,11 +217,6 @@ public function execute(?array $params = null) : void
$this->result = true;
}

public function setFetchMode(int $fetchMode) : void
{
$this->_defaultFetchMode = $fetchMode;
}

/**
* {@inheritdoc}
*/
Expand All @@ -235,16 +228,14 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch(?int $fetchMode = null)
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
{
// 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;
}

$fetchMode = $fetchMode ?? $this->_defaultFetchMode;

if ($fetchMode === FetchMode::COLUMN) {
return $this->fetchColumn();
}
Expand All @@ -262,10 +253,8 @@ public function fetch(?int $fetchMode = null)
/**
* {@inheritdoc}
*/
public function fetchAll(?int $fetchMode = null) : array
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
$fetchMode = $fetchMode ?? $this->_defaultFetchMode;

$result = [];

if (! isset(self::$fetchModeMap[$fetchMode])) {
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function lastInsertId(?string $name = null) : string
*/
protected function createStatement(\PDOStatement $stmt) : PDOStatement
{
$stmt->setFetchMode(PDO::FETCH_ASSOC);

return new PDOStatement($stmt);
}

Expand Down
29 changes: 5 additions & 24 deletions src/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ public function __construct(\PDOStatement $stmt)
$this->stmt = $stmt;
}

public function setFetchMode(int $fetchMode) : void
{
$fetchMode = $this->convertFetchMode($fetchMode);

try {
$this->stmt->setFetchMode($fetchMode);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -122,13 +111,9 @@ public function rowCount() : int
/**
* {@inheritdoc}
*/
public function fetch(?int $fetchMode = null)
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
{
try {
if ($fetchMode === null) {
return $this->stmt->fetch();
}

return $this->stmt->fetch(
$this->convertFetchMode($fetchMode)
);
Expand All @@ -140,16 +125,12 @@ public function fetch(?int $fetchMode = null)
/**
* {@inheritdoc}
*/
public function fetchAll(?int $fetchMode = null) : array
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
{
try {
if ($fetchMode === null) {
$data = $this->stmt->fetchAll();
} else {
$data = $this->stmt->fetchAll(
$this->convertFetchMode($fetchMode)
);
}
$data = $this->stmt->fetchAll(
$this->convertFetchMode($fetchMode)
);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand Down
Loading

0 comments on commit fd0c14e

Please sign in to comment.