summaryrefslogtreecommitdiff
path: root/system/libraries/Database_Mysqli_Result.php
blob: f8b7b588e4360cd7874593f25453936b9e5af17f (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
171
172
173
174
175
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * MySQL database result.
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2008-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Database_Mysqli_Result_Core extends Database_Result {

	protected $internal_row = 0;

	public function __construct($result, $sql, $link, $return_objects)
	{
		if (is_object($result))
		{
			// True to return objects, false for arrays
			$this->return_objects = $return_objects;

			$this->total_rows = $result->num_rows;
		}
		elseif (is_bool($result))
		{
			if ($result == FALSE)
			{
				throw new Database_Exception('#:errno: :error [ :query ]',
					array(':error' => $link->error,
					':query' => $sql,
					':errno' => $link->errno));
			}
			else
			{
				// It's a DELETE, INSERT, REPLACE, or UPDATE query
				$this->insert_id = $link->insert_id;
				$this->total_rows = $link->affected_rows;
			}
		}

		// Store the result locally
		$this->result = $result;

		$this->sql = $sql;
	}

	public function __destruct()
	{
		if (is_object($this->result))
		{
			$this->result->free();
		}
	}

	public function as_array($return = FALSE)
	{
		// Return arrays rather than objects
		$this->return_objects = FALSE;

		if ( ! $return )
		{
			// Return this result object
			return $this;
		}

		// Return a nested array of all results
		if (RUNS_MYSQLND)
			return $this->result->fetch_all(MYSQLI_ASSOC);

		$array = array();

		if ($this->total_rows > 0)
		{
			// Seek to the beginning of the result
			$this->result->data_seek(0);

			while ($row = $this->result->fetch_assoc())
			{
				// Add each row to the array
				$array[] = $row;
			}
			$this->internal_row = $this->total_rows;
		}

		return $array;
	}

	public function as_object($class = NULL, $return = FALSE)
	{
		// Return objects of type $class (or stdClass if none given)
		$this->return_objects = ($class !== NULL) ? $class : TRUE;

		if ( ! $return )
		{
			// Return this result object
			return $this;
		}

		// Return a nested array of all results
		$array = array();

		if ($this->total_rows > 0)
		{
			// Seek to the beginning of the result
			$this->result->data_seek(0);

			if (is_string($this->return_objects))
			{
				while ($row = $this->result->fetch_object($this->return_objects))
				{
					// Add each row to the array
					$array[] = $row;
				}
			}
			else
			{
				while ($row = $this->result->fetch_object())
				{
					// Add each row to the array
					$array[] = $row;
				}
			}

			$this->internal_row = $this->total_rows;
		}

		return $array;
	}

	/**
	 * SeekableIterator: seek
	 */
	public function seek($offset)
	{
		if ($this->offsetExists($offset) AND $this->result->data_seek($offset))
		{
			// Set the current row to the offset
			$this->current_row = $offset;

			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * Iterator: current
	 */
	public function current()
	{
		if ($this->current_row !== $this->internal_row AND ! $this->seek($this->current_row))
			return NULL;

		++$this->internal_row;

		if ($this->return_objects)
		{
			if (is_string($this->return_objects))
			{
				return $this->result->fetch_object($this->return_objects);
			}
			else
			{
				return $this->result->fetch_object();
			}
		}
		else
		{
			// Return an array of the row
			return $this->result->fetch_assoc();
		}
	}

} // End Database_MySQLi_Result