result.php 5.9 KB

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