Skip to content

Commit

Permalink
make aggregate consider the collected query
Browse files Browse the repository at this point in the history
When querying ahead of asking for an aggregate such as
User::where(‘email’, ‘foo’)->count() the count() is ignoring the
previous query and asking for all the User nodes.This commit makes the
aggregate run the query in addition to returning the required aggregate.
  • Loading branch information
Mulkave committed Jun 28, 2014
1 parent aa56126 commit 154431d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/Vinelab/NeoEloquent/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,7 @@ public function aggregate($function, $columns = array('*'), $percentile = null)

$previousColumns = $this->columns;

$cypher = $this->grammar->compileAggregate($this, $this->aggregate);

$results = $this->connection->statement($cypher, [], true);
$results = $this->get($columns);

// Once we have executed the query, we will reset the aggregate property so
// that more select queries can be executed against the database without
Expand Down
16 changes: 9 additions & 7 deletions src/Vinelab/NeoEloquent/Query/Grammars/CypherGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,17 @@ protected function compileOffset(Builder $query, $offset)
*/
protected function compileColumns(Builder $query, $properties)
{
// When we have an aggregate we will have to return it instead of the plain columns
// since aggregates for Cypher are not calculated at the beginning of the query like SQL
// instead we'll have to return in a form such as: RETURN max(user.logins).
if ( ! is_null($query->aggregate)) return $this->compileAggregate($query, $query->aggregate);

// In the case where the query has relationships
// we need to return the requested properties as is
// since they are considered node placeholders.
if ( ! empty($query->matches))
{
$properties = implode(', ', array_values($properties));

} else
{
$properties = $this->columnize($properties);
Expand Down Expand Up @@ -578,15 +582,14 @@ public function compileAggregate(Builder $query, $aggregate)
{
$distinct = null;
$function = $aggregate['function'];
// When calling for the distinct count we'll set the distinct flag.
// When calling for the distinct count we'll set the distinct flag and ask for the count function.
if ($function == 'countDistinct')
{
$function = 'count';
$distinct = 'DISTINCT ';
}

$node = $this->modelAsNode($aggregate['label']);
$label = $this->prepareLabels((array) $aggregate['label']);
$node = $this->modelAsNode($aggregate['label']);

// We need to format the columns to be in the form of n.property unless it is a *.
$columns = implode(', ', array_map(function($column) use($node) {
Expand All @@ -596,10 +599,9 @@ public function compileAggregate(Builder $query, $aggregate)
if ( ! is_null($aggregate['percentile']))
{
$percentile = $aggregate['percentile'];
return 'MATCH ('. $node.$label.") RETURN $function($columns, $percentile)";
return "RETURN $function($columns, $percentile)";
}


return 'MATCH ('. $node.$label.") RETURN $function($distinct$columns)";
return "RETURN $function($distinct$columns)";
}
}

0 comments on commit 154431d

Please sign in to comment.