]>
jfr.im git - z_archive/KronOS.git/blob - system/database/DB_result.php
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Database Result Class
21 * This is the platform-independent result class.
22 * This class will not be called directly. Rather, the adapter
23 * class for the specific database will extend and instantiate it.
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/database/
32 var $result_id = NULL;
33 var $result_array = array();
34 var $result_object = array();
35 var $custom_result_object = array();
42 * Query result. Acts as a wrapper function for the following functions.
45 * @param string can be "object" or "array"
46 * @return mixed either a result object or array
48 public function result($type = 'object')
50 if ($type == 'array') return $this->result_array();
51 else if ($type == 'object') return $this->result_object();
52 else return $this->custom_result_object($type);
55 // --------------------------------------------------------------------
58 * Custom query result.
60 * @param class_name A string that represents the type of object you want back
61 * @return array of objects
63 public function custom_result_object($class_name)
65 if (array_key_exists($class_name, $this->custom_result_object
))
67 return $this->custom_result_object
[$class_name];
70 if ($this->result_id
=== FALSE OR $this->num_rows() == 0)
75 // add the data to the object
77 $result_object = array();
79 while ($row = $this->_fetch_object())
81 $object = new $class_name();
83 foreach ($row as $key => $value)
85 $object->$key = $value;
88 $result_object[] = $object;
92 return $this->custom_result_object
[$class_name] = $result_object;
95 // --------------------------------------------------------------------
98 * Query result. "object" version.
103 public function result_object()
105 if (count($this->result_object
) > 0)
107 return $this->result_object
;
110 // In the event that query caching is on the result_id variable
111 // will return FALSE since there isn't a valid SQL resource so
112 // we'll simply return an empty array.
113 if ($this->result_id
=== FALSE OR $this->num_rows() == 0)
118 $this->_data_seek(0);
119 while ($row = $this->_fetch_object())
121 $this->result_object
[] = $row;
124 return $this->result_object
;
127 // --------------------------------------------------------------------
130 * Query result. "array" version.
135 public function result_array()
137 if (count($this->result_array
) > 0)
139 return $this->result_array
;
142 // In the event that query caching is on the result_id variable
143 // will return FALSE since there isn't a valid SQL resource so
144 // we'll simply return an empty array.
145 if ($this->result_id
=== FALSE OR $this->num_rows() == 0)
150 $this->_data_seek(0);
151 while ($row = $this->_fetch_assoc())
153 $this->result_array
[] = $row;
156 return $this->result_array
;
159 // --------------------------------------------------------------------
162 * Query result. Acts as a wrapper function for the following functions.
166 * @param string can be "object" or "array"
167 * @return mixed either a result object or array
169 public function row($n = 0, $type = 'object')
171 if ( ! is_numeric($n))
173 // We cache the row data for subsequent uses
174 if ( ! is_array($this->row_data
))
176 $this->row_data
= $this->row_array(0);
179 // array_key_exists() instead of isset() to allow for MySQL NULL values
180 if (array_key_exists($n, $this->row_data
))
182 return $this->row_data
[$n];
184 // reset the $n variable if the result was not achieved
188 if ($type == 'object') return $this->row_object($n);
189 else if ($type == 'array') return $this->row_array($n);
190 else return $this->custom_row_object($n, $type);
193 // --------------------------------------------------------------------
196 * Assigns an item into a particular column slot
201 public function set_row($key, $value = NULL)
203 // We cache the row data for subsequent uses
204 if ( ! is_array($this->row_data
))
206 $this->row_data
= $this->row_array(0);
211 foreach ($key as $k => $v)
213 $this->row_data
[$k] = $v;
219 if ($key != '' AND ! is_null($value))
221 $this->row_data
[$key] = $value;
225 // --------------------------------------------------------------------
228 * Returns a single result row - custom object version
233 public function custom_row_object($n, $type)
235 $result = $this->custom_result_object($type);
237 if (count($result) == 0)
242 if ($n != $this->current_row
AND isset($result[$n]))
244 $this->current_row
= $n;
247 return $result[$this->current_row
];
251 * Returns a single result row - object version
256 public function row_object($n = 0)
258 $result = $this->result_object();
260 if (count($result) == 0)
265 if ($n != $this->current_row
AND isset($result[$n]))
267 $this->current_row
= $n;
270 return $result[$this->current_row
];
273 // --------------------------------------------------------------------
276 * Returns a single result row - array version
281 public function row_array($n = 0)
283 $result = $this->result_array();
285 if (count($result) == 0)
290 if ($n != $this->current_row
AND isset($result[$n]))
292 $this->current_row
= $n;
295 return $result[$this->current_row
];
299 // --------------------------------------------------------------------
302 * Returns the "first" row
307 public function first_row($type = 'object')
309 $result = $this->result($type);
311 if (count($result) == 0)
318 // --------------------------------------------------------------------
321 * Returns the "last" row
326 public function last_row($type = 'object')
328 $result = $this->result($type);
330 if (count($result) == 0)
334 return $result[count($result) -1];
337 // --------------------------------------------------------------------
340 * Returns the "next" row
345 public function next_row($type = 'object')
347 $result = $this->result($type);
349 if (count($result) == 0)
354 if (isset($result[$this->current_row +
1]))
356 ++
$this->current_row
;
359 return $result[$this->current_row
];
362 // --------------------------------------------------------------------
365 * Returns the "previous" row
370 public function previous_row($type = 'object')
372 $result = $this->result($type);
374 if (count($result) == 0)
379 if (isset($result[$this->current_row
- 1]))
381 --$this->current_row
;
383 return $result[$this->current_row
];
386 // --------------------------------------------------------------------
389 * The following functions are normally overloaded by the identically named
390 * methods in the platform-specific driver -- except when query caching
391 * is used. When caching is enabled we do not load the other driver.
392 * These functions are primarily here to prevent undefined function errors
393 * when a cached result object is in use. They are not otherwise fully
394 * operational due to the unavailability of the database resource IDs with
397 public function num_rows() { return $this
->num_rows
; }
398 public function num_fields() { return 0; }
399 public function list_fields() { return array(); }
400 public function field_data() { return array(); }
401 public function free_result() { return TRUE; }
402 protected function _data_seek() { return TRUE; }
403 protected function _fetch_assoc() { return array(); }
404 protected function _fetch_object() { return array(); }
407 // END DB_result class
409 /* End of file DB_result.php */
410 /* Location: ./system/database/DB_result.php */