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

Driver::constructPdoDsn()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 24
rs 8.8333
cc 7
nop 1
nc 32
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\PDOMySql;
19
20
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
21
use Doctrine\DBAL\Exception as DBALException;
22
use PDOException;
23
use TYPO3\CMS\Core\Database\Driver\PDOConnection;
24
25
/**
26
 * This is a full "clone" of the class of package doctrine/dbal. Scope is to use the PDOConnection of TYPO3.
27
 * All private methods have to be checked on every release of doctrine/dbal.
28
 */
29
class Driver extends AbstractMySQLDriver
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
35
    {
36
        try {
37
            $conn = new PDOConnection(
38
                $this->constructPdoDsn($params),
39
                $username,
40
                $password,
41
                $driverOptions
42
            );
43
44
            // use prepared statements for pdo_mysql per default to retrieve native data types
45
            if (!isset($driverOptions[\PDO::ATTR_EMULATE_PREPARES])) {
46
                $conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
47
            }
48
        } catch (PDOException $e) {
49
            throw DBALException::driverException($this, $e);
50
        }
51
52
        return $conn;
53
    }
54
55
    /**
56
     * Constructs the MySql PDO DSN.
57
     *
58
     * @param mixed[] $params
59
     *
60
     * @return string The DSN.
61
     */
62
    protected function constructPdoDsn(array $params)
63
    {
64
        $dsn = 'mysql:';
65
        if (isset($params['host']) && $params['host'] !== '') {
66
            $dsn .= 'host=' . $params['host'] . ';';
67
        }
68
69
        if (isset($params['port'])) {
70
            $dsn .= 'port=' . $params['port'] . ';';
71
        }
72
73
        if (isset($params['dbname'])) {
74
            $dsn .= 'dbname=' . $params['dbname'] . ';';
75
        }
76
77
        if (isset($params['unix_socket'])) {
78
            $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
79
        }
80
81
        if (isset($params['charset'])) {
82
            $dsn .= 'charset=' . $params['charset'] . ';';
83
        }
84
85
        return $dsn;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     *
91
     * @deprecated
92
     */
93
    public function getName()
94
    {
95
        return 'pdo_mysql';
96
    }
97
}
98