Skip to content

Commit

Permalink
Update sqlsrv.php
Browse files Browse the repository at this point in the history
  • Loading branch information
williamsandy committed Sep 2, 2016
1 parent 21b3ce3 commit 8be121a
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions libraries/joomla/database/query/sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,22 @@ public function group($columns)

// Get the _formatted_ FROM string and remove everything except `table AS alias`
$fromStr = str_replace(array("[", "]"), "", str_replace("#__", $this->db->getPrefix(), str_replace("FROM ", "", (string) $this->from)));

// Remove any trailing whitespaces
$fromStr = trim($fromStr);
// Start setting up an array of alias => table
list($table, $alias) = preg_split("/\sAS\s/i", $fromStr);

$table = $alias = $fromStr;
if (strpos($fromStr,' AS ') !== false)
{
list($table, $alias) = preg_split("/\sAS\s/i", $fromStr);
}
elseif (preg_match("/\b\s+/i",$fromStr))
{
list($table, $alias) = preg_split("/\b\s+/i", $fromStr);
}
else
{
$table = $alias = $fromStr;
}
$tmpCols = $this->db->getTableColumns(trim($table));
$cols = array();

Expand All @@ -391,17 +403,33 @@ public function group($columns)

// Now we need to get all tables from any joins
// Go through all joins and add them to the tables array
foreach ($this->join as $join)

if ($this->join)
{
$joinTbl = str_replace("#__", $this->db->getPrefix(), str_replace("]", "", preg_replace("/.*(#.+\sAS\s[^\s]*).*/i", "$1", (string) $join)));
foreach ($this->join as $join)
{

list($table, $alias) = preg_split("/\sAS\s/i", $joinTbl);
$joinStr = trim(preg_replace("/.*JOIN/i","" ,(string)$join));

$tmpCols = $this->db->getTableColumns(trim($table));
if (strpos($joinStr,' AS ') !== false)
{
$joinTbl = str_replace("#__", $this->db->getPrefix(), str_replace("]", "", preg_replace("/^(.+?\sAS\s[^\s]*).*/i", "$1", $joinStr)));

foreach ($tmpCols as $name => $tmpColType)
{
array_push($cols, $alias . "." . $name);
list($table, $alias) = preg_split("/\sAS\s/i", $joinTbl);
}
else
{
$joinTbl = str_replace("#__", $this->db->getPrefix(), str_replace("]", "", preg_replace("/^(.+?\s[^\s]*).*/i", "$1", $joinStr)));

list($table, $alias) = preg_split("/\b\s+/i", $joinTbl);
}

$tmpCols = $this->db->getTableColumns(trim($table));

foreach ($tmpCols as $name => $tmpColType)
{
array_push($cols, $alias . "." . $name);
}
}
}

Expand All @@ -411,7 +439,7 @@ public function group($columns)
$selectCols = preg_replace("/([^,]*\([^\)]*\)[^,]*,?)/", "", $selectStr);

// Remove any "as alias" statements
$selectCols = preg_replace("/(\sas\s[^,]*)/i", "", $selectCols);
$selectCols = preg_replace("/((\sas|\b)\s([^,])*)/i", "", $selectCols);

// Remove any extra commas
$selectCols = preg_replace("/,{2,}/", ",", $selectCols);
Expand Down

0 comments on commit 8be121a

Please sign in to comment.