PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
2.15.5 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 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