Passed
Push — master ( 9dbd27...a8663d )
by
unknown
13:44
created

Driver::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nop 0
nc 1
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
18
namespace TYPO3\CMS\Core\Database\Driver\PDOPgSql;
19
20
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
21
use Doctrine\DBAL\Exception as DBALException;
22
use PDO;
23
use PDOException;
24
use TYPO3\CMS\Core\Database\Driver\PDOConnection;
25
26
/**
27
 * This is a full "clone" of the class of package doctrine/dbal. Scope is to use the PDOConnection of TYPO3.
28
 * All private methods have to be checked on every release of doctrine/dbal.
29
 */
30
class Driver extends AbstractPostgreSQLDriver
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
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])
0 ignored issues
show
Bug introduced by
The constant PDO::PGSQL_ATTR_DISABLE_PREPARES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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
68
    /**
69
     * Constructs the Postgres PDO DSN.
70
     *
71
     * @param mixed[] $params
72
     *
73
     * @return string The DSN.
74
     */
75
    private function _constructPdoDsn(array $params)
76
    {
77
        $dsn = 'pgsql:';
78
79
        if (isset($params['host']) && $params['host'] !== '') {
80
            $dsn .= 'host=' . $params['host'] . ';';
81
        }
82
83
        if (isset($params['port']) && $params['port'] !== '') {
84
            $dsn .= 'port=' . $params['port'] . ';';
85
        }
86
87
        if (isset($params['dbname'])) {
88
            $dsn .= 'dbname=' . $params['dbname'] . ';';
89
        } elseif (isset($params['default_dbname'])) {
90
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
91
        } else {
92
            // Used for temporary connections to allow operations like dropping the database currently connected to.
93
            // Connecting without an explicit database does not work, therefore "postgres" database is used
94
            // as it is mostly present in every server setup.
95
            $dsn .= 'dbname=postgres;';
96
        }
97
98
        if (isset($params['sslmode'])) {
99
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
100
        }
101
102
        if (isset($params['sslrootcert'])) {
103
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
104
        }
105
106
        if (isset($params['sslcert'])) {
107
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
108
        }
109
110
        if (isset($params['sslkey'])) {
111
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
112
        }
113
114
        if (isset($params['sslcrl'])) {
115
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
116
        }
117
118
        if (isset($params['application_name'])) {
119
            $dsn .= 'application_name=' . $params['application_name'] . ';';
120
        }
121
122
        return $dsn;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     *
128
     * @deprecated
129
     */
130
    public function getName()
131
    {
132
        return 'pdo_pgsql';
133
    }
134
}
135