dbobjectset.class.php 11 KB

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