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