vendor/doctrine/dbal/src/Driver/PDO/Connection.php line 69

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDO;
  3. use Doctrine\DBAL\Driver\Result as ResultInterface;
  4. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  5. use Doctrine\DBAL\Driver\Statement as StatementInterface;
  6. use Doctrine\DBAL\ParameterType;
  7. use Doctrine\Deprecations\Deprecation;
  8. use PDO;
  9. use PDOException;
  10. use PDOStatement;
  11. use function assert;
  12. final class Connection implements ServerInfoAwareConnection
  13. {
  14.     private PDO $connection;
  15.     /** @internal The connection can be only instantiated by its driver. */
  16.     public function __construct(PDO $connection)
  17.     {
  18.         $connection->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  19.         $this->connection $connection;
  20.     }
  21.     public function exec(string $sql): int
  22.     {
  23.         try {
  24.             $result $this->connection->exec($sql);
  25.             assert($result !== false);
  26.             return $result;
  27.         } catch (PDOException $exception) {
  28.             throw Exception::new($exception);
  29.         }
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function getServerVersion()
  35.     {
  36.         return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  37.     }
  38.     /**
  39.      * {@inheritDoc}
  40.      *
  41.      * @return Statement
  42.      */
  43.     public function prepare(string $sql): StatementInterface
  44.     {
  45.         try {
  46.             $stmt $this->connection->prepare($sql);
  47.             assert($stmt instanceof PDOStatement);
  48.             return new Statement($stmt);
  49.         } catch (PDOException $exception) {
  50.             throw Exception::new($exception);
  51.         }
  52.     }
  53.     public function query(string $sql): ResultInterface
  54.     {
  55.         try {
  56.             $stmt $this->connection->query($sql);
  57.             assert($stmt instanceof PDOStatement);
  58.             return new Result($stmt);
  59.         } catch (PDOException $exception) {
  60.             throw Exception::new($exception);
  61.         }
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function quote($value$type ParameterType::STRING)
  67.     {
  68.         return $this->connection->quote($value$type);
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function lastInsertId($name null)
  74.     {
  75.         try {
  76.             if ($name === null) {
  77.                 return $this->connection->lastInsertId();
  78.             }
  79.             Deprecation::triggerIfCalledFromOutside(
  80.                 'doctrine/dbal',
  81.                 'https://github.com/doctrine/dbal/issues/4687',
  82.                 'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
  83.             );
  84.             return $this->connection->lastInsertId($name);
  85.         } catch (PDOException $exception) {
  86.             throw Exception::new($exception);
  87.         }
  88.     }
  89.     public function beginTransaction(): bool
  90.     {
  91.         return $this->connection->beginTransaction();
  92.     }
  93.     public function commit(): bool
  94.     {
  95.         return $this->connection->commit();
  96.     }
  97.     public function rollBack(): bool
  98.     {
  99.         return $this->connection->rollBack();
  100.     }
  101.     public function getNativeConnection(): PDO
  102.     {
  103.         return $this->connection;
  104.     }
  105.     /** @deprecated Call {@see getNativeConnection()} instead. */
  106.     public function getWrappedConnection(): PDO
  107.     {
  108.         Deprecation::triggerIfCalledFromOutside(
  109.             'doctrine/dbal',
  110.             'https://github.com/doctrine/dbal/pull/5037',
  111.             '%s is deprecated, call getNativeConnection() instead.',
  112.             __METHOD__,
  113.         );
  114.         return $this->getNativeConnection();
  115.     }
  116. }