Statement.php 5.64 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Portability;

5
use Doctrine\DBAL\Driver\Statement as DriverStatement;
6
use Doctrine\DBAL\Driver\StatementIterator;
7 8
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
9 10
use IteratorAggregate;
use PDO;
11 12 13
use function array_change_key_case;
use function is_string;
use function rtrim;
14 15

/**
Benjamin Morel's avatar
Benjamin Morel committed
16
 * Portability wrapper for a Statement.
17
 */
18
class Statement implements IteratorAggregate, DriverStatement
19
{
20
    /** @var int */
21
    private $portability;
22

23
    /** @var DriverStatement */
24
    private $stmt;
25

26
    /** @var int */
27 28
    private $case;

29
    /** @var int */
30
    private $defaultFetchMode = FetchMode::MIXED;
31

32
    /**
Benjamin Morel's avatar
Benjamin Morel committed
33
     * Wraps <tt>Statement</tt> and applies portability measures.
34
     *
35
     * @param DriverStatement $stmt
36 37 38
     */
    public function __construct($stmt, Connection $conn)
    {
39
        $this->stmt        = $stmt;
40
        $this->portability = $conn->getPortability();
41
        $this->case        = $conn->getFetchCase();
42 43
    }

Benjamin Morel's avatar
Benjamin Morel committed
44 45 46
    /**
     * {@inheritdoc}
     */
47
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
48
    {
49
        return $this->stmt->bindParam($column, $variable, $type, $length);
50
    }
51

Benjamin Morel's avatar
Benjamin Morel committed
52 53 54
    /**
     * {@inheritdoc}
     */
55
    public function bindValue($param, $value, $type = ParameterType::STRING)
56 57 58 59
    {
        return $this->stmt->bindValue($param, $value, $type);
    }

Benjamin Morel's avatar
Benjamin Morel committed
60 61 62
    /**
     * {@inheritdoc}
     */
63 64 65 66 67
    public function closeCursor()
    {
        return $this->stmt->closeCursor();
    }

Benjamin Morel's avatar
Benjamin Morel committed
68 69 70
    /**
     * {@inheritdoc}
     */
71 72 73 74 75
    public function columnCount()
    {
        return $this->stmt->columnCount();
    }

Benjamin Morel's avatar
Benjamin Morel committed
76 77 78
    /**
     * {@inheritdoc}
     */
79 80 81 82 83
    public function errorCode()
    {
        return $this->stmt->errorCode();
    }

Benjamin Morel's avatar
Benjamin Morel committed
84 85 86
    /**
     * {@inheritdoc}
     */
87 88 89 90 91
    public function errorInfo()
    {
        return $this->stmt->errorInfo();
    }

Benjamin Morel's avatar
Benjamin Morel committed
92 93 94
    /**
     * {@inheritdoc}
     */
95 96 97 98 99
    public function execute($params = null)
    {
        return $this->stmt->execute($params);
    }

Benjamin Morel's avatar
Benjamin Morel committed
100 101 102
    /**
     * {@inheritdoc}
     */
103
    public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
104
    {
105
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
106 107

        return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
108 109
    }

Benjamin Morel's avatar
Benjamin Morel committed
110 111 112
    /**
     * {@inheritdoc}
     */
113 114
    public function getIterator()
    {
115
        return new StatementIterator($this);
116 117
    }

Benjamin Morel's avatar
Benjamin Morel committed
118 119 120
    /**
     * {@inheritdoc}
     */
121
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
122
    {
123
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
124

125
        $row = $this->stmt->fetch($fetchMode);
126

127
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
128
        $fixCase    = $this->case !== null
129
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
130 131 132
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);

        $row = $this->fixRow($row, $iterateRow, $fixCase);
133

134 135 136
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
137 138 139
    /**
     * {@inheritdoc}
     */
140
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
141
    {
142
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
143

144 145
        if ($fetchArgument) {
            $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
146
        } else {
147
            $rows = $this->stmt->fetchAll($fetchMode);
148
        }
149

150
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
151
        $fixCase    = $this->case !== null
152 153 154
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);

155
        if (! $iterateRow && ! $fixCase) {
156 157 158
            return $rows;
        }

159
        if ($fetchMode === FetchMode::COLUMN) {
160
            foreach ($rows as $num => $row) {
161
                $rows[$num] = [$row];
162 163 164
            }
        }

165
        foreach ($rows as $num => $row) {
166 167
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
        }
168

169
        if ($fetchMode === FetchMode::COLUMN) {
170 171 172 173 174
            foreach ($rows as $num => $row) {
                $rows[$num] = $row[0];
            }
        }

175 176
        return $rows;
    }
177

Benjamin Morel's avatar
Benjamin Morel committed
178
    /**
179 180 181
     * @param mixed $row
     * @param int   $iterateRow
     * @param bool  $fixCase
Benjamin Morel's avatar
Benjamin Morel committed
182
     *
183
     * @return mixed
Benjamin Morel's avatar
Benjamin Morel committed
184
     */
185 186
    protected function fixRow($row, $iterateRow, $fixCase)
    {
187
        if (! $row) {
188 189
            return $row;
        }
190

191 192 193 194 195
        if ($fixCase) {
            $row = array_change_key_case($row, $this->case);
        }

        if ($iterateRow) {
196
            foreach ($row as $k => $v) {
197 198
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
                    $row[$k] = null;
Steve Müller's avatar
Steve Müller committed
199
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
200 201 202 203
                    $row[$k] = rtrim($v);
                }
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
204

205 206 207
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
208 209 210
    /**
     * {@inheritdoc}
     */
211 212 213
    public function fetchColumn($columnIndex = 0)
    {
        $value = $this->stmt->fetchColumn($columnIndex);
214

215 216 217
        if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
            if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
                $value = null;
Steve Müller's avatar
Steve Müller committed
218
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
219 220 221
                $value = rtrim($value);
            }
        }
222

223 224 225
        return $value;
    }

Benjamin Morel's avatar
Benjamin Morel committed
226 227 228
    /**
     * {@inheritdoc}
     */
229 230 231 232
    public function rowCount()
    {
        return $this->stmt->rowCount();
    }
233
}