dbunionsearch.class.php 13 KB

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