vendor/doctrine/dbal/lib/Doctrine/DBAL/ForwardCompatibility/Result.php line 92

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\ForwardCompatibility;
  3. use Doctrine\DBAL\Driver;
  4. use Doctrine\DBAL\Exception;
  5. use Doctrine\DBAL\Exception\NoKeyValue;
  6. use Doctrine\DBAL\ParameterType;
  7. use Doctrine\Deprecations\Deprecation;
  8. use IteratorAggregate;
  9. use PDO;
  10. use ReturnTypeWillChange;
  11. use Traversable;
  12. use function array_shift;
  13. use function func_get_args;
  14. use function method_exists;
  15. /**
  16.  * A wrapper around a Doctrine\DBAL\Driver\ResultStatement that adds 3.0 features
  17.  * defined in Result interface
  18.  */
  19. class Result implements IteratorAggregateDriverStatementDriverResultStatement
  20. {
  21.     /** @var Driver\ResultStatement */
  22.     private $stmt;
  23.     public static function ensure(Driver\ResultStatement $stmt): Result
  24.     {
  25.         if ($stmt instanceof Result) {
  26.             return $stmt;
  27.         }
  28.         return new Result($stmt);
  29.     }
  30.     public function __construct(Driver\ResultStatement $stmt)
  31.     {
  32.         $this->stmt $stmt;
  33.     }
  34.     /**
  35.      * @return Driver\ResultStatement
  36.      */
  37.     #[ReturnTypeWillChange]
  38.     public function getIterator()
  39.     {
  40.         return $this->stmt;
  41.     }
  42.     /**
  43.      * {@inheritDoc}
  44.      *
  45.      * @deprecated Use Result::free() instead.
  46.      */
  47.     public function closeCursor()
  48.     {
  49.         return $this->stmt->closeCursor();
  50.     }
  51.     /**
  52.      * {@inheritDoc}
  53.      */
  54.     public function columnCount()
  55.     {
  56.         return $this->stmt->columnCount();
  57.     }
  58.     /**
  59.      * {@inheritDoc}
  60.      *
  61.      * @deprecated Use one of the fetch- or iterate-related methods.
  62.      */
  63.     public function setFetchMode($fetchMode$arg2 null$arg3 null)
  64.     {
  65.         return $this->stmt->setFetchMode($fetchMode$arg2$arg3);
  66.     }
  67.     /**
  68.      * {@inheritDoc}
  69.      *
  70.      * @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
  71.      */
  72.     public function fetch($fetchMode null$cursorOrientation PDO::FETCH_ORI_NEXT$cursorOffset 0)
  73.     {
  74.         Deprecation::triggerIfCalledFromOutside(
  75.             'doctrine/dbal',
  76.             'https://github.com/doctrine/dbal/pull/4019',
  77.             'Result::fetch() is deprecated, use Result::fetchNumeric(), fetchAssociative() or fetchOne() instead.'
  78.         );
  79.         return $this->stmt->fetch(...func_get_args());
  80.     }
  81.     /**
  82.      * {@inheritDoc}
  83.      *
  84.      * @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
  85.      */
  86.     public function fetchAll($fetchMode null$fetchArgument null$ctorArgs null)
  87.     {
  88.         Deprecation::triggerIfCalledFromOutside(
  89.             'doctrine/dbal',
  90.             'https://github.com/doctrine/dbal/pull/4019',
  91.             'Result::fetchAll() is deprecated, use Result::fetchAllNumeric(), fetchAllAssociative() or ' .
  92.             'fetchFirstColumn() instead.'
  93.         );
  94.         return $this->stmt->fetchAll($fetchMode$fetchArgument$ctorArgs);
  95.     }
  96.     /**
  97.      * {@inheritDoc}
  98.      *
  99.      * @deprecated Use fetchOne() instead.
  100.      */
  101.     public function fetchColumn($columnIndex 0)
  102.     {
  103.         Deprecation::triggerIfCalledFromOutside(
  104.             'doctrine/dbal',
  105.             'https://github.com/doctrine/dbal/pull/4019',
  106.             'Result::fetchColumn() is deprecated, use Result::fetchOne() instead.'
  107.         );
  108.         return $this->stmt->fetchColumn($columnIndex);
  109.     }
  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     public function fetchNumeric()
  114.     {
  115.         return $this->stmt->fetch(PDO::FETCH_NUM);
  116.     }
  117.     /**
  118.      * {@inheritDoc}
  119.      */
  120.     public function fetchAssociative()
  121.     {
  122.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  123.     }
  124.     /**
  125.      * {@inheritDoc}
  126.      */
  127.     public function fetchOne()
  128.     {
  129.         $row $this->fetchNumeric();
  130.         if ($row === false) {
  131.             return false;
  132.         }
  133.         return $row[0];
  134.     }
  135.     /**
  136.      * {@inheritDoc}
  137.      */
  138.     public function fetchAllNumeric(): array
  139.     {
  140.         $rows = [];
  141.         while (($row $this->fetchNumeric()) !== false) {
  142.             $rows[] = $row;
  143.         }
  144.         return $rows;
  145.     }
  146.     /**
  147.      * {@inheritDoc}
  148.      */
  149.     public function fetchAllAssociative(): array
  150.     {
  151.         $rows = [];
  152.         while (($row $this->fetchAssociative()) !== false) {
  153.             $rows[] = $row;
  154.         }
  155.         return $rows;
  156.     }
  157.     /**
  158.      * {@inheritDoc}
  159.      */
  160.     public function fetchAllKeyValue(): array
  161.     {
  162.         $this->ensureHasKeyValue();
  163.         $data = [];
  164.         foreach ($this->fetchAllNumeric() as [$key$value]) {
  165.             $data[$key] = $value;
  166.         }
  167.         return $data;
  168.     }
  169.     /**
  170.      * {@inheritDoc}
  171.      */
  172.     public function fetchAllAssociativeIndexed(): array
  173.     {
  174.         $data = [];
  175.         foreach ($this->fetchAllAssociative() as $row) {
  176.             $data[array_shift($row)] = $row;
  177.         }
  178.         return $data;
  179.     }
  180.     /**
  181.      * {@inheritDoc}
  182.      */
  183.     public function fetchFirstColumn(): array
  184.     {
  185.         $rows = [];
  186.         while (($row $this->fetchOne()) !== false) {
  187.             $rows[] = $row;
  188.         }
  189.         return $rows;
  190.     }
  191.     /**
  192.      * {@inheritdoc}
  193.      *
  194.      * @return Traversable<int,array<int,mixed>>
  195.      */
  196.     public function iterateNumeric(): Traversable
  197.     {
  198.         while (($row $this->fetchNumeric()) !== false) {
  199.             yield $row;
  200.         }
  201.     }
  202.     /**
  203.      * {@inheritDoc}
  204.      *
  205.      * @return Traversable<int,array<string,mixed>>
  206.      */
  207.     public function iterateAssociative(): Traversable
  208.     {
  209.         while (($row $this->fetchAssociative()) !== false) {
  210.             yield $row;
  211.         }
  212.     }
  213.     /**
  214.      * {@inheritDoc}
  215.      *
  216.      * @return Traversable<mixed,mixed>
  217.      */
  218.     public function iterateKeyValue(): Traversable
  219.     {
  220.         $this->ensureHasKeyValue();
  221.         foreach ($this->iterateNumeric() as [$key$value]) {
  222.             yield $key => $value;
  223.         }
  224.     }
  225.     /**
  226.      * {@inheritDoc}
  227.      *
  228.      * @return Traversable<mixed,array<string,mixed>>
  229.      */
  230.     public function iterateAssociativeIndexed(): Traversable
  231.     {
  232.         foreach ($this->iterateAssociative() as $row) {
  233.             yield array_shift($row) => $row;
  234.         }
  235.     }
  236.     /**
  237.      * {@inheritDoc}
  238.      *
  239.      * @return Traversable<int,mixed>
  240.      */
  241.     public function iterateColumn(): Traversable
  242.     {
  243.         while (($value $this->fetchOne()) !== false) {
  244.             yield $value;
  245.         }
  246.     }
  247.     /**
  248.      * {@inheritDoc}
  249.      */
  250.     public function rowCount()
  251.     {
  252.         if (method_exists($this->stmt'rowCount')) {
  253.             return $this->stmt->rowCount();
  254.         }
  255.         throw Exception::notSupported('rowCount');
  256.     }
  257.     public function free(): void
  258.     {
  259.         $this->closeCursor();
  260.     }
  261.     private function ensureHasKeyValue(): void
  262.     {
  263.         $columnCount $this->columnCount();
  264.         if ($columnCount 2) {
  265.             throw NoKeyValue::fromColumnCount($columnCount);
  266.         }
  267.     }
  268.     /**
  269.      * {@inheritDoc}
  270.      *
  271.      * @deprecated This feature will no longer be available on Result object in 3.0.x version.
  272.      */
  273.     public function bindValue($param$value$type ParameterType::STRING)
  274.     {
  275.         Deprecation::triggerIfCalledFromOutside(
  276.             'doctrine/dbal',
  277.             'https://github.com/doctrine/dbal/pull/4019',
  278.             'Result::bindValue() is deprecated, no replacement.'
  279.         );
  280.         if ($this->stmt instanceof Driver\Statement) {
  281.             return $this->stmt->bindValue($param$value$type);
  282.         }
  283.         throw Exception::notSupported('bindValue');
  284.     }
  285.     /**
  286.      * {@inheritDoc}
  287.      *
  288.      * @deprecated This feature will no longer be available on Result object in 3.0.x version.
  289.      */
  290.     public function bindParam($param, &$variable$type ParameterType::STRING$length null)
  291.     {
  292.         Deprecation::triggerIfCalledFromOutside(
  293.             'doctrine/dbal',
  294.             'https://github.com/doctrine/dbal/pull/4019',
  295.             'Result::bindParam() is deprecated, no replacement.'
  296.         );
  297.         if ($this->stmt instanceof Driver\Statement) {
  298.             return $this->stmt->bindParam($param$variable$type$length);
  299.         }
  300.         throw Exception::notSupported('bindParam');
  301.     }
  302.     /**
  303.      * {@inheritDoc}
  304.      *
  305.      * @deprecated The error information is available via exceptions.
  306.      */
  307.     public function errorCode()
  308.     {
  309.         Deprecation::triggerIfCalledFromOutside(
  310.             'doctrine/dbal',
  311.             'https://github.com/doctrine/dbal/pull/4019',
  312.             'Result::errorCode() is deprecated, the error information is available via exceptions.'
  313.         );
  314.         if ($this->stmt instanceof Driver\Statement) {
  315.             return $this->stmt->errorCode();
  316.         }
  317.         throw Exception::notSupported('errorCode');
  318.     }
  319.     /**
  320.      * {@inheritDoc}
  321.      *
  322.      * @deprecated The error information is available via exceptions.
  323.      */
  324.     public function errorInfo()
  325.     {
  326.         Deprecation::triggerIfCalledFromOutside(
  327.             'doctrine/dbal',
  328.             'https://github.com/doctrine/dbal/pull/4019',
  329.             'Result::errorInfo() is deprecated, the error information is available via exceptions.'
  330.         );
  331.         if ($this->stmt instanceof Driver\Statement) {
  332.             return $this->stmt->errorInfo();
  333.         }
  334.         throw Exception::notSupported('errorInfo');
  335.     }
  336.     /**
  337.      * {@inheritDoc}
  338.      *
  339.      * @deprecated This feature will no longer be available on Result object in 3.0.x version.
  340.      */
  341.     public function execute($params null)
  342.     {
  343.         Deprecation::triggerIfCalledFromOutside(
  344.             'doctrine/dbal',
  345.             'https://github.com/doctrine/dbal/pull/4019',
  346.             'Result::execute() is deprecated, no replacement.'
  347.         );
  348.         if ($this->stmt instanceof Driver\Statement) {
  349.             return $this->stmt->execute($params);
  350.         }
  351.         throw Exception::notSupported('execute');
  352.     }
  353. }