3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Portability
;
25 * Portability wrapper for a Statement.
27 * @link www.doctrine-project.org
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 class Statement
implements \IteratorAggregate
, \Doctrine\DBAL\Driver\Statement
39 * @var \Doctrine\DBAL\Driver\Statement
51 private $defaultFetchMode = PDO
::FETCH_BOTH
;
54 * Wraps <tt>Statement</tt> and applies portability measures.
56 * @param \Doctrine\DBAL\Driver\Statement $stmt
57 * @param \Doctrine\DBAL\Portability\Connection $conn
59 public function __construct($stmt, Connection
$conn)
62 $this->portability
= $conn->getPortability();
63 $this->case = $conn->getFetchCase();
69 public function bindParam($column, &$variable, $type = null, $length = null)
71 return $this->stmt
->bindParam($column, $variable, $type, $length);
77 public function bindValue($param, $value, $type = null)
79 return $this->stmt
->bindValue($param, $value, $type);
85 public function closeCursor()
87 return $this->stmt
->closeCursor();
93 public function columnCount()
95 return $this->stmt
->columnCount();
101 public function errorCode()
103 return $this->stmt
->errorCode();
109 public function errorInfo()
111 return $this->stmt
->errorInfo();
117 public function execute($params = null)
119 return $this->stmt
->execute($params);
125 public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
127 $this->defaultFetchMode
= $fetchMode;
129 return $this->stmt
->setFetchMode($fetchMode, $arg1, $arg2);
135 public function getIterator()
137 $data = $this->fetchAll();
139 return new \
ArrayIterator($data);
145 public function fetch($fetchMode = null)
147 $fetchMode = $fetchMode ?
: $this->defaultFetchMode
;
149 $row = $this->stmt
->fetch($fetchMode);
151 $row = $this->fixRow($row,
152 $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
)
162 public function fetchAll($fetchMode = null, $columnIndex = 0)
164 $fetchMode = $fetchMode ?
: $this->defaultFetchMode
;
166 if ($columnIndex != 0) {
167 $rows = $this->stmt
->fetchAll($fetchMode, $columnIndex);
169 $rows = $this->stmt
->fetchAll($fetchMode);
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) {
178 if ($fetchMode === PDO
::FETCH_COLUMN
) {
179 foreach ($rows as $num => $row) {
180 $rows[$num] = array($row);
184 foreach ($rows as $num => $row) {
185 $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
188 if ($fetchMode === PDO
::FETCH_COLUMN
) {
189 foreach ($rows as $num => $row) {
190 $rows[$num] = $row[0];
199 * @param integer $iterateRow
200 * @param boolean $fixCase
204 protected function fixRow($row, $iterateRow, $fixCase)
211 $row = array_change_key_case($row, $this->case);
215 foreach ($row as $k => $v) {
216 if (($this->portability
& Connection
::PORTABILITY_EMPTY_TO_NULL
) && $v === '') {
218 } elseif (($this->portability
& Connection
::PORTABILITY_RTRIM
) && is_string($v)) {
219 $row[$k] = rtrim($v);
230 public function fetchColumn($columnIndex = 0)
232 $value = $this->stmt
->fetchColumn($columnIndex);
234 if ($this->portability
& (Connection
::PORTABILITY_EMPTY_TO_NULL|Connection
::PORTABILITY_RTRIM
)) {
235 if (($this->portability
& Connection
::PORTABILITY_EMPTY_TO_NULL
) && $value === '') {
237 } elseif (($this->portability
& Connection
::PORTABILITY_RTRIM
) && is_string($value)) {
238 $value = rtrim($value);
248 public function rowCount()
250 return $this->stmt
->rowCount();