DB_result.php 8.9 KB

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