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

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