vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php line 42

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
  4. use Doctrine\DBAL\Driver\PDO\Exception;
  5. use Doctrine\DBAL\Driver\PDO\Statement;
  6. use Doctrine\DBAL\ParameterType;
  7. use PDO;
  8. use PDOException;
  9. use PDOStatement;
  10. use function assert;
  11. /**
  12.  * PDO implementation of the Connection interface.
  13.  * Used by all PDO-based drivers.
  14.  *
  15.  * @deprecated Use {@link Connection} instead
  16.  */
  17. class PDOConnection extends PDO implements ConnectionInterfaceServerInfoAwareConnection
  18. {
  19.     use PDOQueryImplementation;
  20.     /**
  21.      * @internal The connection can be only instantiated by its driver.
  22.      *
  23.      * @param string       $dsn
  24.      * @param string|null  $user
  25.      * @param string|null  $password
  26.      * @param mixed[]|null $options
  27.      *
  28.      * @throws PDOException In case of an error.
  29.      */
  30.     public function __construct($dsn$user null$password null, ?array $options null)
  31.     {
  32.         try {
  33.             parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
  34.             $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class, []]);
  35.             $this->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  36.         } catch (PDOException $exception) {
  37.             throw Exception::new($exception);
  38.         }
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function exec($sql)
  44.     {
  45.         try {
  46.             $result parent::exec($sql);
  47.             assert($result !== false);
  48.             return $result;
  49.         } catch (PDOException $exception) {
  50.             throw Exception::new($exception);
  51.         }
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function getServerVersion()
  57.     {
  58.         return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
  59.     }
  60.     /**
  61.      * @param string          $sql
  62.      * @param array<int, int> $driverOptions
  63.      *
  64.      * @return PDOStatement
  65.      */
  66.     public function prepare($sql$driverOptions = [])
  67.     {
  68.         try {
  69.             $statement parent::prepare($sql$driverOptions);
  70.             assert($statement instanceof PDOStatement);
  71.             return $statement;
  72.         } catch (PDOException $exception) {
  73.             throw Exception::new($exception);
  74.         }
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function quote($value$type ParameterType::STRING)
  80.     {
  81.         return parent::quote($value$type);
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     public function lastInsertId($name null)
  87.     {
  88.         try {
  89.             if ($name === null) {
  90.                 return parent::lastInsertId();
  91.             }
  92.             return parent::lastInsertId($name);
  93.         } catch (PDOException $exception) {
  94.             throw Exception::new($exception);
  95.         }
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function requiresQueryForServerVersion()
  101.     {
  102.         return false;
  103.     }
  104.     /**
  105.      * @param mixed ...$args
  106.      */
  107.     private function doQuery(...$args): PDOStatement
  108.     {
  109.         try {
  110.             $stmt parent::query(...$args);
  111.         } catch (PDOException $exception) {
  112.             throw Exception::new($exception);
  113.         }
  114.         assert($stmt instanceof PDOStatement);
  115.         return $stmt;
  116.     }
  117. }