dbobjectset.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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)
  47. $this->m_aId2Row = array();
  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. $aRet = array();
  147. foreach($this->m_aData as $iRow => $aObjects)
  148. {
  149. foreach($aObjects as $sClassAlias => $oObject)
  150. {
  151. $aRet[$iRow][$sClassAlias.'.'.'id'] = $oObject->GetKey();
  152. $sClass = get_class($oObject);
  153. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  154. {
  155. if ($oAttDef->IsScalar())
  156. {
  157. $sAttName = $sClassAlias.'.'.$sAttCode;
  158. $aRet[$iRow][$sAttName] = $oObject->Get($sAttCode);
  159. }
  160. }
  161. }
  162. }
  163. return $aRet;
  164. }
  165. public function GetColumnAsArray($sAttCode, $bWithId = true)
  166. {
  167. $aRet = array();
  168. $this->Rewind();
  169. while ($oObject = $this->Fetch())
  170. {
  171. if ($bWithId)
  172. {
  173. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  174. }
  175. else
  176. {
  177. $aRet[] = $oObject->Get($sAttCode);
  178. }
  179. }
  180. return $aRet;
  181. }
  182. public function GetFilter()
  183. {
  184. // #@# This is false as soon as the set has been manipulated (AddObject...)
  185. return $this->m_oFilter;
  186. }
  187. public function GetClass()
  188. {
  189. return $this->m_oFilter->GetClass();
  190. }
  191. public function GetSelectedClasses()
  192. {
  193. return $this->m_oFilter->GetSelectedClasses();
  194. }
  195. public function GetRootClass()
  196. {
  197. return MetaModel::GetRootClass($this->GetClass());
  198. }
  199. public function SetLimit($iLimitCount, $iLimitStart = 0)
  200. {
  201. $this->m_iLimitCount = $iLimitCount;
  202. $this->m_iLimitStart = $iLimitStart;
  203. }
  204. public function GetLimitCount()
  205. {
  206. return $this->m_iLimitCount;
  207. }
  208. public function GetLimitStart()
  209. {
  210. return $this->m_iLimitStart;
  211. }
  212. public function Load()
  213. {
  214. if ($this->m_bLoaded) return;
  215. // Note: it is mandatory to set this value now, to protect against reentrance
  216. $this->m_bLoaded = true;
  217. if ($this->m_iLimitCount > 0)
  218. {
  219. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec, $this->m_iLimitCount, $this->m_iLimitStart);
  220. }
  221. else
  222. {
  223. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec);
  224. }
  225. $resQuery = CMDBSource::Query($sSQL);
  226. if (!$resQuery) return;
  227. $sClass = $this->m_oFilter->GetClass();
  228. while ($aRow = CMDBSource::FetchArray($resQuery))
  229. {
  230. $aObjects = array();
  231. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  232. {
  233. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias, $this->m_aExtendedDataSpec);
  234. $aObjects[$sClassAlias] = $oObject;
  235. }
  236. $this->AddObjectExtended($aObjects);
  237. }
  238. CMDBSource::FreeResult($resQuery);
  239. }
  240. public function Count()
  241. {
  242. if ($this->m_bLoaded && ($this->m_iLimitCount == 0) && ($this->m_iLimitStart == 0))
  243. {
  244. return count($this->m_aData);
  245. }
  246. else
  247. {
  248. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, 0, 0, true);
  249. $resQuery = CMDBSource::Query($sSQL);
  250. if (!$resQuery) return 0;
  251. $aRow = CMDBSource::FetchArray($resQuery);
  252. CMDBSource::FreeResult($resQuery);
  253. return $aRow['COUNT'];
  254. }
  255. }
  256. public function Fetch($sClassAlias = '')
  257. {
  258. if (!$this->m_bLoaded) $this->Load();
  259. if ($this->m_iCurrRow >= count($this->m_aData))
  260. {
  261. return null;
  262. }
  263. if (strlen($sClassAlias) == 0)
  264. {
  265. $sClassAlias = $this->m_oFilter->GetClassAlias();
  266. }
  267. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  268. $this->m_iCurrRow++;
  269. return $oRetObj;
  270. }
  271. // Return the whole line if several classes have been specified in the query
  272. //
  273. public function FetchAssoc()
  274. {
  275. if (!$this->m_bLoaded) $this->Load();
  276. if ($this->m_iCurrRow >= count($this->m_aData))
  277. {
  278. return null;
  279. }
  280. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  281. $this->m_iCurrRow++;
  282. return $aRetObjects;
  283. }
  284. public function Rewind()
  285. {
  286. $this->Seek(0);
  287. }
  288. public function Seek($iRow)
  289. {
  290. if (!$this->m_bLoaded) $this->Load();
  291. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  292. return $this->m_iCurrRow;
  293. }
  294. public function AddObject($oObject, $sClassAlias = '')
  295. {
  296. if (!$this->m_bLoaded) $this->Load();
  297. if (strlen($sClassAlias) == 0)
  298. {
  299. $sClassAlias = $this->m_oFilter->GetClassAlias();
  300. }
  301. $iNextPos = count($this->m_aData);
  302. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  303. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  304. }
  305. protected function AddObjectExtended($aObjectArray)
  306. {
  307. if (!$this->m_bLoaded) $this->Load();
  308. $iNextPos = count($this->m_aData);
  309. foreach ($aObjectArray as $sClassAlias => $oObject)
  310. {
  311. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  312. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  313. }
  314. }
  315. public function AddObjectArray($aObjects, $sClassAlias = '')
  316. {
  317. if (!$this->m_bLoaded) $this->Load();
  318. // #@# todo - add a check on the object class ?
  319. foreach ($aObjects as $oObj)
  320. {
  321. $this->AddObject($oObj, $sClassAlias);
  322. }
  323. }
  324. public function Merge($oObjectSet)
  325. {
  326. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  327. {
  328. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  329. }
  330. if (!$this->m_bLoaded) $this->Load();
  331. $oObjectSet->Seek(0);
  332. while ($oObject = $oObjectSet->Fetch())
  333. {
  334. $this->AddObject($oObject);
  335. }
  336. }
  337. public function CreateIntersect($oObjectSet)
  338. {
  339. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  340. {
  341. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  342. }
  343. if (!$this->m_bLoaded) $this->Load();
  344. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  345. $sClassAlias = $this->m_oFilter->GetClassAlias();
  346. $oObjectSet->Seek(0);
  347. while ($oObject = $oObjectSet->Fetch())
  348. {
  349. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  350. {
  351. $oNewSet->AddObject($oObject);
  352. }
  353. }
  354. return $oNewSet;
  355. }
  356. public function CreateDelta($oObjectSet)
  357. {
  358. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  359. {
  360. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  361. }
  362. if (!$this->m_bLoaded) $this->Load();
  363. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  364. $sClassAlias = $this->m_oFilter->GetClassAlias();
  365. $oObjectSet->Seek(0);
  366. while ($oObject = $oObjectSet->Fetch())
  367. {
  368. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  369. {
  370. $oNewSet->AddObject($oObject);
  371. }
  372. }
  373. return $oNewSet;
  374. }
  375. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  376. {
  377. $aRelatedObjs = array();
  378. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  379. $this->Seek(0);
  380. while ($oObject = $this->Fetch())
  381. {
  382. $aMore = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  383. foreach ($aMore as $sClass => $aRelated)
  384. {
  385. foreach ($aRelated as $iObj => $oObj)
  386. {
  387. if (!isset($aRelatedObjs[$sClass][$iObj]))
  388. {
  389. $aRelatedObjs[$sClass][$iObj] = $oObj;
  390. }
  391. }
  392. }
  393. }
  394. return $aRelatedObjs;
  395. }
  396. }
  397. ?>