dbunionsearch.class.php 12 KB

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