Iterator.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. /**
  47. * PHP5 Iterator
  48. *
  49. * @package MDB2
  50. * @category Database
  51. * @author Lukas Smith <smith@pooteeweet.org>
  52. */
  53. class MDB2_Iterator implements Iterator
  54. {
  55. protected $fetchmode;
  56. /**
  57. * @var MDB2_Result_Common
  58. */
  59. protected $result;
  60. protected $row;
  61. // {{{ constructor
  62. /**
  63. * Constructor
  64. */
  65. public function __construct(MDB2_Result_Common $result, $fetchmode = MDB2_FETCHMODE_DEFAULT)
  66. {
  67. $this->result = $result;
  68. $this->fetchmode = $fetchmode;
  69. }
  70. // }}}
  71. // {{{ seek()
  72. /**
  73. * Seek forward to a specific row in a result set
  74. *
  75. * @param int number of the row where the data can be found
  76. *
  77. * @return void
  78. * @access public
  79. */
  80. public function seek($rownum)
  81. {
  82. $this->row = null;
  83. if ($this->result) {
  84. $this->result->seek($rownum);
  85. }
  86. }
  87. // }}}
  88. // {{{ next()
  89. /**
  90. * Fetch next row of data
  91. *
  92. * @return void
  93. * @access public
  94. */
  95. public function next()
  96. {
  97. $this->row = null;
  98. }
  99. // }}}
  100. // {{{ current()
  101. /**
  102. * return a row of data
  103. *
  104. * @return void
  105. * @access public
  106. */
  107. public function current()
  108. {
  109. if (null === $this->row) {
  110. $row = $this->result->fetchRow($this->fetchmode);
  111. if (MDB2::isError($row)) {
  112. $row = false;
  113. }
  114. $this->row = $row;
  115. }
  116. return $this->row;
  117. }
  118. // }}}
  119. // {{{ valid()
  120. /**
  121. * Check if the end of the result set has been reached
  122. *
  123. * @return bool true/false, false is also returned on failure
  124. * @access public
  125. */
  126. public function valid()
  127. {
  128. return (bool)$this->current();
  129. }
  130. // }}}
  131. // {{{ free()
  132. /**
  133. * Free the internal resources associated with result.
  134. *
  135. * @return bool|MDB2_Error true on success, false|MDB2_Error if result is invalid
  136. * @access public
  137. */
  138. public function free()
  139. {
  140. if ($this->result) {
  141. return $this->result->free();
  142. }
  143. $this->result = false;
  144. $this->row = null;
  145. return false;
  146. }
  147. // }}}
  148. // {{{ key()
  149. /**
  150. * Returns the row number
  151. *
  152. * @return int|bool|MDB2_Error true on success, false|MDB2_Error if result is invalid
  153. * @access public
  154. */
  155. public function key()
  156. {
  157. if ($this->result) {
  158. return $this->result->rowCount();
  159. }
  160. return false;
  161. }
  162. // }}}
  163. // {{{ rewind()
  164. /**
  165. * Seek to the first row in a result set
  166. *
  167. * @return void
  168. * @access public
  169. */
  170. public function rewind()
  171. {
  172. }
  173. // }}}
  174. // {{{ destructor
  175. /**
  176. * Destructor
  177. */
  178. public function __destruct()
  179. {
  180. $this->free();
  181. }
  182. // }}}
  183. }
  184. /**
  185. * PHP5 buffered Iterator
  186. *
  187. * @package MDB2
  188. * @category Database
  189. * @author Lukas Smith <smith@pooteeweet.org>
  190. */
  191. class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator
  192. {
  193. // {{{ valid()
  194. /**
  195. * Check if the end of the result set has been reached
  196. *
  197. * @return bool|MDB2_Error true on success, false|MDB2_Error if result is invalid
  198. * @access public
  199. */
  200. public function valid()
  201. {
  202. if ($this->result) {
  203. return $this->result->valid();
  204. }
  205. return false;
  206. }
  207. // }}}
  208. // {{{count()
  209. /**
  210. * Returns the number of rows in a result object
  211. *
  212. * @return int|MDB2_Error number of rows, false|MDB2_Error if result is invalid
  213. * @access public
  214. */
  215. public function count()
  216. {
  217. if ($this->result) {
  218. return $this->result->numRows();
  219. }
  220. return false;
  221. }
  222. // }}}
  223. // {{{ rewind()
  224. /**
  225. * Seek to the first row in a result set
  226. *
  227. * @return void
  228. * @access public
  229. */
  230. public function rewind()
  231. {
  232. $this->seek(0);
  233. }
  234. // }}}
  235. }
  236. ?>