Statement.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 MIT license. 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. * @link www.doctrine-project.org
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
  29. {
  30. /**
  31. * @var integer
  32. */
  33. private $portability;
  34. /**
  35. * @var \Doctrine\DBAL\Driver\Statement
  36. */
  37. private $stmt;
  38. /**
  39. * @var integer
  40. */
  41. private $case;
  42. /**
  43. * @var integer
  44. */
  45. private $defaultFetchMode = PDO::FETCH_BOTH;
  46. /**
  47. * Wraps <tt>Statement</tt> and applies portability measures.
  48. *
  49. * @param \Doctrine\DBAL\Driver\Statement $stmt
  50. * @param \Doctrine\DBAL\Portability\Connection $conn
  51. */
  52. public function __construct($stmt, Connection $conn)
  53. {
  54. $this->stmt = $stmt;
  55. $this->portability = $conn->getPortability();
  56. $this->case = $conn->getFetchCase();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function bindParam($column, &$variable, $type = null,$length = null)
  62. {
  63. return $this->stmt->bindParam($column, $variable, $type);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function bindValue($param, $value, $type = null)
  69. {
  70. return $this->stmt->bindValue($param, $value, $type);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function closeCursor()
  76. {
  77. return $this->stmt->closeCursor();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function columnCount()
  83. {
  84. return $this->stmt->columnCount();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function errorCode()
  90. {
  91. return $this->stmt->errorCode();
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function errorInfo()
  97. {
  98. return $this->stmt->errorInfo();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function execute($params = null)
  104. {
  105. return $this->stmt->execute($params);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
  111. {
  112. $this->defaultFetchMode = $fetchMode;
  113. return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getIterator()
  119. {
  120. $data = $this->fetchAll();
  121. return new \ArrayIterator($data);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function fetch($fetchMode = null)
  127. {
  128. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  129. $row = $this->stmt->fetch($fetchMode);
  130. $row = $this->fixRow($row,
  131. $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM),
  132. !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE)
  133. );
  134. return $row;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function fetchAll($fetchMode = null, $columnIndex = 0)
  140. {
  141. $fetchMode = $fetchMode ?: $this->defaultFetchMode;
  142. if ($columnIndex != 0) {
  143. $rows = $this->stmt->fetchAll($fetchMode, $columnIndex);
  144. } else {
  145. $rows = $this->stmt->fetchAll($fetchMode);
  146. }
  147. $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
  148. $fixCase = !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE);
  149. if ( ! $iterateRow && !$fixCase) {
  150. return $rows;
  151. }
  152. foreach ($rows as $num => $row) {
  153. $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
  154. }
  155. return $rows;
  156. }
  157. /**
  158. * @param mixed $row
  159. * @param integer $iterateRow
  160. * @param boolean $fixCase
  161. *
  162. * @return array
  163. */
  164. protected function fixRow($row, $iterateRow, $fixCase)
  165. {
  166. if ( ! $row) {
  167. return $row;
  168. }
  169. if ($fixCase) {
  170. $row = array_change_key_case($row, $this->case);
  171. }
  172. if ($iterateRow) {
  173. foreach ($row as $k => $v) {
  174. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
  175. $row[$k] = null;
  176. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
  177. $row[$k] = rtrim($v);
  178. }
  179. }
  180. }
  181. return $row;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function fetchColumn($columnIndex = 0)
  187. {
  188. $value = $this->stmt->fetchColumn($columnIndex);
  189. if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
  190. if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
  191. $value = null;
  192. } else if (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
  193. $value = rtrim($value);
  194. }
  195. }
  196. return $value;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function rowCount()
  202. {
  203. return $this->stmt->rowCount();
  204. }
  205. }