Statement.php 6.5 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 22 23 24
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Portability;

use PDO;

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

38
    /**
39
     * @var \Doctrine\DBAL\Driver\Statement
40 41
     */
    private $stmt;
42

43
    /**
Benjamin Morel's avatar
Benjamin Morel committed
44
     * @var integer
45 46 47
     */
    private $case;

48
    /**
Benjamin Morel's avatar
Benjamin Morel committed
49
     * @var integer
50
     */
51
    private $defaultFetchMode = PDO::FETCH_BOTH;
52

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

Benjamin Morel's avatar
Benjamin Morel committed
66 67 68
    /**
     * {@inheritdoc}
     */
69
    public function bindParam($column, &$variable, $type = null, $length = null)
70
    {
71
        return $this->stmt->bindParam($column, $variable, $type, $length);
72
    }
Benjamin Morel's avatar
Benjamin Morel committed
73 74 75
    /**
     * {@inheritdoc}
     */
76 77 78 79 80 81

    public function bindValue($param, $value, $type = null)
    {
        return $this->stmt->bindValue($param, $value, $type);
    }

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

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
114 115 116
    /**
     * {@inheritdoc}
     */
117 118 119 120 121
    public function execute($params = null)
    {
        return $this->stmt->execute($params);
    }

Benjamin Morel's avatar
Benjamin Morel committed
122 123 124
    /**
     * {@inheritdoc}
     */
125
    public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
126
    {
127
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
128 129

        return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
130 131
    }

Benjamin Morel's avatar
Benjamin Morel committed
132 133 134
    /**
     * {@inheritdoc}
     */
135 136
    public function getIterator()
    {
137
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
138

139 140 141
        return new \ArrayIterator($data);
    }

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

149
        $row = $this->stmt->fetch($fetchMode);
150

151 152
        $row = $this->fixRow($row,
            $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
153
            !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
154
        );
155

156 157 158
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
159 160 161
    /**
     * {@inheritdoc}
     */
162
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
163
    {
164
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
165

166 167
        if ($fetchArgument) {
            $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
168
        } else {
169
            $rows = $this->stmt->fetchAll($fetchMode);
170
        }
171

172
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
173
        $fixCase = !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
174
        if ( ! $iterateRow && !$fixCase) {
175 176 177
            return $rows;
        }

178 179 180 181 182 183
        if ($fetchMode === PDO::FETCH_COLUMN) {
            foreach ($rows as $num => $row) {
                $rows[$num] = array($row);
            }
        }

184
        foreach ($rows as $num => $row) {
185 186
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
        }
187

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

194 195
        return $rows;
    }
196

Benjamin Morel's avatar
Benjamin Morel committed
197 198 199 200 201 202 203
    /**
     * @param mixed   $row
     * @param integer $iterateRow
     * @param boolean $fixCase
     *
     * @return array
     */
204 205
    protected function fixRow($row, $iterateRow, $fixCase)
    {
206
        if ( ! $row) {
207 208
            return $row;
        }
209

210 211 212 213 214
        if ($fixCase) {
            $row = array_change_key_case($row, $this->case);
        }

        if ($iterateRow) {
215
            foreach ($row as $k => $v) {
216 217
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
                    $row[$k] = null;
Steve Müller's avatar
Steve Müller committed
218
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
219 220 221 222
                    $row[$k] = rtrim($v);
                }
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
223

224 225 226
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
227 228 229
    /**
     * {@inheritdoc}
     */
230 231 232
    public function fetchColumn($columnIndex = 0)
    {
        $value = $this->stmt->fetchColumn($columnIndex);
233

234 235 236
        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
237
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
238 239 240
                $value = rtrim($value);
            }
        }
241

242 243 244
        return $value;
    }

Benjamin Morel's avatar
Benjamin Morel committed
245 246 247
    /**
     * {@inheritdoc}
     */
248 249 250 251
    public function rowCount()
    {
        return $this->stmt->rowCount();
    }
252
}