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