Failed Conditions
Push — drop-deprecated ( d77510...d983c4 )
by Michael
40:59 queued 38:23
created

Connection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 18
eloc 54
dl 0
loc 116
ccs 40
cts 44
cp 0.9091
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A executeQuery() 0 6 1
A getPortability() 0 3 1
A getFetchCase() 0 3 1
A prepare() 0 6 1
A query() 0 9 1
C connect() 0 38 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 */
39
    private $case;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 106
    public function connect() : void
45
    {
46 106
        if ($this->isConnected()) {
47 106
            return;
48
        }
49
50 106
        parent::connect();
51
52 106
        $params = $this->getParams();
53
54 106
        if (isset($params['portability'])) {
55 106
            if ($this->getDatabasePlatform()->getName() === 'oracle') {
56
                $params['portability'] &= self::PORTABILITY_ORACLE;
57 106
            } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
58 30
                $params['portability'] &= self::PORTABILITY_POSTGRESQL;
59 76
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
60
                $params['portability'] &= self::PORTABILITY_SQLITE;
61 76
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
62
                $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
63 76
            } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
64 5
                $params['portability'] &= self::PORTABILITY_DB2;
65 71
            } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
66 9
                $params['portability'] &= self::PORTABILITY_SQLSRV;
67
            } else {
68 62
                $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
69
            }
70 106
            $this->portability = $params['portability'];
71
        }
72
73 106
        if (! isset($params['fetch_case']) || ! ($this->portability & self::PORTABILITY_FIX_CASE)) {
74
            return;
75
        }
76
77 106
        if ($this->_conn instanceof PDOConnection) {
78
            // make use of c-level support for case handling
79 67
            $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
80
        } else {
81 39
            $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
82
        }
83 106
    }
84
85
    /**
86
     * @return int
87
     */
88 106
    public function getPortability()
89
    {
90 106
        return $this->portability;
91
    }
92
93
    /**
94
     * @return int
95
     */
96 106
    public function getFetchCase()
97
    {
98 106
        return $this->case;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 106
    public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
105
    {
106 106
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
107 106
        $stmt->setFetchMode($this->defaultFetchMode);
108
109 106
        return $stmt;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 106
    public function prepare(string $sql) : DriverStatement
116
    {
117 106
        $stmt = new Statement(parent::prepare($sql), $this);
118 106
        $stmt->setFetchMode($this->defaultFetchMode);
119
120 106
        return $stmt;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 106
    public function query(string $sql) : ResultStatement
127
    {
128 106
        $connection = $this->getWrappedConnection();
129
130 106
        $stmt = $connection->query($sql);
131 106
        $stmt = new Statement($stmt, $this);
132 106
        $stmt->setFetchMode($this->defaultFetchMode);
133
134 106
        return $stmt;
135
    }
136
}
137