DB_result.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 - 2019, 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 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://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] = $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. }