/halogy/database/DB_result.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 342 lines · 161 code · 54 blank · 127 comment · 36 complexity · d7ae489e6884374aebd0391b54ff843a MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Database Result Class
  18. *
  19. * This is the platform-independent result class.
  20. * This class will not be called directly. Rather, the adapter
  21. * class for the specific database will extend and instantiate it.
  22. *
  23. * @category Database
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/database/
  26. */
  27. class CI_DB_result {
  28. var $conn_id = NULL;
  29. var $result_id = NULL;
  30. var $result_array = array();
  31. var $result_object = array();
  32. var $current_row = 0;
  33. var $num_rows = 0;
  34. var $row_data = NULL;
  35. /**
  36. * Query result. Acts as a wrapper function for the following functions.
  37. *
  38. * @access public
  39. * @param string can be "object" or "array"
  40. * @return mixed either a result object or array
  41. */
  42. function result($type = 'object')
  43. {
  44. return ($type == 'object') ? $this->result_object() : $this->result_array();
  45. }
  46. // --------------------------------------------------------------------
  47. /**
  48. * Query result. "object" version.
  49. *
  50. * @access public
  51. * @return object
  52. */
  53. function result_object()
  54. {
  55. if (count($this->result_object) > 0)
  56. {
  57. return $this->result_object;
  58. }
  59. // In the event that query caching is on the result_id variable
  60. // will return FALSE since there isn't a valid SQL resource so
  61. // we'll simply return an empty array.
  62. if ($this->result_id === FALSE OR $this->num_rows() == 0)
  63. {
  64. return array();
  65. }
  66. $this->_data_seek(0);
  67. while ($row = $this->_fetch_object())
  68. {
  69. $this->result_object[] = $row;
  70. }
  71. return $this->result_object;
  72. }
  73. // --------------------------------------------------------------------
  74. /**
  75. * Query result. "array" version.
  76. *
  77. * @access public
  78. * @return array
  79. */
  80. function result_array()
  81. {
  82. if (count($this->result_array) > 0)
  83. {
  84. return $this->result_array;
  85. }
  86. // In the event that query caching is on the result_id variable
  87. // will return FALSE since there isn't a valid SQL resource so
  88. // we'll simply return an empty array.
  89. if ($this->result_id === FALSE OR $this->num_rows() == 0)
  90. {
  91. return array();
  92. }
  93. $this->_data_seek(0);
  94. while ($row = $this->_fetch_assoc())
  95. {
  96. $this->result_array[] = $row;
  97. }
  98. return $this->result_array;
  99. }
  100. // --------------------------------------------------------------------
  101. /**
  102. * Query result. Acts as a wrapper function for the following functions.
  103. *
  104. * @access public
  105. * @param string
  106. * @param string can be "object" or "array"
  107. * @return mixed either a result object or array
  108. */
  109. function row($n = 0, $type = 'object')
  110. {
  111. if ( ! is_numeric($n))
  112. {
  113. // We cache the row data for subsequent uses
  114. if ( ! is_array($this->row_data))
  115. {
  116. $this->row_data = $this->row_array(0);
  117. }
  118. // array_key_exists() instead of isset() to allow for MySQL NULL values
  119. if (array_key_exists($n, $this->row_data))
  120. {
  121. return $this->row_data[$n];
  122. }
  123. // reset the $n variable if the result was not achieved
  124. $n = 0;
  125. }
  126. return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Assigns an item into a particular column slot
  131. *
  132. * @access public
  133. * @return object
  134. */
  135. function set_row($key, $value = NULL)
  136. {
  137. // We cache the row data for subsequent uses
  138. if ( ! is_array($this->row_data))
  139. {
  140. $this->row_data = $this->row_array(0);
  141. }
  142. if (is_array($key))
  143. {
  144. foreach ($key as $k => $v)
  145. {
  146. $this->row_data[$k] = $v;
  147. }
  148. return;
  149. }
  150. if ($key != '' AND ! is_null($value))
  151. {
  152. $this->row_data[$key] = $value;
  153. }
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Returns a single result row - object version
  158. *
  159. * @access public
  160. * @return object
  161. */
  162. function row_object($n = 0)
  163. {
  164. $result = $this->result_object();
  165. if (count($result) == 0)
  166. {
  167. return $result;
  168. }
  169. if ($n != $this->current_row AND isset($result[$n]))
  170. {
  171. $this->current_row = $n;
  172. }
  173. return $result[$this->current_row];
  174. }
  175. // --------------------------------------------------------------------
  176. /**
  177. * Returns a single result row - array version
  178. *
  179. * @access public
  180. * @return array
  181. */
  182. function row_array($n = 0)
  183. {
  184. $result = $this->result_array();
  185. if (count($result) == 0)
  186. {
  187. return $result;
  188. }
  189. if ($n != $this->current_row AND isset($result[$n]))
  190. {
  191. $this->current_row = $n;
  192. }
  193. return $result[$this->current_row];
  194. }
  195. // --------------------------------------------------------------------
  196. /**
  197. * Returns the "first" row
  198. *
  199. * @access public
  200. * @return object
  201. */
  202. function first_row($type = 'object')
  203. {
  204. $result = $this->result($type);
  205. if (count($result) == 0)
  206. {
  207. return $result;
  208. }
  209. return $result[0];
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Returns the "last" row
  214. *
  215. * @access public
  216. * @return object
  217. */
  218. function last_row($type = 'object')
  219. {
  220. $result = $this->result($type);
  221. if (count($result) == 0)
  222. {
  223. return $result;
  224. }
  225. return $result[count($result) -1];
  226. }
  227. // --------------------------------------------------------------------
  228. /**
  229. * Returns the "next" row
  230. *
  231. * @access public
  232. * @return object
  233. */
  234. function next_row($type = 'object')
  235. {
  236. $result = $this->result($type);
  237. if (count($result) == 0)
  238. {
  239. return $result;
  240. }
  241. if (isset($result[$this->current_row + 1]))
  242. {
  243. ++$this->current_row;
  244. }
  245. return $result[$this->current_row];
  246. }
  247. // --------------------------------------------------------------------
  248. /**
  249. * Returns the "previous" row
  250. *
  251. * @access public
  252. * @return object
  253. */
  254. function previous_row($type = 'object')
  255. {
  256. $result = $this->result($type);
  257. if (count($result) == 0)
  258. {
  259. return $result;
  260. }
  261. if (isset($result[$this->current_row - 1]))
  262. {
  263. --$this->current_row;
  264. }
  265. return $result[$this->current_row];
  266. }
  267. // --------------------------------------------------------------------
  268. /**
  269. * The following functions are normally overloaded by the identically named
  270. * methods in the platform-specific driver -- except when query caching
  271. * is used. When caching is enabled we do not load the other driver.
  272. * These functions are primarily here to prevent undefined function errors
  273. * when a cached result object is in use. They are not otherwise fully
  274. * operational due to the unavailability of the database resource IDs with
  275. * cached results.
  276. */
  277. function num_rows() { return $this->num_rows; }
  278. function num_fields() { return 0; }
  279. function list_fields() { return array(); }
  280. function field_data() { return array(); }
  281. function free_result() { return TRUE; }
  282. function _data_seek() { return TRUE; }
  283. function _fetch_assoc() { return array(); }
  284. function _fetch_object() { return array(); }
  285. }
  286. // END DB_result class
  287. /* End of file DB_result.php */
  288. /* Location: ./system/database/DB_result.php */