Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1173 jp_milcent 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Contains the DB_QueryTool_Result class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: This source file is subject to version 3.0 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
15
 * @category   Database
16
 * @package    DB_QueryTool
17
 * @author     Wolfram Kriesing <wk@visionp.de>
18
 * @author     Paolo Panto <wk@visionp.de>
19
 * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
20
 * @copyright  2003-2005 Wolfram Kriesing, Paolo Panto, Lorenzo Alberton
21
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
22
 * @version    CVS: $Id: Result.php,v 1.1 2006-12-14 15:04:29 jp_milcent Exp $
23
 * @link       http://pear.php.net/package/DB_QueryTool
24
 */
25
 
26
/**
27
 * DB_QueryTool_Result class
28
 *
29
 * This result actually contains the 'data' itself, the number of rows
30
 * returned and some additional info
31
 * using ZE2 you can also get retrieve data from the result doing the following:
32
 * <DB_QueryTool_Common-instance>->getAll()->getCount()
33
 * or
34
 * <DB_QueryTool_Common-instance>->getAll()->getData()
35
 *
36
 * @category   Database
37
 * @package    DB_QueryTool
38
 * @author     Wolfram Kriesing <wk@visionp.de>
39
 * @author     Paolo Panto <wk@visionp.de>
40
 * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
41
 * @copyright  2003-2005 Wolfram Kriesing, Paolo Panto, Lorenzo Alberton
42
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
43
 * @link       http://pear.php.net/package/DB_QueryTool
44
 */
45
class DB_QueryTool_Result
46
{
47
    // {{{ class vars
48
 
49
    /**
50
     * @var array
51
     */
52
    var $_data = array();
53
 
54
    /**
55
     * @var array
56
     */
57
    var $_dataKeys = array();
58
 
59
    /**
60
     * @var integer
61
     */
62
    var $_count = 0;
63
 
64
    /**
65
     * the counter for the methods getFirst, getNext
66
     * @var array
67
     */
68
    var $_counter = null;
69
 
70
    // }}}
71
    // {{{ DB_QueryTool_Result()
72
 
73
    /**
74
     * create a new instance of result with the data returned by the query
75
     *
76
     * @version    2002/07/11
77
     * @access     public
78
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
79
     * @param      array   the data returned by the result
80
     */
81
    function DB_QueryTool_Result($data)
82
    {
83
        if (!count($data)) {
84
            $this->_count = 0;
85
        } else {
86
            list($firstElement) = $data;
87
            if (is_array($firstElement)) { // is the array a collection of rows?
88
                $this->_count = sizeof($data);
89
            } else {
90
                if (sizeof($data) > 0) {
91
                    $this->_count = 1;
92
                } else {
93
                    $this->_count = 0;
94
                }
95
            }
96
        }
97
        $this->_data = $data;
98
    }
99
 
100
    // }}}
101
    // {{{ numRows
102
 
103
	/**
104
	 * return the number of rows returned. This is an alias for getCount().
105
	 *
106
	 * @access    public
107
	 * @return    integer
108
	 */
109
	function numRows()
110
	{
111
	    return $this->_count;
112
	}
113
 
114
	// }}}
115
    // {{{ getCount()
116
 
117
    /**
118
     * return the number of rows returned
119
     *
120
     * @version    2002/07/11
121
     * @access     public
122
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
123
     * @return integer the number of rows returned
124
     */
125
    function getCount()
126
    {
127
        return $this->_count;
128
    }
129
 
130
    // }}}
131
    // {{{ getData()
132
 
133
    /**
134
     * get all the data returned
135
     *
136
     * @version    2002/07/11
137
     * @access     public
138
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
139
     * @param      string $key
140
     * @return mixed array or PEAR_Error
141
     */
142
    function getData($key=null)
143
    {
144
        if (is_null($key)) {
145
            return $this->_data;
146
        }
147
        if ($this->_data[$key]) {
148
            return $this->_data[$key];
149
        }
150
        return new PEAR_Error("there is no element with the key '$key'!");
151
    }
152
 
153
    // }}}
154
    // {{{ getFirst()
155
 
156
    /**
157
     * get the first result set
158
     * we are not using next, current, and reset, since those ignore keys
159
     * which are empty or 0
160
     *
161
     * @version    2002/07/11
162
     * @access     public
163
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
164
     * @return mixed
165
     */
166
    function getFirst()
167
    {
168
        if ($this->getCount() > 0) {
169
            $this->_dataKeys = array_keys($this->_data);
170
            $this->_counter = 0;
171
            return $this->_data[$this->_dataKeys[$this->_counter]];
172
        }
173
        return new PEAR_Error('There are no elements!');
174
    }
175
 
176
    // }}}
177
    // {{{ getNext()
178
 
179
    /**
180
     * Get next result set. If getFirst() has never been called before,
181
     * it calls that method.
182
     * @return mixed
183
     * @access public
184
     */
185
    function getNext()
186
    {
187
        if (!$this->initDone()) {
188
    		return $this->getFirst();
189
    	}
190
        if ($this->hasMore()) {
191
            $this->_counter++;
192
            return $this->_data[$this->_dataKeys[$this->_counter]];
193
        }
194
        return new PEAR_Error('there are no more elements!');
195
    }
196
 
197
    // }}}
198
    // {{{ hasMore()
199
 
200
    /**
201
     * check if there are other rows
202
     *
203
     * @return boolean
204
     * @access public
205
     */
206
    function hasMore()
207
    {
208
        if ($this->_counter+1 < $this->getCount()) {
209
            return true;
210
        }
211
        return false;
212
    }
213
 
214
    // }}}
215
	// {{{ fetchRow
216
 
217
	/**
218
	 * This function emulates PEAR::DB fetchRow() method.
219
	 * With this method, DB_QueryTool can transparently replace PEAR_DB
220
	 *
221
	 * @todo implement fetchmode support?
222
	 * @access    public
223
	 * @return    void
224
	 */
225
	function fetchRow()
226
	{
227
		if ($this->hasMore()) {
228
    		$arr = $this->getNext();
229
    		if (!PEAR::isError($arr)) {
230
    		    return $arr;
231
    		}
232
    	}
233
    	return false;
234
	}
235
 
236
    // }}}
237
	// {{{ initDone
238
 
239
	/**
240
	 * Helper method. Check if $this->_dataKeys has been initialized
241
	 *
242
	 * @return boolean
243
	 * @access private
244
	 */
245
	function initDone()
246
	{
247
	    return (
248
	        isset($this->_dataKeys) &&
249
            is_array($this->_dataKeys) &&
250
            count($this->_dataKeys)
251
        );
252
	}
253
 
254
	// }}}
255
 
256
    #TODO
257
    #function getPrevious()
258
    #function getLast()
259
 
260
}
261
?>