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