dbobjectset.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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(), $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0)
  38. {
  39. $this->m_oFilter = $oFilter;
  40. $this->m_aOrderBy = $aOrderBy;
  41. $this->m_aArgs = $aArgs;
  42. $this->m_aExtendedDataSpec = $aExtendedDataSpec;
  43. $this->m_iLimitCount = $iLimitCount;
  44. $this->m_iLimitStart = $iLimitStart;
  45. $this->m_bLoaded = false; // true when the filter has been used OR the set is built step by step (AddObject...)
  46. $this->m_aData = array(); // array of (row => array of (classalias) => object/null)
  47. $this->m_aId2Row = array(); // array of (pkey => index in m_aData)
  48. $this->m_iCurrRow = 0;
  49. }
  50. public function __destruct()
  51. {
  52. }
  53. public function __toString()
  54. {
  55. $sRet = '';
  56. $this->Rewind();
  57. $sRet .= "Set (".$this->m_oFilter->ToOQL().")<br/>\n";
  58. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  59. $sRet .= $this->Count()." records<br/>\n";
  60. if ($this->Count() > 0)
  61. {
  62. $sRet .= "<ul class=\"treeview\">\n";
  63. while ($oObj = $this->Fetch())
  64. {
  65. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  66. }
  67. $sRet .= "</ul>\n";
  68. }
  69. return $sRet;
  70. }
  71. static public function FromObject($oObject)
  72. {
  73. $oRetSet = self::FromScratch(get_class($oObject));
  74. $oRetSet->AddObject($oObject);
  75. return $oRetSet;
  76. }
  77. static public function FromScratch($sClass)
  78. {
  79. $oFilter = new CMDBSearchFilter($sClass);
  80. $oRetSet = new self($oFilter);
  81. $oRetSet->m_bLoaded = true; // no DB load
  82. return $oRetSet;
  83. }
  84. // create an object set ex nihilo
  85. // input = array of objects
  86. static public function FromArray($sClass, $aObjects)
  87. {
  88. $oFilter = new CMDBSearchFilter($sClass);
  89. $oRetSet = new self($oFilter);
  90. $oRetSet->m_bLoaded = true; // no DB load
  91. $oRetSet->AddObjectArray($aObjects, $sClass);
  92. return $oRetSet;
  93. }
  94. // create an object set ex nihilo
  95. // aClasses = array of (alias => class)
  96. // input = array of (array of (classalias => object))
  97. static public function FromArrayAssoc($aClasses, $aObjects)
  98. {
  99. // In a perfect world, we should create a complete tree of DBObjectSearch,
  100. // but as we lack most of the information related to the objects,
  101. // let's create one search definition
  102. $sClass = reset($aClasses);
  103. $sAlias = key($aClasses);
  104. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  105. $oRetSet = new self($oFilter);
  106. $oRetSet->m_bLoaded = true; // no DB load
  107. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  108. {
  109. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  110. }
  111. return $oRetSet;
  112. }
  113. static public function FromLinkSet($oObject, $sLinkSetAttCode, $sExtKeyToRemote)
  114. {
  115. $oLinkAttCode = MetaModel::GetAttributeDef(get_class($oObject), $sLinkSetAttCode);
  116. $oExtKeyAttDef = MetaModel::GetAttributeDef($oLinkAttCode->GetLinkedClass(), $sExtKeyToRemote);
  117. $sTargetClass = $oExtKeyAttDef->GetTargetClass();
  118. $oLinkSet = $oObject->Get($sLinkSetAttCode);
  119. $aTargets = array();
  120. while ($oLink = $oLinkSet->Fetch())
  121. {
  122. $aTargets[] = MetaModel::GetObject($sTargetClass, $oLink->Get($sExtKeyToRemote));
  123. }
  124. return self::FromArray($sTargetClass, $aTargets);
  125. }
  126. public function ToArray($bWithId = true)
  127. {
  128. $aRet = array();
  129. $this->Rewind();
  130. while ($oObject = $this->Fetch())
  131. {
  132. if ($bWithId)
  133. {
  134. $aRet[$oObject->GetKey()] = $oObject;
  135. }
  136. else
  137. {
  138. $aRet[] = $oObject;
  139. }
  140. }
  141. return $aRet;
  142. }
  143. public function ToArrayOfValues()
  144. {
  145. if (!$this->m_bLoaded) $this->Load();
  146. $aSelectedClasses = $this->m_oFilter->GetSelectedClasses();
  147. $aRet = array();
  148. foreach($this->m_aData as $iRow => $aObjects)
  149. {
  150. foreach($aObjects as $sClassAlias => $oObject)
  151. {
  152. if (is_null($oObject))
  153. {
  154. $aRet[$iRow][$sClassAlias.'.'.'id'] = null;
  155. }
  156. else
  157. {
  158. $aRet[$iRow][$sClassAlias.'.'.'id'] = $oObject->GetKey();
  159. }
  160. if (is_null($oObject))
  161. {
  162. $sClass = $aSelectedClasses[$sClassAlias];
  163. }
  164. else
  165. {
  166. $sClass = get_class($oObject);
  167. }
  168. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  169. {
  170. if ($oAttDef->IsScalar())
  171. {
  172. $sAttName = $sClassAlias.'.'.$sAttCode;
  173. if (is_null($oObject))
  174. {
  175. $aRet[$iRow][$sAttName] = null;
  176. }
  177. else
  178. {
  179. $aRet[$iRow][$sAttName] = $oObject->Get($sAttCode);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. return $aRet;
  186. }
  187. public function GetColumnAsArray($sAttCode, $bWithId = true)
  188. {
  189. $aRet = array();
  190. $this->Rewind();
  191. while ($oObject = $this->Fetch())
  192. {
  193. if ($bWithId)
  194. {
  195. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  196. }
  197. else
  198. {
  199. $aRet[] = $oObject->Get($sAttCode);
  200. }
  201. }
  202. return $aRet;
  203. }
  204. public function GetFilter()
  205. {
  206. // #@# This is false as soon as the set has been manipulated (AddObject...)
  207. return $this->m_oFilter;
  208. }
  209. public function GetClass()
  210. {
  211. return $this->m_oFilter->GetClass();
  212. }
  213. public function GetSelectedClasses()
  214. {
  215. return $this->m_oFilter->GetSelectedClasses();
  216. }
  217. public function GetRootClass()
  218. {
  219. return MetaModel::GetRootClass($this->GetClass());
  220. }
  221. public function SetLimit($iLimitCount, $iLimitStart = 0)
  222. {
  223. $this->m_iLimitCount = $iLimitCount;
  224. $this->m_iLimitStart = $iLimitStart;
  225. }
  226. public function GetLimitCount()
  227. {
  228. return $this->m_iLimitCount;
  229. }
  230. public function GetLimitStart()
  231. {
  232. return $this->m_iLimitStart;
  233. }
  234. public function Load()
  235. {
  236. if ($this->m_bLoaded) return;
  237. // Note: it is mandatory to set this value now, to protect against reentrance
  238. $this->m_bLoaded = true;
  239. if ($this->m_iLimitCount > 0)
  240. {
  241. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec, $this->m_iLimitCount, $this->m_iLimitStart);
  242. }
  243. else
  244. {
  245. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec);
  246. }
  247. $resQuery = CMDBSource::Query($sSQL);
  248. if (!$resQuery) return;
  249. $sClass = $this->m_oFilter->GetClass();
  250. while ($aRow = CMDBSource::FetchArray($resQuery))
  251. {
  252. $aObjects = array();
  253. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  254. {
  255. if (is_null($aRow[$sClassAlias.'id']))
  256. {
  257. $oObject = null;
  258. }
  259. else
  260. {
  261. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias, $this->m_aExtendedDataSpec);
  262. }
  263. $aObjects[$sClassAlias] = $oObject;
  264. }
  265. $this->AddObjectExtended($aObjects);
  266. }
  267. CMDBSource::FreeResult($resQuery);
  268. }
  269. public function Count()
  270. {
  271. if ($this->m_bLoaded && ($this->m_iLimitCount == 0) && ($this->m_iLimitStart == 0))
  272. {
  273. return count($this->m_aData);
  274. }
  275. else
  276. {
  277. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, 0, 0, true);
  278. $resQuery = CMDBSource::Query($sSQL);
  279. if (!$resQuery) return 0;
  280. $aRow = CMDBSource::FetchArray($resQuery);
  281. CMDBSource::FreeResult($resQuery);
  282. return $aRow['COUNT'];
  283. }
  284. }
  285. public function Fetch($sClassAlias = '')
  286. {
  287. if (!$this->m_bLoaded) $this->Load();
  288. if ($this->m_iCurrRow >= count($this->m_aData))
  289. {
  290. return null;
  291. }
  292. if (strlen($sClassAlias) == 0)
  293. {
  294. $sClassAlias = $this->m_oFilter->GetClassAlias();
  295. }
  296. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  297. $this->m_iCurrRow++;
  298. return $oRetObj;
  299. }
  300. // Return the whole line if several classes have been specified in the query
  301. //
  302. public function FetchAssoc()
  303. {
  304. if (!$this->m_bLoaded) $this->Load();
  305. if ($this->m_iCurrRow >= count($this->m_aData))
  306. {
  307. return null;
  308. }
  309. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  310. $this->m_iCurrRow++;
  311. return $aRetObjects;
  312. }
  313. public function Rewind()
  314. {
  315. $this->Seek(0);
  316. }
  317. public function Seek($iRow)
  318. {
  319. if (!$this->m_bLoaded) $this->Load();
  320. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  321. return $this->m_iCurrRow;
  322. }
  323. public function AddObject($oObject, $sClassAlias = '')
  324. {
  325. if (!$this->m_bLoaded) $this->Load();
  326. if (strlen($sClassAlias) == 0)
  327. {
  328. $sClassAlias = $this->m_oFilter->GetClassAlias();
  329. }
  330. $iNextPos = count($this->m_aData);
  331. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  332. if (!is_null($oObject))
  333. {
  334. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  335. }
  336. }
  337. protected function AddObjectExtended($aObjectArray)
  338. {
  339. if (!$this->m_bLoaded) $this->Load();
  340. $iNextPos = count($this->m_aData);
  341. foreach ($aObjectArray as $sClassAlias => $oObject)
  342. {
  343. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  344. if (!is_null($oObject))
  345. {
  346. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  347. }
  348. }
  349. }
  350. public function AddObjectArray($aObjects, $sClassAlias = '')
  351. {
  352. if (!$this->m_bLoaded) $this->Load();
  353. // #@# todo - add a check on the object class ?
  354. foreach ($aObjects as $oObj)
  355. {
  356. $this->AddObject($oObj, $sClassAlias);
  357. }
  358. }
  359. public function Merge($oObjectSet)
  360. {
  361. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  362. {
  363. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  364. }
  365. if (!$this->m_bLoaded) $this->Load();
  366. $oObjectSet->Seek(0);
  367. while ($oObject = $oObjectSet->Fetch())
  368. {
  369. $this->AddObject($oObject);
  370. }
  371. }
  372. public function CreateIntersect($oObjectSet)
  373. {
  374. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  375. {
  376. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  377. }
  378. if (!$this->m_bLoaded) $this->Load();
  379. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  380. $sClassAlias = $this->m_oFilter->GetClassAlias();
  381. $oObjectSet->Seek(0);
  382. while ($oObject = $oObjectSet->Fetch())
  383. {
  384. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  385. {
  386. $oNewSet->AddObject($oObject);
  387. }
  388. }
  389. return $oNewSet;
  390. }
  391. // Note: This verb works only with objects existing in the database
  392. //
  393. public function HasSameContents($oObjectSet)
  394. {
  395. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  396. {
  397. return false;
  398. }
  399. if (!$this->m_bLoaded) $this->Load();
  400. if ($this->Count() != $oObjectSet->Count())
  401. {
  402. return false;
  403. }
  404. $sClassAlias = $this->m_oFilter->GetClassAlias();
  405. $oObjectSet->Rewind();
  406. while ($oObject = $oObjectSet->Fetch())
  407. {
  408. $iObjectKey = $oObject->GetKey();
  409. if ($iObjectKey < 0)
  410. {
  411. return false;
  412. }
  413. if (!array_key_exists($iObjectKey, $this->m_aId2Row[$sClassAlias]))
  414. {
  415. return false;
  416. }
  417. $iRow = $this->m_aId2Row[$sClassAlias][$iObjectKey];
  418. $oSibling = $this->m_aData[$iRow][$sClassAlias];
  419. if (!$oObject->Equals($oSibling))
  420. {
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. public function CreateDelta($oObjectSet)
  427. {
  428. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  429. {
  430. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  431. }
  432. if (!$this->m_bLoaded) $this->Load();
  433. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  434. $sClassAlias = $this->m_oFilter->GetClassAlias();
  435. $oObjectSet->Seek(0);
  436. while ($oObject = $oObjectSet->Fetch())
  437. {
  438. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  439. {
  440. $oNewSet->AddObject($oObject);
  441. }
  442. }
  443. return $oNewSet;
  444. }
  445. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  446. {
  447. $aRelatedObjs = array();
  448. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  449. $this->Seek(0);
  450. while ($oObject = $this->Fetch())
  451. {
  452. $aMore = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  453. foreach ($aMore as $sClass => $aRelated)
  454. {
  455. foreach ($aRelated as $iObj => $oObj)
  456. {
  457. if (!isset($aRelatedObjs[$sClass][$iObj]))
  458. {
  459. $aRelatedObjs[$sClass][$iObj] = $oObj;
  460. }
  461. }
  462. }
  463. }
  464. return $aRelatedObjs;
  465. }
  466. }
  467. ?>