thomascube
2006-02-22 745b1466fc76d5ded589e2469328086002430c1c
commit | author | age
d13c36 1 <?php
S 2 // +----------------------------------------------------------------------+
3 // | PHP version 5                                                        |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1998-2004 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 /**
48  * @package  MDB2
49  * @category Database
50  * @author   Lukas Smith <smith@pooteeweet.org>
51  */
52 class MDB2_Iterator implements Iterator
53 {
54     protected $fetchmode;
55     protected $result;
56     protected $row;
57
58     // {{{ constructor
59
60     /**
61      * Constructor
62      */
63     public function __construct($result, $fetchmode = MDB2_FETCHMODE_DEFAULT)
64     {
65         $this->result = $result;
66         $this->fetchmode = $fetchmode;
67     }
68
69     // }}}
70     // {{{ seek()
71
72     /**
73     * seek forward to a specific row in a result set
74     *
75     * @param int    $rownum    number of the row where the data can be found
76     * @return void
77     * @access public
78     */
79     public function seek($rownum)
80     {
81         $this->row = null;
82         if ($this->result) {
83             $this->result->seek($rownum);
84         }
85     }
86
87     // }}}
88     // {{{ next()
89
90     /**
91     * Fetch next row of data
92     *
93     * @return void
94     * @access public
95     */
96     public function next()
97     {
98         $this->row = null;
99     }
100
101     // }}}
102     // {{{ current()
103
104     /**
105      * return a row of data
106      *
107     * @return void
108     * @access public
109     */
110     public function current()
111     {
112         if (is_null($this->row)) {
113             $row = $this->result->fetchRow($this->fetchmode);
114             if (PEAR::isError($row)) {
115                 $row = false;
116             }
117             $this->row = $row;
118         }
119         return $this->row;
120     }
121
122     // }}}
123     // {{{ valid()
124
125     /**
126     * check if the end of the result set has been reached
127     *
128     * @return mixed true or false on sucess, a MDB2 error on failure
129     * @access public
130     */
131     public function valid()
132     {
133         return (bool)$this->current();
134     }
135
136     // }}}
137     // {{{ free()
138
139     /**
140      * Free the internal resources associated with result.
141      *
142      * @return boolean true on success, false if result is invalid
143      * @access public
144      */
145     public function free()
146     {
147         if ($this->result) {
148             return $this->result->free();
149         }
150         $this->result = null;
151         $this->row = null;
152         return false;
153     }
154
155     // }}}
156     // {{{ key()
157
158     /**
159     * nothing, but Iterator wants to implement this.
160     *
161     * @return void
162     * @access public
163     */
164     public function key()
165     {
166         if ($this->result) {
167             return $this->result->rowCount();
168         }
169         return false;
170     }
171
172     // }}}
173     // {{{ rewind()
174
175     /**
176     * seek to the first row in a result set
177     *
178     * @return void
179     * @access public
180     */
181     public function rewind()
182     {
183     }
184
185     // }}}
186     // {{{ destructor
187
188     /**
189      * Destructor
190      */
191     public function __destruct()
192     {
193         $this->free();
194     }
195 }
196
197 class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator
198 {
199     // }}}
200     // {{{ valid()
201
202     /**
203     * check if the end of the result set has been reached
204     *
205     * @return mixed true or false on sucess, a MDB2 error on failure
206     * @access public
207     */
208     public function valid()
209     {
210         if ($this->result) {
211             return $this->result->valid();
212         }
213         return false;
214     }
215
216     // }}}
217     // {{{count()
218
219     /**
220      * returns the number of rows in a result object
221      *
222      * @return mixed MDB2 Error Object or the number of rows
223      * @access public
224      */
225     public function count()
226     {
227         if ($this->result) {
228             return $this->result->numRows();
229         }
230         return false;
231     }
232
233     // }}}
234     // {{{ hasPrev()
235
236     /**
237     * check if there is a previous row
238     *
239     * @return mixed true or false on sucess, a MDB2 error on failure
240     * @access public
241     */
242     public function hasPrev()
243     {
244         if ($this->result) {
245             return $this->result->rowCount() > 0;
246         }
247         return false;
248     }
249
250     // }}}
251     // {{{ rewind()
252
253     /**
254     * seek to the first row in a result set
255     *
256     * @return mixed MDB2_OK on success, a MDB2 error on failure
257     * @access public
258     */
259     public function rewind()
260     {
261         $this->seek(0);
262     }
263
264     // }}}
265     // {{{ prev()
266
267     /**
268     * move internal row point to the previous row
269     * Fetch and return a row of data
270     *
271     * @return void
272     * @access public
273     */
274     public function prev()
275     {
276         if ($this->hasPrev()) {
277             $this->seek($this->result->rowCount() - 1);
278         } else {
279             return false;
280         }
281         return $this->next();
282     }
283 }
284
285 ?>