Connector.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Connectors;
  12. use Exception;
  13. use Hyperf\Database\DetectsLostConnections;
  14. use PDO;
  15. use Throwable;
  16. class Connector
  17. {
  18. use DetectsLostConnections;
  19. /**
  20. * The default PDO connection options.
  21. *
  22. * @var array
  23. */
  24. protected $options = [
  25. PDO::ATTR_CASE => PDO::CASE_NATURAL,
  26. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  27. PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
  28. PDO::ATTR_STRINGIFY_FETCHES => false,
  29. PDO::ATTR_EMULATE_PREPARES => false,
  30. ];
  31. /**
  32. * Create a new PDO connection.
  33. *
  34. * @param string $dsn
  35. * @return PDO
  36. * @throws Exception
  37. */
  38. public function createConnection($dsn, array $config, array $options)
  39. {
  40. [$username, $password] = [
  41. $config['username'] ?? null, $config['password'] ?? null,
  42. ];
  43. try {
  44. return $this->createPdoConnection(
  45. $dsn,
  46. $username,
  47. $password,
  48. $options
  49. );
  50. } catch (Exception $e) {
  51. return $this->tryAgainIfCausedByLostConnection(
  52. $e,
  53. $dsn,
  54. $username,
  55. $password,
  56. $options
  57. );
  58. }
  59. }
  60. /**
  61. * Get the PDO options based on the configuration.
  62. *
  63. * @return array
  64. */
  65. public function getOptions(array $config)
  66. {
  67. return array_replace($this->options, $config['options'] ?? []);
  68. }
  69. /**
  70. * Get the default PDO connection options.
  71. *
  72. * @return array
  73. */
  74. public function getDefaultOptions()
  75. {
  76. return $this->options;
  77. }
  78. /**
  79. * Set the default PDO connection options.
  80. */
  81. public function setDefaultOptions(array $options)
  82. {
  83. $this->options = $options;
  84. }
  85. /**
  86. * Create a new PDO connection instance.
  87. *
  88. * @param string $dsn
  89. * @param string $username
  90. * @param string $password
  91. * @param array $options
  92. * @return PDO
  93. */
  94. protected function createPdoConnection($dsn, $username, $password, $options)
  95. {
  96. return new PDO($dsn, $username, $password, $options);
  97. }
  98. /**
  99. * Determine if the connection is persistent.
  100. *
  101. * @param array $options
  102. * @return bool
  103. */
  104. protected function isPersistentConnection($options)
  105. {
  106. return isset($options[PDO::ATTR_PERSISTENT])
  107. && $options[PDO::ATTR_PERSISTENT];
  108. }
  109. /**
  110. * Handle an exception that occurred during connect execution.
  111. *
  112. * @param string $dsn
  113. * @param string $username
  114. * @param string $password
  115. * @param array $options
  116. * @return PDO
  117. * @throws Exception
  118. */
  119. protected function tryAgainIfCausedByLostConnection(Throwable $e, $dsn, $username, $password, $options)
  120. {
  121. if ($this->causedByLostConnection($e)) {
  122. return $this->createPdoConnection($dsn, $username, $password, $options);
  123. }
  124. throw $e;
  125. }
  126. }