sqlobjectquery.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. * SQLObjectQuery
  20. * build a mySQL compatible SQL query
  21. *
  22. * @copyright Copyright (C) 2015-2017 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. /**
  26. * SQLObjectQuery
  27. * build a mySQL compatible SQL query
  28. *
  29. * @package iTopORM
  30. */
  31. class SQLObjectQuery extends SQLQuery
  32. {
  33. private $m_SourceOQL = '';
  34. private $m_sTable = '';
  35. private $m_sTableAlias = '';
  36. private $m_aFields = array();
  37. private $m_aGroupBy = array();
  38. private $m_oConditionExpr = null;
  39. private $m_bToDelete = true; // The current table must be listed for deletion ?
  40. private $m_aValues = array(); // Values to set in case of an update query
  41. private $m_oSelectedIdField = null;
  42. private $m_aJoinSelects = array();
  43. private $m_bBeautifulQuery = false;
  44. // Data set by PrepareRendering()
  45. private $__aFrom;
  46. private $__aFields;
  47. private $__aGroupBy;
  48. private $__aDelTables;
  49. private $__aSetValues;
  50. private $__aSelectedIdFields;
  51. public function __construct($sTable, $sTableAlias, $aFields, $bToDelete = true, $aValues = array(), $oSelectedIdField = null)
  52. {
  53. parent::__construct();
  54. // This check is not needed but for developping purposes
  55. //if (!CMDBSource::IsTable($sTable))
  56. //{
  57. // throw new CoreException("Unknown table '$sTable'");
  58. //}
  59. // $aFields must be an array of "alias"=>"expr"
  60. // $oConditionExpr must be a condition tree
  61. // $aValues is an array of "alias"=>value
  62. $this->m_sTable = $sTable;
  63. $this->m_sTableAlias = $sTableAlias;
  64. $this->m_aFields = $aFields;
  65. $this->m_aGroupBy = null;
  66. $this->m_oConditionExpr = null;
  67. $this->m_bToDelete = $bToDelete;
  68. $this->m_aValues = $aValues;
  69. $this->m_oSelectedIdField = $oSelectedIdField;
  70. }
  71. public function GetTableAlias()
  72. {
  73. return $this->m_sTableAlias;
  74. }
  75. public function DisplayHtml()
  76. {
  77. if (count($this->m_aFields) == 0) $sFields = "";
  78. else
  79. {
  80. $aFieldDesc = array();
  81. foreach ($this->m_aFields as $sAlias => $oExpression)
  82. {
  83. $aFieldDesc[] = $oExpression->Render()." as <em>$sAlias</em>";
  84. }
  85. $sFields = " =&gt; ".implode(', ', $aFieldDesc);
  86. }
  87. echo "<b>$this->m_sTable</b>$sFields<br/>\n";
  88. // #@# todo - display html of an expression tree
  89. //$this->m_oConditionExpr->DisplayHtml()
  90. if (count($this->m_aJoinSelects) > 0)
  91. {
  92. echo "Joined to...<br/>\n";
  93. echo "<ul class=\"treeview\">\n";
  94. foreach ($this->m_aJoinSelects as $aJoinInfo)
  95. {
  96. $sJoinType = $aJoinInfo["jointype"];
  97. $oSQLQuery = $aJoinInfo["select"];
  98. if (isset($aJoinInfo["on_expression"]))
  99. {
  100. $sOnCondition = $aJoinInfo["on_expression"]->Render();
  101. echo "<li>Join '$sJoinType', ON ($sOnCondition)".$oSQLQuery->DisplayHtml()."</li>\n";
  102. }
  103. else
  104. {
  105. $sLeftField = $aJoinInfo["leftfield"];
  106. $sRightField = $aJoinInfo["rightfield"];
  107. $sRightTableAlias = $aJoinInfo["righttablealias"];
  108. echo "<li>Join '$sJoinType', $sLeftField, $sRightTableAlias.$sRightField".$oSQLQuery->DisplayHtml()."</li>\n";
  109. }
  110. }
  111. echo "</ul>";
  112. }
  113. $this->PrepareRendering();
  114. echo "From ...<br/>\n";
  115. echo "<pre style=\"font-size: smaller;\">\n";
  116. print_r($this->__aFrom);
  117. echo "</pre>";
  118. }
  119. public function SetSelect($aExpressions)
  120. {
  121. $this->m_aFields = $aExpressions;
  122. }
  123. public function SortSelectedFields()
  124. {
  125. ksort($this->m_aFields);
  126. }
  127. public function AddSelect($sAlias, $oExpression)
  128. {
  129. $this->m_aFields[$sAlias] = $oExpression;
  130. }
  131. public function SetGroupBy($aExpressions)
  132. {
  133. $this->m_aGroupBy = $aExpressions;
  134. }
  135. public function SetCondition($oConditionExpr)
  136. {
  137. $this->m_oConditionExpr = $oConditionExpr;
  138. }
  139. public function AddCondition($oConditionExpr)
  140. {
  141. if (is_null($this->m_oConditionExpr))
  142. {
  143. $this->m_oConditionExpr = $oConditionExpr;
  144. }
  145. else
  146. {
  147. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oConditionExpr);
  148. }
  149. }
  150. private function AddJoin($sJoinType, $oSQLQuery, $sLeftField, $sRightField, $sRightTableAlias = '')
  151. {
  152. assert((get_class($oSQLQuery) == __CLASS__) || is_subclass_of($oSQLQuery, __CLASS__));
  153. // No need to check this here but for development purposes
  154. //if (!CMDBSource::IsField($this->m_sTable, $sLeftField))
  155. //{
  156. // throw new CoreException("Unknown field '$sLeftField' in table '".$this->m_sTable);
  157. //}
  158. if (empty($sRightTableAlias))
  159. {
  160. $sRightTableAlias = $oSQLQuery->m_sTableAlias;
  161. }
  162. // #@# Could not be verified here because the namespace is unknown - do we need to check it there?
  163. //
  164. // if (!CMDBSource::IsField($sRightTable, $sRightField))
  165. // {
  166. // throw new CoreException("Unknown field '$sRightField' in table '".$sRightTable."'");
  167. // }
  168. $this->m_aJoinSelects[] = array(
  169. "jointype" => $sJoinType,
  170. "select" => $oSQLQuery,
  171. "leftfield" => $sLeftField,
  172. "rightfield" => $sRightField,
  173. "righttablealias" => $sRightTableAlias
  174. );
  175. }
  176. public function AddInnerJoin($oSQLQuery, $sLeftField, $sRightField, $sRightTable = '')
  177. {
  178. $this->AddJoin("inner", $oSQLQuery, $sLeftField, $sRightField, $sRightTable);
  179. }
  180. public function AddInnerJoinTree($oSQLQuery, $sLeftFieldLeft, $sLeftFieldRight, $sRightFieldLeft, $sRightFieldRight, $sRightTableAlias = '', $iOperatorCode = TREE_OPERATOR_BELOW, $bInvertOnClause = false)
  181. {
  182. assert((get_class($oSQLQuery) == __CLASS__) || is_subclass_of($oSQLQuery, __CLASS__));
  183. if (empty($sRightTableAlias))
  184. {
  185. $sRightTableAlias = $oSQLQuery->m_sTableAlias;
  186. }
  187. $this->m_aJoinSelects[] = array(
  188. "jointype" => 'inner_tree',
  189. "select" => $oSQLQuery,
  190. "leftfield" => $sLeftFieldLeft,
  191. "rightfield" => $sLeftFieldRight,
  192. "rightfield_left" => $sRightFieldLeft,
  193. "rightfield_right" => $sRightFieldRight,
  194. "righttablealias" => $sRightTableAlias,
  195. "tree_operator" => $iOperatorCode,
  196. 'invert_on_clause' => $bInvertOnClause
  197. );
  198. }
  199. public function AddLeftJoin($oSQLQuery, $sLeftField, $sRightField)
  200. {
  201. return $this->AddJoin("left", $oSQLQuery, $sLeftField, $sRightField);
  202. }
  203. public function AddInnerJoinEx(SQLQuery $oSQLQuery, Expression $oOnExpression)
  204. {
  205. $this->m_aJoinSelects[] = array(
  206. "jointype" => 'inner',
  207. "select" => $oSQLQuery,
  208. "on_expression" => $oOnExpression
  209. );
  210. }
  211. public function AddLeftJoinEx(SQLQuery $oSQLQuery, Expression $oOnExpression)
  212. {
  213. $this->m_aJoinSelects[] = array(
  214. "jointype" => 'left',
  215. "select" => $oSQLQuery,
  216. "on_expression" => $oOnExpression
  217. );
  218. }
  219. // Interface, build the SQL query
  220. public function RenderDelete($aArgs = array())
  221. {
  222. $this->PrepareRendering();
  223. // Target: DELETE myAlias1, myAlias2 FROM t1 as myAlias1, t2 as myAlias2, t3 as topreserve WHERE ...
  224. $sDelete = self::ClauseDelete($this->__aDelTables);
  225. $sFrom = self::ClauseFrom($this->__aFrom);
  226. // #@# safety net to redo ?
  227. /*
  228. if ($this->m_oConditionExpr->IsAny())
  229. -- if (count($aConditions) == 0) --
  230. {
  231. throw new CoreException("Building a request wich will delete every object of a given table -looks suspicious- please use truncate instead...");
  232. }
  233. */
  234. if (is_null($this->m_oConditionExpr))
  235. {
  236. // Delete all !!!
  237. }
  238. else
  239. {
  240. $sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
  241. return "DELETE $sDelete FROM $sFrom WHERE $sWhere";
  242. }
  243. }
  244. /**
  245. * Needed for the unions
  246. */
  247. public function RenderSelectClause()
  248. {
  249. $this->PrepareRendering();
  250. $sSelect = self::ClauseSelect($this->__aFields);
  251. return $sSelect;
  252. }
  253. /**
  254. * Needed for the unions
  255. */
  256. public function RenderOrderByClause($aOrderBy)
  257. {
  258. $this->PrepareRendering();
  259. $sOrderBy = self::ClauseOrderBy($aOrderBy);
  260. return $sOrderBy;
  261. }
  262. // Interface, build the SQL query
  263. public function RenderUpdate($aArgs = array())
  264. {
  265. $this->PrepareRendering();
  266. $sFrom = self::ClauseFrom($this->__aFrom);
  267. $sValues = self::ClauseValues($this->__aSetValues);
  268. $sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
  269. return "UPDATE $sFrom SET $sValues WHERE $sWhere";
  270. }
  271. // Interface, build the SQL query
  272. public function RenderSelect($aOrderBy = array(), $aArgs = array(), $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulQuery = false)
  273. {
  274. $this->m_bBeautifulQuery = $bBeautifulQuery;
  275. $sLineSep = $this->m_bBeautifulQuery ? "\n" : '';
  276. $sIndent = $this->m_bBeautifulQuery ? " " : null;
  277. $this->PrepareRendering();
  278. $sFrom = self::ClauseFrom($this->__aFrom, $sIndent);
  279. $sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
  280. if ($bGetCount)
  281. {
  282. if (count($this->__aSelectedIdFields) > 0)
  283. {
  284. $aCountFields = array();
  285. foreach ($this->__aSelectedIdFields as $sFieldExpr)
  286. {
  287. $aCountFields[] = "COALESCE($sFieldExpr, 0)"; // Null values are excluded from the count
  288. }
  289. $sCountFields = implode(', ', $aCountFields);
  290. $sSQL = "SELECT$sLineSep COUNT(DISTINCT $sCountFields) AS COUNT$sLineSep FROM $sFrom$sLineSep WHERE $sWhere";
  291. }
  292. else
  293. {
  294. $sSQL = "SELECT$sLineSep COUNT(*) AS COUNT$sLineSep FROM $sFrom$sLineSep WHERE $sWhere";
  295. }
  296. }
  297. else
  298. {
  299. $sSelect = self::ClauseSelect($this->__aFields);
  300. $sOrderBy = self::ClauseOrderBy($aOrderBy);
  301. if (!empty($sOrderBy))
  302. {
  303. $sOrderBy = "ORDER BY $sOrderBy$sLineSep";
  304. }
  305. if ($iLimitCount > 0)
  306. {
  307. $sLimit = 'LIMIT '.$iLimitStart.', '.$iLimitCount;
  308. }
  309. else
  310. {
  311. $sLimit = '';
  312. }
  313. $sSQL = "SELECT$sLineSep DISTINCT $sSelect$sLineSep FROM $sFrom$sLineSep WHERE $sWhere$sLineSep $sOrderBy $sLimit";
  314. }
  315. return $sSQL;
  316. }
  317. // Interface, build the SQL query
  318. public function RenderGroupBy($aArgs = array(), $bBeautifulQuery = false)
  319. {
  320. $this->m_bBeautifulQuery = $bBeautifulQuery;
  321. $sLineSep = $this->m_bBeautifulQuery ? "\n" : '';
  322. $sIndent = $this->m_bBeautifulQuery ? " " : null;
  323. $this->PrepareRendering();
  324. $sSelect = self::ClauseSelect($this->__aFields);
  325. $sFrom = self::ClauseFrom($this->__aFrom, $sIndent);
  326. $sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
  327. $sGroupBy = self::ClauseGroupBy($this->__aGroupBy);
  328. $sSQL = "SELECT $sSelect,$sLineSep COUNT(*) AS _itop_count_$sLineSep FROM $sFrom$sLineSep WHERE $sWhere$sLineSep GROUP BY $sGroupBy";
  329. return $sSQL;
  330. }
  331. // Purpose: prepare the query data, once for all
  332. private function PrepareRendering()
  333. {
  334. if (is_null($this->__aFrom))
  335. {
  336. $this->__aFrom = array();
  337. $this->__aFields = array();
  338. $this->__aGroupBy = array();
  339. $this->__aDelTables = array();
  340. $this->__aSetValues = array();
  341. $this->__aSelectedIdFields = array();
  342. $this->PrepareSingleTable($this, $this->__aFrom, '', array('jointype' => 'first'));
  343. }
  344. }
  345. private function PrepareSingleTable(SQLObjectQuery $oRootQuery, &$aFrom, $sCallerAlias = '', $aJoinData)
  346. {
  347. $aTranslationTable[$this->m_sTable]['*'] = $this->m_sTableAlias;
  348. // Handle the various kinds of join (or first table in the list)
  349. //
  350. if (empty($aJoinData['righttablealias']))
  351. {
  352. $sRightTableAlias = $this->m_sTableAlias;
  353. }
  354. else
  355. {
  356. $sRightTableAlias = $aJoinData['righttablealias'];
  357. }
  358. switch ($aJoinData['jointype'])
  359. {
  360. case "first":
  361. $aFrom[$this->m_sTableAlias] = array("jointype"=>"first", "tablename"=>$this->m_sTable, "joincondition"=>"");
  362. break;
  363. case "inner":
  364. case "left":
  365. if (isset($aJoinData["on_expression"]))
  366. {
  367. $sJoinCond = $aJoinData["on_expression"]->Render();
  368. }
  369. else
  370. {
  371. $sJoinCond = "`$sCallerAlias`.`{$aJoinData['leftfield']}` = `$sRightTableAlias`.`{$aJoinData['rightfield']}`";
  372. }
  373. $aFrom[$this->m_sTableAlias] = array("jointype"=>$aJoinData['jointype'], "tablename"=>$this->m_sTable, "joincondition"=>"$sJoinCond");
  374. break;
  375. case "inner_tree":
  376. if ($aJoinData['invert_on_clause'])
  377. {
  378. $sRootLeft = "`$sCallerAlias`.`{$aJoinData['leftfield']}`";
  379. $sRootRight = "`$sCallerAlias`.`{$aJoinData['rightfield']}`";
  380. $sNodeLeft = "`$sRightTableAlias`.`{$aJoinData['rightfield_left']}`";
  381. $sNodeRight = "`$sRightTableAlias`.`{$aJoinData['rightfield_right']}`";
  382. }
  383. else
  384. {
  385. $sNodeLeft = "`$sCallerAlias`.`{$aJoinData['leftfield']}`";
  386. $sNodeRight = "`$sCallerAlias`.`{$aJoinData['rightfield']}`";
  387. $sRootLeft = "`$sRightTableAlias`.`{$aJoinData['rightfield_left']}`";
  388. $sRootRight = "`$sRightTableAlias`.`{$aJoinData['rightfield_right']}`";
  389. }
  390. switch($aJoinData['tree_operator'])
  391. {
  392. case TREE_OPERATOR_BELOW:
  393. $sJoinCond = "$sNodeLeft >= $sRootLeft AND $sNodeLeft <= $sRootRight";
  394. break;
  395. case TREE_OPERATOR_BELOW_STRICT:
  396. $sJoinCond = "$sNodeLeft > $sRootLeft AND $sNodeLeft < $sRootRight";
  397. break;
  398. case TREE_OPERATOR_NOT_BELOW: // Complementary of 'BELOW'
  399. $sJoinCond = "$sNodeLeft < $sRootLeft OR $sNodeLeft > $sRootRight";
  400. break;
  401. case TREE_OPERATOR_NOT_BELOW_STRICT: // Complementary of BELOW_STRICT
  402. $sJoinCond = "$sNodeLeft <= $sRootLeft OR $sNodeLeft >= $sRootRight";
  403. break;
  404. case TREE_OPERATOR_ABOVE:
  405. $sJoinCond = "$sNodeLeft <= $sRootLeft AND $sNodeRight >= $sRootRight";
  406. break;
  407. case TREE_OPERATOR_ABOVE_STRICT:
  408. $sJoinCond = "$sNodeLeft < $sRootLeft AND $sNodeRight > $sRootRight";
  409. break;
  410. case TREE_OPERATOR_NOT_ABOVE: // Complementary of 'ABOVE'
  411. $sJoinCond = "$sNodeLeft > $sRootLeft OR $sNodeRight < $sRootRight";
  412. break;
  413. case TREE_OPERATOR_NOT_ABOVE_STRICT: // Complementary of ABOVE_STRICT
  414. $sJoinCond = "$sNodeLeft >= $sRootLeft OR $sNodeRight <= $sRootRight";
  415. break;
  416. }
  417. $aFrom[$this->m_sTableAlias] = array("jointype"=>$aJoinData['jointype'], "tablename"=>$this->m_sTable, "joincondition"=>"$sJoinCond");
  418. break;
  419. }
  420. // Given the alias, modify the fields and conditions
  421. // before adding them into the current lists
  422. //
  423. foreach($this->m_aFields as $sAlias => $oExpression)
  424. {
  425. $oRootQuery->__aFields["`$sAlias`"] = $oExpression->Render();
  426. }
  427. if ($this->m_aGroupBy)
  428. {
  429. foreach($this->m_aGroupBy as $sAlias => $oExpression)
  430. {
  431. $oRootQuery->__aGroupBy["`$sAlias`"] = $oExpression->Render();
  432. }
  433. }
  434. if ($this->m_bToDelete)
  435. {
  436. $oRootQuery->__aDelTables[] = "`{$this->m_sTableAlias}`";
  437. }
  438. foreach($this->m_aValues as $sFieldName=>$value)
  439. {
  440. $oRootQuery->__aSetValues["`{$this->m_sTableAlias}`.`$sFieldName`"] = $value; // quoted further!
  441. }
  442. if (!is_null($this->m_oSelectedIdField))
  443. {
  444. $oRootQuery->__aSelectedIdFields[] = $this->m_oSelectedIdField->Render();
  445. }
  446. // loop on joins, to complete the list of tables/fields/conditions
  447. //
  448. $aTempFrom = array(); // temporary subset of 'from' specs, to be grouped in the final query
  449. foreach ($this->m_aJoinSelects as $aJoinData)
  450. {
  451. $oRightSelect = $aJoinData["select"];
  452. $sJoinTableAlias = $oRightSelect->PrepareSingleTable($oRootQuery, $aTempFrom, $this->m_sTableAlias, $aJoinData);
  453. }
  454. $aFrom[$this->m_sTableAlias]['subfrom'] = $aTempFrom;
  455. return $this->m_sTableAlias;
  456. }
  457. public function OptimizeJoins($aUsedTables, $bTopCall = true)
  458. {
  459. if ($bTopCall)
  460. {
  461. // Top call: complete the list of tables absolutely required to perform the right query
  462. $this->CollectUsedTables($aUsedTables);
  463. }
  464. $aToDiscard = array();
  465. foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
  466. {
  467. $oSQLQuery = $aJoinInfo["select"];
  468. $sTableAlias = $oSQLQuery->GetTableAlias();
  469. if ($oSQLQuery->OptimizeJoins($aUsedTables, false) && !array_key_exists($sTableAlias, $aUsedTables))
  470. {
  471. $aToDiscard[] = $i;
  472. }
  473. }
  474. foreach ($aToDiscard as $i)
  475. {
  476. unset($this->m_aJoinSelects[$i]);
  477. }
  478. return (count($this->m_aJoinSelects) == 0);
  479. }
  480. public function CountTables()
  481. {
  482. $iRet = 1;
  483. foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
  484. {
  485. $oSQLQuery = $aJoinInfo["select"];
  486. $iRet += $oSQLQuery->CountTables();
  487. }
  488. return $iRet;
  489. }
  490. protected function CollectUsedTables(&$aTables)
  491. {
  492. $this->m_oConditionExpr->CollectUsedParents($aTables);
  493. foreach($this->m_aFields as $sFieldAlias => $oField)
  494. {
  495. $oField->CollectUsedParents($aTables);
  496. }
  497. if ($this->m_aGroupBy)
  498. {
  499. foreach($this->m_aGroupBy as $sAlias => $oExpression)
  500. {
  501. $oExpression->CollectUsedParents($aTables);
  502. }
  503. }
  504. if (!is_null($this->m_oSelectedIdField))
  505. {
  506. $this->m_oSelectedIdField->CollectUsedParents($aTables);
  507. }
  508. foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
  509. {
  510. $oSQLQuery = $aJoinInfo["select"];
  511. if ($oSQLQuery->HasRequiredTables($aTables))
  512. {
  513. // There is something required in the branch, then this node is a MUST
  514. if (isset($aJoinInfo['righttablealias']))
  515. {
  516. $aTables[$aJoinInfo['righttablealias']] = true;
  517. }
  518. if (isset($aJoinInfo["on_expression"]))
  519. {
  520. $sJoinCond = $aJoinInfo["on_expression"]->CollectUsedParents($aTables);
  521. }
  522. }
  523. }
  524. return $aTables;
  525. }
  526. // Is required in the JOIN, and therefore we must ensure that the join expression will be valid
  527. protected function HasRequiredTables(&$aTables)
  528. {
  529. $bResult = false;
  530. if (array_key_exists($this->m_sTableAlias, $aTables))
  531. {
  532. $bResult = true;
  533. }
  534. foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
  535. {
  536. $oSQLQuery = $aJoinInfo["select"];
  537. if ($oSQLQuery->HasRequiredTables($aTables))
  538. {
  539. // There is something required in the branch, then this node is a MUST
  540. if (isset($aJoinInfo['righttablealias']))
  541. {
  542. $aTables[$aJoinInfo['righttablealias']] = true;
  543. }
  544. if (isset($aJoinInfo["on_expression"]))
  545. {
  546. $sJoinCond = $aJoinInfo["on_expression"]->CollectUsedParents($aTables);
  547. }
  548. $bResult = true;
  549. }
  550. }
  551. // None of the tables is in the list of required tables
  552. return $bResult;
  553. }
  554. }