PageRenderTime 30ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/db/DAOResultFactory.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 238 lines | 124 code | 27 blank | 87 comment | 18 complexity | 97f736845f431807678f1e805dc30339 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/db/DAOResultFactory.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class DAOResultFactory
  9. * @ingroup db
  10. *
  11. * @brief Wrapper around ADORecordSet providing "factory" features for generating
  12. * objects from DAOs.
  13. */
  14. // $Id$
  15. import('core.ItemIterator');
  16. class DAOResultFactory extends ItemIterator {
  17. /** The DAO used to create objects */
  18. var $dao;
  19. /** The name of the DAO's factory function (to be called with an associative array of values) */
  20. var $functionName;
  21. /**
  22. * @var array an array of primary key field names that uniquely
  23. * identify a result row in the record set.
  24. */
  25. var $idFields;
  26. /** The ADORecordSet to be wrapped around */
  27. var $records;
  28. /** True iff the resultset was always empty */
  29. var $wasEmpty;
  30. var $isFirst;
  31. var $isLast;
  32. var $page;
  33. var $count;
  34. var $pageCount;
  35. /**
  36. * Constructor.
  37. * Initialize the DAOResultFactory
  38. * @param $records object ADO record set
  39. * @param $dao object DAO class for factory
  40. * @param $functionName The function to call on $dao to create an object
  41. * @param $idFields array an array of primary key field names that uniquely
  42. * identify a result row in the record set.
  43. * Should be data object _data array key, not database column name
  44. */
  45. function DAOResultFactory(&$records, &$dao, $functionName, $idFields = array()) {
  46. $this->functionName = $functionName;
  47. $this->dao =& $dao;
  48. $this->idFields = $idFields;
  49. if (!$records || $records->EOF) {
  50. if ($records) $records->Close();
  51. $this->records = null;
  52. $this->wasEmpty = true;
  53. $this->page = 1;
  54. $this->isFirst = true;
  55. $this->isLast = true;
  56. $this->count = 0;
  57. $this->pageCount = 1;
  58. } else {
  59. $this->records =& $records;
  60. $this->wasEmpty = false;
  61. $this->page = $records->AbsolutePage();
  62. $this->isFirst = $records->atFirstPage();
  63. $this->isLast = $records->atLastPage();
  64. $this->count = $records->MaxRecordCount();
  65. $this->pageCount = $records->LastPageNo();
  66. }
  67. }
  68. /**
  69. * Return the object representing the next row.
  70. * @return object
  71. */
  72. function &next() {
  73. if ($this->records == null) return $this->records;
  74. if (!$this->records->EOF) {
  75. $functionName = $this->functionName;
  76. $dao =& $this->dao;
  77. $row =& $this->records->getRowAssoc(false);
  78. $result =& $dao->$functionName($row);
  79. if (!$this->records->MoveNext()) $this->_cleanup();
  80. return $result;
  81. } else {
  82. $this->_cleanup();
  83. $nullVar = null;
  84. return $nullVar;
  85. }
  86. }
  87. /**
  88. * Return the next row, with key.
  89. * @return array ($key, $value)
  90. */
  91. function &nextWithKey() {
  92. $result =& $this->next();
  93. if (empty($this->idFields)) {
  94. $key = null;
  95. } else {
  96. assert(is_a($result, 'DataObject') && is_array($this->idFields));
  97. $key = '';
  98. foreach($this->idFields as $idField) {
  99. assert(!is_null($result->getData($idField)));
  100. if (!empty($key)) $key .= '-';
  101. $key .= (string)$result->getData($idField);
  102. }
  103. }
  104. $returner = array($key, &$result);
  105. return $returner;
  106. }
  107. /**
  108. * Determine whether this iterator represents the first page of a set.
  109. * @return boolean
  110. */
  111. function atFirstPage() {
  112. return $this->isFirst;
  113. }
  114. /**
  115. * Determine whether this iterator represents the last page of a set.
  116. * @return boolean
  117. */
  118. function atLastPage() {
  119. return $this->isLast;
  120. }
  121. /**
  122. * Get the page number of a set that this iterator represents.
  123. * @return int
  124. */
  125. function getPage() {
  126. return $this->page;
  127. }
  128. /**
  129. * Get the total number of items in the set.
  130. * @return int
  131. */
  132. function getCount() {
  133. return $this->count;
  134. }
  135. /**
  136. * Get the total number of pages in the set.
  137. * @return int
  138. */
  139. function getPageCount() {
  140. return $this->pageCount;
  141. }
  142. /**
  143. * Return a boolean indicating whether or not we've reached the end of results
  144. * @return boolean
  145. */
  146. function eof() {
  147. if ($this->records == null) return true;
  148. if ($this->records->EOF) {
  149. $this->_cleanup();
  150. return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * Return a boolean indicating whether or not this resultset was empty from the beginning
  156. * @return boolean
  157. */
  158. function wasEmpty() {
  159. return $this->wasEmpty;
  160. }
  161. /**
  162. * PRIVATE function used internally to clean up the record set.
  163. * This is called aggressively because it can free resources.
  164. */
  165. function _cleanup() {
  166. $this->records->close();
  167. unset($this->records);
  168. $this->records = null;
  169. }
  170. /**
  171. * Convert this iterator to an array.
  172. * @return array
  173. */
  174. function &toArray() {
  175. $returner = array();
  176. while (!$this->eof()) {
  177. $returner[] = $this->next();
  178. }
  179. return $returner;
  180. }
  181. /**
  182. * Convert this iterator to an associative array by database ID.
  183. * @return array
  184. */
  185. function &toAssociativeArray($idField) {
  186. $returner = array();
  187. while (!$this->eof()) {
  188. $result =& $this->next();
  189. $returner[$result->getData($idField)] =& $result;
  190. unset($result);
  191. }
  192. return $returner;
  193. }
  194. /**
  195. * Determine whether or not this iterator is in the range of pages for the set it represents
  196. * @return boolean
  197. */
  198. function isInBounds() {
  199. return ($this->pageCount >= $this->page);
  200. }
  201. /**
  202. * Get the RangeInfo representing the last page in the set.
  203. * @return object
  204. */
  205. function &getLastPageRangeInfo() {
  206. import('db.DBResultRange');
  207. $returner = new DBResultRange($this->count, $this->pageCount);
  208. return $returner;
  209. }
  210. }
  211. ?>