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

namespace Doctrine\DBAL\Portability;

5
use Doctrine\DBAL\Cache\QueryCacheProfile;
6
use Doctrine\DBAL\ColumnCase;
7 8
use Doctrine\DBAL\Driver\PDOConnection;
use PDO;
9 10 11
use const CASE_LOWER;
use const CASE_UPPER;
use function func_get_args;
12

13 14 15
/**
 * Portability wrapper for a Connection.
 */
16 17
class Connection extends \Doctrine\DBAL\Connection
{
18 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_DRIZZLE      = 13;
    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 54 55
                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() === 'drizzle') {
                    $params['portability'] &= self::PORTABILITY_DRIZZLE;
56
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
57
                    $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
58
                } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
59
                    $params['portability'] &= self::PORTABILITY_DB2;
60
                } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
61
                    $params['portability'] &= self::PORTABILITY_SQLSRV;
62
                } else {
63
                    $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
64 65 66
                }
                $this->portability = $params['portability'];
            }
67

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

78 79
        return $ret;
    }
80

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

Benjamin Morel's avatar
Benjamin Morel committed
89
    /**
90
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
91
     */
92 93 94 95
    public function getFetchCase()
    {
        return $this->case;
    }
96

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

        return $stmt;
106
    }
107

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

        return $stmt;
117
    }
118

Benjamin Morel's avatar
Benjamin Morel committed
119 120 121
    /**
     * {@inheritdoc}
     */
122 123 124 125
    public function query()
    {
        $this->connect();

126
        $stmt = $this->_conn->query(...func_get_args());
127
        $stmt = new Statement($stmt, $this);
128
        $stmt->setFetchMode($this->defaultFetchMode);
Benjamin Morel's avatar
Benjamin Morel committed
129

130
        return $stmt;
131 132
    }
}