vendor/doctrine/dbal/src/Driver/PDO/Statement.php line 92

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDO;
  3. use Doctrine\DBAL\Driver\Exception as ExceptionInterface;
  4. use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
  5. use Doctrine\DBAL\Driver\Result as ResultInterface;
  6. use Doctrine\DBAL\Driver\Statement as StatementInterface;
  7. use Doctrine\DBAL\ParameterType;
  8. use Doctrine\Deprecations\Deprecation;
  9. use PDO;
  10. use PDOException;
  11. use PDOStatement;
  12. use function array_slice;
  13. use function func_get_args;
  14. use function func_num_args;
  15. final class Statement implements StatementInterface
  16. {
  17.     private const PARAM_TYPE_MAP = [
  18.         ParameterType::NULL => PDO::PARAM_NULL,
  19.         ParameterType::INTEGER => PDO::PARAM_INT,
  20.         ParameterType::STRING => PDO::PARAM_STR,
  21.         ParameterType::ASCII => PDO::PARAM_STR,
  22.         ParameterType::BINARY => PDO::PARAM_LOB,
  23.         ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
  24.         ParameterType::BOOLEAN => PDO::PARAM_BOOL,
  25.     ];
  26.     /** @var PDOStatement */
  27.     private $stmt;
  28.     /**
  29.      * @internal The statement can be only instantiated by its driver connection.
  30.      */
  31.     public function __construct(PDOStatement $stmt)
  32.     {
  33.         $this->stmt $stmt;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function bindValue($param$value$type ParameterType::STRING)
  39.     {
  40.         $type $this->convertParamType($type);
  41.         try {
  42.             return $this->stmt->bindValue($param$value$type);
  43.         } catch (PDOException $exception) {
  44.             throw Exception::new($exception);
  45.         }
  46.     }
  47.     /**
  48.      * {@inheritDoc}
  49.      *
  50.      * @param mixed    $param
  51.      * @param mixed    $variable
  52.      * @param int      $type
  53.      * @param int|null $length
  54.      * @param mixed    $driverOptions The usage of the argument is deprecated.
  55.      *
  56.      * @return bool
  57.      */
  58.     public function bindParam($param, &$variable$type ParameterType::STRING$length null$driverOptions null)
  59.     {
  60.         if (func_num_args() > 4) {
  61.             Deprecation::triggerIfCalledFromOutside(
  62.                 'doctrine/dbal',
  63.                 'https://github.com/doctrine/dbal/issues/4533',
  64.                 'The $driverOptions argument of Statement::bindParam() is deprecated.'
  65.             );
  66.         }
  67.         $type $this->convertParamType($type);
  68.         try {
  69.             return $this->stmt->bindParam($param$variable$type, ...array_slice(func_get_args(), 3));
  70.         } catch (PDOException $exception) {
  71.             throw Exception::new($exception);
  72.         }
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function execute($params null): ResultInterface
  78.     {
  79.         try {
  80.             $this->stmt->execute($params);
  81.         } catch (PDOException $exception) {
  82.             throw Exception::new($exception);
  83.         }
  84.         return new Result($this->stmt);
  85.     }
  86.     /**
  87.      * Converts DBAL parameter type to PDO parameter type
  88.      *
  89.      * @param int $type Parameter type
  90.      *
  91.      * @throws ExceptionInterface
  92.      */
  93.     private function convertParamType(int $type): int
  94.     {
  95.         if (! isset(self::PARAM_TYPE_MAP[$type])) {
  96.             throw UnknownParameterType::new($type);
  97.         }
  98.         return self::PARAM_TYPE_MAP[$type];
  99.     }
  100. }