123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
- namespace Doctrine\DBAL\ForwardCompatibility;
- use Doctrine\DBAL\Driver;
- use Doctrine\DBAL\Exception;
- use Doctrine\DBAL\Exception\NoKeyValue;
- use Doctrine\DBAL\ParameterType;
- use Doctrine\Deprecations\Deprecation;
- use IteratorAggregate;
- use PDO;
- use Traversable;
- use function array_shift;
- use function method_exists;
- /**
- * A wrapper around a Doctrine\DBAL\Driver\ResultStatement that adds 3.0 features
- * defined in Result interface
- */
- class Result implements IteratorAggregate, DriverStatement, DriverResultStatement
- {
- /** @var Driver\ResultStatement */
- private $stmt;
- public static function ensure(Driver\ResultStatement $stmt): Result
- {
- if ($stmt instanceof Result) {
- return $stmt;
- }
- return new Result($stmt);
- }
- public function __construct(Driver\ResultStatement $stmt)
- {
- $this->stmt = $stmt;
- }
- /**
- * @return Driver\ResultStatement
- */
- public function getIterator()
- {
- return $this->stmt;
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated Use Result::free() instead.
- */
- public function closeCursor()
- {
- return $this->stmt->closeCursor();
- }
- /**
- * {@inheritDoc}
- */
- public function columnCount()
- {
- return $this->stmt->columnCount();
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated Use one of the fetch- or iterate-related methods.
- */
- public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
- {
- return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
- */
- public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::fetch() is deprecated, use Result::fetchNumeric(), fetchAssociative() or fetchOne() instead.'
- );
- return $this->stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset);
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
- */
- public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::fetchAll() is deprecated, use Result::fetchAllNumeric(), fetchAllAssociative() or ' .
- 'fetchFirstColumn() instead.'
- );
- return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated Use fetchOne() instead.
- */
- public function fetchColumn($columnIndex = 0)
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::fetchColumn() is deprecated, use Result::fetchOne() instead.'
- );
- return $this->stmt->fetchColumn($columnIndex);
- }
- /**
- * {@inheritDoc}
- */
- public function fetchNumeric()
- {
- return $this->stmt->fetch(PDO::FETCH_NUM);
- }
- /**
- * {@inheritDoc}
- */
- public function fetchAssociative()
- {
- return $this->stmt->fetch(PDO::FETCH_ASSOC);
- }
- /**
- * {@inheritDoc}
- */
- public function fetchOne()
- {
- $row = $this->fetchNumeric();
- if ($row === false) {
- return false;
- }
- return $row[0];
- }
- /**
- * {@inheritDoc}
- */
- public function fetchAllNumeric(): array
- {
- $rows = [];
- while (($row = $this->fetchNumeric()) !== false) {
- $rows[] = $row;
- }
- return $rows;
- }
- /**
- * {@inheritDoc}
- */
- public function fetchAllAssociative(): array
- {
- $rows = [];
- while (($row = $this->fetchAssociative()) !== false) {
- $rows[] = $row;
- }
- return $rows;
- }
- /**
- * {@inheritDoc}
- */
- public function fetchAllKeyValue(): array
- {
- $this->ensureHasKeyValue();
- $data = [];
- foreach ($this->fetchAllNumeric() as [$key, $value]) {
- $data[$key] = $value;
- }
- return $data;
- }
- /**
- * {@inheritDoc}
- */
- public function fetchAllAssociativeIndexed(): array
- {
- $data = [];
- foreach ($this->fetchAllAssociative() as $row) {
- $data[array_shift($row)] = $row;
- }
- return $data;
- }
- /**
- * {@inheritDoc}
- */
- public function fetchFirstColumn(): array
- {
- $rows = [];
- while (($row = $this->fetchOne()) !== false) {
- $rows[] = $row;
- }
- return $rows;
- }
- /**
- * {@inheritdoc}
- *
- * @return Traversable<int,array<int,mixed>>
- */
- public function iterateNumeric(): Traversable
- {
- while (($row = $this->fetchNumeric()) !== false) {
- yield $row;
- }
- }
- /**
- * {@inheritDoc}
- *
- * @return Traversable<int,array<string,mixed>>
- */
- public function iterateAssociative(): Traversable
- {
- while (($row = $this->fetchAssociative()) !== false) {
- yield $row;
- }
- }
- /**
- * {@inheritDoc}
- *
- * @return Traversable<mixed,mixed>
- */
- public function iterateKeyValue(): Traversable
- {
- $this->ensureHasKeyValue();
- foreach ($this->iterateNumeric() as [$key, $value]) {
- yield $key => $value;
- }
- }
- /**
- * {@inheritDoc}
- *
- * @return Traversable<mixed,array<string,mixed>>
- */
- public function iterateAssociativeIndexed(): Traversable
- {
- foreach ($this->iterateAssociative() as $row) {
- yield array_shift($row) => $row;
- }
- }
- /**
- * {@inheritDoc}
- *
- * @return Traversable<int,mixed>
- */
- public function iterateColumn(): Traversable
- {
- while (($value = $this->fetchOne()) !== false) {
- yield $value;
- }
- }
- /**
- * {@inheritDoc}
- */
- public function rowCount()
- {
- if (method_exists($this->stmt, 'rowCount')) {
- return $this->stmt->rowCount();
- }
- throw Exception::notSupported('rowCount');
- }
- public function free(): void
- {
- $this->closeCursor();
- }
- private function ensureHasKeyValue(): void
- {
- $columnCount = $this->columnCount();
- if ($columnCount < 2) {
- throw NoKeyValue::fromColumnCount($columnCount);
- }
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated This feature will no longer be available on Result object in 3.0.x version.
- */
- public function bindValue($param, $value, $type = ParameterType::STRING)
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::bindValue() is deprecated, no replacement.'
- );
- if ($this->stmt instanceof Driver\Statement) {
- return $this->stmt->bindValue($param, $value, $type);
- }
- throw Exception::notSupported('bindValue');
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated This feature will no longer be available on Result object in 3.0.x version.
- */
- public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::bindParam() is deprecated, no replacement.'
- );
- if ($this->stmt instanceof Driver\Statement) {
- return $this->stmt->bindParam($param, $variable, $type, $length);
- }
- throw Exception::notSupported('bindParam');
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated The error information is available via exceptions.
- */
- public function errorCode()
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::errorCode() is deprecated, the error information is available via exceptions.'
- );
- if ($this->stmt instanceof Driver\Statement) {
- return $this->stmt->errorCode();
- }
- throw Exception::notSupported('errorCode');
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated The error information is available via exceptions.
- */
- public function errorInfo()
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::errorInfo() is deprecated, the error information is available via exceptions.'
- );
- if ($this->stmt instanceof Driver\Statement) {
- return $this->stmt->errorInfo();
- }
- throw Exception::notSupported('errorInfo');
- }
- /**
- * {@inheritDoc}
- *
- * @deprecated This feature will no longer be available on Result object in 3.0.x version.
- */
- public function execute($params = null)
- {
- Deprecation::triggerIfCalledFromOutside(
- 'doctrine/dbal',
- 'https://github.com/doctrine/dbal/pull/4019',
- 'Result::execute() is deprecated, no replacement.'
- );
- if ($this->stmt instanceof Driver\Statement) {
- return $this->stmt->execute($params);
- }
- throw Exception::notSupported('execute');
- }
- }
|