PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.3
Independent Analytics – WordPress Analytics Plugin v2.15.3
2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / illuminate / database / Connectors / PostgresConnector.php
independent-analytics / vendor / illuminate / database / Connectors Last commit date
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