dbobjectset.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array())
  22. {
  23. $this->m_oFilter = $oFilter;
  24. $this->m_aOrderBy = $aOrderBy;
  25. $this->m_aArgs = $aArgs;
  26. $this->m_bLoaded = false;
  27. $this->m_aData = array();
  28. $this->m_aId2Row = array();
  29. $this->m_iCurrRow = 0;
  30. }
  31. public function __destruct()
  32. {
  33. }
  34. public function __toString()
  35. {
  36. $sRet = '';
  37. $this->Rewind();
  38. $sRet .= "Set (".$this->m_oFilter->ToSibuSQL().")<br/>\n";
  39. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  40. $sRet .= $this->Count()." records<br/>\n";
  41. if ($this->Count() > 0)
  42. {
  43. $sRet .= "<ul class=\"treeview\">\n";
  44. while ($oObj = $this->Fetch())
  45. {
  46. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  47. }
  48. $sRet .= "</ul>\n";
  49. }
  50. return $sRet;
  51. }
  52. static public function FromObject($oObject)
  53. {
  54. $oRetSet = self::FromScratch(get_class($oObject));
  55. $oRetSet->AddObject($oObject);
  56. return $oRetSet;
  57. }
  58. static public function FromScratch($sClass)
  59. {
  60. $oFilter = new CMDBSearchFilter($sClass);
  61. $oRetSet = new self($oFilter);
  62. $oRetSet->m_bLoaded = true; // no DB load
  63. return $oRetSet;
  64. }
  65. static public function FromArray($sClass, $aObjects)
  66. {
  67. $oFilter = new CMDBSearchFilter($sClass);
  68. $oRetSet = new self($oFilter);
  69. $oRetSet->m_bLoaded = true; // no DB load
  70. $oRetSet->AddObjectArray($aObjects);
  71. return $oRetSet;
  72. }
  73. public function ToArray($bWithId = true)
  74. {
  75. $aRet = array();
  76. $this->Rewind();
  77. while ($oObject = $this->Fetch())
  78. {
  79. if ($bWithId)
  80. {
  81. $aRet[$oObject->GetKey()] = $oObject;
  82. }
  83. else
  84. {
  85. $aRet[] = $oObject;
  86. }
  87. }
  88. return $aRet;
  89. }
  90. public function GetColumnAsArray($sAttCode, $bWithId = true)
  91. {
  92. $aRet = array();
  93. $this->Rewind();
  94. while ($oObject = $this->Fetch())
  95. {
  96. if ($bWithId)
  97. {
  98. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  99. }
  100. else
  101. {
  102. $aRet[] = $oObject->Get($sAttCode);
  103. }
  104. }
  105. return $aRet;
  106. }
  107. public function GetFilter()
  108. {
  109. return $this->m_oFilter;
  110. }
  111. public function GetClass()
  112. {
  113. return $this->m_oFilter->GetClass();
  114. }
  115. public function GetRootClass()
  116. {
  117. return MetaModel::GetRootClass($this->GetClass());
  118. }
  119. public function Load()
  120. {
  121. if ($this->m_bLoaded) return;
  122. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs);
  123. $resQuery = CMDBSource::Query($sSQL);
  124. if (!$resQuery) return;
  125. while ($aRow = CMDBSource::FetchArray($resQuery))
  126. {
  127. $sClass = $this->m_oFilter->GetClass();
  128. $oObject = MetaModel::GetObjectByRow($sClass, $aRow);
  129. $this->AddObject($oObject);
  130. }
  131. CMDBSource::FreeResult($resQuery);
  132. $this->m_bLoaded = true;
  133. }
  134. public function Count()
  135. {
  136. if (!$this->m_bLoaded) $this->Load();
  137. return count($this->m_aData);
  138. }
  139. public function Fetch()
  140. {
  141. if (!$this->m_bLoaded) $this->Load();
  142. if ($this->m_iCurrRow >= count($this->m_aData))
  143. {
  144. return null;
  145. }
  146. $oRetObj = $this->m_aData[$this->m_iCurrRow];
  147. $this->m_iCurrRow++;
  148. return $oRetObj;
  149. }
  150. public function Rewind()
  151. {
  152. $this->Seek(0);
  153. }
  154. public function Seek($iRow)
  155. {
  156. if (!$this->m_bLoaded) $this->Load();
  157. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  158. return $this->m_iCurrRow;
  159. }
  160. public function AddObject($oObject)
  161. {
  162. // ?usefull? if ($oObject->GetClass() != $this->GetClass()) return;
  163. // it is mandatory to avoid duplicates
  164. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row)) return;
  165. // Do not load here, because the load uses that method too
  166. $iNextPos = count($this->m_aData);
  167. $this->m_aData[$iNextPos] = $oObject;
  168. $this->m_aId2Row[$oObject->GetKey()] = $iNextPos;
  169. }
  170. public function AddObjectArray($aObjects)
  171. {
  172. foreach ($aObjects as $oObj)
  173. {
  174. $this->AddObject($oObj);
  175. }
  176. }
  177. public function Merge($oObjectSet)
  178. {
  179. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  180. {
  181. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  182. }
  183. if (!$this->m_bLoaded) $this->Load();
  184. $oObjectSet->Seek(0);
  185. while ($oObject = $oObjectSet->Fetch())
  186. {
  187. $this->AddObject($oObject);
  188. }
  189. }
  190. public function CreateIntersect($oObjectSet)
  191. {
  192. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  193. {
  194. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  195. }
  196. if (!$this->m_bLoaded) $this->Load();
  197. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  198. $oObjectSet->Seek(0);
  199. while ($oObject = $oObjectSet->Fetch())
  200. {
  201. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  202. {
  203. $oNewSet->AddObject($oObject);
  204. }
  205. }
  206. return $oNewSet;
  207. }
  208. public function CreateDelta($oObjectSet)
  209. {
  210. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  211. {
  212. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  213. }
  214. if (!$this->m_bLoaded) $this->Load();
  215. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  216. $oObjectSet->Seek(0);
  217. while ($oObject = $oObjectSet->Fetch())
  218. {
  219. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  220. {
  221. $oNewSet->AddObject($oObject);
  222. }
  223. }
  224. return $oNewSet;
  225. }
  226. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  227. {
  228. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  229. $this->Seek(0);
  230. while ($oObject = $this->Fetch())
  231. {
  232. $aRelatedObjs = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  233. }
  234. return $aRelatedObjs;
  235. }
  236. }
  237. ?>