4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the MIT license. For more information, see
18 * <http://www.doctrine-project.org>.
21 namespace Doctrine\DBAL\Portability;
26 * Portability Wrapper for a Statement
28 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29 * @link www.doctrine-project.com
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
42 * @var \Doctrine\DBAL\Driver\Statement
54 private $defaultFetchMode = PDO::FETCH_BOTH;
57 * Wraps <tt>Statement</tt> and applies portability measures
59 * @param \Doctrine\DBAL\Driver\Statement $stmt
60 * @param \Doctrine\DBAL\Connection $conn
62 public function __construct($stmt, Connection $conn)
65 $this->portability = $conn->getPortability();
66 $this->case = $conn->getFetchCase();
69 public function bindParam($column, &$variable, $type = null,$length = null)
71 return $this->stmt->bindParam($column, $variable, $type);
74 public function bindValue($param, $value, $type = null)
76 return $this->stmt->bindValue($param, $value, $type);
79 public function closeCursor()
81 return $this->stmt->closeCursor();
84 public function columnCount()
86 return $this->stmt->columnCount();
89 public function errorCode()
91 return $this->stmt->errorCode();
94 public function errorInfo()
96 return $this->stmt->errorInfo();
99 public function execute($params = null)
101 return $this->stmt->execute($params);
104 public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
106 $this->defaultFetchMode = $fetchMode;
107 $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
110 public function getIterator()
112 $data = $this->fetchAll();
113 return new \ArrayIterator($data);
116 public function fetch($fetchMode = null)
118 $fetchMode = $fetchMode ?: $this->defaultFetchMode;
120 $row = $this->stmt->fetch($fetchMode);
122 $row = $this->fixRow($row,
123 $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
124 !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
130 public function fetchAll($fetchMode = null, $columnIndex = 0)
132 $fetchMode = $fetchMode ?: $this->defaultFetchMode;
134 if ($columnIndex != 0) {
135 $rows = $this->stmt->fetchAll($fetchMode, $columnIndex);
137 $rows = $this->stmt->fetchAll($fetchMode);
140 $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
141 $fixCase = !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
142 if ( ! $iterateRow && !$fixCase) {
146 foreach ($rows as $num => $row) {
147 $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
153 protected function fixRow($row, $iterateRow, $fixCase)
160 $row = array_change_key_case($row, $this->case);
164 foreach ($row as $k => $v) {
165 if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
167 } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
168 $row[$k] = rtrim($v);
175 public function fetchColumn($columnIndex = 0)
177 $value = $this->stmt->fetchColumn($columnIndex);
179 if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
180 if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
182 } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
183 $value = rtrim($value);
190 public function rowCount()
192 return $this->stmt->rowCount();