Re-arrange the re-order code to only run if multi-aliases are used

git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9178 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2008-12-06 11:11:10 +00:00
parent 23de8dd0ce
commit 689831ecdd

View file

@ -502,10 +502,14 @@ class dbal
// Build table array. We also build an alias array for later checks. // Build table array. We also build an alias array for later checks.
$table_array = $aliases = array(); $table_array = $aliases = array();
$used_multi_alias = false;
foreach ($array['FROM'] as $table_name => $alias) foreach ($array['FROM'] as $table_name => $alias)
{ {
if (is_array($alias)) if (is_array($alias))
{ {
$used_multi_alias = true;
foreach ($alias as $multi_alias) foreach ($alias as $multi_alias)
{ {
$table_array[] = $table_name . ' ' . $multi_alias; $table_array[] = $table_name . ' ' . $multi_alias;
@ -520,11 +524,9 @@ class dbal
} }
// We run the following code to determine if we need to re-order the table array. ;) // We run the following code to determine if we need to re-order the table array. ;)
// The reason for this is that for multiple tables in the FROM statement the last table need to match the first LEFT JOIN'ed table. // The reason for this is that for multi-aliased tables (two equal tables) in the FROM statement the last table need to match the first comparison.
// DBMS who rely on this (at the moment i only spotted it on multi-aliases): Oracle, PostgreSQL and MSSQL // DBMS who rely on this: Oracle, PostgreSQL and MSSQL. For all other DBMS it makes absolutely no difference in which order the table is.
$first_join_match = false; if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1 && $used_multi_alias !== false)
if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1)
{ {
// Take first LEFT JOIN // Take first LEFT JOIN
$join = current($array['LEFT_JOIN']); $join = current($array['LEFT_JOIN']);
@ -532,33 +534,29 @@ class dbal
// Determine the table used there (even if there are more than one used, we only want to have one // Determine the table used there (even if there are more than one used, we only want to have one
preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches); preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches);
// If there is a first join match, we need to make sure the table order is correct
if (!empty($matches[1])) if (!empty($matches[1]))
{ {
$first_join_match = trim($matches[1]); $first_join_match = trim($matches[1]);
} $table_array = $last = array();
}
// If there is a first join match, we need to make sure the table order is correct foreach ($array['FROM'] as $table_name => $alias)
if ($first_join_match !== false)
{
$table_array = $last = array();
foreach ($array['FROM'] as $table_name => $alias)
{
if (is_array($alias))
{ {
foreach ($alias as $multi_alias) if (is_array($alias))
{ {
($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias; foreach ($alias as $multi_alias)
{
($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias;
}
}
else
{
($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias;
} }
} }
else
{
($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias;
}
}
$table_array = array_merge($table_array, $last); $table_array = array_merge($table_array, $last);
}
} }
$sql .= $this->_sql_custom_build('FROM', implode(', ', $table_array)); $sql .= $this->_sql_custom_build('FROM', implode(', ', $table_array));