Explorar o código

N.609 Some multi-column UNION queries failing with various symptoms such as "Class 'IT Department' not found" or "An object id must be an integer value"

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@4634 a333f486-631f-4898-b8df-5754b55c2be0
romainq %!s(int64=8) %!d(string=hai) anos
pai
achega
36eb7bde94
Modificáronse 3 ficheiros con 42 adicións e 4 borrados
  1. 8 2
      core/dbunionsearch.class.php
  2. 7 2
      core/sqlobjectquery.class.inc.php
  3. 27 0
      test/testlist.inc.php

+ 8 - 2
core/dbunionsearch.class.php

@@ -1,5 +1,5 @@
 <?php
 <?php
-// Copyright (C) 2015-2016 Combodo SARL
+// Copyright (C) 2015-2017 Combodo SARL
 //
 //
 //   This file is part of iTop.
 //   This file is part of iTop.
 //
 //
@@ -20,7 +20,7 @@
 /**
 /**
  * A union of DBObjectSearches 
  * A union of DBObjectSearches 
  *
  *
- * @copyright   Copyright (C) 2015-2016 Combodo SARL
+ * @copyright   Copyright (C) 2015-2017 Combodo SARL
  * @license     http://opensource.org/licenses/AGPL-3.0
  * @license     http://opensource.org/licenses/AGPL-3.0
  */
  */
  
  
@@ -516,6 +516,12 @@ class DBUnionSearch extends DBSearch
 				}
 				}
 			}
 			}
 			$oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses);
 			$oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses);
+			if (count($aSearchAliases) > 1)
+			{
+				// Necessary to make sure that selected columns will match throughout all the queries
+				// (default order of selected fields depending on the order of JOINS)
+				$oSubQuery->SortSelectedFields();
+			}
 			$aSQLQueries[] = $oSubQuery;
 			$aSQLQueries[] = $oSubQuery;
 		}
 		}
 
 

+ 7 - 2
core/sqlobjectquery.class.inc.php

@@ -1,5 +1,5 @@
 <?php
 <?php
-// Copyright (C) 2015-2016 Combodo SARL
+// Copyright (C) 2015-2017 Combodo SARL
 //
 //
 //   This file is part of iTop.
 //   This file is part of iTop.
 //
 //
@@ -21,7 +21,7 @@
  * SQLObjectQuery
  * SQLObjectQuery
  * build a mySQL compatible SQL query
  * build a mySQL compatible SQL query
  *
  *
- * @copyright   Copyright (C) 2015-2016 Combodo SARL
+ * @copyright   Copyright (C) 2015-2017 Combodo SARL
  * @license     http://opensource.org/licenses/AGPL-3.0
  * @license     http://opensource.org/licenses/AGPL-3.0
  */
  */
 
 
@@ -138,6 +138,11 @@ class SQLObjectQuery extends SQLQuery
 		$this->m_aFields = $aExpressions;
 		$this->m_aFields = $aExpressions;
 	}
 	}
 
 
+	public function SortSelectedFields()
+	{
+		ksort($this->m_aFields);
+	}
+
 	public function AddSelect($sAlias, $oExpression)
 	public function AddSelect($sAlias, $oExpression)
 	{
 	{
 		$this->m_aFields[$sAlias] = $oExpression;
 		$this->m_aFields[$sAlias] = $oExpression;

+ 27 - 0
test/testlist.inc.php

@@ -5518,3 +5518,30 @@ class TestIntersectNotOptimized extends TestBizModel
 		echo "<p>Successfully tested the SQL query.</p>\n";
 		echo "<p>Successfully tested the SQL query.</p>\n";
 	}
 	}
 }
 }
+
+class TestBug609 extends TestBizModel
+{
+	static public function GetName()
+	{
+		return 'UNION with JOINS ordered differently';
+	}
+
+	static public function GetDescription()
+	{
+		return '(N.609) Inconsistent SQL query (various symptoms, must mostly in the form of "Class \'IT Department\' not found"';
+	}
+
+	protected function DoExecute()
+	{
+		$sQueryA = 'SELECT t,o FROM Team AS t JOIN Organization AS o ON t.org_id = o.id';
+		$sQueryB = 'SELECT t,o FROM Organization AS o JOIN Team AS t ON t.org_id = o.id';
+
+		$oSearch = DBSearch::FromOQL("$sQueryB UNION $sQueryA");
+
+		$oSet = new DBObjectSet($oSearch);
+		while($oObject = $oSet->Fetch())
+		{
+			echo "Successfull load for <b>".$oObject->GetName()."</b><br>\n";
+		}
+	}
+}