Statement.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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($this->defaultFetchStyle);
  94. return new \ArrayIterator($data);
  95. }
  96. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  97. {
  98. $row = $this->stmt->fetch($fetchStyle);
  99. $row = $this->fixRow($row,
  100. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
  101. !is_null($this->case) && ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
  102. );
  103. return $row;
  104. }
  105. public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
  106. {
  107. if ($columnIndex != 0) {
  108. $rows = $this->stmt->fetchAll($fetchStyle, $columnIndex);
  109. } else {
  110. $rows = $this->stmt->fetchAll($fetchStyle);
  111. }
  112. $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
  113. $fixCase = !is_null($this->case) && ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
  114. if (!$iterateRow && !$fixCase) {
  115. return $rows;
  116. }
  117. foreach ($rows AS $num => $row) {
  118. $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
  119. }
  120. return $rows;
  121. }
  122. protected function fixRow($row, $iterateRow, $fixCase)
  123. {
  124. if (!$row) {
  125. return $row;
  126. }
  127. if ($fixCase) {
  128. $row = array_change_key_case($row, $this->case);
  129. }
  130. if ($iterateRow) {
  131. foreach ($row AS $k => $v) {
  132. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
  133. $row[$k] = null;
  134. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
  135. $row[$k] = rtrim($v);
  136. }
  137. }
  138. }
  139. return $row;
  140. }
  141. public function fetchColumn($columnIndex = 0)
  142. {
  143. $value = $this->stmt->fetchColumn($columnIndex);
  144. if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
  145. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
  146. $value = null;
  147. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
  148. $value = rtrim($value);
  149. }
  150. }
  151. return $value;
  152. }
  153. public function rowCount()
  154. {
  155. return $this->stmt->rowCount();
  156. }
  157. }