Automatically exported from code.google.com/p/planningalerts
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Iterator.php 7.0 KiB

17 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: Iterator.php,v 1.22 2006/05/06 14:03:41 lsmith Exp $
  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. protected $result;
  57. protected $row;
  58. // {{{ constructor
  59. /**
  60. * Constructor
  61. */
  62. public function __construct($result, $fetchmode = MDB2_FETCHMODE_DEFAULT)
  63. {
  64. $this->result = $result;
  65. $this->fetchmode = $fetchmode;
  66. }
  67. // }}}
  68. // {{{ seek()
  69. /**
  70. * Seek forward to a specific row in a result set
  71. *
  72. * @param int number of the row where the data can be found
  73. *
  74. * @return void
  75. * @access public
  76. */
  77. public function seek($rownum)
  78. {
  79. $this->row = null;
  80. if ($this->result) {
  81. $this->result->seek($rownum);
  82. }
  83. }
  84. // }}}
  85. // {{{ next()
  86. /**
  87. * Fetch next row of data
  88. *
  89. * @return void
  90. * @access public
  91. */
  92. public function next()
  93. {
  94. $this->row = null;
  95. }
  96. // }}}
  97. // {{{ current()
  98. /**
  99. * return a row of data
  100. *
  101. * @return void
  102. * @access public
  103. */
  104. public function current()
  105. {
  106. if (is_null($this->row)) {
  107. $row = $this->result->fetchRow($this->fetchmode);
  108. if (PEAR::isError($row)) {
  109. $row = false;
  110. }
  111. $this->row = $row;
  112. }
  113. return $this->row;
  114. }
  115. // }}}
  116. // {{{ valid()
  117. /**
  118. * Check if the end of the result set has been reached
  119. *
  120. * @return bool true/false, false is also returned on failure
  121. * @access public
  122. */
  123. public function valid()
  124. {
  125. return (bool)$this->current();
  126. }
  127. // }}}
  128. // {{{ free()
  129. /**
  130. * Free the internal resources associated with result.
  131. *
  132. * @return bool|MDB2_Error true on success, false|MDB2_Error if result is invalid
  133. * @access public
  134. */
  135. public function free()
  136. {
  137. if ($this->result) {
  138. return $this->result->free();
  139. }
  140. $this->result = false;
  141. $this->row = null;
  142. return false;
  143. }
  144. // }}}
  145. // {{{ key()
  146. /**
  147. * Returns the row number
  148. *
  149. * @return int|bool|MDB2_Error true on success, false|MDB2_Error if result is invalid
  150. * @access public
  151. */
  152. public function key()
  153. {
  154. if ($this->result) {
  155. return $this->result->rowCount();
  156. }
  157. return false;
  158. }
  159. // }}}
  160. // {{{ rewind()
  161. /**
  162. * Seek to the first row in a result set
  163. *
  164. * @return void
  165. * @access public
  166. */
  167. public function rewind()
  168. {
  169. }
  170. // }}}
  171. // {{{ destructor
  172. /**
  173. * Destructor
  174. */
  175. public function __destruct()
  176. {
  177. $this->free();
  178. }
  179. // }}}
  180. }
  181. /**
  182. * PHP5 buffered Iterator
  183. *
  184. * @package MDB2
  185. * @category Database
  186. * @author Lukas Smith <smith@pooteeweet.org>
  187. */
  188. class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator
  189. {
  190. // {{{ valid()
  191. /**
  192. * Check if the end of the result set has been reached
  193. *
  194. * @return bool|MDB2_Error true on success, false|MDB2_Error if result is invalid
  195. * @access public
  196. */
  197. public function valid()
  198. {
  199. if ($this->result) {
  200. return $this->result->valid();
  201. }
  202. return false;
  203. }
  204. // }}}
  205. // {{{count()
  206. /**
  207. * Returns the number of rows in a result object
  208. *
  209. * @return int|MDB2_Error number of rows, false|MDB2_Error if result is invalid
  210. * @access public
  211. */
  212. public function count()
  213. {
  214. if ($this->result) {
  215. return $this->result->numRows();
  216. }
  217. return false;
  218. }
  219. // }}}
  220. // {{{ rewind()
  221. /**
  222. * Seek to the first row in a result set
  223. *
  224. * @return void
  225. * @access public
  226. */
  227. public function rewind()
  228. {
  229. $this->seek(0);
  230. }
  231. // }}}
  232. }
  233. ?>