mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-19 16:48:42 +02:00
N°8796 - Add PHP code style validation in iTop and extensions - format whole code base
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
<?php
|
||||
|
||||
// Copyright (C) 2015-2024 Combodo SAS
|
||||
//
|
||||
// This file is part of iTop.
|
||||
//
|
||||
// iTop is free software; you can redistribute it and/or modify
|
||||
// iTop is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
@@ -16,7 +17,6 @@
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with iTop. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
|
||||
/**
|
||||
* SQLObjectQuery
|
||||
* build a mySQL compatible SQL query
|
||||
@@ -25,7 +25,6 @@
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* SQLObjectQuery
|
||||
* build a mySQL compatible SQL query
|
||||
@@ -33,20 +32,19 @@
|
||||
* @package iTopORM
|
||||
*/
|
||||
|
||||
|
||||
class SQLObjectQuery extends SQLQuery
|
||||
{
|
||||
public $m_aContextData = null;
|
||||
public $m_iOriginalTableCount = 0;
|
||||
private $m_sTable = '';
|
||||
private $m_sTableAlias = '';
|
||||
private $m_aFields = array();
|
||||
private $m_aGroupBy = array();
|
||||
private $m_aFields = [];
|
||||
private $m_aGroupBy = [];
|
||||
private $m_oConditionExpr = null;
|
||||
private $m_bToDelete = true; // The current table must be listed for deletion ?
|
||||
private $m_aValues = array(); // Values to set in case of an update query
|
||||
private $m_aValues = []; // Values to set in case of an update query
|
||||
private $m_oSelectedIdField = null;
|
||||
private $m_aJoinSelects = array();
|
||||
private $m_aJoinSelects = [];
|
||||
protected $m_bBeautifulQuery = false;
|
||||
|
||||
// Data set by PrepareRendering()
|
||||
@@ -57,8 +55,7 @@ class SQLObjectQuery extends SQLQuery
|
||||
private $__aSetValues;
|
||||
private $__aSelectedIdFields;
|
||||
|
||||
|
||||
public function __construct($sTable, $sTableAlias, $aFields, $bToDelete = true, $aValues = array(), $oSelectedIdField = null)
|
||||
public function __construct($sTable, $sTableAlias, $aFields, $bToDelete = true, $aValues = [], $oSelectedIdField = null)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
@@ -89,12 +86,11 @@ class SQLObjectQuery extends SQLQuery
|
||||
|
||||
public function DisplayHtml()
|
||||
{
|
||||
if (count($this->m_aFields) == 0) $sFields = "";
|
||||
else
|
||||
{
|
||||
$aFieldDesc = array();
|
||||
foreach ($this->m_aFields as $sAlias => $oExpression)
|
||||
{
|
||||
if (count($this->m_aFields) == 0) {
|
||||
$sFields = "";
|
||||
} else {
|
||||
$aFieldDesc = [];
|
||||
foreach ($this->m_aFields as $sAlias => $oExpression) {
|
||||
$aFieldDesc[] = $oExpression->RenderExpression(false)." as <em>$sAlias</em>";
|
||||
}
|
||||
$sFields = " => ".implode(', ', $aFieldDesc);
|
||||
@@ -102,26 +98,21 @@ class SQLObjectQuery extends SQLQuery
|
||||
echo "<b>$this->m_sTable</b>$sFields<br/>\n";
|
||||
// #@# todo - display html of an expression tree
|
||||
//$this->m_oConditionExpr->DisplayHtml()
|
||||
if (count($this->m_aJoinSelects) > 0)
|
||||
{
|
||||
if (count($this->m_aJoinSelects) > 0) {
|
||||
echo "Joined to...<br/>\n";
|
||||
echo "<ul class=\"treeview\">\n";
|
||||
foreach ($this->m_aJoinSelects as $aJoinInfo)
|
||||
{
|
||||
foreach ($this->m_aJoinSelects as $aJoinInfo) {
|
||||
$sJoinType = $aJoinInfo["jointype"];
|
||||
$oSQLQuery = $aJoinInfo["select"];
|
||||
if (isset($aJoinInfo["on_expression"]))
|
||||
{
|
||||
if (isset($aJoinInfo["on_expression"])) {
|
||||
$sOnCondition = $aJoinInfo["on_expression"]->RenderExpression(false);
|
||||
|
||||
echo "<li>Join '$sJoinType', ON ($sOnCondition)".$oSQLQuery->DisplayHtml()."</li>\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sLeftField = $aJoinInfo["leftfield"];
|
||||
$sRightField = $aJoinInfo["rightfield"];
|
||||
$sRightTableAlias = $aJoinInfo["righttablealias"];
|
||||
|
||||
|
||||
echo "<li>Join '$sJoinType', $sLeftField, $sRightTableAlias.$sRightField".$oSQLQuery->DisplayHtml()."</li>\n";
|
||||
}
|
||||
}
|
||||
@@ -161,12 +152,9 @@ class SQLObjectQuery extends SQLQuery
|
||||
|
||||
public function AddCondition($oConditionExpr)
|
||||
{
|
||||
if (is_null($this->m_oConditionExpr))
|
||||
{
|
||||
if (is_null($this->m_oConditionExpr)) {
|
||||
$this->m_oConditionExpr = $oConditionExpr;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oConditionExpr);
|
||||
}
|
||||
}
|
||||
@@ -180,23 +168,22 @@ class SQLObjectQuery extends SQLQuery
|
||||
// throw new CoreException("Unknown field '$sLeftField' in table '".$this->m_sTable);
|
||||
//}
|
||||
|
||||
if (empty($sRightTableAlias))
|
||||
{
|
||||
if (empty($sRightTableAlias)) {
|
||||
$sRightTableAlias = $oSQLQuery->m_sTableAlias;
|
||||
}
|
||||
// #@# Could not be verified here because the namespace is unknown - do we need to check it there?
|
||||
//
|
||||
// if (!CMDBSource::IsField($sRightTable, $sRightField))
|
||||
// {
|
||||
// throw new CoreException("Unknown field '$sRightField' in table '".$sRightTable."'");
|
||||
// }
|
||||
$this->m_aJoinSelects[] = array(
|
||||
// #@# Could not be verified here because the namespace is unknown - do we need to check it there?
|
||||
//
|
||||
// if (!CMDBSource::IsField($sRightTable, $sRightField))
|
||||
// {
|
||||
// throw new CoreException("Unknown field '$sRightField' in table '".$sRightTable."'");
|
||||
// }
|
||||
$this->m_aJoinSelects[] = [
|
||||
"jointype" => $sJoinType,
|
||||
"select" => $oSQLQuery,
|
||||
"leftfield" => $sLeftField,
|
||||
"rightfield" => $sRightField,
|
||||
"righttablealias" => $sRightTableAlias
|
||||
);
|
||||
"righttablealias" => $sRightTableAlias,
|
||||
];
|
||||
}
|
||||
public function AddInnerJoin($oSQLQuery, $sLeftField, $sRightField, $sRightTable = '')
|
||||
{
|
||||
@@ -205,11 +192,10 @@ class SQLObjectQuery extends SQLQuery
|
||||
public function AddInnerJoinTree($oSQLQuery, $sLeftFieldLeft, $sLeftFieldRight, $sRightFieldLeft, $sRightFieldRight, $sRightTableAlias = '', $iOperatorCode = TREE_OPERATOR_BELOW, $bInvertOnClause = false)
|
||||
{
|
||||
assert((get_class($oSQLQuery) == __CLASS__) || is_subclass_of($oSQLQuery, __CLASS__));
|
||||
if (empty($sRightTableAlias))
|
||||
{
|
||||
if (empty($sRightTableAlias)) {
|
||||
$sRightTableAlias = $oSQLQuery->m_sTableAlias;
|
||||
}
|
||||
$this->m_aJoinSelects[] = array(
|
||||
$this->m_aJoinSelects[] = [
|
||||
"jointype" => 'inner_tree',
|
||||
"select" => $oSQLQuery,
|
||||
"leftfield" => $sLeftFieldLeft,
|
||||
@@ -218,8 +204,8 @@ class SQLObjectQuery extends SQLQuery
|
||||
"rightfield_right" => $sRightFieldRight,
|
||||
"righttablealias" => $sRightTableAlias,
|
||||
"tree_operator" => $iOperatorCode,
|
||||
'invert_on_clause' => $bInvertOnClause
|
||||
);
|
||||
'invert_on_clause' => $bInvertOnClause,
|
||||
];
|
||||
}
|
||||
public function AddLeftJoin($oSQLQuery, $sLeftField, $sRightField)
|
||||
{
|
||||
@@ -228,22 +214,22 @@ class SQLObjectQuery extends SQLQuery
|
||||
|
||||
public function AddInnerJoinEx(SQLQuery $oSQLQuery, Expression $oOnExpression)
|
||||
{
|
||||
$this->m_aJoinSelects[] = array(
|
||||
$this->m_aJoinSelects[] = [
|
||||
"jointype" => 'inner',
|
||||
"select" => $oSQLQuery,
|
||||
"on_expression" => $oOnExpression
|
||||
);
|
||||
"on_expression" => $oOnExpression,
|
||||
];
|
||||
}
|
||||
|
||||
public function AddLeftJoinEx(SQLQuery $oSQLQuery, Expression $oOnExpression)
|
||||
{
|
||||
$this->m_aJoinSelects[] = array(
|
||||
$this->m_aJoinSelects[] = [
|
||||
"jointype" => 'left',
|
||||
"select" => $oSQLQuery,
|
||||
"on_expression" => $oOnExpression
|
||||
);
|
||||
"on_expression" => $oOnExpression,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
// Interface, build the SQL query
|
||||
|
||||
/**
|
||||
@@ -251,7 +237,7 @@ class SQLObjectQuery extends SQLQuery
|
||||
* @return string
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function RenderDelete($aArgs = array())
|
||||
public function RenderDelete($aArgs = [])
|
||||
{
|
||||
$this->PrepareRendering();
|
||||
|
||||
@@ -267,12 +253,9 @@ class SQLObjectQuery extends SQLQuery
|
||||
throw new CoreException("Building a request wich will delete every object of a given table -looks suspicious- please use truncate instead...");
|
||||
}
|
||||
*/
|
||||
if (is_null($this->m_oConditionExpr))
|
||||
{
|
||||
if (is_null($this->m_oConditionExpr)) {
|
||||
// Delete all !!!
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
|
||||
return "DELETE $sDelete FROM $sFrom WHERE $sWhere";
|
||||
}
|
||||
@@ -309,7 +292,7 @@ class SQLObjectQuery extends SQLQuery
|
||||
* @return string
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function RenderUpdate($aArgs = array())
|
||||
public function RenderUpdate($aArgs = [])
|
||||
{
|
||||
$this->PrepareRendering();
|
||||
$sFrom = self::ClauseFrom($this->__aFrom);
|
||||
@@ -318,7 +301,6 @@ class SQLObjectQuery extends SQLQuery
|
||||
return "UPDATE $sFrom SET $sValues WHERE $sWhere";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate an INSERT statement.
|
||||
* Note : unlike `RenderUpdate` and `RenderSelect`, it is limited to one and only one table.
|
||||
@@ -328,13 +310,12 @@ class SQLObjectQuery extends SQLQuery
|
||||
* @return string
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function RenderInsert($aArgs = array())
|
||||
public function RenderInsert($aArgs = [])
|
||||
{
|
||||
$this->PrepareRendering();
|
||||
$aJoinInfo = reset($this->__aFrom);
|
||||
|
||||
if ($aJoinInfo['jointype'] != 'first' || count($this->__aFrom) > 1)
|
||||
{
|
||||
if ($aJoinInfo['jointype'] != 'first' || count($this->__aFrom) > 1) {
|
||||
throw new CoreException('Cannot render insert');
|
||||
}
|
||||
|
||||
@@ -342,9 +323,8 @@ class SQLObjectQuery extends SQLQuery
|
||||
|
||||
$sColList = '`'.implode('`,`', array_keys($this->m_aValues)).'`';
|
||||
|
||||
$aSetValues = array();
|
||||
foreach ($this->__aSetValues as $sFieldSpec => $value)
|
||||
{
|
||||
$aSetValues = [];
|
||||
foreach ($this->__aSetValues as $sFieldSpec => $value) {
|
||||
$aSetValues[] = CMDBSource::Quote($value);
|
||||
}
|
||||
$sValues = implode(',', $aSetValues);
|
||||
@@ -352,7 +332,6 @@ class SQLObjectQuery extends SQLQuery
|
||||
return "INSERT INTO $sFrom ($sColList) VALUES ($sValues)";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $aOrderBy
|
||||
* @param array $aArgs
|
||||
@@ -363,7 +342,7 @@ class SQLObjectQuery extends SQLQuery
|
||||
* @return string
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function RenderSelect($aOrderBy = array(), $aArgs = array(), $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulQuery = false)
|
||||
public function RenderSelect($aOrderBy = [], $aArgs = [], $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulQuery = false)
|
||||
{
|
||||
$this->m_bBeautifulQuery = $bBeautifulQuery;
|
||||
$sLineSep = $this->m_bBeautifulQuery ? "\n" : '';
|
||||
@@ -374,20 +353,15 @@ class SQLObjectQuery extends SQLQuery
|
||||
$sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
|
||||
// Sanity
|
||||
$iLimitCount = (int)$iLimitCount;
|
||||
if ($iLimitCount > 0)
|
||||
{
|
||||
if ($iLimitCount > 0) {
|
||||
// Sanity
|
||||
$iLimitStart = (int)$iLimitStart;
|
||||
$sLimit = 'LIMIT '.$iLimitStart.', '.$iLimitCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sLimit = '';
|
||||
}
|
||||
if ($bGetCount)
|
||||
{
|
||||
if (count($this->__aSelectedIdFields) > 0)
|
||||
{
|
||||
if ($bGetCount) {
|
||||
if (count($this->__aSelectedIdFields) > 0) {
|
||||
$aCountFields = [];
|
||||
$aCountI = [];
|
||||
$i = 0;
|
||||
@@ -400,18 +374,13 @@ class SQLObjectQuery extends SQLQuery
|
||||
// Count can be limited for performance reason, in this case the total amount is not important,
|
||||
// we only need to know if the number of entries is greater than a certain amount.
|
||||
$sSQL = "SELECT COUNT(*) AS COUNT FROM (SELECT$sLineSep DISTINCT $sCountFields $sLineSep FROM $sFrom$sLineSep WHERE $sWhere $sLimit) AS _alderaan_ WHERE $sCountI>0";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sSQL = "SELECT COUNT(*) AS COUNT FROM (SELECT$sLineSep 1 $sLineSep FROM $sFrom$sLineSep WHERE $sWhere $sLimit) AS _alderaan_";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sSelect = self::ClauseSelect($this->__aFields, $sLineSep);
|
||||
$sOrderBy = self::ClauseOrderBy($aOrderBy, $this->__aFields);
|
||||
if (!empty($sOrderBy))
|
||||
{
|
||||
if (!empty($sOrderBy)) {
|
||||
$sOrderBy = "ORDER BY $sOrderBy$sLineSep";
|
||||
}
|
||||
|
||||
@@ -431,7 +400,7 @@ class SQLObjectQuery extends SQLQuery
|
||||
* @return string
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function RenderGroupBy($aArgs = array(), $bBeautifulQuery = false, $aOrderBy = array(), $iLimitCount = 0, $iLimitStart = 0)
|
||||
public function RenderGroupBy($aArgs = [], $bBeautifulQuery = false, $aOrderBy = [], $iLimitCount = 0, $iLimitStart = 0)
|
||||
{
|
||||
$this->m_bBeautifulQuery = $bBeautifulQuery;
|
||||
$sLineSep = $this->m_bBeautifulQuery ? "\n" : '';
|
||||
@@ -444,34 +413,25 @@ class SQLObjectQuery extends SQLQuery
|
||||
$sWhere = self::ClauseWhere($this->m_oConditionExpr, $aArgs);
|
||||
$sGroupBy = self::ClauseGroupBy($this->__aGroupBy);
|
||||
$sOrderBy = self::ClauseOrderBy($aOrderBy, $this->__aFields);
|
||||
if (!empty($sGroupBy))
|
||||
{
|
||||
if (!empty($sGroupBy)) {
|
||||
$sGroupBy = "GROUP BY $sGroupBy$sLineSep";
|
||||
}
|
||||
if (!empty($sOrderBy))
|
||||
{
|
||||
if (!empty($sOrderBy)) {
|
||||
$sOrderBy = "ORDER BY $sOrderBy$sLineSep";
|
||||
}
|
||||
if ($iLimitCount > 0)
|
||||
{
|
||||
if ($iLimitCount > 0) {
|
||||
$sLimit = 'LIMIT '.$iLimitStart.', '.$iLimitCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sLimit = '';
|
||||
}
|
||||
if (count($this->__aSelectedIdFields) > 0)
|
||||
{
|
||||
$aCountFields = array();
|
||||
foreach ($this->__aSelectedIdFields as $sFieldExpr)
|
||||
{
|
||||
if (count($this->__aSelectedIdFields) > 0) {
|
||||
$aCountFields = [];
|
||||
foreach ($this->__aSelectedIdFields as $sFieldExpr) {
|
||||
$aCountFields[] = "COALESCE($sFieldExpr, 0)"; // Null values are excluded from the count
|
||||
}
|
||||
$sCountFields = implode(', ', $aCountFields);
|
||||
$sCountClause = "DISTINCT $sCountFields";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sCountClause = '*';
|
||||
}
|
||||
$sSQL = "SELECT $sSelect,$sLineSep COUNT($sCountClause) AS _itop_count_$sLineSep FROM $sFrom$sLineSep WHERE $sWhere$sLineSep $sGroupBy $sOrderBy$sLineSep $sLimit";
|
||||
@@ -481,16 +441,15 @@ class SQLObjectQuery extends SQLQuery
|
||||
// Purpose: prepare the query data, once for all
|
||||
private function PrepareRendering()
|
||||
{
|
||||
if (is_null($this->__aFrom))
|
||||
{
|
||||
$this->__aFrom = array();
|
||||
$this->__aFields = array();
|
||||
$this->__aGroupBy = array();
|
||||
$this->__aDelTables = array();
|
||||
$this->__aSetValues = array();
|
||||
$this->__aSelectedIdFields = array();
|
||||
|
||||
$this->PrepareSingleTable($this, $this->__aFrom, '', array('jointype' => 'first'));
|
||||
if (is_null($this->__aFrom)) {
|
||||
$this->__aFrom = [];
|
||||
$this->__aFields = [];
|
||||
$this->__aGroupBy = [];
|
||||
$this->__aDelTables = [];
|
||||
$this->__aSetValues = [];
|
||||
$this->__aSelectedIdFields = [];
|
||||
|
||||
$this->PrepareSingleTable($this, $this->__aFrom, '', ['jointype' => 'first']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,118 +470,100 @@ class SQLObjectQuery extends SQLQuery
|
||||
|
||||
// Handle the various kinds of join (or first table in the list)
|
||||
//
|
||||
if (empty($aJoinData['righttablealias']))
|
||||
{
|
||||
if (empty($aJoinData['righttablealias'])) {
|
||||
$sRightTableAlias = $this->m_sTableAlias;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sRightTableAlias = $aJoinData['righttablealias'];
|
||||
}
|
||||
switch ($aJoinData['jointype'])
|
||||
{
|
||||
switch ($aJoinData['jointype']) {
|
||||
case "first":
|
||||
$aFrom[$this->m_sTableAlias] = array("jointype"=>"first", "tablename"=>$this->m_sTable, "joincondition"=>"");
|
||||
$aFrom[$this->m_sTableAlias] = ["jointype" => "first", "tablename" => $this->m_sTable, "joincondition" => ""];
|
||||
break;
|
||||
case "inner":
|
||||
case "left":
|
||||
if (isset($aJoinData["on_expression"]))
|
||||
{
|
||||
if (isset($aJoinData["on_expression"])) {
|
||||
$sJoinCond = $aJoinData["on_expression"]->RenderExpression(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sJoinCond = "`$sCallerAlias`.`{$aJoinData['leftfield']}` = `$sRightTableAlias`.`{$aJoinData['rightfield']}`";
|
||||
}
|
||||
$aFrom[$this->m_sTableAlias] = array("jointype"=>$aJoinData['jointype'], "tablename"=>$this->m_sTable, "joincondition"=>"$sJoinCond");
|
||||
$aFrom[$this->m_sTableAlias] = ["jointype" => $aJoinData['jointype'], "tablename" => $this->m_sTable, "joincondition" => "$sJoinCond"];
|
||||
break;
|
||||
case "inner_tree":
|
||||
if ($aJoinData['invert_on_clause'])
|
||||
{
|
||||
if ($aJoinData['invert_on_clause']) {
|
||||
$sRootLeft = "`$sCallerAlias`.`{$aJoinData['leftfield']}`";
|
||||
$sRootRight = "`$sCallerAlias`.`{$aJoinData['rightfield']}`";
|
||||
$sNodeLeft = "`$sRightTableAlias`.`{$aJoinData['rightfield_left']}`";
|
||||
$sNodeRight = "`$sRightTableAlias`.`{$aJoinData['rightfield_right']}`";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sNodeLeft = "`$sCallerAlias`.`{$aJoinData['leftfield']}`";
|
||||
$sNodeRight = "`$sCallerAlias`.`{$aJoinData['rightfield']}`";
|
||||
$sRootLeft = "`$sRightTableAlias`.`{$aJoinData['rightfield_left']}`";
|
||||
$sRootRight = "`$sRightTableAlias`.`{$aJoinData['rightfield_right']}`";
|
||||
}
|
||||
switch($aJoinData['tree_operator'])
|
||||
{
|
||||
switch ($aJoinData['tree_operator']) {
|
||||
case TREE_OPERATOR_BELOW:
|
||||
$sJoinCond = "$sNodeLeft >= $sRootLeft AND $sNodeLeft <= $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft >= $sRootLeft AND $sNodeLeft <= $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_BELOW_STRICT:
|
||||
$sJoinCond = "$sNodeLeft > $sRootLeft AND $sNodeLeft < $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft > $sRootLeft AND $sNodeLeft < $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_NOT_BELOW: // Complementary of 'BELOW'
|
||||
$sJoinCond = "$sNodeLeft < $sRootLeft OR $sNodeLeft > $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft < $sRootLeft OR $sNodeLeft > $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_NOT_BELOW_STRICT: // Complementary of BELOW_STRICT
|
||||
$sJoinCond = "$sNodeLeft <= $sRootLeft OR $sNodeLeft >= $sRootRight";
|
||||
break;
|
||||
$sJoinCond = "$sNodeLeft <= $sRootLeft OR $sNodeLeft >= $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_ABOVE:
|
||||
$sJoinCond = "$sNodeLeft <= $sRootLeft AND $sNodeRight >= $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft <= $sRootLeft AND $sNodeRight >= $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_ABOVE_STRICT:
|
||||
$sJoinCond = "$sNodeLeft < $sRootLeft AND $sNodeRight > $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft < $sRootLeft AND $sNodeRight > $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_NOT_ABOVE: // Complementary of 'ABOVE'
|
||||
$sJoinCond = "$sNodeLeft > $sRootLeft OR $sNodeRight < $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft > $sRootLeft OR $sNodeRight < $sRootRight";
|
||||
break;
|
||||
|
||||
case TREE_OPERATOR_NOT_ABOVE_STRICT: // Complementary of ABOVE_STRICT
|
||||
$sJoinCond = "$sNodeLeft >= $sRootLeft OR $sNodeRight <= $sRootRight";
|
||||
break;
|
||||
|
||||
$sJoinCond = "$sNodeLeft >= $sRootLeft OR $sNodeRight <= $sRootRight";
|
||||
break;
|
||||
|
||||
}
|
||||
$aFrom[$this->m_sTableAlias] = array("jointype"=>$aJoinData['jointype'], "tablename"=>$this->m_sTable, "joincondition"=>"$sJoinCond");
|
||||
$aFrom[$this->m_sTableAlias] = ["jointype" => $aJoinData['jointype'], "tablename" => $this->m_sTable, "joincondition" => "$sJoinCond"];
|
||||
break;
|
||||
}
|
||||
|
||||
// Given the alias, modify the fields and conditions
|
||||
// before adding them into the current lists
|
||||
//
|
||||
foreach($this->m_aFields as $sAlias => $oExpression)
|
||||
{
|
||||
foreach ($this->m_aFields as $sAlias => $oExpression) {
|
||||
$oRootQuery->__aFields["`$sAlias`"] = $oExpression->RenderExpression(true);
|
||||
}
|
||||
if ($this->m_aGroupBy)
|
||||
{
|
||||
foreach($this->m_aGroupBy as $sAlias => $oExpression)
|
||||
{
|
||||
if ($this->m_aGroupBy) {
|
||||
foreach ($this->m_aGroupBy as $sAlias => $oExpression) {
|
||||
$oRootQuery->__aGroupBy["`$sAlias`"] = $oExpression->RenderExpression(true);
|
||||
}
|
||||
}
|
||||
if ($this->m_bToDelete)
|
||||
{
|
||||
if ($this->m_bToDelete) {
|
||||
$oRootQuery->__aDelTables[] = "`{$this->m_sTableAlias}`";
|
||||
}
|
||||
foreach($this->m_aValues as $sFieldName=>$value)
|
||||
{
|
||||
foreach ($this->m_aValues as $sFieldName => $value) {
|
||||
$oRootQuery->__aSetValues["`{$this->m_sTableAlias}`.`$sFieldName`"] = $value; // quoted further!
|
||||
}
|
||||
|
||||
if (!is_null($this->m_oSelectedIdField))
|
||||
{
|
||||
$oRootQuery->__aSelectedIdFields[] = $this->m_oSelectedIdField->RenderExpression(true);
|
||||
if (!is_null($this->m_oSelectedIdField)) {
|
||||
$oRootQuery->__aSelectedIdFields[] = $this->m_oSelectedIdField->RenderExpression(true);
|
||||
}
|
||||
|
||||
// loop on joins, to complete the list of tables/fields/conditions
|
||||
//
|
||||
$aTempFrom = array(); // temporary subset of 'from' specs, to be grouped in the final query
|
||||
foreach ($this->m_aJoinSelects as $aJoinData)
|
||||
{
|
||||
$aTempFrom = []; // temporary subset of 'from' specs, to be grouped in the final query
|
||||
foreach ($this->m_aJoinSelects as $aJoinData) {
|
||||
/** @var \SQLObjectQuery $oRightSelect */
|
||||
$oRightSelect = $aJoinData["select"];
|
||||
|
||||
@@ -636,24 +577,20 @@ class SQLObjectQuery extends SQLQuery
|
||||
public function OptimizeJoins($aUsedTables, $bTopCall = true)
|
||||
{
|
||||
$this->m_iOriginalTableCount = $this->CountTables();
|
||||
if ($bTopCall)
|
||||
{
|
||||
if ($bTopCall) {
|
||||
// Top call: complete the list of tables absolutely required to perform the right query
|
||||
$this->CollectUsedTables($aUsedTables);
|
||||
}
|
||||
|
||||
$aToDiscard = array();
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
|
||||
{
|
||||
$aToDiscard = [];
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo) {
|
||||
$oSQLQuery = $aJoinInfo["select"];
|
||||
$sTableAlias = $oSQLQuery->GetTableAlias();
|
||||
if ($oSQLQuery->OptimizeJoins($aUsedTables, false) && !array_key_exists($sTableAlias, $aUsedTables))
|
||||
{
|
||||
if ($oSQLQuery->OptimizeJoins($aUsedTables, false) && !array_key_exists($sTableAlias, $aUsedTables)) {
|
||||
$aToDiscard[] = $i;
|
||||
}
|
||||
}
|
||||
foreach ($aToDiscard as $i)
|
||||
{
|
||||
foreach ($aToDiscard as $i) {
|
||||
unset($this->m_aJoinSelects[$i]);
|
||||
}
|
||||
|
||||
@@ -663,8 +600,7 @@ class SQLObjectQuery extends SQLQuery
|
||||
public function CountTables()
|
||||
{
|
||||
$iRet = 1;
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
|
||||
{
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo) {
|
||||
$oSQLQuery = $aJoinInfo["select"];
|
||||
$iRet += $oSQLQuery->CountTables();
|
||||
}
|
||||
@@ -674,34 +610,26 @@ class SQLObjectQuery extends SQLQuery
|
||||
public function CollectUsedTables(&$aTables)
|
||||
{
|
||||
$this->m_oConditionExpr->CollectUsedParents($aTables);
|
||||
foreach($this->m_aFields as $sFieldAlias => $oField)
|
||||
{
|
||||
foreach ($this->m_aFields as $sFieldAlias => $oField) {
|
||||
$oField->CollectUsedParents($aTables);
|
||||
}
|
||||
if ($this->m_aGroupBy)
|
||||
{
|
||||
foreach($this->m_aGroupBy as $sAlias => $oExpression)
|
||||
{
|
||||
if ($this->m_aGroupBy) {
|
||||
foreach ($this->m_aGroupBy as $sAlias => $oExpression) {
|
||||
$oExpression->CollectUsedParents($aTables);
|
||||
}
|
||||
}
|
||||
if (!is_null($this->m_oSelectedIdField))
|
||||
{
|
||||
$this->m_oSelectedIdField->CollectUsedParents($aTables);
|
||||
if (!is_null($this->m_oSelectedIdField)) {
|
||||
$this->m_oSelectedIdField->CollectUsedParents($aTables);
|
||||
}
|
||||
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
|
||||
{
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo) {
|
||||
$oSQLQuery = $aJoinInfo["select"];
|
||||
if ($oSQLQuery->HasRequiredTables($aTables))
|
||||
{
|
||||
if ($oSQLQuery->HasRequiredTables($aTables)) {
|
||||
// There is something required in the branch, then this node is a MUST
|
||||
if (isset($aJoinInfo['righttablealias']))
|
||||
{
|
||||
if (isset($aJoinInfo['righttablealias'])) {
|
||||
$aTables[$aJoinInfo['righttablealias']] = true;
|
||||
}
|
||||
if (isset($aJoinInfo["on_expression"]))
|
||||
{
|
||||
if (isset($aJoinInfo["on_expression"])) {
|
||||
$aJoinInfo["on_expression"]->CollectUsedParents($aTables);
|
||||
}
|
||||
}
|
||||
@@ -714,22 +642,17 @@ class SQLObjectQuery extends SQLQuery
|
||||
protected function HasRequiredTables(&$aTables)
|
||||
{
|
||||
$bResult = false;
|
||||
if (array_key_exists($this->m_sTableAlias, $aTables))
|
||||
{
|
||||
if (array_key_exists($this->m_sTableAlias, $aTables)) {
|
||||
$bResult = true;
|
||||
}
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
|
||||
{
|
||||
foreach ($this->m_aJoinSelects as $i => $aJoinInfo) {
|
||||
$oSQLQuery = $aJoinInfo["select"];
|
||||
if ($oSQLQuery->HasRequiredTables($aTables))
|
||||
{
|
||||
if ($oSQLQuery->HasRequiredTables($aTables)) {
|
||||
// There is something required in the branch, then this node is a MUST
|
||||
if (isset($aJoinInfo['righttablealias']))
|
||||
{
|
||||
if (isset($aJoinInfo['righttablealias'])) {
|
||||
$aTables[$aJoinInfo['righttablealias']] = true;
|
||||
}
|
||||
if (isset($aJoinInfo["on_expression"]))
|
||||
{
|
||||
if (isset($aJoinInfo["on_expression"])) {
|
||||
$aJoinInfo["on_expression"]->CollectUsedParents($aTables);
|
||||
}
|
||||
$bResult = true;
|
||||
|
||||
Reference in New Issue
Block a user