summaryrefslogtreecommitdiff
path: root/system/libraries/Database_Result.php
blob: cf2056f34003d7b240fa3ebef5466342c8fced64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Database result wrapper.
 * 
 * $Id: Database_Result.php 4679 2009-11-10 01:45:52Z isaiah $
 * 
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2008-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
abstract class Database_Result_Core implements Countable, Iterator, SeekableIterator, ArrayAccess {

	protected $result;

	protected $total_rows  = 0;
	protected $current_row = 0;
	protected $insert_id;

	// Return objects or arrays for each row
	protected $return_objects;

	/**
	 * Sets the total number of rows and stores the result locally.
	 *
	 * @param   mixed   $result query result
	 * @param   boolean $return_objects True for results as objects, false for arrays
	 * @return  void
	 */
	abstract public function __construct($result, $sql, $link, $return_objects);

	/**
	 * Result destruction cleans up all open result sets.
	 */
	abstract public function __destruct();

	/**
	 * Return arrays for reach result, or the entire set of results
	 *
	 * @param  boolean $return  True to return entire result array
	 * @return Database_Result|array
	 */
	abstract public function as_array($return = FALSE);

	/**
	 * Returns objects for each result
	 *
	 * @param  string $class  Class name to return objects as or NULL for stdClass
	 * @return Database_Result
	 */
	abstract public function as_object($class = NULL, $return = FALSE);

	/**
	 * Returns the insert id
	 *
	 * @return int
	 */
	public function insert_id()
	{
		return $this->insert_id;
	}

	/**
	 * Return the named column from the current row.
	 *
	 * @param  string  Column name
	 * @return mixed
	 */
	public function get($name)
	{
		// Get the current row
		$row = $this->current();

		if ( ! $this->return_objects)
			return $row[$name];

		return $row->$name;
	}

	/**
	 * Countable: count
	 */
	public function count()
	{
		return $this->total_rows;
	}

	/**
	 * ArrayAccess: offsetExists
	 */
	public function offsetExists($offset)
	{
		return ($offset >= 0 AND $offset < $this->total_rows);
	}

	/**
	 * ArrayAccess: offsetGet
	 */
	public function offsetGet($offset)
	{
		if ( ! $this->seek($offset))
			return NULL;

		return $this->current();
	}

	/**
	 * ArrayAccess: offsetSet
	 *
	 * @throws  Kohana_Database_Exception
	 */
	final public function offsetSet($offset, $value)
	{
		throw new Kohana_Exception('Database results are read-only');
	}

	/**
	 * ArrayAccess: offsetUnset
	 *
	 * @throws  Kohana_Database_Exception
	 */
	final public function offsetUnset($offset)
	{
		throw new Kohana_Exception('Database results are read-only');
	}

	/**
	 * Iterator: key
	 */
	public function key()
	{
		return $this->current_row;
	}

	/**
	 * Iterator: next
	 */
	public function next()
	{
		++$this->current_row;
		return $this;
	}

	/**
	 * Iterator: prev
	 */
	public function prev()
	{
		--$this->current_row;
		return $this;
	}

	/**
	 * Iterator: rewind
	 */
	public function rewind()
	{
		$this->current_row = 0;
		return $this;
	}

	/**
	 * Iterator: valid
	 */
	public function valid()
	{
		return $this->offsetExists($this->current_row);
	}

} // End Database_Result