Connection.php 4.02 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Portability;

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

14 15 16
/**
 * Portability wrapper for a Connection.
 */
17 18
class Connection extends \Doctrine\DBAL\Connection
{
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    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 */
34
    private $portability = self::PORTABILITY_NONE;
35

36
    /** @var int */
37
    private $case;
38

Benjamin Morel's avatar
Benjamin Morel committed
39 40 41
    /**
     * {@inheritdoc}
     */
42 43 44
    public function connect()
    {
        $ret = parent::connect();
45
        if ($ret) {
46 47
            $params = $this->getParams();
            if (isset($params['portability'])) {
48 49 50 51 52 53
                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;
54
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
55
                    $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
56
                } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
57
                    $params['portability'] &= self::PORTABILITY_DB2;
58
                } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
59
                    $params['portability'] &= self::PORTABILITY_SQLSRV;
60
                } else {
61
                    $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
62 63 64
                }
                $this->portability = $params['portability'];
            }
65

66
            if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) {
67
                if ($this->_conn instanceof PDOConnection) {
68
                    // make use of c-level support for case handling
69
                    $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
70
                } else {
71
                    $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
72
                }
73
            }
74
        }
Benjamin Morel's avatar
Benjamin Morel committed
75

76 77
        return $ret;
    }
78

Benjamin Morel's avatar
Benjamin Morel committed
79
    /**
80
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
81
     */
82 83 84 85
    public function getPortability()
    {
        return $this->portability;
    }
86

Benjamin Morel's avatar
Benjamin Morel committed
87
    /**
88
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
89
     */
90 91 92 93
    public function getFetchCase()
    {
        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 122 123
    {
        $this->connect();

124
        $stmt = $this->_conn->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
    }
}