MySqlConnector.php 5.3KB

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