Statement.php 6.11 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)
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, $columnIndex = 0)
163
    {
164
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
165

166
        if ($columnIndex != 0) {
167
            $rows = $this->stmt->fetchAll($fetchMode, $columnIndex);
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
        foreach ($rows as $num => $row) {
179 180
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
        }
181

182 183
        return $rows;
    }
184

Benjamin Morel's avatar
Benjamin Morel committed
185 186 187 188 189 190 191
    /**
     * @param mixed   $row
     * @param integer $iterateRow
     * @param boolean $fixCase
     *
     * @return array
     */
192 193
    protected function fixRow($row, $iterateRow, $fixCase)
    {
194
        if ( ! $row) {
195 196
            return $row;
        }
197

198 199 200 201 202
        if ($fixCase) {
            $row = array_change_key_case($row, $this->case);
        }

        if ($iterateRow) {
203
            foreach ($row as $k => $v) {
204 205
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
                    $row[$k] = null;
Steve Müller's avatar
Steve Müller committed
206
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
207 208 209 210
                    $row[$k] = rtrim($v);
                }
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
211

212 213 214
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
215 216 217
    /**
     * {@inheritdoc}
     */
218 219 220
    public function fetchColumn($columnIndex = 0)
    {
        $value = $this->stmt->fetchColumn($columnIndex);
221

222 223 224
        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
225
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
226 227 228
                $value = rtrim($value);
            }
        }
229

230 231 232
        return $value;
    }

Benjamin Morel's avatar
Benjamin Morel committed
233 234 235
    /**
     * {@inheritdoc}
     */
236 237 238 239
    public function rowCount()
    {
        return $this->stmt->rowCount();
    }
240
}