Connection.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\DBAL;
  12. use Doctrine\DBAL\Driver\PDO\Exception;
  13. use Doctrine\DBAL\Driver\PDO\Result;
  14. use Doctrine\DBAL\Driver\PDO\Statement;
  15. use Doctrine\DBAL\Driver\Result as ResultInterface;
  16. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  17. use Doctrine\DBAL\Driver\Statement as StatementInterface;
  18. use Doctrine\DBAL\ParameterType;
  19. use PDO;
  20. use PDOException;
  21. use PDOStatement;
  22. use function assert;
  23. class Connection implements ServerInfoAwareConnection
  24. {
  25. /**
  26. * Create a new PDO connection instance.
  27. */
  28. public function __construct(protected PDO $connection)
  29. {
  30. }
  31. /**
  32. * Execute an SQL statement.
  33. */
  34. public function exec(string $sql): int
  35. {
  36. try {
  37. $result = $this->connection->exec($sql);
  38. assert($result !== false);
  39. return $result;
  40. } catch (PDOException $exception) {
  41. throw Exception::new($exception);
  42. }
  43. }
  44. /**
  45. * Prepare a new SQL statement.
  46. */
  47. public function prepare(string $sql): StatementInterface
  48. {
  49. try {
  50. return $this->createStatement(
  51. $this->connection->prepare($sql)
  52. );
  53. } catch (PDOException $exception) {
  54. throw Exception::new($exception);
  55. }
  56. }
  57. /**
  58. * Execute a new query against the connection.
  59. */
  60. public function query(string $sql): ResultInterface
  61. {
  62. try {
  63. $stmt = $this->connection->query($sql);
  64. assert($stmt instanceof PDOStatement);
  65. return new Result($stmt);
  66. } catch (PDOException $exception) {
  67. throw Exception::new($exception);
  68. }
  69. }
  70. /**
  71. * Get the last insert ID.
  72. *
  73. * @param null|string $name
  74. * @return string
  75. */
  76. public function lastInsertId($name = null)
  77. {
  78. try {
  79. if ($name === null) {
  80. return $this->connection->lastInsertId();
  81. }
  82. return $this->connection->lastInsertId($name);
  83. } catch (PDOException $exception) {
  84. throw Exception::new($exception);
  85. }
  86. }
  87. /**
  88. * Begin a new database transaction.
  89. */
  90. public function beginTransaction()
  91. {
  92. return $this->connection->beginTransaction();
  93. }
  94. /**
  95. * Commit a database transaction.
  96. */
  97. public function commit()
  98. {
  99. return $this->connection->commit();
  100. }
  101. /**
  102. * Roll back a database transaction.
  103. */
  104. public function rollBack()
  105. {
  106. return $this->connection->rollBack();
  107. }
  108. /**
  109. * Wrap quotes around the given input.
  110. *
  111. * @param string $input
  112. * @param string $type
  113. * @return string
  114. */
  115. public function quote($input, $type = ParameterType::STRING)
  116. {
  117. return $this->connection->quote($input, $type);
  118. }
  119. /**
  120. * Get the server version for the connection.
  121. *
  122. * @return string
  123. */
  124. public function getServerVersion()
  125. {
  126. return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  127. }
  128. /**
  129. * Get the wrapped PDO connection.
  130. */
  131. public function getWrappedConnection(): PDO
  132. {
  133. return $this->connection;
  134. }
  135. /**
  136. * Create a new statement instance.
  137. */
  138. protected function createStatement(PDOStatement $stmt): Statement
  139. {
  140. return new Statement($stmt);
  141. }
  142. }