Result.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Database result wrapper. See [Results](/database/results) for usage and examples.
  4. *
  5. * @package Kohana/Database
  6. * @category Query/Result
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess {
  12. // Executed SQL for this result
  13. protected $_query;
  14. // Raw result resource
  15. protected $_result;
  16. // Total number of rows and current row
  17. protected $_total_rows = 0;
  18. protected $_current_row = 0;
  19. // Return rows as an object or associative array
  20. protected $_as_object;
  21. // Parameters for __construct when using object results
  22. protected $_object_params = NULL;
  23. /**
  24. * Sets the total number of rows and stores the result locally.
  25. *
  26. * @param mixed $result query result
  27. * @param string $sql SQL query
  28. * @param mixed $as_object
  29. * @param array $params
  30. * @return void
  31. */
  32. public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
  33. {
  34. // Store the result locally
  35. $this->_result = $result;
  36. // Store the SQL locally
  37. $this->_query = $sql;
  38. if (is_object($as_object))
  39. {
  40. // Get the object class name
  41. $as_object = get_class($as_object);
  42. }
  43. // Results as objects or associative arrays
  44. $this->_as_object = $as_object;
  45. if ($params)
  46. {
  47. // Object constructor params
  48. $this->_object_params = $params;
  49. }
  50. }
  51. /**
  52. * Result destruction cleans up all open result sets.
  53. *
  54. * @return void
  55. */
  56. abstract public function __destruct();
  57. /**
  58. * Get a cached database result from the current result iterator.
  59. *
  60. * $cachable = serialize($result->cached());
  61. *
  62. * @return Database_Result_Cached
  63. * @since 3.0.5
  64. */
  65. public function cached()
  66. {
  67. return new Database_Result_Cached($this->as_array(), $this->_query, $this->_as_object);
  68. }
  69. /**
  70. * Return all of the rows in the result as an array.
  71. *
  72. * // Indexed array of all rows
  73. * $rows = $result->as_array();
  74. *
  75. * // Associative array of rows by "id"
  76. * $rows = $result->as_array('id');
  77. *
  78. * // Associative array of rows, "id" => "name"
  79. * $rows = $result->as_array('id', 'name');
  80. *
  81. * @param string $key column for associative keys
  82. * @param string $value column for values
  83. * @return array
  84. */
  85. public function as_array($key = NULL, $value = NULL)
  86. {
  87. $results = array();
  88. if ($key === NULL AND $value === NULL)
  89. {
  90. // Indexed rows
  91. foreach ($this as $row)
  92. {
  93. $results[] = $row;
  94. }
  95. }
  96. elseif ($key === NULL)
  97. {
  98. // Indexed columns
  99. if ($this->_as_object)
  100. {
  101. foreach ($this as $row)
  102. {
  103. $results[] = $row->$value;
  104. }
  105. }
  106. else
  107. {
  108. foreach ($this as $row)
  109. {
  110. $results[] = $row[$value];
  111. }
  112. }
  113. }
  114. elseif ($value === NULL)
  115. {
  116. // Associative rows
  117. if ($this->_as_object)
  118. {
  119. foreach ($this as $row)
  120. {
  121. $results[$row->$key] = $row;
  122. }
  123. }
  124. else
  125. {
  126. foreach ($this as $row)
  127. {
  128. $results[$row[$key]] = $row;
  129. }
  130. }
  131. }
  132. else
  133. {
  134. // Associative columns
  135. if ($this->_as_object)
  136. {
  137. foreach ($this as $row)
  138. {
  139. $results[$row->$key] = $row->$value;
  140. }
  141. }
  142. else
  143. {
  144. foreach ($this as $row)
  145. {
  146. $results[$row[$key]] = $row[$value];
  147. }
  148. }
  149. }
  150. $this->rewind();
  151. return $results;
  152. }
  153. /**
  154. * Return the named column from the current row.
  155. *
  156. * // Get the "id" value
  157. * $id = $result->get('id');
  158. *
  159. * @param string $name column to get
  160. * @param mixed $default default value if the column does not exist
  161. * @return mixed
  162. */
  163. public function get($name, $default = NULL)
  164. {
  165. $row = $this->current();
  166. if ($this->_as_object)
  167. {
  168. if (isset($row->$name))
  169. return $row->$name;
  170. }
  171. else
  172. {
  173. if (isset($row[$name]))
  174. return $row[$name];
  175. }
  176. return $default;
  177. }
  178. /**
  179. * Implements [Countable::count], returns the total number of rows.
  180. *
  181. * echo count($result);
  182. *
  183. * @return integer
  184. */
  185. public function count()
  186. {
  187. return $this->_total_rows;
  188. }
  189. /**
  190. * Implements [ArrayAccess::offsetExists], determines if row exists.
  191. *
  192. * if (isset($result[10]))
  193. * {
  194. * // Row 10 exists
  195. * }
  196. *
  197. * @param int $offset
  198. * @return boolean
  199. */
  200. public function offsetExists($offset)
  201. {
  202. return ($offset >= 0 AND $offset < $this->_total_rows);
  203. }
  204. /**
  205. * Implements [ArrayAccess::offsetGet], gets a given row.
  206. *
  207. * $row = $result[10];
  208. *
  209. * @param int $offset
  210. * @return mixed
  211. */
  212. public function offsetGet($offset)
  213. {
  214. if ( ! $this->seek($offset))
  215. return NULL;
  216. return $this->current();
  217. }
  218. /**
  219. * Implements [ArrayAccess::offsetSet], throws an error.
  220. *
  221. * [!!] You cannot modify a database result.
  222. *
  223. * @param int $offset
  224. * @param mixed $value
  225. * @return void
  226. * @throws Kohana_Exception
  227. */
  228. final public function offsetSet($offset, $value)
  229. {
  230. throw new Kohana_Exception('Database results are read-only');
  231. }
  232. /**
  233. * Implements [ArrayAccess::offsetUnset], throws an error.
  234. *
  235. * [!!] You cannot modify a database result.
  236. *
  237. * @param int $offset
  238. * @return void
  239. * @throws Kohana_Exception
  240. */
  241. final public function offsetUnset($offset)
  242. {
  243. throw new Kohana_Exception('Database results are read-only');
  244. }
  245. /**
  246. * Implements [Iterator::key], returns the current row number.
  247. *
  248. * echo key($result);
  249. *
  250. * @return integer
  251. */
  252. public function key()
  253. {
  254. return $this->_current_row;
  255. }
  256. /**
  257. * Implements [Iterator::next], moves to the next row.
  258. *
  259. * next($result);
  260. *
  261. * @return $this
  262. */
  263. public function next()
  264. {
  265. ++$this->_current_row;
  266. return $this;
  267. }
  268. /**
  269. * Implements [Iterator::prev], moves to the previous row.
  270. *
  271. * prev($result);
  272. *
  273. * @return $this
  274. */
  275. public function prev()
  276. {
  277. --$this->_current_row;
  278. return $this;
  279. }
  280. /**
  281. * Implements [Iterator::rewind], sets the current row to zero.
  282. *
  283. * rewind($result);
  284. *
  285. * @return $this
  286. */
  287. public function rewind()
  288. {
  289. $this->_current_row = 0;
  290. return $this;
  291. }
  292. /**
  293. * Implements [Iterator::valid], checks if the current row exists.
  294. *
  295. * [!!] This method is only used internally.
  296. *
  297. * @return boolean
  298. */
  299. public function valid()
  300. {
  301. return $this->offsetExists($this->_current_row);
  302. }
  303. } // End Database_Result