Failed Conditions
Pull Request — master (#3973)
by Grégoire
03:04
created

Connection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 106
ccs 41
cts 44
cp 0.9318
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A executeQuery() 0 10 1
A getPortability() 0 3 1
A getFetchCase() 0 3 1
A prepare() 0 6 1
A query() 0 9 1
C connect() 0 39 13
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Portability;
6
7
use Doctrine\DBAL\Cache\QueryCacheProfile;
8
use Doctrine\DBAL\ColumnCase;
9
use Doctrine\DBAL\Driver\PDOConnection;
10
use Doctrine\DBAL\Driver\ResultStatement;
11
use Doctrine\DBAL\Driver\Statement as DriverStatement;
12
use PDO;
13
use const CASE_LOWER;
14
use const CASE_UPPER;
15
16
/**
17
 * Portability wrapper for a Connection.
18
 */
19
class Connection extends \Doctrine\DBAL\Connection
20
{
21
    public const PORTABILITY_ALL           = 255;
22
    public const PORTABILITY_NONE          = 0;
23
    public const PORTABILITY_RTRIM         = 1;
24
    public const PORTABILITY_EMPTY_TO_NULL = 4;
25
    public const PORTABILITY_FIX_CASE      = 8;
26
27
    public const PORTABILITY_DB2          = 13;
28
    public const PORTABILITY_ORACLE       = 9;
29
    public const PORTABILITY_POSTGRESQL   = 13;
30
    public const PORTABILITY_SQLITE       = 13;
31
    public const PORTABILITY_OTHERVENDORS = 12;
32
    public const PORTABILITY_SQLANYWHERE  = 13;
33
    public const PORTABILITY_SQLSRV       = 13;
34
35
    /** @var int */
36
    private $portability = self::PORTABILITY_NONE;
37
38
    /** @var int|null */
39
    private $case;
40
41 105
    public function connect() : void
42
    {
43 105
        if ($this->isConnected()) {
44 105
            return;
45
        }
46
47 105
        parent::connect();
48
49 105
        $params = $this->getParams();
50
51 105
        if (isset($params['portability'])) {
52 105
            if ($this->getDatabasePlatform()->getName() === 'oracle') {
53 5
                $params['portability'] &= self::PORTABILITY_ORACLE;
54 100
            } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
55 25
                $params['portability'] &= self::PORTABILITY_POSTGRESQL;
56 75
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
57 5
                $params['portability'] &= self::PORTABILITY_SQLITE;
58 70
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
59
                $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
60 70
            } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
61
                $params['portability'] &= self::PORTABILITY_DB2;
62 70
            } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
63 10
                $params['portability'] &= self::PORTABILITY_SQLSRV;
64
            } else {
65 60
                $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
66
            }
67
68 105
            $this->portability = $params['portability'];
69
        }
70
71 105
        if (! isset($params['fetch_case']) || ($this->portability & self::PORTABILITY_FIX_CASE) === 0) {
72
            return;
73
        }
74
75 105
        if ($this->_conn instanceof PDOConnection) {
76
            // make use of c-level support for case handling
77 65
            $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
78
        } else {
79 40
            $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
80
        }
81 105
    }
82
83 105
    public function getPortability() : int
84
    {
85 105
        return $this->portability;
86
    }
87
88 105
    public function getFetchCase() : ?int
89
    {
90 105
        return $this->case;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 42
    public function executeQuery(
97
        string $query,
98
        array $params = [],
99
        array $types = [],
100
        ?QueryCacheProfile $qcp = null
101
    ) : ResultStatement {
102 42
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
103 42
        $stmt->setFetchMode($this->defaultFetchMode);
104
105 42
        return $stmt;
106
    }
107
108 42
    public function prepare(string $sql) : DriverStatement
109
    {
110 42
        $stmt = new Statement(parent::prepare($sql), $this);
111 42
        $stmt->setFetchMode($this->defaultFetchMode);
112
113 42
        return $stmt;
114
    }
115
116 105
    public function query(string $sql) : ResultStatement
117
    {
118 105
        $connection = $this->getWrappedConnection();
119
120 105
        $stmt = $connection->query($sql);
121 105
        $stmt = new Statement($stmt, $this);
122 105
        $stmt->setFetchMode($this->defaultFetchMode);
123
124 105
        return $stmt;
125
    }
126
}
127