composer package updates
[openemr.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Portability / Statement.php
blob41470bb52a40fe86acfd3303eed48e3174203a24
1 <?php
2 /*
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;
22 use PDO;
24 /**
25 * Portability wrapper for a Statement.
27 * @link www.doctrine-project.org
28 * @since 2.0
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
33 /**
34 * @var integer
36 private $portability;
38 /**
39 * @var \Doctrine\DBAL\Driver\Statement
41 private $stmt;
43 /**
44 * @var integer
46 private $case;
48 /**
49 * @var integer
51 private $defaultFetchMode = PDO::FETCH_BOTH;
53 /**
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)
61 $this->stmt = $stmt;
62 $this->portability = $conn->getPortability();
63 $this->case = $conn->getFetchCase();
66 /**
67 * {@inheritdoc}
69 public function bindParam($column, &$variable, $type = null, $length = null)
71 return $this->stmt->bindParam($column, $variable, $type, $length);
73 /**
74 * {@inheritdoc}
77 public function bindValue($param, $value, $type = null)
79 return $this->stmt->bindValue($param, $value, $type);
82 /**
83 * {@inheritdoc}
85 public function closeCursor()
87 return $this->stmt->closeCursor();
90 /**
91 * {@inheritdoc}
93 public function columnCount()
95 return $this->stmt->columnCount();
98 /**
99 * {@inheritdoc}
101 public function errorCode()
103 return $this->stmt->errorCode();
107 * {@inheritdoc}
109 public function errorInfo()
111 return $this->stmt->errorInfo();
115 * {@inheritdoc}
117 public function execute($params = null)
119 return $this->stmt->execute($params);
123 * {@inheritdoc}
125 public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
127 $this->defaultFetchMode = $fetchMode;
129 return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
133 * {@inheritdoc}
135 public function getIterator()
137 $data = $this->fetchAll();
139 return new \ArrayIterator($data);
143 * {@inheritdoc}
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)
156 return $row;
160 * {@inheritdoc}
162 public function fetchAll($fetchMode = null, $columnIndex = 0)
164 $fetchMode = $fetchMode ?: $this->defaultFetchMode;
166 if ($columnIndex != 0) {
167 $rows = $this->stmt->fetchAll($fetchMode, $columnIndex);
168 } else {
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) {
175 return $rows;
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];
194 return $rows;
198 * @param mixed $row
199 * @param integer $iterateRow
200 * @param boolean $fixCase
202 * @return array
204 protected function fixRow($row, $iterateRow, $fixCase)
206 if ( ! $row) {
207 return $row;
210 if ($fixCase) {
211 $row = array_change_key_case($row, $this->case);
214 if ($iterateRow) {
215 foreach ($row as $k => $v) {
216 if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
217 $row[$k] = null;
218 } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
219 $row[$k] = rtrim($v);
224 return $row;
228 * {@inheritdoc}
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 === '') {
236 $value = null;
237 } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
238 $value = rtrim($value);
242 return $value;
246 * {@inheritdoc}
248 public function rowCount()
250 return $this->stmt->rowCount();