PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/DB_result.php

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