MySqlConnector.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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->configureConnection($connection, $config);
  24. return $connection;
  25. }
  26. /**
  27. * Create a DSN string from a configuration.
  28. *
  29. * Chooses socket or host/port based on the 'unix_socket' config value.
  30. *
  31. * @param array $config
  32. * @return string
  33. */
  34. protected function getDsn(array $config)
  35. {
  36. return $this->hasSocket($config)
  37. ? $this->getSocketDsn($config)
  38. : $this->getHostDsn($config);
  39. }
  40. /**
  41. * Determine if the given configuration array has a UNIX socket value.
  42. *
  43. * @param array $config
  44. * @return bool
  45. */
  46. protected function hasSocket(array $config)
  47. {
  48. return isset($config['unix_socket']) && ! empty($config['unix_socket']);
  49. }
  50. /**
  51. * Get the DSN string for a socket configuration.
  52. *
  53. * @param array $config
  54. * @return string
  55. */
  56. protected function getSocketDsn(array $config)
  57. {
  58. return "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
  59. }
  60. /**
  61. * Get the DSN string for a host / port configuration.
  62. *
  63. * @param array $config
  64. * @return string
  65. */
  66. protected function getHostDsn(array $config)
  67. {
  68. extract($config, EXTR_SKIP);
  69. return isset($port)
  70. ? "mysql:host={$host};port={$port};dbname={$database}"
  71. : "mysql:host={$host};dbname={$database}";
  72. }
  73. /**
  74. * Configure the given PDO connection.
  75. *
  76. * @param \PDO $connection
  77. * @param array $config
  78. * @return void
  79. */
  80. protected function configureConnection(PDO $connection, array $config)
  81. {
  82. if (isset($config['isolation_level'])) {
  83. $connection->exec(sprintf('SET SESSION TRANSACTION ISOLATION LEVEL %s;', $config['isolation_level']));
  84. }
  85. $statements = [];
  86. if (isset($config['charset'])) {
  87. if (isset($config['collation'])) {
  88. $statements[] = sprintf("NAMES '%s' COLLATE '%s'", $config['charset'], $config['collation']);
  89. } else {
  90. $statements[] = sprintf("NAMES '%s'", $config['charset']);
  91. }
  92. }
  93. if (isset($config['timezone'])) {
  94. $statements[] = sprintf("time_zone='%s'", $config['timezone']);
  95. }
  96. $sqlMode = $this->getSqlMode($connection, $config);
  97. if ($sqlMode !== null) {
  98. $statements[] = sprintf("SESSION sql_mode='%s'", $sqlMode);
  99. }
  100. if ($statements !== []) {
  101. $connection->exec(sprintf('SET %s;', implode(', ', $statements)));
  102. }
  103. }
  104. /**
  105. * Get the sql_mode value.
  106. *
  107. * @param \PDO $connection
  108. * @param array $config
  109. * @return string|null
  110. */
  111. protected function getSqlMode(PDO $connection, array $config)
  112. {
  113. if (isset($config['modes'])) {
  114. return implode(',', $config['modes']);
  115. }
  116. if (! isset($config['strict'])) {
  117. return null;
  118. }
  119. if (! $config['strict']) {
  120. return 'NO_ENGINE_SUBSTITUTION';
  121. }
  122. $version = $config['version'] ?? $connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  123. if (version_compare($version, '8.0.11') >= 0) {
  124. return 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
  125. }
  126. return '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';
  127. }
  128. }