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

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.     public function bindParam(
  57.         $param,
  58.         &$variable,
  59.         $type ParameterType::STRING,
  60.         $length null,
  61.         $driverOptions null
  62.     ): bool {
  63.         if (func_num_args() > 4) {
  64.             Deprecation::triggerIfCalledFromOutside(
  65.                 'doctrine/dbal',
  66.                 'https://github.com/doctrine/dbal/issues/4533',
  67.                 'The $driverOptions argument of Statement::bindParam() is deprecated.'
  68.             );
  69.         }
  70.         $type $this->convertParamType($type);
  71.         try {
  72.             return $this->stmt->bindParam(
  73.                 $param,
  74.                 $variable,
  75.                 $type,
  76.                 $length ?? 0,
  77.                 ...array_slice(func_get_args(), 4)
  78.             );
  79.         } catch (PDOException $exception) {
  80.             throw Exception::new($exception);
  81.         }
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     public function execute($params null): ResultInterface
  87.     {
  88.         try {
  89.             $this->stmt->execute($params);
  90.         } catch (PDOException $exception) {
  91.             throw Exception::new($exception);
  92.         }
  93.         return new Result($this->stmt);
  94.     }
  95.     /**
  96.      * Converts DBAL parameter type to PDO parameter type
  97.      *
  98.      * @param int $type Parameter type
  99.      *
  100.      * @throws ExceptionInterface
  101.      */
  102.     private function convertParamType(int $type): int
  103.     {
  104.         if (! isset(self::PARAM_TYPE_MAP[$type])) {
  105.             throw UnknownParameterType::new($type);
  106.         }
  107.         return self::PARAM_TYPE_MAP[$type];
  108.     }
  109. }