Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\PDO\PgSQL;
4
 
5
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
6
use Doctrine\DBAL\Driver\PDO\Connection;
7
use Doctrine\DBAL\Driver\PDO\Exception;
8
use Doctrine\Deprecations\Deprecation;
9
use PDO;
10
use PDOException;
621 lars 11
use SensitiveParameter;
148 lars 12
 
13
final class Driver extends AbstractPostgreSQLDriver
14
{
15
    /**
16
     * {@inheritdoc}
17
     *
18
     * @return Connection
19
     */
621 lars 20
    public function connect(
21
        #[SensitiveParameter]
22
        array $params
23
    ) {
148 lars 24
        $driverOptions = $params['driverOptions'] ?? [];
25
 
26
        if (! empty($params['persistent'])) {
27
            $driverOptions[PDO::ATTR_PERSISTENT] = true;
28
        }
29
 
621 lars 30
        $safeParams = $params;
31
        unset($safeParams['password'], $safeParams['url']);
32
 
148 lars 33
        try {
34
            $pdo = new PDO(
621 lars 35
                $this->constructPdoDsn($safeParams),
148 lars 36
                $params['user'] ?? '',
37
                $params['password'] ?? '',
38
                $driverOptions,
39
            );
40
        } catch (PDOException $exception) {
41
            throw Exception::new($exception);
42
        }
43
 
44
        if (
45
            ! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
46
            || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
47
        ) {
48
            $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
49
        }
50
 
51
        $connection = new Connection($pdo);
52
 
53
        /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
54
         * - passing client_encoding via the 'options' param breaks pgbouncer support
55
         */
56
        if (isset($params['charset'])) {
57
            $connection->exec('SET NAMES \'' . $params['charset'] . '\'');
58
        }
59
 
60
        return $connection;
61
    }
62
 
63
    /**
64
     * Constructs the Postgres PDO DSN.
65
     *
621 lars 66
     * @param array<string, mixed> $params
148 lars 67
     */
68
    private function constructPdoDsn(array $params): string
69
    {
70
        $dsn = 'pgsql:';
71
 
72
        if (isset($params['host']) && $params['host'] !== '') {
73
            $dsn .= 'host=' . $params['host'] . ';';
74
        }
75
 
76
        if (isset($params['port']) && $params['port'] !== '') {
77
            $dsn .= 'port=' . $params['port'] . ';';
78
        }
79
 
80
        if (isset($params['dbname'])) {
81
            $dsn .= 'dbname=' . $params['dbname'] . ';';
82
        } elseif (isset($params['default_dbname'])) {
83
            Deprecation::trigger(
84
                'doctrine/dbal',
85
                'https://github.com/doctrine/dbal/pull/5705',
86
                'The "default_dbname" connection parameter is deprecated. Use "dbname" instead.',
87
            );
88
 
89
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
90
        } else {
91
            if (isset($params['user']) && $params['user'] !== 'postgres') {
92
                Deprecation::trigger(
93
                    'doctrine/dbal',
94
                    'https://github.com/doctrine/dbal/pull/5705',
95
                    'Relying on the DBAL connecting to the "postgres" database by default is deprecated.'
96
                        . ' Unless you want to have the server determine the default database for the connection,'
97
                        . ' specify the database name explicitly.',
98
                );
99
            }
100
 
101
            // Used for temporary connections to allow operations like dropping the database currently connected to.
102
            $dsn .= 'dbname=postgres;';
103
        }
104
 
105
        if (isset($params['sslmode'])) {
106
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
107
        }
108
 
109
        if (isset($params['sslrootcert'])) {
110
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
111
        }
112
 
113
        if (isset($params['sslcert'])) {
114
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
115
        }
116
 
117
        if (isset($params['sslkey'])) {
118
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
119
        }
120
 
121
        if (isset($params['sslcrl'])) {
122
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
123
        }
124
 
125
        if (isset($params['application_name'])) {
126
            $dsn .= 'application_name=' . $params['application_name'] . ';';
127
        }
128
 
129
        return $dsn;
130
    }
131
}