dbunionsearch.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. // Copyright (C) 2015-2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * A union of DBObjectSearches
  20. *
  21. * @copyright Copyright (C) 2015-2017 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class DBUnionSearch extends DBSearch
  25. {
  26. protected $aSearches; // source queries
  27. protected $aSelectedClasses; // alias => classes (lowest common ancestors) computed at construction
  28. public function __construct($aSearches)
  29. {
  30. if (count ($aSearches) == 0)
  31. {
  32. throw new CoreException('A DBUnionSearch must be made of at least one search');
  33. }
  34. $this->aSearches = array();
  35. foreach ($aSearches as $oSearch)
  36. {
  37. if ($oSearch instanceof DBUnionSearch)
  38. {
  39. foreach ($oSearch->aSearches as $oSubSearch)
  40. {
  41. $this->aSearches[] = $oSubSearch->DeepClone();
  42. }
  43. }
  44. else
  45. {
  46. $this->aSearches[] = $oSearch->DeepClone();
  47. }
  48. }
  49. $this->ComputeSelectedClasses();
  50. }
  51. public function AllowAllData()
  52. {
  53. foreach ($this->aSearches as $oSearch)
  54. {
  55. $oSearch->AllowAllData();
  56. }
  57. }
  58. public function IsAllDataAllowed()
  59. {
  60. foreach ($this->aSearches as $oSearch)
  61. {
  62. if ($oSearch->IsAllDataAllowed() === false) return false;
  63. }
  64. return true;
  65. }
  66. public function SetArchiveMode($bEnable)
  67. {
  68. foreach ($this->aSearches as $oSearch)
  69. {
  70. $oSearch->SetArchiveMode($bEnable);
  71. }
  72. parent::SetArchiveMode($bEnable);
  73. }
  74. public function SetShowObsoleteData($bShow)
  75. {
  76. foreach ($this->aSearches as $oSearch)
  77. {
  78. $oSearch->SetShowObsoleteData($bShow);
  79. }
  80. parent::SetShowObsoleteData($bShow);
  81. }
  82. /**
  83. * Find the lowest common ancestor for each of the selected class
  84. */
  85. protected function ComputeSelectedClasses()
  86. {
  87. // 1 - Collect all the column/classes
  88. $aColumnToClasses = array();
  89. foreach ($this->aSearches as $iPos => $oSearch)
  90. {
  91. $aSelected = array_values($oSearch->GetSelectedClasses());
  92. if ($iPos != 0)
  93. {
  94. if (count($aSelected) < count($aColumnToClasses))
  95. {
  96. throw new Exception('Too few selected classes in the subquery #'.($iPos+1));
  97. }
  98. if (count($aSelected) > count($aColumnToClasses))
  99. {
  100. throw new Exception('Too many selected classes in the subquery #'.($iPos+1));
  101. }
  102. }
  103. foreach ($aSelected as $iColumn => $sClass)
  104. {
  105. $aColumnToClasses[$iColumn][] = $sClass;
  106. }
  107. }
  108. // 2 - Build the index column => alias
  109. $oFirstSearch = $this->aSearches[0];
  110. $aColumnToAlias = array_keys($oFirstSearch->GetSelectedClasses());
  111. // 3 - Compute alias => lowest common ancestor
  112. $this->aSelectedClasses = array();
  113. foreach ($aColumnToClasses as $iColumn => $aClasses)
  114. {
  115. $sAlias = $aColumnToAlias[$iColumn];
  116. $sAncestor = MetaModel::GetLowestCommonAncestor($aClasses);
  117. if (is_null($sAncestor))
  118. {
  119. throw new Exception('Could not find a common ancestor for the column '.($iColumn+1).' (Classes: '.implode(', ', $aClasses).')');
  120. }
  121. $this->aSelectedClasses[$sAlias] = $sAncestor;
  122. }
  123. }
  124. public function GetSearches()
  125. {
  126. return $this->aSearches;
  127. }
  128. /**
  129. * Limited to the selected classes
  130. */
  131. public function GetClassName($sAlias)
  132. {
  133. if (array_key_exists($sAlias, $this->aSelectedClasses))
  134. {
  135. return $this->aSelectedClasses[$sAlias];
  136. }
  137. else
  138. {
  139. throw new CoreException("Invalid class alias '$sAlias'");
  140. }
  141. }
  142. public function GetClass()
  143. {
  144. return reset($this->aSelectedClasses);
  145. }
  146. public function GetClassAlias()
  147. {
  148. reset($this->aSelectedClasses);
  149. return key($this->aSelectedClasses);
  150. }
  151. /**
  152. * Change the class (only subclasses are supported as of now, because the conditions must fit the new class)
  153. * Defaults to the first selected class
  154. * Only the selected classes can be changed
  155. */
  156. public function ChangeClass($sNewClass, $sAlias = null)
  157. {
  158. if (is_null($sAlias))
  159. {
  160. $sAlias = $this->GetClassAlias();
  161. }
  162. elseif (!array_key_exists($sAlias, $this->aSelectedClasses))
  163. {
  164. // discard silently - necessary when recursing (??? copied from DBObjectSearch)
  165. return;
  166. }
  167. // 1 - identify the impacted column
  168. $iColumn = array_search($sAlias, array_keys($this->aSelectedClasses));
  169. // 2 - change for each search
  170. foreach ($this->aSearches as $oSearch)
  171. {
  172. $aSearchAliases = array_keys($oSearch->GetSelectedClasses());
  173. $sSearchAlias = $aSearchAliases[$iColumn];
  174. $oSearch->ChangeClass($sNewClass, $sSearchAlias);
  175. }
  176. // 3 - record the change
  177. $this->aSelectedClasses[$sAlias] = $sNewClass;
  178. }
  179. public function GetSelectedClasses()
  180. {
  181. return $this->aSelectedClasses;
  182. }
  183. /**
  184. * @param array $aSelectedClasses array of aliases
  185. * @throws CoreException
  186. */
  187. public function SetSelectedClasses($aSelectedClasses)
  188. {
  189. // 1 - change for each search
  190. foreach ($this->aSearches as $oSearch)
  191. {
  192. // Throws an exception if not valid
  193. $oSearch->SetSelectedClasses($aSelectedClasses);
  194. }
  195. // 2 - update the lowest common ancestors
  196. $this->ComputeSelectedClasses();
  197. }
  198. /**
  199. * Change any alias of the query tree
  200. *
  201. * @param $sOldName
  202. * @param $sNewName
  203. * @return bool True if the alias has been found and changed
  204. */
  205. public function RenameAlias($sOldName, $sNewName)
  206. {
  207. $bRet = false;
  208. foreach ($this->aSearches as $oSearch)
  209. {
  210. $bRet = $oSearch->RenameAlias($sOldName, $sNewName) || $bRet;
  211. }
  212. return $bRet;
  213. }
  214. public function IsAny()
  215. {
  216. $bIsAny = true;
  217. foreach ($this->aSearches as $oSearch)
  218. {
  219. if (!$oSearch->IsAny())
  220. {
  221. $bIsAny = false;
  222. break;
  223. }
  224. }
  225. return $bIsAny;
  226. }
  227. public function ResetCondition()
  228. {
  229. foreach ($this->aSearches as $oSearch)
  230. {
  231. $oSearch->ResetCondition();
  232. }
  233. }
  234. public function MergeConditionExpression($oExpression)
  235. {
  236. $aAliases = array_keys($this->aSelectedClasses);
  237. foreach ($this->aSearches as $iSearchIndex => $oSearch)
  238. {
  239. $oClonedExpression = $oExpression->DeepClone();
  240. if ($iSearchIndex != 0)
  241. {
  242. foreach (array_keys($oSearch->GetSelectedClasses()) as $iColumn => $sSearchAlias)
  243. {
  244. $oClonedExpression->RenameAlias($aAliases[$iColumn], $sSearchAlias);
  245. }
  246. }
  247. $oSearch->MergeConditionExpression($oClonedExpression);
  248. }
  249. }
  250. public function AddConditionExpression($oExpression)
  251. {
  252. $aAliases = array_keys($this->aSelectedClasses);
  253. foreach ($this->aSearches as $iSearchIndex => $oSearch)
  254. {
  255. $oClonedExpression = $oExpression->DeepClone();
  256. if ($iSearchIndex != 0)
  257. {
  258. foreach (array_keys($oSearch->GetSelectedClasses()) as $iColumn => $sSearchAlias)
  259. {
  260. $oClonedExpression->RenameAlias($aAliases[$iColumn], $sSearchAlias);
  261. }
  262. }
  263. $oSearch->AddConditionExpression($oClonedExpression);
  264. }
  265. }
  266. public function AddNameCondition($sName)
  267. {
  268. foreach ($this->aSearches as $oSearch)
  269. {
  270. $oSearch->AddNameCondition($sName);
  271. }
  272. }
  273. public function AddCondition($sFilterCode, $value, $sOpCode = null)
  274. {
  275. foreach ($this->aSearches as $oSearch)
  276. {
  277. $oSearch->AddCondition($sFilterCode, $value, $sOpCode);
  278. }
  279. }
  280. /**
  281. * Specify a condition on external keys or link sets
  282. * @param sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
  283. * Example: infra_list->ci_id->location_id->country
  284. * @param value The value to match (can be an array => IN(val1, val2...)
  285. * @return void
  286. */
  287. public function AddConditionAdvanced($sAttSpec, $value)
  288. {
  289. foreach ($this->aSearches as $oSearch)
  290. {
  291. $oSearch->AddConditionAdvanced($sAttSpec, $value);
  292. }
  293. }
  294. public function AddCondition_FullText($sFullText)
  295. {
  296. foreach ($this->aSearches as $oSearch)
  297. {
  298. $oSearch->AddCondition_FullText($sFullText);
  299. }
  300. }
  301. /**
  302. * @param DBObjectSearch $oFilter
  303. * @param $sExtKeyAttCode
  304. * @param int $iOperatorCode
  305. * @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
  306. * @throws CoreException
  307. * @throws CoreWarning
  308. */
  309. public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
  310. {
  311. foreach ($this->aSearches as $oSearch)
  312. {
  313. $oSearch->AddCondition_PointingTo($oFilter, $sExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
  314. }
  315. }
  316. /**
  317. * @param DBObjectSearch $oFilter
  318. * @param $sForeignExtKeyAttCode
  319. * @param int $iOperatorCode
  320. * @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
  321. */
  322. public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
  323. {
  324. foreach ($this->aSearches as $oSearch)
  325. {
  326. $oSearch->AddCondition_ReferencedBy($oFilter, $sForeignExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
  327. }
  328. }
  329. public function Intersect(DBSearch $oFilter)
  330. {
  331. $aSearches = array();
  332. foreach ($this->aSearches as $oSearch)
  333. {
  334. $aSearches[] = $oSearch->Intersect($oFilter);
  335. }
  336. return new DBUnionSearch($aSearches);
  337. }
  338. public function SetInternalParams($aParams)
  339. {
  340. foreach ($this->aSearches as $oSearch)
  341. {
  342. $oSearch->SetInternalParams($aParams);
  343. }
  344. }
  345. public function GetInternalParams()
  346. {
  347. $aParams = array();
  348. foreach ($this->aSearches as $oSearch)
  349. {
  350. $aParams = array_merge($oSearch->GetInternalParams(), $aParams);
  351. }
  352. return $aParams;
  353. }
  354. public function GetQueryParams($bExcludeMagicParams = true)
  355. {
  356. $aParams = array();
  357. foreach ($this->aSearches as $oSearch)
  358. {
  359. $aParams = array_merge($oSearch->GetQueryParams($bExcludeMagicParams), $aParams);
  360. }
  361. return $aParams;
  362. }
  363. public function ListConstantFields()
  364. {
  365. // Somewhat complex to implement for unions, for a poor benefit
  366. return array();
  367. }
  368. /**
  369. * Turn the parameters (:xxx) into scalar values in order to easily
  370. * serialize a search
  371. */
  372. public function ApplyParameters($aArgs)
  373. {
  374. foreach ($this->aSearches as $oSearch)
  375. {
  376. $oSearch->ApplyParameters($aArgs);
  377. }
  378. }
  379. /**
  380. * Overloads for query building
  381. */
  382. public function ToOQL($bDevelopParams = false, $aContextParams = null, $bWithAllowAllFlag = false)
  383. {
  384. $aSubQueries = array();
  385. foreach ($this->aSearches as $oSearch)
  386. {
  387. $aSubQueries[] = $oSearch->ToOQL($bDevelopParams, $aContextParams, $bWithAllowAllFlag);
  388. }
  389. $sRet = implode(' UNION ', $aSubQueries);
  390. return $sRet;
  391. }
  392. /**
  393. * Returns a new DBUnionSearch object where duplicates queries have been removed based on their OQLs
  394. *
  395. * @return \DBUnionSearch
  396. */
  397. public function RemoveDuplicateQueries()
  398. {
  399. $aQueries = array();
  400. $aSearches = array();
  401. foreach ($this->GetSearches() as $oTmpSearch)
  402. {
  403. $sQuery = $oTmpSearch->ToOQL(true);
  404. if (!in_array($sQuery, $aQueries))
  405. {
  406. $aQueries[] = $sQuery;
  407. $aSearches[] = $oTmpSearch;
  408. }
  409. }
  410. $oNewSearch = new DBUnionSearch($aSearches);
  411. return $oNewSearch;
  412. }
  413. ////////////////////////////////////////////////////////////////////////////
  414. //
  415. // Construction of the SQL queries
  416. //
  417. ////////////////////////////////////////////////////////////////////////////
  418. public function MakeDeleteQuery($aArgs = array())
  419. {
  420. throw new Exception('MakeDeleteQuery is not implemented for the unions!');
  421. }
  422. public function MakeUpdateQuery($aValues, $aArgs = array())
  423. {
  424. throw new Exception('MakeUpdateQuery is not implemented for the unions!');
  425. }
  426. protected function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null)
  427. {
  428. if (count($this->aSearches) == 1)
  429. {
  430. return $this->aSearches[0]->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr);
  431. }
  432. $aSQLQueries = array();
  433. $aAliases = array_keys($this->aSelectedClasses);
  434. foreach ($this->aSearches as $iSearch => $oSearch)
  435. {
  436. $aSearchAliases = array_keys($oSearch->GetSelectedClasses());
  437. // The selected classes from the query build perspective are the lowest common ancestors amongst the various queries
  438. // (used when it comes to determine which attributes must be selected)
  439. $aSearchSelectedClasses = array();
  440. foreach ($aSearchAliases as $iColumn => $sSearchAlias)
  441. {
  442. $sAlias = $aAliases[$iColumn];
  443. $aSearchSelectedClasses[$sSearchAlias] = $this->aSelectedClasses[$sAlias];
  444. }
  445. if (is_null($aAttToLoad))
  446. {
  447. $aQueryAttToLoad = null;
  448. }
  449. else
  450. {
  451. // (Eventually) Transform the aliases
  452. $aQueryAttToLoad = array();
  453. foreach ($aAttToLoad as $sAlias => $aAttributes)
  454. {
  455. $iColumn = array_search($sAlias, $aAliases);
  456. $sQueryAlias = ($iColumn === false) ? $sAlias : $aSearchAliases[$iColumn];
  457. $aQueryAttToLoad[$sQueryAlias] = $aAttributes;
  458. }
  459. }
  460. if (is_null($aGroupByExpr))
  461. {
  462. $aQueryGroupByExpr = null;
  463. }
  464. else
  465. {
  466. // Clone (and eventually transform) the group by expressions
  467. $aQueryGroupByExpr = array();
  468. $aTranslationData = array();
  469. $aQueryColumns = array_keys($oSearch->GetSelectedClasses());
  470. foreach ($aAliases as $iColumn => $sAlias)
  471. {
  472. $sQueryAlias = $aQueryColumns[$iColumn];
  473. $aTranslationData[$sAlias]['*'] = $sQueryAlias;
  474. $aQueryGroupByExpr[$sAlias.'id'] = new FieldExpression('id', $sQueryAlias);
  475. }
  476. foreach ($aGroupByExpr as $sExpressionAlias => $oExpression)
  477. {
  478. $aQueryGroupByExpr[$sExpressionAlias] = $oExpression->Translate($aTranslationData, false, false);
  479. }
  480. }
  481. $oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses);
  482. if (count($aSearchAliases) > 1)
  483. {
  484. // Necessary to make sure that selected columns will match throughout all the queries
  485. // (default order of selected fields depending on the order of JOINS)
  486. $oSubQuery->SortSelectedFields();
  487. }
  488. $aSQLQueries[] = $oSubQuery;
  489. }
  490. $oSQLQuery = new SQLUnionQuery($aSQLQueries, $aGroupByExpr);
  491. //MyHelpers::var_dump_html($oSQLQuery, true);
  492. //MyHelpers::var_dump_html($oSQLQuery->RenderSelect(), true);
  493. if (self::$m_bDebugQuery) $oSQLQuery->DisplayHtml();
  494. return $oSQLQuery;
  495. }
  496. }