source: branches/version-2_5-dev/data/module/MDB2/Iterator.php @ 20119

Revision 20119, 7.0 KB checked in by nanasess, 12 years ago (diff)

module 以下は svn:keywords を除外

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