dbobjectset.class.php 6.0 KB

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