MySqlConnector.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 PDO;
  13. class MySqlConnector extends Connector implements ConnectorInterface
  14. {
  15. /**
  16. * Establish a database connection.
  17. *
  18. * @return PDO
  19. */
  20. public function connect(array $config)
  21. {
  22. $dsn = $this->getDsn($config);
  23. $options = $this->getOptions($config);
  24. // We need to grab the PDO options that should be used while making the brand
  25. // new connection instance. The PDO options control various aspects of the
  26. // connection's behavior, and some might be specified by the developers.
  27. $connection = $this->createConnection($dsn, $config, $options);
  28. if (! empty($config['database'])) {
  29. $connection->exec("use `{$config['database']}`;");
  30. }
  31. $this->configureEncoding($connection, $config);
  32. // Next, we will check to see if a timezone has been specified in this config
  33. // and if it has we will issue a statement to modify the timezone with the
  34. // database. Setting this DB timezone is an optional configuration item.
  35. $this->configureTimezone($connection, $config);
  36. $this->setModes($connection, $config);
  37. return $connection;
  38. }
  39. /**
  40. * Set the connection character set and collation.
  41. *
  42. * @param PDO $connection
  43. */
  44. protected function configureEncoding($connection, array $config)
  45. {
  46. if (! isset($config['charset'])) {
  47. return $connection;
  48. }
  49. $connection->prepare(
  50. "set names '{$config['charset']}'" . $this->getCollation($config)
  51. )->execute();
  52. }
  53. /**
  54. * Get the collation for the configuration.
  55. *
  56. * @return string
  57. */
  58. protected function getCollation(array $config)
  59. {
  60. return isset($config['collation']) ? " collate '{$config['collation']}'" : '';
  61. }
  62. /**
  63. * Set the timezone on the connection.
  64. *
  65. * @param PDO $connection
  66. */
  67. protected function configureTimezone($connection, array $config)
  68. {
  69. if (isset($config['timezone'])) {
  70. $connection->prepare('set time_zone="' . $config['timezone'] . '"')->execute();
  71. }
  72. }
  73. /**
  74. * Create a DSN string from a configuration.
  75. *
  76. * Chooses socket or host/port based on the 'unix_socket' config value.
  77. *
  78. * @return string
  79. */
  80. protected function getDsn(array $config)
  81. {
  82. return $this->hasSocket($config)
  83. ? $this->getSocketDsn($config)
  84. : $this->getHostDsn($config);
  85. }
  86. /**
  87. * Determine if the given configuration array has a UNIX socket value.
  88. *
  89. * @return bool
  90. */
  91. protected function hasSocket(array $config)
  92. {
  93. return isset($config['unix_socket']) && ! empty($config['unix_socket']);
  94. }
  95. /**
  96. * Get the DSN string for a socket configuration.
  97. *
  98. * @return string
  99. */
  100. protected function getSocketDsn(array $config)
  101. {
  102. return "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
  103. }
  104. /**
  105. * Get the DSN string for a host / port configuration.
  106. *
  107. * @return string
  108. */
  109. protected function getHostDsn(array $config)
  110. {
  111. $host = $config['host'] ?? null;
  112. $port = $config['port'] ?? null;
  113. $database = $config['database'] ?? null;
  114. return isset($port)
  115. ? "mysql:host={$host};port={$port};dbname={$database}"
  116. : "mysql:host={$host};dbname={$database}";
  117. }
  118. /**
  119. * Set the modes for the connection.
  120. */
  121. protected function setModes(PDO $connection, array $config)
  122. {
  123. if (isset($config['modes'])) {
  124. $this->setCustomModes($connection, $config);
  125. } elseif (isset($config['strict'])) {
  126. if ($config['strict']) {
  127. $connection->prepare($this->strictMode($connection))->execute();
  128. } else {
  129. $connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute();
  130. }
  131. }
  132. }
  133. /**
  134. * Set the custom modes on the connection.
  135. */
  136. protected function setCustomModes(PDO $connection, array $config)
  137. {
  138. $modes = implode(',', $config['modes']);
  139. $connection->prepare("set session sql_mode='{$modes}'")->execute();
  140. }
  141. /**
  142. * Get the query to enable strict mode.
  143. *
  144. * @return string
  145. */
  146. protected function strictMode(PDO $connection)
  147. {
  148. if (version_compare($connection->getAttribute(PDO::ATTR_SERVER_VERSION), '8.0.11') >= 0) {
  149. return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
  150. }
  151. return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
  152. }
  153. }