PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Base/DB/ResultSet.php

http://github.com/spadefoot/kohana-orm-leap
PHP | 362 lines | 140 code | 30 blank | 192 comment | 18 complexity | f22508dd2358c1b7aed0a9b0eb87e56a MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Copyright © 2011–2013 Spadefoot Team.
  4. *
  5. * Unless otherwise noted, LEAP is licensed under the Apache License,
  6. * Version 2.0 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License
  8. * at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * This class represents a result set.
  20. *
  21. * @package Leap
  22. * @category Connection
  23. * @version 2013-01-27
  24. *
  25. * @abstract
  26. */
  27. abstract class Base_DB_ResultSet extends Core_Object implements ArrayAccess, Countable, Iterator, SeekableIterator {
  28. /**
  29. * This variable stores the current position in the records array.
  30. *
  31. * @access protected
  32. * @var integer
  33. */
  34. protected $position;
  35. /**
  36. * This variable stores the records.
  37. *
  38. * @access protected
  39. * @var array
  40. */
  41. protected $records;
  42. /**
  43. * This variable stores the length of the records array.
  44. *
  45. * @access protected
  46. * @var integer
  47. */
  48. protected $size;
  49. /**
  50. * This variable stores the return type being used.
  51. *
  52. * @access protected
  53. * @var string
  54. */
  55. protected $type;
  56. /**
  57. * This function initializes the class by wrapping the result set so that all database
  58. * result sets are accessible alike.
  59. *
  60. * @access public
  61. * @param mixed $buffer either an array of records or a data reader
  62. * @param string $type the return type being used
  63. */
  64. public function __construct($buffer, $type = 'array') {
  65. if (is_array($buffer)) {
  66. $this->records = $buffer;
  67. $this->size = count($buffer);
  68. }
  69. else {
  70. $this->records = array();
  71. $this->size = 0;
  72. if (is_object($buffer) AND ($buffer instanceof DB_SQL_DataReader)) {
  73. while ($buffer->read()) {
  74. $this->records[] = $buffer->row($type);
  75. $this->size++;
  76. }
  77. $buffer->free();
  78. }
  79. }
  80. $this->position = 0;
  81. $this->type = $type;
  82. }
  83. /**
  84. * This function returns an array of records of the desired object type.
  85. *
  86. * @access public
  87. * @return array an array of records
  88. */
  89. public function as_array() {
  90. return $this->records;
  91. }
  92. /**
  93. * This function will create an instance of the CSV class using the data contained
  94. * in the result set.
  95. *
  96. * @access public
  97. * @param array $config the configuration array
  98. * @return CSV an instance of the CSV class
  99. */
  100. public function as_csv(Array $config = array()) {
  101. $csv = new CSV($config);
  102. if ($this->is_loaded()) {
  103. switch ($this->type) {
  104. case 'array':
  105. case 'object':
  106. foreach ($this->records as $record) {
  107. $csv->add_row( (array) $record);
  108. }
  109. break;
  110. default:
  111. if (class_exists($this->type)) {
  112. if (($this->records[0] instanceof DB_ORM_Model) OR method_exists($this->records[0], 'as_array')) {
  113. foreach ($this->records as $record) {
  114. $csv->add_row($record->as_array());
  115. }
  116. }
  117. else if ($this->records[0] instanceof Iterator) {
  118. foreach ($this->records as $record) {
  119. $row = array();
  120. foreach ($record as $column) {
  121. $row[] = $column;
  122. }
  123. $csv->add_row($row);
  124. }
  125. }
  126. else {
  127. foreach ($this->records as $record) {
  128. $csv->add_row(get_object_vars($record));
  129. }
  130. }
  131. }
  132. break;
  133. }
  134. }
  135. return $csv;
  136. }
  137. /**
  138. * This function returns the total number of records contained in result set.
  139. *
  140. * @access public
  141. * @override
  142. * @return integer the total number of records
  143. */
  144. public function count() {
  145. return $this->size;
  146. }
  147. /**
  148. * This function returns the current record.
  149. *
  150. * @access public
  151. * @override
  152. * @return mixed the current record
  153. */
  154. public function current() {
  155. return isset($this->records[$this->position]) ? $this->records[$this->position] : NULL;
  156. }
  157. /**
  158. * This function returns a record either at the current position or
  159. * the specified position.
  160. *
  161. * @access public
  162. * @param integer $index the record's index
  163. * @return mixed the record
  164. */
  165. public function fetch($index = -1) {
  166. settype($index, 'integer');
  167. if ($index < 0) {
  168. $index = $this->position;
  169. $this->position++;
  170. }
  171. if (isset($this->records[$index])) {
  172. return $this->records[$index];
  173. }
  174. return FALSE;
  175. }
  176. /**
  177. * This function frees all data stored in the result set.
  178. *
  179. * @access public
  180. */
  181. public function free() {
  182. $this->records = array();
  183. $this->position = 0;
  184. $this->size = 0;
  185. }
  186. /**
  187. * This function returns the value for the named column from the current record.
  188. *
  189. * // Gets the value of "id" from the current record
  190. * $id = $results->get('id');
  191. *
  192. * @access public
  193. * @param string $name the name of the column
  194. * @param mixed $default the default value should the column
  195. * does not exist
  196. * @return mixed the value for the named column
  197. */
  198. public function get($name, $default = NULL) {
  199. $record = $this->current();
  200. if (is_object($record)) {
  201. try {
  202. $value = $record->{$name};
  203. if ($value !== NULL) {
  204. return $value;
  205. }
  206. }
  207. catch (Exception $ex) {}
  208. }
  209. else if (is_array($record) AND isset($record[$name])) {
  210. return $record[$name];
  211. }
  212. return $default;
  213. }
  214. /**
  215. * This function returns whether any records were loaded.
  216. *
  217. * @access public
  218. * @return boolean whether any records were loaded
  219. */
  220. public function is_loaded() {
  221. return ($this->size > 0);
  222. }
  223. /**
  224. * This function returns the position to the current record.
  225. *
  226. * @access public
  227. * @override
  228. * @return integer the position of the current record
  229. */
  230. public function key() {
  231. return $this->position;
  232. }
  233. /**
  234. * This function moves forward the position to the next record, lazy loading only
  235. * when necessary.
  236. *
  237. * @access public
  238. * @override
  239. */
  240. public function next() {
  241. $this->position++;
  242. }
  243. /**
  244. * This function determines whether an offset exists.
  245. *
  246. * @access public
  247. * @override
  248. * @param integer $offset the offset to be evaluated
  249. * @return boolean whether the requested offset exists
  250. */
  251. public function offsetExists($offset) {
  252. return isset($this->records[$offset]);
  253. }
  254. /**
  255. * This functions gets value at the specified offset.
  256. *
  257. * @access public
  258. * @override
  259. * @param integer $offset the offset to be fetched
  260. * @return mixed the value at the specified offset
  261. */
  262. public function offsetGet($offset) {
  263. return isset($this->records[$offset]) ? $this->records[$offset] : NULL;
  264. }
  265. /**
  266. * This functions sets the specified value at the specified offset.
  267. *
  268. * @access public
  269. * @override
  270. * @param integer $offset the offset to be set
  271. * @param mixed $value the value to be set
  272. * @throws Throwable_UnimplementedMethod_Exception indicates the result cannot be modified
  273. */
  274. public function offsetSet($offset, $value) {
  275. throw new Throwable_UnimplementedMethod_Exception('Message: Invalid call to member function. Reason: Result set cannot be modified.', array(':offset' => $offset, ':value' => $value));
  276. }
  277. /**
  278. * This functions allows for the specified offset to be unset.
  279. *
  280. * @access public
  281. * @override
  282. * @param integer $offset the offset to be unset
  283. * @throws Throwable_UnimplementedMethod_Exception indicates the result cannot be modified
  284. */
  285. public function offsetUnset($offset) {
  286. throw new Throwable_UnimplementedMethod_Exception('Message: Invalid call to member function. Reason: Result set cannot be modified.', array(':offset' => $offset));
  287. }
  288. /**
  289. * This function returns the current iterator position.
  290. *
  291. * @access public
  292. * @override
  293. * @return integer the current iterator position
  294. */
  295. public function position() {
  296. return $this->position;
  297. }
  298. /**
  299. * This function rewinds the iterator back to starting position.
  300. *
  301. * @access public
  302. * @override
  303. */
  304. public function rewind() {
  305. $this->position = 0;
  306. }
  307. /**
  308. * This function sets the position pointer to the seeked position.
  309. *
  310. * @access public
  311. * @override
  312. * @param integer $position the seeked position
  313. * @throws Throwable_OutOfBounds_Exception indicates that the seeked position
  314. * is out of bounds
  315. */
  316. public function seek($position) {
  317. if ( ! isset($this->records[$position])) {
  318. throw new Throwable_OutOfBounds_Exception('Message: Invalid array position. Reason: The specified position is out of bounds.', array(':position' => $position, ':count' => $this->size));
  319. }
  320. $this->position = $position;
  321. }
  322. /**
  323. * This function checks if the current iterator position is valid.
  324. *
  325. * @access public
  326. * @override
  327. * @return boolean whether the current iterator position is valid
  328. */
  329. public function valid() {
  330. return isset($this->records[$this->position]);
  331. }
  332. }