dbobjectset.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Object set management
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * A set of persistent objects, could be heterogeneous
  26. *
  27. * @package iTopORM
  28. */
  29. class DBObjectSet
  30. {
  31. private $m_oFilter;
  32. private $m_aOrderBy;
  33. public $m_bLoaded;
  34. private $m_aData;
  35. private $m_aId2Row;
  36. private $m_iCurrRow;
  37. public function __construct(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array())
  38. {
  39. $this->m_oFilter = $oFilter;
  40. $this->m_aOrderBy = $aOrderBy;
  41. $this->m_aArgs = $aArgs;
  42. $this->m_bLoaded = false;
  43. $this->m_aData = array(); // array of (row => array of (classalias) => object)
  44. $this->m_aId2Row = array();
  45. $this->m_iCurrRow = 0;
  46. }
  47. public function __destruct()
  48. {
  49. }
  50. public function __toString()
  51. {
  52. $sRet = '';
  53. $this->Rewind();
  54. $sRet .= "Set (".$this->m_oFilter->ToOQL().")<br/>\n";
  55. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  56. $sRet .= $this->Count()." records<br/>\n";
  57. if ($this->Count() > 0)
  58. {
  59. $sRet .= "<ul class=\"treeview\">\n";
  60. while ($oObj = $this->Fetch())
  61. {
  62. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  63. }
  64. $sRet .= "</ul>\n";
  65. }
  66. return $sRet;
  67. }
  68. static public function FromObject($oObject)
  69. {
  70. $oRetSet = self::FromScratch(get_class($oObject));
  71. $oRetSet->AddObject($oObject);
  72. return $oRetSet;
  73. }
  74. static public function FromScratch($sClass)
  75. {
  76. $oFilter = new CMDBSearchFilter($sClass);
  77. $oRetSet = new self($oFilter);
  78. $oRetSet->m_bLoaded = true; // no DB load
  79. return $oRetSet;
  80. }
  81. // create an object set ex nihilo
  82. // input = array of objects
  83. static public function FromArray($sClass, $aObjects)
  84. {
  85. $oFilter = new CMDBSearchFilter($sClass);
  86. $oRetSet = new self($oFilter);
  87. $oRetSet->m_bLoaded = true; // no DB load
  88. $oRetSet->AddObjectArray($aObjects);
  89. return $oRetSet;
  90. }
  91. // create an object set ex nihilo
  92. // aClasses = array of (alias => class)
  93. // input = array of (array of (classalias => object))
  94. static public function FromArrayAssoc($aClasses, $aObjects)
  95. {
  96. // In a perfect world, we should create a complete tree of DBObjectSearch,
  97. // but as we lack most of the information related to the objects,
  98. // let's create one search definition
  99. $sClass = reset($aClasses);
  100. $sAlias = key($aClasses);
  101. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  102. $oRetSet = new self($oFilter);
  103. $oRetSet->m_bLoaded = true; // no DB load
  104. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  105. {
  106. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  107. }
  108. return $oRetSet;
  109. }
  110. static public function FromLinkSet($oObject, $sLinkSetAttCode, $sExtKeyToRemote)
  111. {
  112. $oLinkAttCode = MetaModel::GetAttributeDef(get_class($oObject), $sLinkSetAttCode);
  113. $oExtKeyAttDef = MetaModel::GetAttributeDef($oLinkAttCode->GetLinkedClass(), $sExtKeyToRemote);
  114. $sTargetClass = $oExtKeyAttDef->GetTargetClass();
  115. $oLinkSet = $oObject->Get($sLinkSetAttCode);
  116. $aTargets = array();
  117. while ($oLink = $oLinkSet->Fetch())
  118. {
  119. $aTargets[] = MetaModel::GetObject($sTargetClass, $oLink->Get($sExtKeyToRemote));
  120. }
  121. return self::FromArray($sTargetClass, $aTargets);
  122. }
  123. public function ToArray($bWithId = true)
  124. {
  125. $aRet = array();
  126. $this->Rewind();
  127. while ($oObject = $this->Fetch())
  128. {
  129. if ($bWithId)
  130. {
  131. $aRet[$oObject->GetKey()] = $oObject;
  132. }
  133. else
  134. {
  135. $aRet[] = $oObject;
  136. }
  137. }
  138. return $aRet;
  139. }
  140. public function GetColumnAsArray($sAttCode, $bWithId = true)
  141. {
  142. $aRet = array();
  143. $this->Rewind();
  144. while ($oObject = $this->Fetch())
  145. {
  146. if ($bWithId)
  147. {
  148. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  149. }
  150. else
  151. {
  152. $aRet[] = $oObject->Get($sAttCode);
  153. }
  154. }
  155. return $aRet;
  156. }
  157. public function GetFilter()
  158. {
  159. return $this->m_oFilter;
  160. }
  161. public function GetClass()
  162. {
  163. return $this->m_oFilter->GetClass();
  164. }
  165. public function GetSelectedClasses()
  166. {
  167. return $this->m_oFilter->GetSelectedClasses();
  168. }
  169. public function GetRootClass()
  170. {
  171. return MetaModel::GetRootClass($this->GetClass());
  172. }
  173. public function Load()
  174. {
  175. if ($this->m_bLoaded) return;
  176. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs);
  177. $resQuery = CMDBSource::Query($sSQL);
  178. if (!$resQuery) return;
  179. $sClass = $this->m_oFilter->GetClass();
  180. while ($aRow = CMDBSource::FetchArray($resQuery))
  181. {
  182. $aObjects = array();
  183. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  184. {
  185. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias);
  186. $aObjects[$sClassAlias] = $oObject;
  187. }
  188. $this->AddObjectExtended($aObjects);
  189. }
  190. CMDBSource::FreeResult($resQuery);
  191. $this->m_bLoaded = true;
  192. }
  193. public function Count()
  194. {
  195. if (!$this->m_bLoaded) $this->Load();
  196. return count($this->m_aData);
  197. }
  198. public function Fetch($sClassAlias = '')
  199. {
  200. if (!$this->m_bLoaded) $this->Load();
  201. if ($this->m_iCurrRow >= count($this->m_aData))
  202. {
  203. return null;
  204. }
  205. if (strlen($sClassAlias) == 0)
  206. {
  207. $sClassAlias = $this->m_oFilter->GetClassAlias();
  208. }
  209. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  210. $this->m_iCurrRow++;
  211. return $oRetObj;
  212. }
  213. // Return the whole line if several classes have been specified in the query
  214. //
  215. public function FetchAssoc()
  216. {
  217. if (!$this->m_bLoaded) $this->Load();
  218. if ($this->m_iCurrRow >= count($this->m_aData))
  219. {
  220. return null;
  221. }
  222. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  223. $this->m_iCurrRow++;
  224. return $aRetObjects;
  225. }
  226. public function Rewind()
  227. {
  228. $this->Seek(0);
  229. }
  230. public function Seek($iRow)
  231. {
  232. if (!$this->m_bLoaded) $this->Load();
  233. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  234. return $this->m_iCurrRow;
  235. }
  236. public function AddObject($oObject, $sClassAlias = '')
  237. {
  238. $sObjClass = get_class($oObject);
  239. if (strlen($sClassAlias) == 0)
  240. {
  241. $sClassAlias = $sObjClass;
  242. }
  243. $iNextPos = count($this->m_aData);
  244. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  245. $this->m_aId2Row[$sObjClass][$oObject->GetKey()] = $iNextPos;
  246. }
  247. protected function AddObjectExtended($aObjectArray)
  248. {
  249. $iNextPos = count($this->m_aData);
  250. foreach ($aObjectArray as $sClassAlias => $oObject)
  251. {
  252. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  253. $this->m_aId2Row[get_class($oObject)][$oObject->GetKey()] = $iNextPos;
  254. }
  255. }
  256. public function AddObjectArray($aObjects, $sClassAlias = '')
  257. {
  258. // #@# todo - add a check on the object class ?
  259. foreach ($aObjects as $oObj)
  260. {
  261. $this->AddObject($oObj, $sClassAlias);
  262. }
  263. }
  264. public function Merge($oObjectSet)
  265. {
  266. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  267. {
  268. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  269. }
  270. if (!$this->m_bLoaded) $this->Load();
  271. $oObjectSet->Seek(0);
  272. while ($oObject = $oObjectSet->Fetch())
  273. {
  274. $this->AddObject($oObject);
  275. }
  276. }
  277. public function CreateIntersect($oObjectSet)
  278. {
  279. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  280. {
  281. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  282. }
  283. if (!$this->m_bLoaded) $this->Load();
  284. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  285. $oObjectSet->Seek(0);
  286. while ($oObject = $oObjectSet->Fetch())
  287. {
  288. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  289. {
  290. $oNewSet->AddObject($oObject);
  291. }
  292. }
  293. return $oNewSet;
  294. }
  295. public function CreateDelta($oObjectSet)
  296. {
  297. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  298. {
  299. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  300. }
  301. if (!$this->m_bLoaded) $this->Load();
  302. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  303. $oObjectSet->Seek(0);
  304. while ($oObject = $oObjectSet->Fetch())
  305. {
  306. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row))
  307. {
  308. $oNewSet->AddObject($oObject);
  309. }
  310. }
  311. return $oNewSet;
  312. }
  313. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  314. {
  315. $aRelatedObjs = array();
  316. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  317. $this->Seek(0);
  318. while ($oObject = $this->Fetch())
  319. {
  320. // #@# todo - actually merge !
  321. $aRelatedObjs = array_merge_recursive($aRelatedObjs, $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited));
  322. }
  323. return $aRelatedObjs;
  324. }
  325. }
  326. ?>