PDOConnection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
  4. use Doctrine\DBAL\Driver\PDO\Exception;
  5. use Doctrine\DBAL\Driver\PDO\Statement;
  6. use Doctrine\DBAL\ParameterType;
  7. use Doctrine\Deprecations\Deprecation;
  8. use PDO;
  9. use PDOException;
  10. use PDOStatement;
  11. use function assert;
  12. /**
  13. * PDO implementation of the Connection interface.
  14. * Used by all PDO-based drivers.
  15. *
  16. * @deprecated Use {@link Connection} instead
  17. */
  18. class PDOConnection extends PDO implements ConnectionInterface, ServerInfoAwareConnection
  19. {
  20. use PDOQueryImplementation;
  21. /**
  22. * @internal The connection can be only instantiated by its driver.
  23. *
  24. * @param string $dsn
  25. * @param string|null $user
  26. * @param string|null $password
  27. * @param mixed[]|null $options
  28. *
  29. * @throws PDOException In case of an error.
  30. */
  31. public function __construct($dsn, $user = null, $password = null, ?array $options = null)
  32. {
  33. try {
  34. parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
  35. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class, []]);
  36. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  37. } catch (PDOException $exception) {
  38. throw Exception::new($exception);
  39. }
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function exec($sql)
  45. {
  46. try {
  47. $result = parent::exec($sql);
  48. assert($result !== false);
  49. return $result;
  50. } catch (PDOException $exception) {
  51. throw Exception::new($exception);
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getServerVersion()
  58. {
  59. return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
  60. }
  61. /**
  62. * @param string $sql
  63. * @param array<int, int> $driverOptions
  64. *
  65. * @return PDOStatement
  66. */
  67. public function prepare($sql, $driverOptions = [])
  68. {
  69. try {
  70. $statement = parent::prepare($sql, $driverOptions);
  71. assert($statement instanceof PDOStatement);
  72. return $statement;
  73. } catch (PDOException $exception) {
  74. throw Exception::new($exception);
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function quote($value, $type = ParameterType::STRING)
  81. {
  82. return parent::quote($value, $type);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function lastInsertId($name = null)
  88. {
  89. try {
  90. if ($name === null) {
  91. return parent::lastInsertId();
  92. }
  93. return parent::lastInsertId($name);
  94. } catch (PDOException $exception) {
  95. throw Exception::new($exception);
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function requiresQueryForServerVersion()
  102. {
  103. Deprecation::triggerIfCalledFromOutside(
  104. 'doctrine/dbal',
  105. 'https://github.com/doctrine/dbal/pull/4114',
  106. 'ServerInfoAwareConnection::requiresQueryForServerVersion() is deprecated and removed in DBAL 3.'
  107. );
  108. return false;
  109. }
  110. /**
  111. * @param mixed ...$args
  112. */
  113. private function doQuery(...$args): PDOStatement
  114. {
  115. try {
  116. $stmt = parent::query(...$args);
  117. } catch (PDOException $exception) {
  118. throw Exception::new($exception);
  119. }
  120. assert($stmt instanceof PDOStatement);
  121. return $stmt;
  122. }
  123. }