vendor/doctrine/dbal/src/Statement.php line 215

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL;
  3. use Doctrine\DBAL\Platforms\AbstractPlatform;
  4. use Doctrine\DBAL\Types\Type;
  5. use Doctrine\Deprecations\Deprecation;
  6. use function func_num_args;
  7. use function is_string;
  8. /**
  9.  * A database abstraction-level statement that implements support for logging, DBAL mapping types, etc.
  10.  */
  11. class Statement
  12. {
  13.     /**
  14.      * The SQL statement.
  15.      *
  16.      * @var string
  17.      */
  18.     protected $sql;
  19.     /**
  20.      * The bound parameters.
  21.      *
  22.      * @var mixed[]
  23.      */
  24.     protected $params = [];
  25.     /**
  26.      * The parameter types.
  27.      *
  28.      * @var int[]|string[]
  29.      */
  30.     protected $types = [];
  31.     /**
  32.      * The underlying driver statement.
  33.      *
  34.      * @var Driver\Statement
  35.      */
  36.     protected $stmt;
  37.     /**
  38.      * The underlying database platform.
  39.      *
  40.      * @var AbstractPlatform
  41.      */
  42.     protected $platform;
  43.     /**
  44.      * The connection this statement is bound to and executed on.
  45.      *
  46.      * @var Connection
  47.      */
  48.     protected $conn;
  49.     /**
  50.      * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
  51.      *
  52.      * @internal The statement can be only instantiated by {@see Connection}.
  53.      *
  54.      * @param Connection       $conn      The connection for handling statement errors.
  55.      * @param Driver\Statement $statement The underlying driver-level statement.
  56.      * @param string           $sql       The SQL of the statement.
  57.      *
  58.      * @throws Exception
  59.      */
  60.     public function __construct(Connection $connDriver\Statement $statementstring $sql)
  61.     {
  62.         $this->conn     $conn;
  63.         $this->stmt     $statement;
  64.         $this->sql      $sql;
  65.         $this->platform $conn->getDatabasePlatform();
  66.     }
  67.     /**
  68.      * Binds a parameter value to the statement.
  69.      *
  70.      * The value can optionally be bound with a DBAL mapping type.
  71.      * If bound with a DBAL mapping type, the binding type is derived from the mapping
  72.      * type and the value undergoes the conversion routines of the mapping type before
  73.      * being bound.
  74.      *
  75.      * @param string|int $param The name or position of the parameter.
  76.      * @param mixed      $value The value of the parameter.
  77.      * @param mixed      $type  Either a PDO binding type or a DBAL mapping type name or instance.
  78.      *
  79.      * @return bool TRUE on success, FALSE on failure.
  80.      *
  81.      * @throws Exception
  82.      */
  83.     public function bindValue($param$value$type ParameterType::STRING)
  84.     {
  85.         $this->params[$param] = $value;
  86.         $this->types[$param]  = $type;
  87.         $bindingType ParameterType::STRING;
  88.         if ($type !== null) {
  89.             if (is_string($type)) {
  90.                 $type Type::getType($type);
  91.             }
  92.             $bindingType $type;
  93.             if ($type instanceof Type) {
  94.                 $value       $type->convertToDatabaseValue($value$this->platform);
  95.                 $bindingType $type->getBindingType();
  96.             }
  97.         }
  98.         try {
  99.             return $this->stmt->bindValue($param$value$bindingType);
  100.         } catch (Driver\Exception $e) {
  101.             throw $this->conn->convertException($e);
  102.         }
  103.     }
  104.     /**
  105.      * Binds a parameter to a value by reference.
  106.      *
  107.      * Binding a parameter by reference does not support DBAL mapping types.
  108.      *
  109.      * @param string|int $param    The name or position of the parameter.
  110.      * @param mixed      $variable The reference to the variable to bind.
  111.      * @param int        $type     The binding type.
  112.      * @param int|null   $length   Must be specified when using an OUT bind
  113.      *                             so that PHP allocates enough memory to hold the returned value.
  114.      *
  115.      * @return bool TRUE on success, FALSE on failure.
  116.      *
  117.      * @throws Exception
  118.      */
  119.     public function bindParam($param, &$variable$type ParameterType::STRING$length null)
  120.     {
  121.         $this->params[$param] = $variable;
  122.         $this->types[$param]  = $type;
  123.         try {
  124.             if (func_num_args() > 3) {
  125.                 return $this->stmt->bindParam($param$variable$type$length);
  126.             }
  127.             return $this->stmt->bindParam($param$variable$type);
  128.         } catch (Driver\Exception $e) {
  129.             throw $this->conn->convertException($e);
  130.         }
  131.     }
  132.     /**
  133.      * Executes the statement with the currently bound parameters.
  134.      *
  135.      * @deprecated Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead
  136.      *
  137.      * @param mixed[]|null $params
  138.      *
  139.      * @throws Exception
  140.      */
  141.     public function execute($params null): Result
  142.     {
  143.         Deprecation::triggerIfCalledFromOutside(
  144.             'doctrine/dbal',
  145.             'https://github.com/doctrine/dbal/pull/4580',
  146.             'Statement::execute() is deprecated, use Statement::executeQuery() or Statement::executeStatement() instead'
  147.         );
  148.         if ($params !== null) {
  149.             $this->params $params;
  150.         }
  151.         $logger $this->conn->getConfiguration()->getSQLLogger();
  152.         if ($logger !== null) {
  153.             $logger->startQuery($this->sql$this->params$this->types);
  154.         }
  155.         try {
  156.             return new Result(
  157.                 $this->stmt->execute($params),
  158.                 $this->conn
  159.             );
  160.         } catch (Driver\Exception $ex) {
  161.             throw $this->conn->convertExceptionDuringQuery($ex$this->sql$this->params$this->types);
  162.         } finally {
  163.             if ($logger !== null) {
  164.                 $logger->stopQuery();
  165.             }
  166.         }
  167.     }
  168.     /**
  169.      * Executes the statement with the currently bound parameters and return result.
  170.      *
  171.      * @param mixed[] $params
  172.      *
  173.      * @throws Exception
  174.      */
  175.     public function executeQuery(array $params = []): Result
  176.     {
  177.         if ($params === []) {
  178.             $params null// Workaround as long execute() exists and used internally.
  179.         }
  180.         return $this->execute($params);
  181.     }
  182.     /**
  183.      * Executes the statement with the currently bound parameters and return affected rows.
  184.      *
  185.      * @param mixed[] $params
  186.      *
  187.      * @throws Exception
  188.      */
  189.     public function executeStatement(array $params = []): int
  190.     {
  191.         if ($params === []) {
  192.             $params null// Workaround as long execute() exists and used internally.
  193.         }
  194.         return $this->execute($params)->rowCount();
  195.     }
  196.     /**
  197.      * Gets the wrapped driver statement.
  198.      *
  199.      * @return Driver\Statement
  200.      */
  201.     public function getWrappedStatement()
  202.     {
  203.         return $this->stmt;
  204.     }
  205. }