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
This commit is contained in:
106
dblayeroo.php
106
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))
|
||||
|
||||
Reference in New Issue
Block a user