dbunionsearch.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. public function IsAny()
  183. {
  184. $bIsAny = true;
  185. foreach ($this->aSearches as $oSearch)
  186. {
  187. if (!$oSearch->IsAny())
  188. {
  189. $bIsAny = false;
  190. break;
  191. }
  192. }
  193. return $bIsAny;
  194. }
  195. public function ResetCondition()
  196. {
  197. foreach ($this->aSearches as $oSearch)
  198. {
  199. $oSearch->ResetCondition();
  200. }
  201. }
  202. public function MergeConditionExpression($oExpression)
  203. {
  204. $aAliases = array_keys($this->aSelectedClasses);
  205. foreach ($this->aSearches as $iSearchIndex => $oSearch)
  206. {
  207. $oClonedExpression = $oExpression->DeepClone();
  208. if ($iSearchIndex != 0)
  209. {
  210. foreach (array_keys($oSearch->GetSelectedClasses()) as $iColumn => $sSearchAlias)
  211. {
  212. $oClonedExpression->RenameAlias($aAliases[$iColumn], $sSearchAlias);
  213. }
  214. }
  215. $oSearch->MergeConditionExpression($oClonedExpression);
  216. }
  217. }
  218. public function AddConditionExpression($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->AddConditionExpression($oClonedExpression);
  232. }
  233. }
  234. public function AddNameCondition($sName)
  235. {
  236. foreach ($this->aSearches as $oSearch)
  237. {
  238. $oSearch->AddNameCondition($sName);
  239. }
  240. }
  241. public function AddCondition($sFilterCode, $value, $sOpCode = null)
  242. {
  243. foreach ($this->aSearches as $oSearch)
  244. {
  245. $oSearch->AddCondition($sFilterCode, $value, $sOpCode);
  246. }
  247. }
  248. /**
  249. * Specify a condition on external keys or link sets
  250. * @param sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
  251. * Example: infra_list->ci_id->location_id->country
  252. * @param value The value to match (can be an array => IN(val1, val2...)
  253. * @return void
  254. */
  255. public function AddConditionAdvanced($sAttSpec, $value)
  256. {
  257. foreach ($this->aSearches as $oSearch)
  258. {
  259. $oSearch->AddConditionAdvanced($sAttSpec, $value);
  260. }
  261. }
  262. public function AddCondition_FullText($sFullText)
  263. {
  264. foreach ($this->aSearches as $oSearch)
  265. {
  266. $oSearch->AddCondition_FullText($sFullText);
  267. }
  268. }
  269. public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS)
  270. {
  271. foreach ($this->aSearches as $oSearch)
  272. {
  273. $oSearch->AddCondition_PointingTo($oFilter, $sExtKeyAttCode, $iOperatorCode);
  274. }
  275. }
  276. public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS)
  277. {
  278. foreach ($this->aSearches as $oSearch)
  279. {
  280. $oSearch->AddCondition_ReferencedBy($oFilter, $sForeignExtKeyAttCode, $iOperatorCode);
  281. }
  282. }
  283. public function Intersect(DBSearch $oFilter)
  284. {
  285. $aSearches = array();
  286. foreach ($this->aSearches as $oSearch)
  287. {
  288. $aSearches[] = $oSearch->Intersect($oFilter);
  289. }
  290. return new DBUnionSearch($aSearches);
  291. }
  292. public function SetInternalParams($aParams)
  293. {
  294. foreach ($this->aSearches as $oSearch)
  295. {
  296. $oSearch->SetInternalParams($aParams);
  297. }
  298. }
  299. public function GetInternalParams()
  300. {
  301. $aParams = array();
  302. foreach ($this->aSearches as $oSearch)
  303. {
  304. $aParams = array_merge($oSearch->GetInternalParams(), $aParams);
  305. }
  306. return $aParams;
  307. }
  308. public function GetQueryParams($bExcludeMagicParams = true)
  309. {
  310. $aParams = array();
  311. foreach ($this->aSearches as $oSearch)
  312. {
  313. $aParams = array_merge($oSearch->GetQueryParams($bExcludeMagicParams), $aParams);
  314. }
  315. return $aParams;
  316. }
  317. public function ListConstantFields()
  318. {
  319. // Somewhat complex to implement for unions, for a poor benefit
  320. return array();
  321. }
  322. /**
  323. * Turn the parameters (:xxx) into scalar values in order to easily
  324. * serialize a search
  325. */
  326. public function ApplyParameters($aArgs)
  327. {
  328. foreach ($this->aSearches as $oSearch)
  329. {
  330. $oSearch->ApplyParameters($aArgs);
  331. }
  332. }
  333. /**
  334. * Overloads for query building
  335. */
  336. public function ToOQL($bDevelopParams = false, $aContextParams = null, $bWithAllowAllFlag = false)
  337. {
  338. $aSubQueries = array();
  339. foreach ($this->aSearches as $oSearch)
  340. {
  341. $aSubQueries[] = $oSearch->ToOQL($bDevelopParams, $aContextParams, $bWithAllowAllFlag);
  342. }
  343. $sRet = implode(' UNION ', $aSubQueries);
  344. return $sRet;
  345. }
  346. /**
  347. * Returns a new DBUnionSearch object where duplicates queries have been removed based on their OQLs
  348. *
  349. * @return \DBUnionSearch
  350. */
  351. public function RemoveDuplicateQueries()
  352. {
  353. $aQueries = array();
  354. $aSearches = array();
  355. foreach ($this->GetSearches() as $oTmpSearch)
  356. {
  357. $sQuery = $oTmpSearch->ToOQL(true);
  358. if (!in_array($sQuery, $aQueries))
  359. {
  360. $aQueries[] = $sQuery;
  361. $aSearches[] = $oTmpSearch;
  362. }
  363. }
  364. $oNewSearch = new DBUnionSearch($aSearches);
  365. return $oNewSearch;
  366. }
  367. ////////////////////////////////////////////////////////////////////////////
  368. //
  369. // Construction of the SQL queries
  370. //
  371. ////////////////////////////////////////////////////////////////////////////
  372. public function MakeDeleteQuery($aArgs = array())
  373. {
  374. throw new Exception('MakeDeleteQuery is not implemented for the unions!');
  375. }
  376. public function MakeUpdateQuery($aValues, $aArgs = array())
  377. {
  378. throw new Exception('MakeUpdateQuery is not implemented for the unions!');
  379. }
  380. protected function MakeSQLQuery($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr = null, $aSelectedClasses = null)
  381. {
  382. if (count($this->aSearches) == 1)
  383. {
  384. return $this->aSearches[0]->MakeSQLQuery($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr);
  385. }
  386. $aSQLQueries = array();
  387. $aAliases = array_keys($this->aSelectedClasses);
  388. foreach ($this->aSearches as $iSearch => $oSearch)
  389. {
  390. $aSearchAliases = array_keys($oSearch->GetSelectedClasses());
  391. // The selected classes from the query build perspective are the lowest common ancestors amongst the various queries
  392. // (used when it comes to determine which attributes must be selected)
  393. $aSearchSelectedClasses = array();
  394. foreach ($aSearchAliases as $iColumn => $sSearchAlias)
  395. {
  396. $sAlias = $aAliases[$iColumn];
  397. $aSearchSelectedClasses[$sSearchAlias] = $this->aSelectedClasses[$sAlias];
  398. }
  399. if (is_null($aAttToLoad))
  400. {
  401. $aQueryAttToLoad = null;
  402. }
  403. else
  404. {
  405. // (Eventually) Transform the aliases
  406. $aQueryAttToLoad = array();
  407. foreach ($aAttToLoad as $sAlias => $aAttributes)
  408. {
  409. $iColumn = array_search($sAlias, $aAliases);
  410. $sQueryAlias = ($iColumn === false) ? $sAlias : $aSearchAliases[$iColumn];
  411. $aQueryAttToLoad[$sQueryAlias] = $aAttributes;
  412. }
  413. }
  414. if (is_null($aGroupByExpr))
  415. {
  416. $aQueryGroupByExpr = null;
  417. }
  418. else
  419. {
  420. // Clone (and eventually transform) the group by expressions
  421. $aQueryGroupByExpr = array();
  422. $aTranslationData = array();
  423. $aQueryColumns = array_keys($oSearch->GetSelectedClasses());
  424. foreach ($aAliases as $iColumn => $sAlias)
  425. {
  426. $sQueryAlias = $aQueryColumns[$iColumn];
  427. $aTranslationData[$sAlias]['*'] = $sQueryAlias;
  428. $aQueryGroupByExpr[$sAlias.'id'] = new FieldExpression('id', $sQueryAlias);
  429. }
  430. foreach ($aGroupByExpr as $sExpressionAlias => $oExpression)
  431. {
  432. $aQueryGroupByExpr[$sExpressionAlias] = $oExpression->Translate($aTranslationData, false, false);
  433. }
  434. }
  435. $oSubQuery = $oSearch->MakeSQLQuery($aQueryAttToLoad, false, $aModifierProperties, $aQueryGroupByExpr, $aSearchSelectedClasses);
  436. $aSQLQueries[] = $oSubQuery;
  437. }
  438. $oSQLQuery = new SQLUnionQuery($aSQLQueries, $aGroupByExpr);
  439. //MyHelpers::var_dump_html($oSQLQuery, true);
  440. //MyHelpers::var_dump_html($oSQLQuery->RenderSelect(), true);
  441. if (self::$m_bDebugQuery) $oSQLQuery->DisplayHtml();
  442. return $oSQLQuery;
  443. }
  444. }