PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/DB_result.php

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