‪TYPO3CMS  ‪main
Driver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
21 use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface;
22 use Doctrine\DBAL\Driver\PDO\Exception;
24 
31 final class ‪Driver extends AbstractPostgreSQLDriver
32 {
33  public function ‪connect(array $params): DriverConnectionInterface
34  {
35  $driverOptions = $params['driverOptions'] ?? [];
36 
37  if (!empty($params['persistent'])) {
38  $driverOptions[\PDO::ATTR_PERSISTENT] = true;
39  }
40 
41  try {
42  $pdo = new \PDO(
43  $this->‪constructPdoDsn($params),
44  $params['user'] ?? '',
45  $params['password'] ?? '',
46  $driverOptions,
47  );
48 
49  if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
50  && (
51  !isset($driverOptions[\PDO::PGSQL_ATTR_DISABLE_PREPARES])
52  || $driverOptions[\PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
53  )
54  ) {
55  $pdo->setAttribute(\PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
56  }
57 
58  // defining client_encoding via SET NAMES to avoid inconsistent DSN support:
59  // - the 'client_encoding' connection param only works with postgres >= 9.1
60  // - passing client_encoding via the 'options' param breaks pgbouncer support
61  if (isset($params['charset'])) {
62  $pdo->exec('SET NAMES \'' . $params['charset'] . '\'');
63  }
64  } catch (\PDOException $exception) {
65  throw Exception::new($exception);
66  }
67 
68  return new ‪DriverConnection($pdo);
69  }
70 
74  private function ‪constructPdoDsn(array $params): string
75  {
76  $dsn = 'pgsql:';
77  if (isset($params['host']) && $params['host'] !== '') {
78  $dsn .= 'host=' . $params['host'] . ';';
79  }
80  if (isset($params['port']) && $params['port'] !== '') {
81  $dsn .= 'port=' . $params['port'] . ';';
82  }
83  if (isset($params['dbname'])) {
84  $dsn .= 'dbname=' . $params['dbname'] . ';';
85  } elseif (isset($params['default_dbname'])) {
86  $dsn .= 'dbname=' . $params['default_dbname'] . ';';
87  } else {
88  // Used for temporary connections to allow operations like dropping the database currently connected to.
89  // Connecting without an explicit database does not work, therefore "postgres" database is used
90  // as it is mostly present in every server setup.
91  $dsn .= 'dbname=postgres;';
92  }
93  if (isset($params['sslmode'])) {
94  $dsn .= 'sslmode=' . $params['sslmode'] . ';';
95  }
96  if (isset($params['sslrootcert'])) {
97  $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
98  }
99  if (isset($params['sslcert'])) {
100  $dsn .= 'sslcert=' . $params['sslcert'] . ';';
101  }
102  if (isset($params['sslkey'])) {
103  $dsn .= 'sslkey=' . $params['sslkey'] . ';';
104  }
105  if (isset($params['sslcrl'])) {
106  $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
107  }
108  if (isset($params['application_name'])) {
109  $dsn .= 'application_name=' . $params['application_name'] . ';';
110  }
111  return $dsn;
112  }
113 }
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver
Definition: Driver.php:32
‪TYPO3\CMS\Core\Database\Driver\DriverConnection
Definition: DriverConnection.php:35
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver\connect
‪connect(array $params)
Definition: Driver.php:33
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql
Definition: Driver.php:18
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver\constructPdoDsn
‪string constructPdoDsn(array $params)
Definition: Driver.php:74