PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/bonfire/ci3/database/DB_result.php

http://github.com/ci-bonfire/Bonfire
PHP | 665 lines | 281 code | 93 blank | 291 comment | 37 complexity | 375c3accf2bbadc159d364271ddc09c4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Database Result Class
  41. *
  42. * This is the platform-independent result class.
  43. * This class will not be called directly. Rather, the adapter
  44. * class for the specific database will extend and instantiate it.
  45. *
  46. * @category Database
  47. * @author EllisLab Dev Team
  48. * @link https://codeigniter.com/user_guide/database/
  49. */
  50. class CI_DB_result {
  51. /**
  52. * Connection ID
  53. *
  54. * @var resource|object
  55. */
  56. public $conn_id;
  57. /**
  58. * Result ID
  59. *
  60. * @var resource|object
  61. */
  62. public $result_id;
  63. /**
  64. * Result Array
  65. *
  66. * @var array[]
  67. */
  68. public $result_array = array();
  69. /**
  70. * Result Object
  71. *
  72. * @var object[]
  73. */
  74. public $result_object = array();
  75. /**
  76. * Custom Result Object
  77. *
  78. * @var object[]
  79. */
  80. public $custom_result_object = array();
  81. /**
  82. * Current Row index
  83. *
  84. * @var int
  85. */
  86. public $current_row = 0;
  87. /**
  88. * Number of rows
  89. *
  90. * @var int
  91. */
  92. public $num_rows;
  93. /**
  94. * Row data
  95. *
  96. * @var array
  97. */
  98. public $row_data;
  99. // --------------------------------------------------------------------
  100. /**
  101. * Constructor
  102. *
  103. * @param object $driver_object
  104. * @return void
  105. */
  106. public function __construct(&$driver_object)
  107. {
  108. $this->conn_id = $driver_object->conn_id;
  109. $this->result_id = $driver_object->result_id;
  110. }
  111. // --------------------------------------------------------------------
  112. /**
  113. * Number of rows in the result set
  114. *
  115. * @return int
  116. */
  117. public function num_rows()
  118. {
  119. if (is_int($this->num_rows))
  120. {
  121. return $this->num_rows;
  122. }
  123. elseif (count($this->result_array) > 0)
  124. {
  125. return $this->num_rows = count($this->result_array);
  126. }
  127. elseif (count($this->result_object) > 0)
  128. {
  129. return $this->num_rows = count($this->result_object);
  130. }
  131. return $this->num_rows = count($this->result_array());
  132. }
  133. // --------------------------------------------------------------------
  134. /**
  135. * Query result. Acts as a wrapper function for the following functions.
  136. *
  137. * @param string $type 'object', 'array' or a custom class name
  138. * @return array
  139. */
  140. public function result($type = 'object')
  141. {
  142. if ($type === 'array')
  143. {
  144. return $this->result_array();
  145. }
  146. elseif ($type === 'object')
  147. {
  148. return $this->result_object();
  149. }
  150. return $this->custom_result_object($type);
  151. }
  152. // --------------------------------------------------------------------
  153. /**
  154. * Custom query result.
  155. *
  156. * @param string $class_name
  157. * @return array
  158. */
  159. public function custom_result_object($class_name)
  160. {
  161. if (isset($this->custom_result_object[$class_name]))
  162. {
  163. return $this->custom_result_object[$class_name];
  164. }
  165. elseif ( ! $this->result_id OR $this->num_rows === 0)
  166. {
  167. return array();
  168. }
  169. // Don't fetch the result set again if we already have it
  170. $_data = NULL;
  171. if (($c = count($this->result_array)) > 0)
  172. {
  173. $_data = 'result_array';
  174. }
  175. elseif (($c = count($this->result_object)) > 0)
  176. {
  177. $_data = 'result_object';
  178. }
  179. if ($_data !== NULL)
  180. {
  181. for ($i = 0; $i < $c; $i++)
  182. {
  183. $this->custom_result_object[$class_name][$i] = new $class_name();
  184. foreach ($this->{$_data}[$i] as $key => $value)
  185. {
  186. $this->custom_result_object[$class_name][$i]->$key = $value;
  187. }
  188. }
  189. return $this->custom_result_object[$class_name];
  190. }
  191. is_null($this->row_data) OR $this->data_seek(0);
  192. $this->custom_result_object[$class_name] = array();
  193. while ($row = $this->_fetch_object($class_name))
  194. {
  195. $this->custom_result_object[$class_name][] = $row;
  196. }
  197. return $this->custom_result_object[$class_name];
  198. }
  199. // --------------------------------------------------------------------
  200. /**
  201. * Query result. "object" version.
  202. *
  203. * @return array
  204. */
  205. public function result_object()
  206. {
  207. if (count($this->result_object) > 0)
  208. {
  209. return $this->result_object;
  210. }
  211. // In the event that query caching is on, the result_id variable
  212. // will not be a valid resource so we'll simply return an empty
  213. // array.
  214. if ( ! $this->result_id OR $this->num_rows === 0)
  215. {
  216. return array();
  217. }
  218. if (($c = count($this->result_array)) > 0)
  219. {
  220. for ($i = 0; $i < $c; $i++)
  221. {
  222. $this->result_object[$i] = (object) $this->result_array[$i];
  223. }
  224. return $this->result_object;
  225. }
  226. is_null($this->row_data) OR $this->data_seek(0);
  227. while ($row = $this->_fetch_object())
  228. {
  229. $this->result_object[] = $row;
  230. }
  231. return $this->result_object;
  232. }
  233. // --------------------------------------------------------------------
  234. /**
  235. * Query result. "array" version.
  236. *
  237. * @return array
  238. */
  239. public function result_array()
  240. {
  241. if (count($this->result_array) > 0)
  242. {
  243. return $this->result_array;
  244. }
  245. // In the event that query caching is on, the result_id variable
  246. // will not be a valid resource so we'll simply return an empty
  247. // array.
  248. if ( ! $this->result_id OR $this->num_rows === 0)
  249. {
  250. return array();
  251. }
  252. if (($c = count($this->result_object)) > 0)
  253. {
  254. for ($i = 0; $i < $c; $i++)
  255. {
  256. $this->result_array[$i] = (array) $this->result_object[$i];
  257. }
  258. return $this->result_array;
  259. }
  260. is_null($this->row_data) OR $this->data_seek(0);
  261. while ($row = $this->_fetch_assoc())
  262. {
  263. $this->result_array[] = $row;
  264. }
  265. return $this->result_array;
  266. }
  267. // --------------------------------------------------------------------
  268. /**
  269. * Row
  270. *
  271. * A wrapper method.
  272. *
  273. * @param mixed $n
  274. * @param string $type 'object' or 'array'
  275. * @return mixed
  276. */
  277. public function row($n = 0, $type = 'object')
  278. {
  279. if ( ! is_numeric($n))
  280. {
  281. // We cache the row data for subsequent uses
  282. is_array($this->row_data) OR $this->row_data = $this->row_array(0);
  283. // array_key_exists() instead of isset() to allow for NULL values
  284. if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data))
  285. {
  286. return NULL;
  287. }
  288. return $this->row_data[$n];
  289. }
  290. if ($type === 'object') return $this->row_object($n);
  291. elseif ($type === 'array') return $this->row_array($n);
  292. return $this->custom_row_object($n, $type);
  293. }
  294. // --------------------------------------------------------------------
  295. /**
  296. * Assigns an item into a particular column slot
  297. *
  298. * @param mixed $key
  299. * @param mixed $value
  300. * @return void
  301. */
  302. public function set_row($key, $value = NULL)
  303. {
  304. // We cache the row data for subsequent uses
  305. if ( ! is_array($this->row_data))
  306. {
  307. $this->row_data = $this->row_array(0);
  308. }
  309. if (is_array($key))
  310. {
  311. foreach ($key as $k => $v)
  312. {
  313. $this->row_data[$k] = $v;
  314. }
  315. return;
  316. }
  317. if ($key !== '' && $value !== NULL)
  318. {
  319. $this->row_data[$key] = $value;
  320. }
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Returns a single result row - custom object version
  325. *
  326. * @param int $n
  327. * @param string $type
  328. * @return object
  329. */
  330. public function custom_row_object($n, $type)
  331. {
  332. isset($this->custom_result_object[$type]) OR $this->custom_result_object($type);
  333. if (count($this->custom_result_object[$type]) === 0)
  334. {
  335. return NULL;
  336. }
  337. if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n]))
  338. {
  339. $this->current_row = $n;
  340. }
  341. return $this->custom_result_object[$type][$this->current_row];
  342. }
  343. // --------------------------------------------------------------------
  344. /**
  345. * Returns a single result row - object version
  346. *
  347. * @param int $n
  348. * @return object
  349. */
  350. public function row_object($n = 0)
  351. {
  352. $result = $this->result_object();
  353. if (count($result) === 0)
  354. {
  355. return NULL;
  356. }
  357. if ($n !== $this->current_row && isset($result[$n]))
  358. {
  359. $this->current_row = $n;
  360. }
  361. return $result[$this->current_row];
  362. }
  363. // --------------------------------------------------------------------
  364. /**
  365. * Returns a single result row - array version
  366. *
  367. * @param int $n
  368. * @return array
  369. */
  370. public function row_array($n = 0)
  371. {
  372. $result = $this->result_array();
  373. if (count($result) === 0)
  374. {
  375. return NULL;
  376. }
  377. if ($n !== $this->current_row && isset($result[$n]))
  378. {
  379. $this->current_row = $n;
  380. }
  381. return $result[$this->current_row];
  382. }
  383. // --------------------------------------------------------------------
  384. /**
  385. * Returns the "first" row
  386. *
  387. * @param string $type
  388. * @return mixed
  389. */
  390. public function first_row($type = 'object')
  391. {
  392. $result = $this->result($type);
  393. return (count($result) === 0) ? NULL : $result[0];
  394. }
  395. // --------------------------------------------------------------------
  396. /**
  397. * Returns the "last" row
  398. *
  399. * @param string $type
  400. * @return mixed
  401. */
  402. public function last_row($type = 'object')
  403. {
  404. $result = $this->result($type);
  405. return (count($result) === 0) ? NULL : $result[count($result) - 1];
  406. }
  407. // --------------------------------------------------------------------
  408. /**
  409. * Returns the "next" row
  410. *
  411. * @param string $type
  412. * @return mixed
  413. */
  414. public function next_row($type = 'object')
  415. {
  416. $result = $this->result($type);
  417. if (count($result) === 0)
  418. {
  419. return NULL;
  420. }
  421. return isset($result[$this->current_row + 1])
  422. ? $result[++$this->current_row]
  423. : NULL;
  424. }
  425. // --------------------------------------------------------------------
  426. /**
  427. * Returns the "previous" row
  428. *
  429. * @param string $type
  430. * @return mixed
  431. */
  432. public function previous_row($type = 'object')
  433. {
  434. $result = $this->result($type);
  435. if (count($result) === 0)
  436. {
  437. return NULL;
  438. }
  439. if (isset($result[$this->current_row - 1]))
  440. {
  441. --$this->current_row;
  442. }
  443. return $result[$this->current_row];
  444. }
  445. // --------------------------------------------------------------------
  446. /**
  447. * Returns an unbuffered row and move pointer to next row
  448. *
  449. * @param string $type 'array', 'object' or a custom class name
  450. * @return mixed
  451. */
  452. public function unbuffered_row($type = 'object')
  453. {
  454. if ($type === 'array')
  455. {
  456. return $this->_fetch_assoc();
  457. }
  458. elseif ($type === 'object')
  459. {
  460. return $this->_fetch_object();
  461. }
  462. return $this->_fetch_object($type);
  463. }
  464. // --------------------------------------------------------------------
  465. /**
  466. * The following methods are normally overloaded by the identically named
  467. * methods in the platform-specific driver -- except when query caching
  468. * is used. When caching is enabled we do not load the other driver.
  469. * These functions are primarily here to prevent undefined function errors
  470. * when a cached result object is in use. They are not otherwise fully
  471. * operational due to the unavailability of the database resource IDs with
  472. * cached results.
  473. */
  474. // --------------------------------------------------------------------
  475. /**
  476. * Number of fields in the result set
  477. *
  478. * Overridden by driver result classes.
  479. *
  480. * @return int
  481. */
  482. public function num_fields()
  483. {
  484. return 0;
  485. }
  486. // --------------------------------------------------------------------
  487. /**
  488. * Fetch Field Names
  489. *
  490. * Generates an array of column names.
  491. *
  492. * Overridden by driver result classes.
  493. *
  494. * @return array
  495. */
  496. public function list_fields()
  497. {
  498. return array();
  499. }
  500. // --------------------------------------------------------------------
  501. /**
  502. * Field data
  503. *
  504. * Generates an array of objects containing field meta-data.
  505. *
  506. * Overridden by driver result classes.
  507. *
  508. * @return array
  509. */
  510. public function field_data()
  511. {
  512. return array();
  513. }
  514. // --------------------------------------------------------------------
  515. /**
  516. * Free the result
  517. *
  518. * Overridden by driver result classes.
  519. *
  520. * @return void
  521. */
  522. public function free_result()
  523. {
  524. $this->result_id = FALSE;
  525. }
  526. // --------------------------------------------------------------------
  527. /**
  528. * Data Seek
  529. *
  530. * Moves the internal pointer to the desired offset. We call
  531. * this internally before fetching results to make sure the
  532. * result set starts at zero.
  533. *
  534. * Overridden by driver result classes.
  535. *
  536. * @param int $n
  537. * @return bool
  538. */
  539. public function data_seek($n = 0)
  540. {
  541. return FALSE;
  542. }
  543. // --------------------------------------------------------------------
  544. /**
  545. * Result - associative array
  546. *
  547. * Returns the result set as an array.
  548. *
  549. * Overridden by driver result classes.
  550. *
  551. * @return array
  552. */
  553. protected function _fetch_assoc()
  554. {
  555. return array();
  556. }
  557. // --------------------------------------------------------------------
  558. /**
  559. * Result - object
  560. *
  561. * Returns the result set as an object.
  562. *
  563. * Overridden by driver result classes.
  564. *
  565. * @param string $class_name
  566. * @return object
  567. */
  568. protected function _fetch_object($class_name = 'stdClass')
  569. {
  570. return new $class_name();
  571. }
  572. }