sqlobjectquery.class.inc.php 18 KB

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