‪TYPO3CMS  10.4
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\DBALException;
21 use Doctrine\DBAL\Driver\PDOPgSql\Driver as DoctrineDbalPDOPgSqlDriver;
22 use PDO;
23 use PDOException;
25 
30 class ‪Driver extends DoctrineDbalPDOPgSqlDriver
31 {
35  public function ‪connect(array $params, $username = null, $password = null, array $driverOptions = [])
36  {
37  try {
38  $pdo = new ‪PDOConnection(
39  $this->‪_constructPdoDsn($params),
40  $username,
41  $password,
42  $driverOptions
43  );
44 
45  if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
46  && (
47  ! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
48  || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
49  )
50  ) {
51  $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
52  }
53 
54  /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
55  * - the 'client_encoding' connection param only works with postgres >= 9.1
56  * - passing client_encoding via the 'options' param breaks pgbouncer support
57  */
58  if (isset($params['charset'])) {
59  $pdo->exec('SET NAMES \'' . $params['charset'] . '\'');
60  }
61 
62  return $pdo;
63  } catch (PDOException $e) {
64  throw DBALException::driverException($this, $e);
65  }
66  }
67 
71  private function ‪_constructPdoDsn(array $params)
72  {
73  $dsn = 'pgsql:';
74 
75  if (isset($params['host']) && $params['host'] !== '') {
76  $dsn .= 'host=' . $params['host'] . ';';
77  }
78 
79  if (isset($params['port']) && $params['port'] !== '') {
80  $dsn .= 'port=' . $params['port'] . ';';
81  }
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 
94  if (isset($params['sslmode'])) {
95  $dsn .= 'sslmode=' . $params['sslmode'] . ';';
96  }
97 
98  if (isset($params['sslrootcert'])) {
99  $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
100  }
101 
102  if (isset($params['sslcert'])) {
103  $dsn .= 'sslcert=' . $params['sslcert'] . ';';
104  }
105 
106  if (isset($params['sslkey'])) {
107  $dsn .= 'sslkey=' . $params['sslkey'] . ';';
108  }
109 
110  if (isset($params['sslcrl'])) {
111  $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
112  }
113 
114  if (isset($params['application_name'])) {
115  $dsn .= 'application_name=' . $params['application_name'] . ';';
116  }
117 
118  return $dsn;
119  }
120 }
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver
Definition: Driver.php:31
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver\connect
‪connect(array $params, $username=null, $password=null, array $driverOptions=[])
Definition: Driver.php:35
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql
Definition: Driver.php:18
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver\_constructPdoDsn
‪_constructPdoDsn(array $params)
Definition: Driver.php:71
‪TYPO3\CMS\Core\Database\Driver\PDOConnection
Definition: PDOConnection.php:29