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