Connector.php 3.3 KB

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