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

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDO;
  3. use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
  4. use Doctrine\DBAL\Driver\PDO\PDOException as DriverPDOException;
  5. use Doctrine\DBAL\Driver\Result as ResultInterface;
  6. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  7. use Doctrine\DBAL\Driver\Statement as StatementInterface;
  8. use Doctrine\DBAL\ParameterType;
  9. use Doctrine\Deprecations\Deprecation;
  10. use PDO;
  11. use PDOException;
  12. use PDOStatement;
  13. use function assert;
  14. final class Connection implements ServerInfoAwareConnection
  15. {
  16. private PDO $connection;
  17. /** @internal The connection can be only instantiated by its driver. */
  18. public function __construct(PDO $connection)
  19. {
  20. $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::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. * @throws UnknownParameterType
  69. *
  70. * @psalm-assert ParameterType::* $type
  71. */
  72. public function quote($value, $type = ParameterType::STRING)
  73. {
  74. return $this->connection->quote($value, ParameterTypeMap::convertParamType($type));
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function lastInsertId($name = null)
  80. {
  81. try {
  82. if ($name === null) {
  83. return $this->connection->lastInsertId();
  84. }
  85. Deprecation::triggerIfCalledFromOutside(
  86. 'doctrine/dbal',
  87. 'https://github.com/doctrine/dbal/issues/4687',
  88. 'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
  89. );
  90. return $this->connection->lastInsertId($name);
  91. } catch (PDOException $exception) {
  92. throw Exception::new($exception);
  93. }
  94. }
  95. public function beginTransaction(): bool
  96. {
  97. try {
  98. return $this->connection->beginTransaction();
  99. } catch (PDOException $exception) {
  100. throw DriverPDOException::new($exception);
  101. }
  102. }
  103. public function commit(): bool
  104. {
  105. try {
  106. return $this->connection->commit();
  107. } catch (PDOException $exception) {
  108. throw DriverPDOException::new($exception);
  109. }
  110. }
  111. public function rollBack(): bool
  112. {
  113. try {
  114. return $this->connection->rollBack();
  115. } catch (PDOException $exception) {
  116. throw DriverPDOException::new($exception);
  117. }
  118. }
  119. public function getNativeConnection(): PDO
  120. {
  121. return $this->connection;
  122. }
  123. /** @deprecated Call {@see getNativeConnection()} instead. */
  124. public function getWrappedConnection(): PDO
  125. {
  126. Deprecation::triggerIfCalledFromOutside(
  127. 'doctrine/dbal',
  128. 'https://github.com/doctrine/dbal/pull/5037',
  129. '%s is deprecated, call getNativeConnection() instead.',
  130. __METHOD__,
  131. );
  132. return $this->getNativeConnection();
  133. }
  134. }