sqlobjectquery.class.inc.php 18 KB

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