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

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