dbobjectset.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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(), $iLimitCount = 0, $iLimitStart = 0)
  38. {
  39. $this->m_oFilter = $oFilter;
  40. $this->m_aOrderBy = $aOrderBy;
  41. $this->m_aArgs = $aArgs;
  42. $this->m_iLimitCount = $iLimitCount;
  43. $this->m_iLimitStart = $iLimitStart;
  44. $this->m_bLoaded = false;
  45. $this->m_aData = array(); // array of (row => array of (classalias) => object)
  46. $this->m_aId2Row = array();
  47. $this->m_iCurrRow = 0;
  48. }
  49. public function __destruct()
  50. {
  51. }
  52. public function __toString()
  53. {
  54. $sRet = '';
  55. $this->Rewind();
  56. $sRet .= "Set (".$this->m_oFilter->ToOQL().")<br/>\n";
  57. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  58. $sRet .= $this->Count()." records<br/>\n";
  59. if ($this->Count() > 0)
  60. {
  61. $sRet .= "<ul class=\"treeview\">\n";
  62. while ($oObj = $this->Fetch())
  63. {
  64. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  65. }
  66. $sRet .= "</ul>\n";
  67. }
  68. return $sRet;
  69. }
  70. static public function FromObject($oObject)
  71. {
  72. $oRetSet = self::FromScratch(get_class($oObject));
  73. $oRetSet->AddObject($oObject);
  74. return $oRetSet;
  75. }
  76. static public function FromScratch($sClass)
  77. {
  78. $oFilter = new CMDBSearchFilter($sClass);
  79. $oRetSet = new self($oFilter);
  80. $oRetSet->m_bLoaded = true; // no DB load
  81. return $oRetSet;
  82. }
  83. // create an object set ex nihilo
  84. // input = array of objects
  85. static public function FromArray($sClass, $aObjects)
  86. {
  87. $oFilter = new CMDBSearchFilter($sClass);
  88. $oRetSet = new self($oFilter);
  89. $oRetSet->m_bLoaded = true; // no DB load
  90. $oRetSet->AddObjectArray($aObjects, $sClass);
  91. return $oRetSet;
  92. }
  93. // create an object set ex nihilo
  94. // aClasses = array of (alias => class)
  95. // input = array of (array of (classalias => object))
  96. static public function FromArrayAssoc($aClasses, $aObjects)
  97. {
  98. // In a perfect world, we should create a complete tree of DBObjectSearch,
  99. // but as we lack most of the information related to the objects,
  100. // let's create one search definition
  101. $sClass = reset($aClasses);
  102. $sAlias = key($aClasses);
  103. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  104. $oRetSet = new self($oFilter);
  105. $oRetSet->m_bLoaded = true; // no DB load
  106. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  107. {
  108. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  109. }
  110. return $oRetSet;
  111. }
  112. static public function FromLinkSet($oObject, $sLinkSetAttCode, $sExtKeyToRemote)
  113. {
  114. $oLinkAttCode = MetaModel::GetAttributeDef(get_class($oObject), $sLinkSetAttCode);
  115. $oExtKeyAttDef = MetaModel::GetAttributeDef($oLinkAttCode->GetLinkedClass(), $sExtKeyToRemote);
  116. $sTargetClass = $oExtKeyAttDef->GetTargetClass();
  117. $oLinkSet = $oObject->Get($sLinkSetAttCode);
  118. $aTargets = array();
  119. while ($oLink = $oLinkSet->Fetch())
  120. {
  121. $aTargets[] = MetaModel::GetObject($sTargetClass, $oLink->Get($sExtKeyToRemote));
  122. }
  123. return self::FromArray($sTargetClass, $aTargets);
  124. }
  125. public function ToArray($bWithId = true)
  126. {
  127. $aRet = array();
  128. $this->Rewind();
  129. while ($oObject = $this->Fetch())
  130. {
  131. if ($bWithId)
  132. {
  133. $aRet[$oObject->GetKey()] = $oObject;
  134. }
  135. else
  136. {
  137. $aRet[] = $oObject;
  138. }
  139. }
  140. return $aRet;
  141. }
  142. public function GetColumnAsArray($sAttCode, $bWithId = true)
  143. {
  144. $aRet = array();
  145. $this->Rewind();
  146. while ($oObject = $this->Fetch())
  147. {
  148. if ($bWithId)
  149. {
  150. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  151. }
  152. else
  153. {
  154. $aRet[] = $oObject->Get($sAttCode);
  155. }
  156. }
  157. return $aRet;
  158. }
  159. public function GetFilter()
  160. {
  161. return $this->m_oFilter;
  162. }
  163. public function GetClass()
  164. {
  165. return $this->m_oFilter->GetClass();
  166. }
  167. public function GetSelectedClasses()
  168. {
  169. return $this->m_oFilter->GetSelectedClasses();
  170. }
  171. public function GetRootClass()
  172. {
  173. return MetaModel::GetRootClass($this->GetClass());
  174. }
  175. public function SetLimit($iLimitCount, $iLimitStart = 0)
  176. {
  177. $this->m_iLimitCount = $iLimitCount;
  178. $this->m_iLimitStart = $iLimitStart;
  179. }
  180. public function GetLimitCount()
  181. {
  182. return $this->m_iLimitCount;
  183. }
  184. public function GetLimitStart()
  185. {
  186. return $this->m_iLimitStart;
  187. }
  188. public function Load()
  189. {
  190. if ($this->m_bLoaded) return;
  191. if ($this->m_iLimitCount > 0)
  192. {
  193. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_iLimitCount, $this->m_iLimitStart);
  194. }
  195. else
  196. {
  197. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs);
  198. }
  199. $resQuery = CMDBSource::Query($sSQL);
  200. if (!$resQuery) return;
  201. $sClass = $this->m_oFilter->GetClass();
  202. while ($aRow = CMDBSource::FetchArray($resQuery))
  203. {
  204. $aObjects = array();
  205. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  206. {
  207. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias);
  208. $aObjects[$sClassAlias] = $oObject;
  209. }
  210. $this->AddObjectExtended($aObjects);
  211. }
  212. CMDBSource::FreeResult($resQuery);
  213. $this->m_bLoaded = true;
  214. }
  215. public function Count()
  216. {
  217. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, 0, 0, true);
  218. $resQuery = CMDBSource::Query($sSQL);
  219. if (!$resQuery) return 0;
  220. $aRow = CMDBSource::FetchArray($resQuery);
  221. CMDBSource::FreeResult($resQuery);
  222. return $aRow['COUNT'];
  223. }
  224. public function Fetch($sClassAlias = '')
  225. {
  226. if (!$this->m_bLoaded) $this->Load();
  227. if ($this->m_iCurrRow >= count($this->m_aData))
  228. {
  229. return null;
  230. }
  231. if (strlen($sClassAlias) == 0)
  232. {
  233. $sClassAlias = $this->m_oFilter->GetClassAlias();
  234. }
  235. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  236. $this->m_iCurrRow++;
  237. return $oRetObj;
  238. }
  239. // Return the whole line if several classes have been specified in the query
  240. //
  241. public function FetchAssoc()
  242. {
  243. if (!$this->m_bLoaded) $this->Load();
  244. if ($this->m_iCurrRow >= count($this->m_aData))
  245. {
  246. return null;
  247. }
  248. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  249. $this->m_iCurrRow++;
  250. return $aRetObjects;
  251. }
  252. public function Rewind()
  253. {
  254. $this->Seek(0);
  255. }
  256. public function Seek($iRow)
  257. {
  258. if (!$this->m_bLoaded) $this->Load();
  259. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  260. return $this->m_iCurrRow;
  261. }
  262. public function AddObject($oObject, $sClassAlias = '')
  263. {
  264. if (strlen($sClassAlias) == 0)
  265. {
  266. $sClassAlias = $this->m_oFilter->GetClassAlias();
  267. }
  268. $iNextPos = count($this->m_aData);
  269. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  270. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  271. }
  272. protected function AddObjectExtended($aObjectArray)
  273. {
  274. $iNextPos = count($this->m_aData);
  275. foreach ($aObjectArray as $sClassAlias => $oObject)
  276. {
  277. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  278. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  279. }
  280. }
  281. public function AddObjectArray($aObjects, $sClassAlias = '')
  282. {
  283. // #@# todo - add a check on the object class ?
  284. foreach ($aObjects as $oObj)
  285. {
  286. $this->AddObject($oObj, $sClassAlias);
  287. }
  288. }
  289. public function Merge($oObjectSet)
  290. {
  291. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  292. {
  293. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  294. }
  295. if (!$this->m_bLoaded) $this->Load();
  296. $oObjectSet->Seek(0);
  297. while ($oObject = $oObjectSet->Fetch())
  298. {
  299. $this->AddObject($oObject);
  300. }
  301. }
  302. public function CreateIntersect($oObjectSet)
  303. {
  304. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  305. {
  306. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  307. }
  308. if (!$this->m_bLoaded) $this->Load();
  309. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  310. $sClassAlias = $this->m_oFilter->GetClassAlias();
  311. $oObjectSet->Seek(0);
  312. while ($oObject = $oObjectSet->Fetch())
  313. {
  314. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  315. {
  316. $oNewSet->AddObject($oObject);
  317. }
  318. }
  319. return $oNewSet;
  320. }
  321. public function CreateDelta($oObjectSet)
  322. {
  323. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  324. {
  325. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  326. }
  327. if (!$this->m_bLoaded) $this->Load();
  328. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  329. $sClassAlias = $this->m_oFilter->GetClassAlias();
  330. $oObjectSet->Seek(0);
  331. while ($oObject = $oObjectSet->Fetch())
  332. {
  333. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  334. {
  335. $oNewSet->AddObject($oObject);
  336. }
  337. }
  338. return $oNewSet;
  339. }
  340. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  341. {
  342. $aRelatedObjs = array();
  343. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  344. $this->Seek(0);
  345. while ($oObject = $this->Fetch())
  346. {
  347. $aMore = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  348. foreach ($aMore as $sClass => $aRelated)
  349. {
  350. foreach ($aRelated as $iObj => $oObj)
  351. {
  352. if (!isset($aRelatedObjs[$sClass][$iObj]))
  353. {
  354. $aRelatedObjs[$sClass][$iObj] = $oObj;
  355. }
  356. }
  357. }
  358. }
  359. return $aRelatedObjs;
  360. }
  361. }
  362. ?>