Failed Conditions
Pull Request — master (#4007)
by Sergei
11:47
created

Connection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 97
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPortability() 0 3 1
C connect() 0 39 13
A getFetchCase() 0 3 1
A executeQuery() 0 7 1
A prepare() 0 3 1
A query() 0 6 1
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 110
    public function connect() : void
42
    {
43 110
        if ($this->isConnected()) {
44 110
            return;
45
        }
46
47 110
        parent::connect();
48
49 110
        $params = $this->getParams();
50
51 110
        if (isset($params['portability'])) {
52 110
            if ($this->getDatabasePlatform()->getName() === 'oracle') {
53 5
                $params['portability'] &= self::PORTABILITY_ORACLE;
54 105
            } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
55 25
                $params['portability'] &= self::PORTABILITY_POSTGRESQL;
56 80
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
57 5
                $params['portability'] &= self::PORTABILITY_SQLITE;
58 75
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
59
                $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
60 75
            } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
61 5
                $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 110
            $this->portability = $params['portability'];
69
        }
70
71 110
        if (! isset($params['fetch_case']) || ($this->portability & self::PORTABILITY_FIX_CASE) === 0) {
72
            return;
73
        }
74
75 110
        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 45
            $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
80
        }
81 110
    }
82
83 110
    public function getPortability() : int
84
    {
85 110
        return $this->portability;
86
    }
87
88 110
    public function getFetchCase() : ?int
89
    {
90 110
        return $this->case;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 44
    public function executeQuery(
97
        string $query,
98
        array $params = [],
99
        array $types = [],
100
        ?QueryCacheProfile $qcp = null
101
    ) : ResultStatement {
102 44
        return new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
103
    }
104
105 44
    public function prepare(string $sql) : DriverStatement
106
    {
107 44
        return new Statement(parent::prepare($sql), $this);
108
    }
109
110 110
    public function query(string $sql) : ResultStatement
111
    {
112 110
        return new Statement(
113 110
            $this->getWrappedConnection()
114 110
                ->query($sql),
115
            $this
116
        );
117
    }
118
}
119