From 6001b8f4494b02c7e9577f54b860dfc2d9b039c5 Mon Sep 17 00:00:00 2001 From: Lance McCulley Date: Tue, 3 Feb 2015 07:49:05 -0800 Subject: [PATCH] Clean up formatting for Travis CI build error messages, try #2. --- libraries/joomla/database/query/sqlsrv.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libraries/joomla/database/query/sqlsrv.php b/libraries/joomla/database/query/sqlsrv.php index 97171819e1338..4423bde093e78 100644 --- a/libraries/joomla/database/query/sqlsrv.php +++ b/libraries/joomla/database/query/sqlsrv.php @@ -373,8 +373,10 @@ public function group($columns) { // Transform $columns into an array for filtering purposes $columns = explode(",", str_replace(" ", "", $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))); + // Start setting up an array of alias => table list($table, $alias) = preg_split("/\sAS\s/i", $fromStr); $tmpCols = $this->db->getTableColumns(trim($table)); @@ -383,6 +385,7 @@ public function group($columns) { $cols[] = $alias . "." . $name; } + // Now we need to get all tables from any joins // Go through all joins and add them to the tables array $joins = array(); @@ -397,15 +400,20 @@ public function group($columns) } } - $selectStr = str_replace("SELECT ", "", (string)$this->select); + $selectStr = str_replace("SELECT ", "", (string) $this->select); + // Remove any functions (e.g. COUNT(), SUM(), CONCAT()) $selectCols = preg_replace("/([^,]*\([^\)]*\)[^,]*,?)/", "", $selectStr); + // Remove any "as alias" statements $selectCols = preg_replace("/(\sas\s[^,]*)/i", "", $selectCols); + // Remove any extra commas $selectCols = preg_replace("/,{2,}/", ",", $selectCols); + // Remove any trailing commas and all whitespaces $selectCols = trim(str_replace(" ", "", preg_replace("/,?$/", "", $selectCols))); + // Get an array to compare against $selectCols = explode(",", $selectCols); @@ -414,15 +422,19 @@ public function group($columns) { if (preg_match("/.+\*/", $aliasColName, $match)) { + // Grab the table alias minus the .* $aliasStar = preg_replace("/(.+)\.\*/", "$1", $aliasColName); + // Unset the array key unset($selectCols[$key]); + // Get the table name $tableColumns = preg_grep("/{$aliasStar}\.+/", $cols); $columns = array_merge($columns, $tableColumns); } } + // Finally, get a unique string of all column names that need to be included in the group statement $columns = array_unique(array_merge($columns, $selectCols)); $columns = implode(",", $columns);