dbunionsearch.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. // Copyright (C) 2015-2016 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-2016 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. /**
  67. * Find the lowest common ancestor for each of the selected class
  68. */
  69. protected function ComputeSelectedClasses()
  70. {
  71. // 1 - Collect all the column/classes
  72. $aColumnToClasses = array();
  73. foreach ($this->aSearches as $iPos => $oSearch)
  74. {
  75. $aSelected = array_values($oSearch->GetSelectedClasses());
  76. if ($iPos != 0)
  77. {
  78. if (count($aSelected) < count($aColumnToClasses))
  79. {
  80. throw new Exception('Too few selected classes in the subquery #'.($iPos+1));
  81. }
  82. if (count($aSelected) > count($aColumnToClasses))
  83. {
  84. throw new Exception('Too many selected classes in the subquery #'.($iPos+1));
  85. }
  86. }
  87. foreach ($aSelected as $iColumn => $sClass)
  88. {
  89. $aColumnToClasses[$iColumn][] = $sClass;
  90. }
  91. }
  92. // 2 - Build the index column => alias
  93. $oFirstSearch = $this->aSearches[0];
  94. $aColumnToAlias = array_keys($oFirstSearch->GetSelectedClasses());
  95. // 3 - Compute alias => lowest common ancestor
  96. $this->aSelectedClasses = array();
  97. foreach ($aColumnToClasses as $iColumn => $aClasses)
  98. {
  99. $sAlias = $aColumnToAlias[$iColumn];
  100. $sAncestor = MetaModel::GetLowestCommonAncestor($aClasses);
  101. if (is_null($sAncestor))
  102. {
  103. throw new Exception('Could not find a common ancestor for the column '.($iColumn+1).' (Classes: '.implode(', ', $aClasses).')');
  104. }
  105. $this->aSelectedClasses[$sAlias] = $sAncestor;
  106. }
  107. }
  108. public function GetSearches()
  109. {
  110. return $this->aSearches;
  111. }
  112. /**
  113. * Limited to the selected classes
  114. */
  115. public function GetClassName($sAlias)
  116. {
  117. if (array_key_exists($sAlias, $this->aSelectedClasses))
  118. {
  119. return $this->aSelectedClasses[$sAlias];
  120. }
  121. else
  122. {
  123. throw new CoreException("Invalid class alias '$sAlias'");
  124. }
  125. }
  126. public function GetClass()
  127. {
  128. return reset($this->aSelectedClasses);
  129. }
  130. public function GetClassAlias()
  131. {
  132. reset($this->aSelectedClasses);
  133. return key($this->aSelectedClasses);
  134. }
  135. /**
  136. * Change the class (only subclasses are supported as of now, because the conditions must fit the new class)
  137. * Defaults to the first selected class
  138. * Only the selected classes can be changed
  139. */
  140. public function ChangeClass($sNewClass, $sAlias = null)
  141. {
  142. if (is_null($sAlias))
  143. {
  144. $sAlias = $this->GetClassAlias();
  145. }
  146. elseif (!array_key_exists($sAlias, $this->aSelectedClasses))
  147. {
  148. // discard silently - necessary when recursing (??? copied from DBObjectSearch)
  149. return;
  150. }
  151. // 1 - identify the impacted column
  152. $iColumn = array_search($sAlias, array_keys($this->aSelectedClasses));
  153. // 2 - change for each search
  154. foreach ($this->aSearches as $oSearch)
  155. {
  156. $aSearchAliases = array_keys($oSearch->GetSelectedClasses());
  157. $sSearchAlias = $aSearchAliases[$iColumn];
  158. $oSearch->ChangeClass($sNewClass, $sSearchAlias);
  159. }
  160. // 3 - record the change
  161. $this->aSelectedClasses[$sAlias] = $sNewClass;
  162. }
  163. public function GetSelectedClasses()
  164. {
  165. return $this->aSelectedClasses;
  166. }
  167. /**
  168. * @param array $aSelectedClasses array of aliases
  169. * @throws CoreException
  170. */
  171. public function SetSelectedClasses($aSelectedClasses)
  172. {
  173. // 1 - change for each search
  174. foreach ($this->aSearches as $oSearch)
  175. {
  176. // Throws an exception if not valid
  177. $oSearch->SetSelectedClasses($aSelectedClasses);
  178. }
  179. // 2 - update the lowest common ancestors
  180. $this->ComputeSelectedClasses();
  181. }
  182. /**
  183. * Change any alias of the query tree
  184. *
  185. * @param $sOldName
  186. * @param $sNewName
  187. * @return bool True if the alias has been found and changed
  188. */
  189. public function RenameAlias($sOldName, $sNewName)
  190. {
  191. $bRet = false;
  192. foreach ($this->aSearches as $oSearch)
  193. {
  194. $bRet = $oSearch->RenameAlias($sOldName, $sNewName) || $bRet;
  195. }
  196. return $bRet;
  197. }
  198. public function IsAny()
  199. {
  200. $bIsAny = true;
  201. foreach ($this->aSearches as $oSearch)
  202. {
  203. if (!$oSearch->IsAny())
  204. {
  205. $bIsAny = false;
  206. break;
  207. }
  208. }
  209. return $bIsAny;
  210. }
  211. public function ResetCondition()
  212. {
  213. foreach ($this->aSearches as $oSearch)
  214. {
  215. $oSearch->ResetCondition();
  216. }
  217. }
  218. public function MergeConditionExpression($oExpression)
  219. {
  220. $aAliases = array_keys($this->aSelectedClasses);
  221. foreach ($this->aSearches as $iSearchIndex => $oSearch)
  222. {
  223. $oClonedExpression = $oExpression->DeepClone();
  224. if ($iSearchIndex != 0)
  225. {
  226. foreach (array_keys($oSearch->GetSelectedClasses()) as $iColumn => $sSearchAlias)
  227. {
  228. $oClonedExpression->RenameAlias($aAliases[$iColumn], $sSearchAlias);
  229. }
  230. }
  231. $oSearch->MergeConditionExpression($oClonedExpression);
  232. }
  233. }
  234. public function AddConditionExpression($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->AddConditionExpression($oClonedExpression);
  248. }
  249. }
  250. public function AddNameCondition($sName)
  251. {
  252. foreach ($this->aSearches as $oSearch)
  253. {
  254. $oSearch->AddNameCondition($sName);
  255. }
  256. }
  257. public function AddCondition($sFilterCode, $value, $sOpCode = null)
  258. {
  259. foreach ($this->aSearches as $oSearch)
  260. {
  261. $oSearch->AddCondition($sFilterCode, $value, $sOpCode);
  262. }
  263. }
  264. /**
  265. * Specify a condition on external keys or link sets
  266. * @param sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
  267. * Example: infra_list->ci_id->location_id->country
  268. * @param value The value to match (can be an array => IN(val1, val2...)
  269. * @return void
  270. */
  271. public function AddConditionAdvanced($sAttSpec, $value)
  272. {
  273. foreach ($this->aSearches as $oSearch)
  274. {
  275. $oSearch->AddConditionAdvanced($sAttSpec, $value);
  276. }
  277. }
  278. public function AddCondition_FullText($sFullText)
  279. {
  280. foreach ($this->aSearches as $oSearch)
  281. {
  282. $oSearch->AddCondition_FullText($sFullText);
  283. }
  284. }
  285. /**
  286. * @param DBObjectSearch $oFilter
  287. * @param $sExtKeyAttCode
  288. * @param int $iOperatorCode
  289. * @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
  290. * @throws CoreException
  291. * @throws CoreWarning
  292. */
  293. public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
  294. {
  295. foreach ($this->aSearches as $oSearch)
  296. {
  297. $oSearch->AddCondition_PointingTo($oFilter, $sExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
  298. }
  299. }
  300. /**
  301. * @param DBObjectSearch $oFilter
  302. * @param $sForeignExtKeyAttCode
  303. * @param int $iOperatorCode
  304. * @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
  305. */
  306. public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
  307. {
  308. foreach ($this->aSearches as $oSearch)
  309. {
  310. $oSearch->AddCondition_ReferencedBy($oFilter, $sForeignExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
  311. }
  312. }
  313. public function Intersect(DBSearch $oFilter)
  314. {
  315. $aSearches = array();
  316. foreach ($this->aSearches as $oSearch)
  317. {
  318. $aSearches[] = $oSearch->Intersect($oFilter);
  319. }
  320. return new DBUnionSearch($aSearches);
  321. }
  322. public function SetInternalParams($aParams)
  323. {
  324. foreach ($this->aSearches as $oSearch)
  325. {
  326. $oSearch->SetInternalParams($aParams);
  327. }
  328. }
  329. public function GetInternalParams()
  330. {
  331. $aParams = array();
  332. foreach ($this->aSearches as $oSearch)
  333. {
  334. $aParams = array_merge($oSearch->GetInternalParams(), $aParams);
  335. }
  336. return $aParams;
  337. }
  338. public function GetQueryParams($bExcludeMagicParams = true)
  339. {
  340. $aParams = array();
  341. foreach ($this->aSearches as $oSearch)
  342. {
  343. $aParams = array_merge($oSearch->GetQueryParams($bExcludeMagicParams), $aParams);
  344. }
  345. return $aParams;
  346. }
  347. public function ListConstantFields()
  348. {
  349. // Somewhat complex to implement for unions, for a poor benefit
  350. return array();
  351. }
  352. /**
  353. * Turn the parameters (:xxx) into scalar values in order to easily
  354. * serialize a search
  355. */
  356. public function ApplyParameters($aArgs)
  357. {
  358. foreach ($this->aSearches as $oSearch)
  359. {
  360. $oSearch->ApplyParameters($aArgs);
  361. }
  362. }
  363. /**
  364. * Overloads for query building
  365. */
  366. public function ToOQL($bDevelopParams = false, $aContextParams = null, $bWithAllowAllFlag = false)
  367. {
  368. $aSubQueries = array();
  369. foreach ($this->aSearches as $oSearch)
  370. {
  371. $aSubQueries[] = $oSearch->ToOQL($bDevelopParams, $aContextParams, $bWithAllowAllFlag);
  372. }
  373. $sRet = implode(' UNION ', $aSubQueries);
  374. return $sRet;
  375. }
  376. /**
  377. * Returns a new DBUnionSearch object where duplicates queries have been removed based on their OQLs
  378. *
  379. * @return \DBUnionSearch
  380. */
  381. public function RemoveDuplicateQueries()
  382. {
  383. $aQueries = array();
  384. $aSearches = array();
  385. foreach ($this->GetSearches() as $oTmpSearch)
  386. {
  387. $sQuery = $oTmpSearch->ToOQL(true);
  388. if (!in_array($sQuery, $aQueries))
  389. {
  390. $aQueries[] = $sQuery;
  391. $aSearches[] = $oTmpSearch;
  392. }
  393. }
  394. $oNewSearch = new DBUnionSearch($aSearches);
  395. return $oNewSearch;
  396. }
  397. ////////////////////////////////////////////////////////////////////////////
  398. //
  399. // Construction of the SQL queries
  400. //
  401. ////////////////////////////////////////////////////////////////////////////
  402. public function MakeDeleteQuery($aArgs = array())
  403. {
  404. throw new Exception('MakeDeleteQuery is not implemented for the unions!');
  405. }
  406. public function MakeUpdateQuery($aValues, $aArgs = array())
  407. {
  408. throw new Exception('MakeUpdateQuery is not implemented for the unions!');
  409. }
  410. protected function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null)
  411. {
  412. if (count($this->aSearches) == 1)
  413. {
  414. return $this->aSearches[0]->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr);
  415. }
  416. $aSQLQueries = array();
  417. $aAliases = array_keys($this->aSelectedClasses);
  418. foreach ($this->aSearches as $iSearch => $oSearch)
  419. {
  420. $aSearchAliases = array_keys($oSearch->GetSelectedClasses());
  421. // The selected classes from the query build perspective are the lowest common ancestors amongst the various queries
  422. // (used when it comes to determine which attributes must be selected)
  423. $aSearchSelectedClasses = array();
  424. foreach ($aSearchAliases as $iColumn => $sSearchAlias)
  425. {
  426. $sAlias = $aAliases[$iColumn];
  427. $aSearchSelectedClasses[$sSearchAlias] = $this->aSelectedClasses[$sAlias];
  428. }
  429. if (is_null($aAttToLoad))
  430. {
  431. $aQueryAttToLoad = null;
  432. }
  433. else
  434. {
  435. // (Eventually) Transform the aliases
  436. $aQueryAttToLoad = array();
  437. foreach ($aAttToLoad as $sAlias => $aAttributes)
  438. {
  439. $iColumn = array_search($sAlias, $aAliases);
  440. $sQueryAlias = ($iColumn === false) ? $sAlias : $aSearchAliases[$iColumn];
  441. $aQueryAttToLoad[$sQueryAlias] = $aAttributes;
  442. }
  443. }
  444. if (is_null($aGroupByExpr))
  445. {
  446. $aQueryGroupByExpr = null;
  447. }
  448. else
  449. {
  450. // Clone (and eventually transform) the group by expressions
  451. $aQueryGroupByExpr = array();
  452. $aTranslationData = array();
  453. $aQueryColumns = array_keys($oSearch->GetSelectedClasses());
  454. foreach ($aAliases as $iColumn => $sAlias)
  455. {
  456. $sQueryAlias = $aQueryColumns[$iColumn];
  457. $aTranslationData[$sAlias]['*'] = $sQueryAlias;
  458. $aQueryGroupByExpr[$sAlias.'id'] = new FieldExpression('id', $sQueryAlias);
  459. }
  460. foreach ($aGroupByExpr as $sExpressionAlias => $oExpression)
  461. {
  462. $aQueryGroupByExpr[$sExpressionAlias] = $oExpression->Translate($aTranslationData, false, false);
  463. }
  464. }
  465. $oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses);
  466. $aSQLQueries[] = $oSubQuery;
  467. }
  468. $oSQLQuery = new SQLUnionQuery($aSQLQueries, $aGroupByExpr);
  469. //MyHelpers::var_dump_html($oSQLQuery, true);
  470. //MyHelpers::var_dump_html($oSQLQuery->RenderSelect(), true);
  471. if (self::$m_bDebugQuery) $oSQLQuery->DisplayHtml();
  472. return $oSQLQuery;
  473. }
  474. }