Statement.php 6.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Portability;

22 23
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
24 25 26 27
use function array_change_key_case;
use function is_null;
use function is_string;
use function rtrim;
28 29

/**
Benjamin Morel's avatar
Benjamin Morel committed
30
 * Portability wrapper for a Statement.
31
 *
Benjamin Morel's avatar
Benjamin Morel committed
32 33 34
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Benjamin Eberlei <kontakt@beberlei.de>
35
 */
36
class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
37 38
{
    /**
39
     * @var int
40 41
     */
    private $portability;
42

43
    /**
44
     * @var \Doctrine\DBAL\Driver\Statement
45 46
     */
    private $stmt;
47

48
    /**
49
     * @var int
50 51 52
     */
    private $case;

53
    /**
54
     * @var int
55
     */
56
    private $defaultFetchMode = FetchMode::MIXED;
57

58
    /**
Benjamin Morel's avatar
Benjamin Morel committed
59
     * Wraps <tt>Statement</tt> and applies portability measures.
60
     *
Benjamin Morel's avatar
Benjamin Morel committed
61 62
     * @param \Doctrine\DBAL\Driver\Statement       $stmt
     * @param \Doctrine\DBAL\Portability\Connection $conn
63 64 65 66 67 68 69 70
     */
    public function __construct($stmt, Connection $conn)
    {
        $this->stmt = $stmt;
        $this->portability = $conn->getPortability();
        $this->case = $conn->getFetchCase();
    }

Benjamin Morel's avatar
Benjamin Morel committed
71 72 73
    /**
     * {@inheritdoc}
     */
74
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
75
    {
76
        return $this->stmt->bindParam($column, $variable, $type, $length);
77
    }
78

Benjamin Morel's avatar
Benjamin Morel committed
79 80 81
    /**
     * {@inheritdoc}
     */
82
    public function bindValue($param, $value, $type = ParameterType::STRING)
83 84 85 86
    {
        return $this->stmt->bindValue($param, $value, $type);
    }

Benjamin Morel's avatar
Benjamin Morel committed
87 88 89
    /**
     * {@inheritdoc}
     */
90 91 92 93 94
    public function closeCursor()
    {
        return $this->stmt->closeCursor();
    }

Benjamin Morel's avatar
Benjamin Morel committed
95 96 97
    /**
     * {@inheritdoc}
     */
98 99 100 101 102
    public function columnCount()
    {
        return $this->stmt->columnCount();
    }

Benjamin Morel's avatar
Benjamin Morel committed
103 104 105
    /**
     * {@inheritdoc}
     */
106 107 108 109 110
    public function errorCode()
    {
        return $this->stmt->errorCode();
    }

Benjamin Morel's avatar
Benjamin Morel committed
111 112 113
    /**
     * {@inheritdoc}
     */
114 115 116 117 118
    public function errorInfo()
    {
        return $this->stmt->errorInfo();
    }

Benjamin Morel's avatar
Benjamin Morel committed
119 120 121
    /**
     * {@inheritdoc}
     */
122 123 124 125 126
    public function execute($params = null)
    {
        return $this->stmt->execute($params);
    }

Benjamin Morel's avatar
Benjamin Morel committed
127 128 129
    /**
     * {@inheritdoc}
     */
130
    public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
131
    {
132
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
133 134

        return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
135 136
    }

Benjamin Morel's avatar
Benjamin Morel committed
137 138 139
    /**
     * {@inheritdoc}
     */
140 141
    public function getIterator()
    {
142
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
143

144 145 146
        return new \ArrayIterator($data);
    }

Benjamin Morel's avatar
Benjamin Morel committed
147 148 149
    /**
     * {@inheritdoc}
     */
150
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
151
    {
152
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
153

154
        $row = $this->stmt->fetch($fetchMode);
155

156
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
157 158
        $fixCase    = ! is_null($this->case)
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
159 160 161
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);

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

163 164 165
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
166 167 168
    /**
     * {@inheritdoc}
     */
169
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
170
    {
171
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
172

173 174
        if ($fetchArgument) {
            $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
175
        } else {
176
            $rows = $this->stmt->fetchAll($fetchMode);
177
        }
178

179
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
180 181 182 183
        $fixCase    = ! is_null($this->case)
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);

184
        if ( ! $iterateRow && !$fixCase) {
185 186 187
            return $rows;
        }

188
        if ($fetchMode === FetchMode::COLUMN) {
189
            foreach ($rows as $num => $row) {
190
                $rows[$num] = [$row];
191 192 193
            }
        }

194
        foreach ($rows as $num => $row) {
195 196
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
        }
197

198
        if ($fetchMode === FetchMode::COLUMN) {
199 200 201 202 203
            foreach ($rows as $num => $row) {
                $rows[$num] = $row[0];
            }
        }

204 205
        return $rows;
    }
206

Benjamin Morel's avatar
Benjamin Morel committed
207
    /**
208 209 210
     * @param mixed $row
     * @param int   $iterateRow
     * @param bool  $fixCase
Benjamin Morel's avatar
Benjamin Morel committed
211 212 213
     *
     * @return array
     */
214 215
    protected function fixRow($row, $iterateRow, $fixCase)
    {
216
        if ( ! $row) {
217 218
            return $row;
        }
219

220 221 222 223 224
        if ($fixCase) {
            $row = array_change_key_case($row, $this->case);
        }

        if ($iterateRow) {
225
            foreach ($row as $k => $v) {
226 227
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
                    $row[$k] = null;
Steve Müller's avatar
Steve Müller committed
228
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
229 230 231 232
                    $row[$k] = rtrim($v);
                }
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
233

234 235 236
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
237 238 239
    /**
     * {@inheritdoc}
     */
240 241 242
    public function fetchColumn($columnIndex = 0)
    {
        $value = $this->stmt->fetchColumn($columnIndex);
243

244 245 246
        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
247
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
248 249 250
                $value = rtrim($value);
            }
        }
251

252 253 254
        return $value;
    }

Benjamin Morel's avatar
Benjamin Morel committed
255 256 257
    /**
     * {@inheritdoc}
     */
258 259 260 261
    public function rowCount()
    {
        return $this->stmt->rowCount();
    }
262
}