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

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