vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php line 117

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Driver\PDO\Exception;
  4. use Doctrine\DBAL\Driver\Statement as StatementInterface;
  5. use Doctrine\DBAL\FetchMode;
  6. use Doctrine\DBAL\ParameterType;
  7. use Doctrine\Deprecations\Deprecation;
  8. use PDO;
  9. use PDOException;
  10. use ReturnTypeWillChange;
  11. use function array_slice;
  12. use function assert;
  13. use function func_get_args;
  14. use function is_array;
  15. /**
  16.  * The PDO implementation of the Statement interface.
  17.  * Used by all PDO-based drivers.
  18.  *
  19.  * @deprecated Use {@link Statement} instead
  20.  */
  21. class PDOStatement extends \PDOStatement implements StatementInterfaceResult
  22. {
  23.     use PDOStatementImplementations;
  24.     private const PARAM_TYPE_MAP = [
  25.         ParameterType::NULL         => PDO::PARAM_NULL,
  26.         ParameterType::INTEGER      => PDO::PARAM_INT,
  27.         ParameterType::STRING       => PDO::PARAM_STR,
  28.         ParameterType::ASCII        => PDO::PARAM_STR,
  29.         ParameterType::BINARY       => PDO::PARAM_LOB,
  30.         ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
  31.         ParameterType::BOOLEAN      => PDO::PARAM_BOOL,
  32.     ];
  33.     private const FETCH_MODE_MAP = [
  34.         FetchMode::ASSOCIATIVE     => PDO::FETCH_ASSOC,
  35.         FetchMode::NUMERIC         => PDO::FETCH_NUM,
  36.         FetchMode::MIXED           => PDO::FETCH_BOTH,
  37.         FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ,
  38.         FetchMode::COLUMN          => PDO::FETCH_COLUMN,
  39.         FetchMode::CUSTOM_OBJECT   => PDO::FETCH_CLASS,
  40.     ];
  41.     /**
  42.      * Protected constructor.
  43.      *
  44.      * @internal The statement can be only instantiated by its driver connection.
  45.      */
  46.     protected function __construct()
  47.     {
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     #[ReturnTypeWillChange]
  53.     public function bindValue($param$value$type ParameterType::STRING)
  54.     {
  55.         $type $this->convertParamType($type);
  56.         try {
  57.             return parent::bindValue($param$value$type);
  58.         } catch (PDOException $exception) {
  59.             throw Exception::new($exception);
  60.         }
  61.     }
  62.     /**
  63.      * @param mixed    $param
  64.      * @param mixed    $variable
  65.      * @param int      $type
  66.      * @param int|null $length
  67.      * @param mixed    $driverOptions
  68.      *
  69.      * @return bool
  70.      */
  71.     #[ReturnTypeWillChange]
  72.     public function bindParam($param, &$variable$type ParameterType::STRING$length null$driverOptions null)
  73.     {
  74.         $type $this->convertParamType($type);
  75.         try {
  76.             return parent::bindParam($param$variable$type, ...array_slice(func_get_args(), 3));
  77.         } catch (PDOException $exception) {
  78.             throw Exception::new($exception);
  79.         }
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      *
  84.      * @deprecated Use free() instead.
  85.      */
  86.     #[ReturnTypeWillChange]
  87.     public function closeCursor()
  88.     {
  89.         try {
  90.             return parent::closeCursor();
  91.         } catch (PDOException $exception) {
  92.             // Exceptions not allowed by the interface.
  93.             // In case driver implementations do not adhere to the interface, silence exceptions here.
  94.             return true;
  95.         }
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     #[ReturnTypeWillChange]
  101.     public function execute($params null)
  102.     {
  103.         try {
  104.             return parent::execute($params);
  105.         } catch (PDOException $exception) {
  106.             throw Exception::new($exception);
  107.         }
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      *
  112.      * @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
  113.      */
  114.     #[ReturnTypeWillChange]
  115.     public function fetch($fetchMode null$cursorOrientation PDO::FETCH_ORI_NEXT$cursorOffset 0)
  116.     {
  117.         $args func_get_args();
  118.         if (isset($args[0])) {
  119.             $args[0] = $this->convertFetchMode($args[0]);
  120.         }
  121.         try {
  122.             return parent::fetch(...$args);
  123.         } catch (PDOException $exception) {
  124.             throw Exception::new($exception);
  125.         }
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      *
  130.      * @deprecated Use fetchOne() instead.
  131.      */
  132.     #[ReturnTypeWillChange]
  133.     public function fetchColumn($columnIndex 0)
  134.     {
  135.         try {
  136.             return parent::fetchColumn($columnIndex);
  137.         } catch (PDOException $exception) {
  138.             throw Exception::new($exception);
  139.         }
  140.     }
  141.     /**
  142.      * {@inheritdoc}
  143.      */
  144.     public function fetchNumeric()
  145.     {
  146.         return $this->fetch(PDO::FETCH_NUM);
  147.     }
  148.     /**
  149.      * {@inheritdoc}
  150.      */
  151.     public function fetchAssociative()
  152.     {
  153.         return $this->fetch(PDO::FETCH_ASSOC);
  154.     }
  155.     /**
  156.      * {@inheritdoc}
  157.      */
  158.     public function fetchOne()
  159.     {
  160.         return $this->fetch(PDO::FETCH_COLUMN);
  161.     }
  162.     /**
  163.      * {@inheritdoc}
  164.      */
  165.     public function fetchAllNumeric(): array
  166.     {
  167.         return $this->fetchAll(PDO::FETCH_NUM);
  168.     }
  169.     /**
  170.      * {@inheritdoc}
  171.      */
  172.     public function fetchAllAssociative(): array
  173.     {
  174.         return $this->fetchAll(PDO::FETCH_ASSOC);
  175.     }
  176.     /**
  177.      * {@inheritdoc}
  178.      */
  179.     public function fetchFirstColumn(): array
  180.     {
  181.         return $this->fetchAll(PDO::FETCH_COLUMN);
  182.     }
  183.     public function free(): void
  184.     {
  185.         parent::closeCursor();
  186.     }
  187.     /**
  188.      * @param mixed ...$args
  189.      */
  190.     private function doSetFetchMode(int $fetchMode, ...$args): bool
  191.     {
  192.         $fetchMode $this->convertFetchMode($fetchMode);
  193.         // This thin wrapper is necessary to shield against the weird signature
  194.         // of PDOStatement::setFetchMode(): even if the second and third
  195.         // parameters are optional, PHP will not let us remove it from this
  196.         // declaration.
  197.         $slice = [];
  198.         foreach ($args as $arg) {
  199.             if ($arg === null) {
  200.                 break;
  201.             }
  202.             $slice[] = $arg;
  203.         }
  204.         try {
  205.             return parent::setFetchMode($fetchMode, ...$slice);
  206.         } catch (PDOException $exception) {
  207.             throw Exception::new($exception);
  208.         }
  209.     }
  210.     /**
  211.      * @param mixed ...$args
  212.      *
  213.      * @return mixed[]
  214.      */
  215.     private function doFetchAll(...$args): array
  216.     {
  217.         if (isset($args[0])) {
  218.             $args[0] = $this->convertFetchMode($args[0]);
  219.         }
  220.         $slice = [];
  221.         foreach ($args as $arg) {
  222.             if ($arg === null) {
  223.                 break;
  224.             }
  225.             $slice[] = $arg;
  226.         }
  227.         try {
  228.             $data parent::fetchAll(...$slice);
  229.         } catch (PDOException $exception) {
  230.             throw Exception::new($exception);
  231.         }
  232.         assert(is_array($data));
  233.         return $data;
  234.     }
  235.     /**
  236.      * Converts DBAL parameter type to PDO parameter type
  237.      *
  238.      * @param int $type Parameter type
  239.      */
  240.     private function convertParamType(int $type): int
  241.     {
  242.         if (! isset(self::PARAM_TYPE_MAP[$type])) {
  243.             // TODO: next major: throw an exception
  244.             Deprecation::trigger(
  245.                 'doctrine/dbal',
  246.                 'https://github.com/doctrine/dbal/pull/3088',
  247.                 'Using a PDO parameter type (%d given) is deprecated, ' .
  248.                 'use \Doctrine\DBAL\Types\Types constants instead.',
  249.                 $type
  250.             );
  251.             return $type;
  252.         }
  253.         return self::PARAM_TYPE_MAP[$type];
  254.     }
  255.     /**
  256.      * Converts DBAL fetch mode to PDO fetch mode
  257.      *
  258.      * @param int $fetchMode Fetch mode
  259.      */
  260.     private function convertFetchMode(int $fetchMode): int
  261.     {
  262.         if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
  263.             Deprecation::trigger(
  264.                 'doctrine/dbal',
  265.                 'https://github.com/doctrine/dbal/pull/3088',
  266.                 'Using an unsupported PDO fetch mode or a bitmask of fetch modes (%d given)' .
  267.                 ' is deprecated and will cause an error in Doctrine DBAL 3.0',
  268.                 $fetchMode
  269.             );
  270.             return $fetchMode;
  271.         }
  272.         return self::FETCH_MODE_MAP[$fetchMode];
  273.     }
  274. }