Passed
Pull Request — 3.0.x (#4000)
by Grégoire
08:27
created

Connection::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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