Connection.php 3.93 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Portability;

7
use Doctrine\DBAL\Cache\QueryCacheProfile;
8
use Doctrine\DBAL\ColumnCase;
9
use Doctrine\DBAL\Driver\PDOConnection;
10 11
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
12
use PDO;
13 14
use const CASE_LOWER;
use const CASE_UPPER;
15

16 17 18
/**
 * Portability wrapper for a Connection.
 */
19 20
class Connection extends \Doctrine\DBAL\Connection
{
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    public const PORTABILITY_ALL           = 255;
    public const PORTABILITY_NONE          = 0;
    public const PORTABILITY_RTRIM         = 1;
    public const PORTABILITY_EMPTY_TO_NULL = 4;
    public const PORTABILITY_FIX_CASE      = 8;

    public const PORTABILITY_DB2          = 13;
    public const PORTABILITY_ORACLE       = 9;
    public const PORTABILITY_POSTGRESQL   = 13;
    public const PORTABILITY_SQLITE       = 13;
    public const PORTABILITY_OTHERVENDORS = 12;
    public const PORTABILITY_SQLANYWHERE  = 13;
    public const PORTABILITY_SQLSRV       = 13;

    /** @var int */
36
    private $portability = self::PORTABILITY_NONE;
37

38
    /** @var int */
39
    private $case;
40

Benjamin Morel's avatar
Benjamin Morel committed
41 42 43
    /**
     * {@inheritdoc}
     */
44
    public function connect() : void
45
    {
46 47 48 49 50 51 52
        if ($this->isConnected()) {
            return;
        }

        parent::connect();

        $params = $this->getParams();
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        if (isset($params['portability'])) {
            if ($this->getDatabasePlatform()->getName() === 'oracle') {
                $params['portability'] &= self::PORTABILITY_ORACLE;
            } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
                $params['portability'] &= self::PORTABILITY_POSTGRESQL;
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
                $params['portability'] &= self::PORTABILITY_SQLITE;
            } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
                $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
            } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
                $params['portability'] &= self::PORTABILITY_DB2;
            } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
                $params['portability'] &= self::PORTABILITY_SQLSRV;
            } else {
                $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
69
            }
70
            $this->portability = $params['portability'];
71
        }
Benjamin Morel's avatar
Benjamin Morel committed
72

73 74 75 76 77 78 79 80 81 82
        if (! isset($params['fetch_case']) || ! ($this->portability & self::PORTABILITY_FIX_CASE)) {
            return;
        }

        if ($this->_conn instanceof PDOConnection) {
            // make use of c-level support for case handling
            $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
        } else {
            $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
        }
83
    }
84

85
    public function getPortability() : int
86 87 88
    {
        return $this->portability;
    }
89

90
    public function getFetchCase() : ?int
91 92 93
    {
        return $this->case;
    }
94

Benjamin Morel's avatar
Benjamin Morel committed
95 96 97
    /**
     * {@inheritdoc}
     */
98
    public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
99
    {
100
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
101
        $stmt->setFetchMode($this->defaultFetchMode);
102 103

        return $stmt;
104
    }
105

106
    /**
Benjamin Morel's avatar
Benjamin Morel committed
107
     * {@inheritdoc}
108
     */
109
    public function prepare(string $sql) : DriverStatement
110
    {
111
        $stmt = new Statement(parent::prepare($sql), $this);
112
        $stmt->setFetchMode($this->defaultFetchMode);
113 114

        return $stmt;
115
    }
116

Benjamin Morel's avatar
Benjamin Morel committed
117 118 119
    /**
     * {@inheritdoc}
     */
120
    public function query(string $sql) : ResultStatement
121
    {
Sergei Morozov's avatar
Sergei Morozov committed
122
        $connection = $this->getWrappedConnection();
123

124
        $stmt = $connection->query($sql);
125
        $stmt = new Statement($stmt, $this);
126
        $stmt->setFetchMode($this->defaultFetchMode);
Benjamin Morel's avatar
Benjamin Morel committed
127

128
        return $stmt;
129 130
    }
}