PDOConnection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\ParameterType;
  4. use PDO;
  5. use function count;
  6. use function func_get_args;
  7. /**
  8. * PDO implementation of the Connection interface.
  9. * Used by all PDO-based drivers.
  10. */
  11. class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
  12. {
  13. /**
  14. * @param string $dsn
  15. * @param string|null $user
  16. * @param string|null $password
  17. * @param mixed[]|null $options
  18. *
  19. * @throws PDOException In case of an error.
  20. */
  21. public function __construct($dsn, $user = null, $password = null, ?array $options = null)
  22. {
  23. try {
  24. parent::__construct($dsn, $user, $password, $options);
  25. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
  26. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27. } catch (\PDOException $exception) {
  28. throw new PDOException($exception);
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function exec($statement)
  35. {
  36. try {
  37. return parent::exec($statement);
  38. } catch (\PDOException $exception) {
  39. throw new PDOException($exception);
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getServerVersion()
  46. {
  47. return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function prepare($prepareString, $driverOptions = [])
  53. {
  54. try {
  55. return parent::prepare($prepareString, $driverOptions);
  56. } catch (\PDOException $exception) {
  57. throw new PDOException($exception);
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function query()
  64. {
  65. $args = func_get_args();
  66. $argsCount = count($args);
  67. try {
  68. if ($argsCount === 4) {
  69. return parent::query($args[0], $args[1], $args[2], $args[3]);
  70. }
  71. if ($argsCount === 3) {
  72. return parent::query($args[0], $args[1], $args[2]);
  73. }
  74. if ($argsCount === 2) {
  75. return parent::query($args[0], $args[1]);
  76. }
  77. return parent::query($args[0]);
  78. } catch (\PDOException $exception) {
  79. throw new PDOException($exception);
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function quote($input, $type = ParameterType::STRING)
  86. {
  87. return parent::quote($input, $type);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function lastInsertId($name = null)
  93. {
  94. try {
  95. return parent::lastInsertId($name);
  96. } catch (\PDOException $exception) {
  97. throw new PDOException($exception);
  98. }
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function requiresQueryForServerVersion()
  104. {
  105. return false;
  106. }
  107. }