dbobjectset.class.php 11 KB

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