dbobjectset.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * A set of persistent objects, could be heterogeneous
  4. *
  5. * @package iTopORM
  6. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  7. * @author Denis Flaven <denisflave@free.fr>
  8. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9. * @link www.itop.com
  10. * @since 1.0
  11. * @version 1.1.1.1 $
  12. */
  13. class DBObjectSet
  14. {
  15. private $m_oFilter;
  16. private $m_aOrderBy;
  17. public $m_bLoaded;
  18. private $m_aData;
  19. private $m_aId2Row;
  20. private $m_iCurrRow;
  21. public function __construct($oFilter, $aOrderBy = array())
  22. {
  23. $this->m_oFilter = $oFilter;
  24. $this->m_aOrderBy = $aOrderBy;
  25. $this->m_bLoaded = false;
  26. $this->m_aData = array();
  27. $this->m_aId2Row = array();
  28. $this->m_iCurrRow = 0;
  29. }
  30. public function __destruct()
  31. {
  32. }
  33. public function __toString()
  34. {
  35. $sRet = '';
  36. $this->Rewind();
  37. $sRet .= "Set (".$this->m_oFilter->ToSibuSQL().")<br/>\n";
  38. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  39. $sRet .= $this->Count()." records<br/>\n";
  40. if ($this->Count() > 0)
  41. {
  42. $sRet .= "<ul class=\"treeview\">\n";
  43. while ($oObj = $this->Fetch())
  44. {
  45. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  46. }
  47. $sRet .= "</ul>\n";
  48. }
  49. return $sRet;
  50. }
  51. static public function FromScratch($sClass)
  52. {
  53. $oFilter = new CMDBSearchFilter($sClass);
  54. $oRetSet = new self($oFilter);
  55. $oRetSet->m_bLoaded = true; // no DB load
  56. return $oRetSet;
  57. }
  58. static public function FromArray($sClass, $aObjects)
  59. {
  60. $oFilter = new CMDBSearchFilter($sClass);
  61. $oRetSet = new self($oFilter);
  62. $oRetSet->m_bLoaded = true; // no DB load
  63. $oRetSet->AddObjectArray($aObjects);
  64. return $oRetSet;
  65. }
  66. public function ToArray($bWithId = true)
  67. {
  68. $aRet = array();
  69. $this->Rewind();
  70. while ($oObject = $this->Fetch())
  71. {
  72. if ($bWithId)
  73. {
  74. $aRet[$oObject->GetKey()] = $oObject;
  75. }
  76. else
  77. {
  78. $aRet[] = $oObject;
  79. }
  80. }
  81. return $aRet;
  82. }
  83. public function GetFilter()
  84. {
  85. return $this->m_oFilter;
  86. }
  87. public function GetClass()
  88. {
  89. return $this->m_oFilter->GetClass();
  90. }
  91. public function GetRootClass()
  92. {
  93. return MetaModel::GetRootClass($this->GetClass());
  94. }
  95. public function Load()
  96. {
  97. if ($this->m_bLoaded) return;
  98. // #@# debug - echo "Loading (".$this->m_oFilter->ToSibuSQL().")....</br>\n";
  99. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy);
  100. $resQuery = CMDBSource::Query($sSQL);
  101. if (!$resQuery) return;
  102. while ($aRow = CMDBSource::FetchArray($resQuery))
  103. {
  104. $sClass = $this->m_oFilter->GetClass();
  105. $oObject = MetaModel::GetObjectByRow($sClass, $aRow);
  106. $this->AddObject($oObject);
  107. }
  108. CMDBSource::FreeResult($resQuery);
  109. $this->m_bLoaded = true;
  110. }
  111. public function Count()
  112. {
  113. if (!$this->m_bLoaded) $this->Load();
  114. return count($this->m_aData);
  115. }
  116. public function Fetch()
  117. {
  118. if (!$this->m_bLoaded) $this->Load();
  119. if ($this->m_iCurrRow >= count($this->m_aData))
  120. {
  121. return null;
  122. }
  123. $oRetObj = $this->m_aData[$this->m_iCurrRow];
  124. $this->m_iCurrRow++;
  125. return $oRetObj;
  126. }
  127. public function Rewind()
  128. {
  129. $this->Seek(0);
  130. }
  131. public function Seek($iRow)
  132. {
  133. if (!$this->m_bLoaded) $this->Load();
  134. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  135. return $this->m_iCurrRow;
  136. }
  137. public function AddObject($oObject)
  138. {
  139. // ?usefull? if ($oObject->GetClass() != $this->GetClass()) return;
  140. // it is mandatory to avoid duplicates
  141. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row)) return;
  142. // Do not load here, because the load uses that method too
  143. $iNextPos = count($this->m_aData);
  144. $this->m_aData[$iNextPos] = $oObject;
  145. $this->m_aId2Row[$oObject->GetKey()] = $iNextPos;
  146. }
  147. public function AddObjectArray($aObjects)
  148. {
  149. foreach ($aObjects as $oObj)
  150. {
  151. $this->AddObject($oObj);
  152. }
  153. }
  154. public function Merge($oObjectSet)
  155. {
  156. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  157. {
  158. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  159. }
  160. if (!$this->m_bLoaded) $this->Load();
  161. $oObjectSet->Seek(0);
  162. while ($oObject = $oObjectSet->Fetch())
  163. {
  164. $this->AddObject($oObject);
  165. }
  166. }
  167. public function CreateIntersect($oObjectSet)
  168. {
  169. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  170. {
  171. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  172. }
  173. if (!$this->m_bLoaded) $this->Load();
  174. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  175. $oObjectSet->Seek(0);
  176. while ($oObject = $oObjectSet->Fetch())
  177. {
  178. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  179. {
  180. $oNewSet->AddObject($oObject);
  181. }
  182. }
  183. return $oNewSet;
  184. }
  185. public function CreateDelta($oObjectSet)
  186. {
  187. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  188. {
  189. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  190. }
  191. if (!$this->m_bLoaded) $this->Load();
  192. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  193. $oObjectSet->Seek(0);
  194. while ($oObject = $oObjectSet->Fetch())
  195. {
  196. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  197. {
  198. $oNewSet->AddObject($oObject);
  199. }
  200. }
  201. return $oNewSet;
  202. }
  203. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  204. {
  205. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  206. $this->Seek(0);
  207. while ($oObject = $this->Fetch())
  208. {
  209. $aRelatedObjs = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  210. }
  211. return $aRelatedObjs;
  212. }
  213. }
  214. ?>