From db06724fa94999098ce9740c53da7c4cac2ddd75 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 19 Jul 2017 14:11:28 +0000 Subject: [PATCH] dblayeroo: in GROUP_CONCAT, allow the Separator to be set (use sqlite syntax, with a comma and the second optional parameter is the separator string) git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3818 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index 2985d24..0b2dbf4 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1533,16 +1533,29 @@ class dblayeroo { $func = strtoupper (trim (substr ($name, 0, $pos))); $name = trim (substr ($name, $pos+1, -1)); + $separator = ""; if (in_array ($func, array ("AVG", "COUNT", "GROUP_CONCAT", "MAX", "MIN","SUM"))) { + $aggregateFunction = true; // Aggregate function. Add the non aggregate fields to the GROUP BY // expression, if not already done if ($this->groupByExpression === array ()) { - $aggregateFunction = true; $this->groupByExpression = $this->displayColumn; } + if ($func === "GROUP_CONCAT" && ($pos = strpos ($name, ",'"))) + { + // 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") + $separator = ",'$separator'"; + elseif ($this->driver === "mysql") + $separator = " SEPARATOR '$separator'"; + } + if ($func === "GROUP_CONCAT" && $this->driver === "psql") + $func = "string_agg"; } $display = "$func($name)"; } @@ -1554,12 +1567,13 @@ class dblayeroo $this->displayColumn[$getSortOrder] = $this->sep.$display.$this->sep; else $this->displayColumn[$getSortOrder] = - "$func($this->sep$name$this->sep)"; + "$func($this->sep$name$this->sep$separator)"; if ($this->groupByExpression !== array () && ! isset ($aggregateFunction)) { // Not a aggregate function, but groupBy is set : add the new field name $this->groupByExpression[$getSortOrder] = $this->sep.$name.$this->sep; } + unset ($aggregateFunction); } return $this; } @@ -1585,7 +1599,7 @@ class dblayeroo } if ($name[0] !== $this->sep) $name = $this->sep.$name; - if (substr ($name, -1) !== $this->sep) + if (! isset ($func) && substr ($name, -1) !== $this->sep) $name = $name.$this->sep; if ($full !== false) $name = $this->sep.$this->tableprefix.$this->table.$this->sep.".".$name;