ConnectionFactory.php
2 years ago
Connector.php
2 years ago
ConnectorInterface.php
2 years ago
MySqlConnector.php
2 years ago
PostgresConnector.php
2 years ago
SQLiteConnector.php
2 years ago
SqlServerConnector.php
2 years ago
PostgresConnector.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Illuminate\Database\Connectors; |
| 4 | |
| 5 | use PDO; |
| 6 | /** @internal */ |
| 7 | class PostgresConnector extends Connector implements ConnectorInterface |
| 8 | { |
| 9 | /** |
| 10 | * The default PDO connection options. |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | 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]; |
| 15 | /** |
| 16 | * Establish a database connection. |
| 17 | * |
| 18 | * @param array $config |
| 19 | * @return \PDO |
| 20 | */ |
| 21 | public function connect(array $config) |
| 22 | { |
| 23 | // First we'll create the basic DSN and connection instance connecting to the |
| 24 | // using the configuration option specified by the developer. We will also |
| 25 | // set the default character set on the connections to UTF-8 by default. |
| 26 | $connection = $this->createConnection($this->getDsn($config), $config, $this->getOptions($config)); |
| 27 | $this->configureIsolationLevel($connection, $config); |
| 28 | $this->configureEncoding($connection, $config); |
| 29 | // Next, we will check to see if a timezone has been specified in this config |
| 30 | // and if it has we will issue a statement to modify the timezone with the |
| 31 | // database. Setting this DB timezone is an optional configuration item. |
| 32 | $this->configureTimezone($connection, $config); |
| 33 | $this->configureSchema($connection, $config); |
| 34 | // Postgres allows an application_name to be set by the user and this name is |
| 35 | // used to when monitoring the application with pg_stat_activity. So we'll |
| 36 | // determine if the option has been specified and run a statement if so. |
| 37 | $this->configureApplicationName($connection, $config); |
| 38 | $this->configureSynchronousCommit($connection, $config); |
| 39 | return $connection; |
| 40 | } |
| 41 | /** |
| 42 | * Set the connection transaction isolation level. |
| 43 | * |
| 44 | * @param \PDO $connection |
| 45 | * @param array $config |
| 46 | * @return void |
| 47 | */ |
| 48 | protected function configureIsolationLevel($connection, array $config) |
| 49 | { |
| 50 | if (isset($config['isolation_level'])) { |
| 51 | $connection->prepare("set session characteristics as transaction isolation level {$config['isolation_level']}")->execute(); |
| 52 | } |
| 53 | } |
| 54 | /** |
| 55 | * Set the connection character set and collation. |
| 56 | * |
| 57 | * @param \PDO $connection |
| 58 | * @param array $config |
| 59 | * @return void |
| 60 | */ |
| 61 | protected function configureEncoding($connection, $config) |
| 62 | { |
| 63 | if (!isset($config['charset'])) { |
| 64 | return; |
| 65 | } |
| 66 | $connection->prepare("set names '{$config['charset']}'")->execute(); |
| 67 | } |
| 68 | /** |
| 69 | * Set the timezone on the connection. |
| 70 | * |
| 71 | * @param \PDO $connection |
| 72 | * @param array $config |
| 73 | * @return void |
| 74 | */ |
| 75 | protected function configureTimezone($connection, array $config) |
| 76 | { |
| 77 | if (isset($config['timezone'])) { |
| 78 | $timezone = $config['timezone']; |
| 79 | $connection->prepare("set time zone '{$timezone}'")->execute(); |
| 80 | } |
| 81 | } |
| 82 | /** |
| 83 | * Set the schema on the connection. |
| 84 | * |
| 85 | * @param \PDO $connection |
| 86 | * @param array $config |
| 87 | * @return void |
| 88 | */ |
| 89 | protected function configureSchema($connection, $config) |
| 90 | { |
| 91 | if (isset($config['schema'])) { |
| 92 | $schema = $this->formatSchema($config['schema']); |
| 93 | $connection->prepare("set search_path to {$schema}")->execute(); |
| 94 | } |
| 95 | } |
| 96 | /** |
| 97 | * Format the schema for the DSN. |
| 98 | * |
| 99 | * @param array|string $schema |
| 100 | * @return string |
| 101 | */ |
| 102 | protected function formatSchema($schema) |
| 103 | { |
| 104 | if (\is_array($schema)) { |
| 105 | return '"' . \implode('", "', $schema) . '"'; |
| 106 | } |
| 107 | return '"' . $schema . '"'; |
| 108 | } |
| 109 | /** |
| 110 | * Set the schema on the connection. |
| 111 | * |
| 112 | * @param \PDO $connection |
| 113 | * @param array $config |
| 114 | * @return void |
| 115 | */ |
| 116 | protected function configureApplicationName($connection, $config) |
| 117 | { |
| 118 | if (isset($config['application_name'])) { |
| 119 | $applicationName = $config['application_name']; |
| 120 | $connection->prepare("set application_name to '{$applicationName}'")->execute(); |
| 121 | } |
| 122 | } |
| 123 | /** |
| 124 | * Create a DSN string from a configuration. |
| 125 | * |
| 126 | * @param array $config |
| 127 | * @return string |
| 128 | */ |
| 129 | protected function getDsn(array $config) |
| 130 | { |
| 131 | // First we will create the basic DSN setup as well as the port if it is in |
| 132 | // in the configuration options. This will give us the basic DSN we will |
| 133 | // need to establish the PDO connections and return them back for use. |
| 134 | \extract($config, \EXTR_SKIP); |
| 135 | $host = isset($host) ? "host={$host};" : ''; |
| 136 | $dsn = "pgsql:{$host}dbname='{$database}'"; |
| 137 | // If a port was specified, we will add it to this Postgres DSN connections |
| 138 | // format. Once we have done that we are ready to return this connection |
| 139 | // string back out for usage, as this has been fully constructed here. |
| 140 | if (isset($config['port'])) { |
| 141 | $dsn .= ";port={$port}"; |
| 142 | } |
| 143 | return $this->addSslOptions($dsn, $config); |
| 144 | } |
| 145 | /** |
| 146 | * Add the SSL options to the DSN. |
| 147 | * |
| 148 | * @param string $dsn |
| 149 | * @param array $config |
| 150 | * @return string |
| 151 | */ |
| 152 | protected function addSslOptions($dsn, array $config) |
| 153 | { |
| 154 | foreach (['sslmode', 'sslcert', 'sslkey', 'sslrootcert'] as $option) { |
| 155 | if (isset($config[$option])) { |
| 156 | $dsn .= ";{$option}={$config[$option]}"; |
| 157 | } |
| 158 | } |
| 159 | return $dsn; |
| 160 | } |
| 161 | /** |
| 162 | * Configure the synchronous_commit setting. |
| 163 | * |
| 164 | * @param \PDO $connection |
| 165 | * @param array $config |
| 166 | * @return void |
| 167 | */ |
| 168 | protected function configureSynchronousCommit($connection, array $config) |
| 169 | { |
| 170 | if (!isset($config['synchronous_commit'])) { |
| 171 | return; |
| 172 | } |
| 173 | $connection->prepare("set synchronous_commit to '{$config['synchronous_commit']}'")->execute(); |
| 174 | } |
| 175 | } |
| 176 |