Result.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  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) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  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|null $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 = [];
  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. #[\ReturnTypeWillChange]
  186. public function count()
  187. {
  188. return $this->_total_rows;
  189. }
  190. /**
  191. * Implements [ArrayAccess::offsetExists], determines if row exists.
  192. *
  193. * if (isset($result[10]))
  194. * {
  195. * // Row 10 exists
  196. * }
  197. *
  198. * @param int $offset
  199. * @return boolean
  200. */
  201. #[\ReturnTypeWillChange]
  202. public function offsetExists($offset)
  203. {
  204. return ($offset >= 0 AND $offset < $this->_total_rows);
  205. }
  206. /**
  207. * Implements [ArrayAccess::offsetGet], gets a given row.
  208. *
  209. * $row = $result[10];
  210. *
  211. * @param int $offset
  212. * @return mixed
  213. */
  214. #[\ReturnTypeWillChange]
  215. public function offsetGet($offset)
  216. {
  217. if ( ! $this->seek($offset))
  218. return NULL;
  219. return $this->current();
  220. }
  221. /**
  222. * Implements [ArrayAccess::offsetSet], throws an error.
  223. *
  224. * [!!] You cannot modify a database result.
  225. *
  226. * @param int $offset
  227. * @param mixed $value
  228. * @return void
  229. * @throws Kohana_Exception
  230. */
  231. #[\ReturnTypeWillChange]
  232. final public function offsetSet($offset, $value)
  233. {
  234. throw new Kohana_Exception('Database results are read-only');
  235. }
  236. /**
  237. * Implements [ArrayAccess::offsetUnset], throws an error.
  238. *
  239. * [!!] You cannot modify a database result.
  240. *
  241. * @param int $offset
  242. * @return void
  243. * @throws Kohana_Exception
  244. */
  245. #[\ReturnTypeWillChange]
  246. final public function offsetUnset($offset)
  247. {
  248. throw new Kohana_Exception('Database results are read-only');
  249. }
  250. /**
  251. * Implements [Iterator::key], returns the current row number.
  252. *
  253. * echo key($result);
  254. *
  255. * @return integer
  256. */
  257. #[\ReturnTypeWillChange]
  258. public function key()
  259. {
  260. return $this->_current_row;
  261. }
  262. /**
  263. * Implements [Iterator::next], moves to the next row.
  264. *
  265. * next($result);
  266. *
  267. * @return $this
  268. */
  269. #[\ReturnTypeWillChange]
  270. public function next()
  271. {
  272. ++$this->_current_row;
  273. return $this;
  274. }
  275. /**
  276. * Implements [Iterator::prev], moves to the previous row.
  277. *
  278. * prev($result);
  279. *
  280. * @return $this
  281. */
  282. public function prev()
  283. {
  284. --$this->_current_row;
  285. return $this;
  286. }
  287. /**
  288. * Implements [Iterator::rewind], sets the current row to zero.
  289. *
  290. * rewind($result);
  291. *
  292. * @return $this
  293. */
  294. #[\ReturnTypeWillChange]
  295. public function rewind()
  296. {
  297. $this->_current_row = 0;
  298. return $this;
  299. }
  300. /**
  301. * Implements [Iterator::valid], checks if the current row exists.
  302. *
  303. * [!!] This method is only used internally.
  304. *
  305. * @return boolean
  306. */
  307. #[\ReturnTypeWillChange]
  308. public function valid()
  309. {
  310. return $this->offsetExists($this->_current_row);
  311. }
  312. } // End Database_Result