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