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

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