From efe31391b57b5c571e02fbdd401e63fa9865581c Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 12 Sep 2017 12:59:33 +0000 Subject: [PATCH] dblayeroo: manage correctely the GROUP BY part: Add it only if needed and with the local and join objects parameters git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3941 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 106 +++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index 8d64cf4..6a9c02e 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -2100,6 +2100,27 @@ class dblayeroo } /* }}} */ + /** Return true if this object or one of the join objects need GROUP BY SQL + * part + */ + public function groupByNeeded () + /* {{{ */ + { + $needGroupBy = false; + if ($this->groupByExpression !== null) + $needGroupBy = true; + if ($this->joinObject) + { + foreach ($this->joinObject as $obj) + { + if ($obj->groupByNeeded () === true) + $needGroupBy = true; + } + } + return $needGroupBy; + } + /* }}} */ + /** Get the GROUP BY fields defined. If a joinObject is set with GROUP BY * statement, return the joinObject order with its tableprefix/name in * addition of the ones of this object @@ -2109,61 +2130,48 @@ class dblayeroo public function groupByGet ($full=false) /* {{{ */ { - $groupBy = null; if ($this->joinObject) $full = true; + + $localGroupBy = array (); + // Manage the local object group by entries. In full mode, return the + // groupByExpression if it is set, or the list of the displayed fields. + if ($full) + { + if ($this->groupByExpression !== null) + { + foreach ($this->groupByExpression as $pos=>$o) + { + if ($localGroupBy === null) + $localGroupBy = array (); + $localGroupBy[$pos] = $this->sep.$this->tableprefix.$this->table. + $this->sep.".".$o; + } + } + else + $localGroupBy = $this->displayGet (true); + } + else + { + if ($this->groupByExpression !== null) + $localGroupBy = $this->groupByExpression; + else + $localGroupBy = array (); + } + + // Add the distant entries + $distantGroupBy = array (); if ($this->joinObject) { foreach ($this->joinObject as $obj) { $ext = $obj->groupByGet (true); - if ($ext !== null) - { - if (! is_array ($groupBy)) - $groupBy = array (); - $groupBy = array_merge ($groupBy, $ext); - } + $distantGroupBy = array_merge ($distantGroupBy, $ext); } } - // If there is some GROUP BY in join Object, but not in this object, all - // the displayed fields must be added to be added in the GROUP BY - if (is_array ($groupBy) && - (is_null ($this->groupByExpression) || - (is_array ($this->groupByExpression) && - empty ($this->groupByExpression)) - )) - $this->groupByExpression = $this->displayColumn; - // If the local groupByExpression is set, but the external object was null, - // request the displayed fields to external objects - if (is_array ($this->groupByExpression) && $groupBy === null && - $this->joinObject) - { - foreach ($this->joinObject as $obj) - { - $ext = $obj->displayGet (true); - if ($ext !== null) - { - if (! is_array ($groupBy)) - $groupBy = array (); - $groupBy = array_merge ($groupBy, $ext); - } - } - } - if (is_array ($this->groupByExpression)) - { - if (! is_array ($groupBy)) - $groupBy = array (); - foreach ($this->groupByExpression as $pos=>$o) - { - if ($full !== false) - $groupBy[$pos] = $this->sep.$this->tableprefix.$this->table.$this->sep - .".".$o; - else - $groupBy[$pos] = $o; - } - } - if (is_array ($groupBy)) - ksort ($groupBy, SORT_NATURAL); + + $groupBy = array_merge ($localGroupBy, $distantGroupBy); + ksort ($groupBy, SORT_NATURAL); return $groupBy; } /* }}} */ @@ -2282,9 +2290,11 @@ class dblayeroo $whereGetExpression = $this->whereGetExpression (); if (! empty ($whereGetExpression)) $sql .= "\n WHERE ". implode (" ", $whereGetExpression); - $groupByExpression = $this->groupByGet (); - if (! empty ($groupByExpression)) + if ($this->groupByNeeded ()) + { + $groupByExpression = $this->groupByGet (); $sql .= "\n GROUP BY ". implode (",", $groupByExpression); + } if (count ($order)) $sql .= "\n ORDER BY ". implode (",", $order); if (! empty ($this->limitExpression))