Connection.php 3.2 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

Grégoire Paris's avatar
Grégoire Paris committed
10
use function func_get_args;
11

12 13
use const CASE_LOWER;
use const CASE_UPPER;
14

15 16 17
/**
 * Portability wrapper for a Connection.
 */
18 19
class Connection extends \Doctrine\DBAL\Connection
{
20 21 22 23 24 25
    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;

26 27 28 29
    /**#@+
     *
     * @deprecated Will be removed as internal implementation details.
     */
30 31 32 33 34 35 36 37
    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;
38
    /**#@-*/
39 40

    /** @var int */
41
    private $portability = self::PORTABILITY_NONE;
42

43
    /** @var int */
44
    private $case;
45

Benjamin Morel's avatar
Benjamin Morel committed
46 47 48
    /**
     * {@inheritdoc}
     */
49 50 51
    public function connect()
    {
        $ret = parent::connect();
52
        if ($ret) {
53 54
            $params = $this->getParams();
            if (isset($params['portability'])) {
55 56 57 58
                $this->portability = $params['portability'] = (new OptimizeFlags())(
                    $this->getDatabasePlatform(),
                    $params['portability']
                );
59
            }
60

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

71 72
        return $ret;
    }
73

Benjamin Morel's avatar
Benjamin Morel committed
74
    /**
75
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
76
     */
77 78 79 80
    public function getPortability()
    {
        return $this->portability;
    }
81

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

Benjamin Morel's avatar
Benjamin Morel committed
90 91 92
    /**
     * {@inheritdoc}
     */
93
    public function executeQuery($query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null)
94
    {
95
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
96
        $stmt->setFetchMode($this->defaultFetchMode);
97 98

        return $stmt;
99
    }
100

101
    /**
Benjamin Morel's avatar
Benjamin Morel committed
102
     * {@inheritdoc}
103 104
     *
     * @return Statement
105 106 107
     */
    public function prepare($statement)
    {
108
        $stmt = new Statement(parent::prepare($statement), $this);
109
        $stmt->setFetchMode($this->defaultFetchMode);
110 111

        return $stmt;
112
    }
113

Benjamin Morel's avatar
Benjamin Morel committed
114 115 116
    /**
     * {@inheritdoc}
     */
117 118
    public function query()
    {
Sergei Morozov's avatar
Sergei Morozov committed
119
        $connection = $this->getWrappedConnection();
120

Sergei Morozov's avatar
Sergei Morozov committed
121
        $stmt = $connection->query(...func_get_args());
122
        $stmt = new Statement($stmt, $this);
123
        $stmt->setFetchMode($this->defaultFetchMode);
Benjamin Morel's avatar
Benjamin Morel committed
124

125
        return $stmt;
126 127
    }
}