From 2b17459b655045374571adf2ea60593a469c1e5f Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 10 May 2017 12:40:26 +0000 Subject: [PATCH] Add the GROUP BY part in the SQL request git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3632 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index ee9ca2f..b254c4b 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1918,6 +1918,36 @@ class dblayeroo } /* }}} */ + /** 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 + * If the parameter $full is set, add the table prefix/name to the result + * @param boolean|null $full Add the table prefix/name if set + */ + public function groupByGet ($full=false) + /* {{{ */ + { + $groupBy = array (); + if ($this->joinObject) + $full = true; + 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 ($this->joinObject) + { + foreach ($this->joinObject as $obj) + $groupBy = array_merge ($groupBy, $obj->groupByGet (true)); + } + ksort ($groupBy, SORT_NATURAL); + return $groupBy; + } + /* }}} */ + /** Define a LIMIT for the request. * To use only the nbLines, put a 0 on startLine * @param integer $startLine The starting line in the result list @@ -2026,10 +2056,15 @@ class dblayeroo $order = $this->orderGet (false); $sql .= " $displayColumns\n FROM $this->sep$this->tableprefix". "$this->table$this->sep"; - $sql .= "\n ".$this->joinsGet (); + $joinsExpression = $this->joinsGet (); + if ($joinsExpression !== "") + $sql .= "\n ".$this->joinsGet (); $whereGetExpression = $this->whereGetExpression (); if (! empty ($whereGetExpression)) $sql .= "\n WHERE ". implode (" ", $whereGetExpression); + $groupByExpression = $this->groupByGet (); + if (! empty ($groupByExpression)) + $sql .= "\n GROUP BY ". implode (" ", $groupByExpression); if (count ($order)) $sql .= "\n ORDER BY ". implode (",", $order); if (! empty ($this->limitExpression)) @@ -2358,7 +2393,7 @@ class dblayeroo { if (! array_key_exists ($column, $setValues)) { - if (array_key_exists (0, $resUpdate)) + if ($update !== false && array_key_exists (0, $resUpdate)) { // In UPDATE, if a column is not modified (doesn't appears in // setValues), use the old value to search