Statement.php 6.78 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
use Doctrine\DBAL\Driver\StatementIterator;
23 24
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
25 26 27 28
use function array_change_key_case;
use function is_null;
use function is_string;
use function rtrim;
29 30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
138 139 140
    /**
     * {@inheritdoc}
     */
141 142
    public function getIterator()
    {
143
        return new StatementIterator($this);
144 145
    }

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

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

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

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

162 163 164
        return $row;
    }

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

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

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

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

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

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

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

203 204
        return $rows;
    }
205

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

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

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

233 234 235
        return $row;
    }

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

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

251 252 253
        return $value;
    }

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