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

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