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

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\FetchMode;
  4. use Doctrine\DBAL\ParameterType;
  5. use PDO;
  6. use const E_USER_DEPRECATED;
  7. use function array_slice;
  8. use function assert;
  9. use function func_get_args;
  10. use function is_array;
  11. use function sprintf;
  12. use function trigger_error;
  13. /**
  14.  * The PDO implementation of the Statement interface.
  15.  * Used by all PDO-based drivers.
  16.  */
  17. class PDOStatement extends \PDOStatement implements Statement
  18. {
  19.     private const PARAM_TYPE_MAP = [
  20.         ParameterType::NULL         => PDO::PARAM_NULL,
  21.         ParameterType::INTEGER      => PDO::PARAM_INT,
  22.         ParameterType::STRING       => PDO::PARAM_STR,
  23.         ParameterType::BINARY       => PDO::PARAM_LOB,
  24.         ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
  25.         ParameterType::BOOLEAN      => PDO::PARAM_BOOL,
  26.     ];
  27.     private const FETCH_MODE_MAP = [
  28.         FetchMode::ASSOCIATIVE     => PDO::FETCH_ASSOC,
  29.         FetchMode::NUMERIC         => PDO::FETCH_NUM,
  30.         FetchMode::MIXED           => PDO::FETCH_BOTH,
  31.         FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ,
  32.         FetchMode::COLUMN          => PDO::FETCH_COLUMN,
  33.         FetchMode::CUSTOM_OBJECT   => PDO::FETCH_CLASS,
  34.     ];
  35.     /**
  36.      * Protected constructor.
  37.      */
  38.     protected function __construct()
  39.     {
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function setFetchMode($fetchMode$arg2 null$arg3 null)
  45.     {
  46.         $fetchMode $this->convertFetchMode($fetchMode);
  47.         // This thin wrapper is necessary to shield against the weird signature
  48.         // of PDOStatement::setFetchMode(): even if the second and third
  49.         // parameters are optional, PHP will not let us remove it from this
  50.         // declaration.
  51.         try {
  52.             if ($arg2 === null && $arg3 === null) {
  53.                 return parent::setFetchMode($fetchMode);
  54.             }
  55.             if ($arg3 === null) {
  56.                 return parent::setFetchMode($fetchMode$arg2);
  57.             }
  58.             return parent::setFetchMode($fetchMode$arg2$arg3);
  59.         } catch (\PDOException $exception) {
  60.             throw new PDOException($exception);
  61.         }
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function bindValue($param$value$type ParameterType::STRING)
  67.     {
  68.         $type $this->convertParamType($type);
  69.         try {
  70.             return parent::bindValue($param$value$type);
  71.         } catch (\PDOException $exception) {
  72.             throw new PDOException($exception);
  73.         }
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function bindParam($column, &$variable$type ParameterType::STRING$length null$driverOptions null)
  79.     {
  80.         $type $this->convertParamType($type);
  81.         try {
  82.             return parent::bindParam($column$variable$type, ...array_slice(func_get_args(), 3));
  83.         } catch (\PDOException $exception) {
  84.             throw new PDOException($exception);
  85.         }
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function closeCursor()
  91.     {
  92.         try {
  93.             return parent::closeCursor();
  94.         } catch (\PDOException $exception) {
  95.             // Exceptions not allowed by the interface.
  96.             // In case driver implementations do not adhere to the interface, silence exceptions here.
  97.             return true;
  98.         }
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     public function execute($params null)
  104.     {
  105.         try {
  106.             return parent::execute($params);
  107.         } catch (\PDOException $exception) {
  108.             throw new PDOException($exception);
  109.         }
  110.     }
  111.     /**
  112.      * {@inheritdoc}
  113.      */
  114.     public function fetch($fetchMode null$cursorOrientation PDO::FETCH_ORI_NEXT$cursorOffset 0)
  115.     {
  116.         $args func_get_args();
  117.         if (isset($args[0])) {
  118.             $args[0] = $this->convertFetchMode($args[0]);
  119.         }
  120.         try {
  121.             return parent::fetch(...$args);
  122.         } catch (\PDOException $exception) {
  123.             throw new PDOException($exception);
  124.         }
  125.     }
  126.     /**
  127.      * {@inheritdoc}
  128.      */
  129.     public function fetchAll($fetchMode null$fetchArgument null$ctorArgs null)
  130.     {
  131.         $args func_get_args();
  132.         if (isset($args[0])) {
  133.             $args[0] = $this->convertFetchMode($args[0]);
  134.         }
  135.         if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) {
  136.             $args = [];
  137.         } elseif ($fetchArgument === null && $ctorArgs === null) {
  138.             $args = [$fetchMode];
  139.         } elseif ($ctorArgs === null) {
  140.             $args = [$fetchMode$fetchArgument];
  141.         } else {
  142.             $args = [$fetchMode$fetchArgument$ctorArgs];
  143.         }
  144.         try {
  145.             $data parent::fetchAll(...$args);
  146.             assert(is_array($data));
  147.             return $data;
  148.         } catch (\PDOException $exception) {
  149.             throw new PDOException($exception);
  150.         }
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public function fetchColumn($columnIndex 0)
  156.     {
  157.         try {
  158.             return parent::fetchColumn($columnIndex);
  159.         } catch (\PDOException $exception) {
  160.             throw new PDOException($exception);
  161.         }
  162.     }
  163.     /**
  164.      * Converts DBAL parameter type to PDO parameter type
  165.      *
  166.      * @param int $type Parameter type
  167.      */
  168.     private function convertParamType(int $type) : int
  169.     {
  170.         if (! isset(self::PARAM_TYPE_MAP[$type])) {
  171.             // TODO: next major: throw an exception
  172.             @trigger_error(sprintf(
  173.                 'Using a PDO parameter type (%d given) is deprecated and will cause an error in Doctrine DBAL 3.0',
  174.                 $type
  175.             ), E_USER_DEPRECATED);
  176.             return $type;
  177.         }
  178.         return self::PARAM_TYPE_MAP[$type];
  179.     }
  180.     /**
  181.      * Converts DBAL fetch mode to PDO fetch mode
  182.      *
  183.      * @param int $fetchMode Fetch mode
  184.      */
  185.     private function convertFetchMode(int $fetchMode) : int
  186.     {
  187.         if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
  188.             // TODO: next major: throw an exception
  189.             @trigger_error(sprintf(
  190.                 'Using a PDO fetch mode or their combination (%d given)' .
  191.                 ' is deprecated and will cause an error in Doctrine DBAL 3.0',
  192.                 $fetchMode
  193.             ), E_USER_DEPRECATED);
  194.             return $fetchMode;
  195.         }
  196.         return self::FETCH_MODE_MAP[$fetchMode];
  197.     }
  198. }