diff --git a/Tests/dblayerooComplet.php b/Tests/dblayerooComplet.php index b7d48af..37eefcb 100644 --- a/Tests/dblayerooComplet.php +++ b/Tests/dblayerooComplet.php @@ -1047,4 +1047,15 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase 'groups' => 'group1,group2' )), $res); } + + public function test_GROUPCONCATwithAlias3 () + { + $tbl1 = $this->tbl1 (); + $res = $tbl1->select () + ->displayAdd ("GROUP_CONCAT(group,' ')", "groups") + ->execute (); + $this->assertSame (array (array ( + 'groups' => 'group1 group2' + )), $res); + } } diff --git a/dblayeroo.php b/dblayeroo.php index bd979cf..8674e46 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1552,10 +1552,37 @@ class dblayeroo "Invalid aliasNames provided (not string and not array)"); if (is_string ($columnNames)) { - // Remove the GROUP_CONCAT(col,',') as ',' is the default and the comma - // will be counted in error - $columnNames = str_replace (",','", "", $columnNames); - $columnNames = explode (",", $columnNames); + // A string must be separated by comma. But comma can be also in function + // parameter like GROUP_CONCAT(group,','),field1,field2 + // This block will explode on comma but only if not in a parenthesis block + $tmpArr = array (); + $parenthesis = false; + $tmp = ""; + for ($i = 0 ; $i < strlen ($columnNames) ; $i++) + { + $char = $columnNames{$i}; + if ($parenthesis === true) + { + $tmp .= $char; + if ($char === ")") + $parenthesis = false; + } + elseif ($char === "(") + { + $tmp .= $char; + $parenthesis = true; + } + elseif ($char === ",") + { + $tmpArr[] = $tmp; + $tmp = ""; + } + else + $tmp .= $char; + } + if ($tmp !== "") + $tmpArr[] = $tmp; + $columnNames = $tmpArr; } if (is_string ($aliasNames)) $aliasNames = explode (",", $aliasNames); @@ -1598,13 +1625,17 @@ class dblayeroo // There is a comma: the developper add the separator string $separator = addslashes (substr ($name, $pos + 2, -1)); $name = substr ($name, 0, $pos); - if ($this->driver === "sqlite" || $this->driver === "psql") + if ($this->driver === "sqlite" || $this->driver === "pgsql") $separator = ",'$separator'"; elseif ($this->driver === "mysql") $separator = " SEPARATOR '$separator'"; } if ($func === "GROUP_CONCAT" && $this->driver === "pgsql") + { $func = "string_agg"; + if ($separator === "") + $separator = ", ','"; + } } } $fieldName = $name; @@ -1625,8 +1656,6 @@ class dblayeroo { // For Postgres, the entry must be : // string_agg(distinct "group"::character varying, ',' order by "group") - if ($separator == "") - $separator = ", ','"; $this->displayColumn[$getSortOrder] = "$func($distinct$this->sep$fieldName$this->sep". "::character varying$separator ".