Statement.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 \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. * Wraps <tt>Statement</tt> and applies portability measures
  45. *
  46. * @param Doctrine\DBAL\Driver\Statement $stmt
  47. * @param Doctrine\DBAL\Connection $conn
  48. */
  49. public function __construct($stmt, Connection $conn)
  50. {
  51. $this->stmt = $stmt;
  52. $this->portability = $conn->getPortability();
  53. $this->case = $conn->getFetchCase();
  54. }
  55. public function bindParam($column, &$variable, $type = null)
  56. {
  57. return $this->stmt->bindParam($column, $variable, $type);
  58. }
  59. public function bindValue($param, $value, $type = null)
  60. {
  61. return $this->stmt->bindValue($param, $value, $type);
  62. }
  63. public function closeCursor()
  64. {
  65. return $this->stmt->closeCursor();
  66. }
  67. public function columnCount()
  68. {
  69. return $this->stmt->columnCount();
  70. }
  71. public function errorCode()
  72. {
  73. return $this->stmt->errorCode();
  74. }
  75. public function errorInfo()
  76. {
  77. return $this->stmt->errorInfo();
  78. }
  79. public function execute($params = null)
  80. {
  81. return $this->stmt->execute($params);
  82. }
  83. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  84. {
  85. $row = $this->stmt->fetch($fetchStyle);
  86. $row = $this->fixRow($row,
  87. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
  88. ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
  89. );
  90. return $row;
  91. }
  92. public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
  93. {
  94. if ($columnIndex != 0) {
  95. $rows = $this->stmt->fetchAll($fetchStyle, $columnIndex);
  96. } else {
  97. $rows = $this->stmt->fetchAll($fetchStyle);
  98. }
  99. $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
  100. $fixCase = ($fetchStyle == PDO::FETCH_ASSOC || $fetchStyle == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
  101. if (!$iterateRow && !$fixCase) {
  102. return $rows;
  103. }
  104. foreach ($rows AS $num => $row) {
  105. $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
  106. }
  107. return $rows;
  108. }
  109. protected function fixRow($row, $iterateRow, $fixCase)
  110. {
  111. if (!$row) {
  112. return $row;
  113. }
  114. if ($fixCase) {
  115. $row = array_change_key_case($row, $this->case);
  116. }
  117. if ($iterateRow) {
  118. foreach ($row AS $k => $v) {
  119. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
  120. $row[$k] = null;
  121. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
  122. $row[$k] = rtrim($v);
  123. }
  124. }
  125. }
  126. return $row;
  127. }
  128. public function fetchColumn($columnIndex = 0)
  129. {
  130. $value = $this->stmt->fetchColumn($columnIndex);
  131. if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
  132. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
  133. $value = null;
  134. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
  135. $value = rtrim($value);
  136. }
  137. }
  138. return $value;
  139. }
  140. public function rowCount()
  141. {
  142. return $this->stmt->rowCount();
  143. }
  144. }