/dbal/lib/Doctrine/DBAL/Portability/Statement.php

https://github.com/Amleth/Patchwork-Doctrine · PHP · 194 lines · 116 code · 35 blank · 43 comment · 29 complexity · 3bf3b93a6a15fe2fb69fecdc5af6b7bb MD5 · raw file

  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.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Portability;
  20. use PDO;
  21. /**
  22. * Portability Wrapper for a Statement
  23. *
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @link www.doctrine-project.com
  26. * @since 2.0
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
  30. {
  31. /**
  32. * @var int
  33. */
  34. private $portability;
  35. /**
  36. * @var Doctrine\DBAL\Driver\Statement
  37. */
  38. private $stmt;
  39. /**
  40. * @var int
  41. */
  42. private $case;
  43. /**
  44. * @var int
  45. */
  46. private $defaultFetchStyle = PDO::FETCH_BOTH;
  47. /**
  48. * Wraps <tt>Statement</tt> and applies portability measures
  49. *
  50. * @param Doctrine\DBAL\Driver\Statement $stmt
  51. * @param Doctrine\DBAL\Connection $conn
  52. */
  53. public function __construct($stmt, Connection $conn)
  54. {
  55. $this->stmt = $stmt;
  56. $this->portability = $conn->getPortability();
  57. $this->case = $conn->getFetchCase();
  58. }
  59. public function bindParam($column, &$variable, $type = null)
  60. {
  61. return $this->stmt->bindParam($column, $variable, $type);
  62. }
  63. public function bindValue($param, $value, $type = null)
  64. {
  65. return $this->stmt->bindValue($param, $value, $type);
  66. }
  67. public function closeCursor()
  68. {
  69. return $this->stmt->closeCursor();
  70. }
  71. public function columnCount()
  72. {
  73. return $this->stmt->columnCount();
  74. }
  75. public function errorCode()
  76. {
  77. return $this->stmt->errorCode();
  78. }
  79. public function errorInfo()
  80. {
  81. return $this->stmt->errorInfo();
  82. }
  83. public function execute($params = null)
  84. {
  85. return $this->stmt->execute($params);
  86. }
  87. public function setFetchMode($fetchStyle)
  88. {
  89. $this->defaultFetchStyle = $fetchStyle;
  90. }
  91. public function getIterator()
  92. {
  93. $data = $this->fetchAll();
  94. return new \ArrayIterator($data);
  95. }
  96. public function fetch($fetchStyle = null)
  97. {
  98. $fetchStyle or $fetchStyle = $this->defaultFetchStyle;
  99. $row = $this->stmt->fetch($fetchStyle);
  100. $row = $this->fixRow($row,
  101. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
  102. !is_null($this->case) && ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
  103. );
  104. return $row;
  105. }
  106. public function fetchAll($fetchStyle = null, $columnIndex = 0)
  107. {
  108. $fetchStyle or $fetchStyle = $this->defaultFetchStyle;
  109. if ($columnIndex != 0) {
  110. $rows = $this->stmt->fetchAll($fetchStyle, $columnIndex);
  111. } else {
  112. $rows = $this->stmt->fetchAll($fetchStyle);
  113. }
  114. $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
  115. $fixCase = !is_null($this->case) && ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
  116. if (!$iterateRow && !$fixCase) {
  117. return $rows;
  118. }
  119. foreach ($rows AS $num => $row) {
  120. $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
  121. }
  122. return $rows;
  123. }
  124. protected function fixRow($row, $iterateRow, $fixCase)
  125. {
  126. if (!$row) {
  127. return $row;
  128. }
  129. if ($fixCase) {
  130. $row = array_change_key_case($row, $this->case);
  131. }
  132. if ($iterateRow) {
  133. foreach ($row AS $k => $v) {
  134. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
  135. $row[$k] = null;
  136. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
  137. $row[$k] = rtrim($v);
  138. }
  139. }
  140. }
  141. return $row;
  142. }
  143. public function fetchColumn($columnIndex = 0)
  144. {
  145. $value = $this->stmt->fetchColumn($columnIndex);
  146. if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
  147. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
  148. $value = null;
  149. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
  150. $value = rtrim($value);
  151. }
  152. }
  153. return $value;
  154. }
  155. public function rowCount()
  156. {
  157. return $this->stmt->rowCount();
  158. }
  159. }