Skip to content

Commit

Permalink
Merge pull request #5 from the-tinderbox/103-added_add_prefixed_methods
Browse files Browse the repository at this point in the history
added fixes to getCountQuery
  • Loading branch information
TinderBox authored Jun 26, 2017
2 parents 77f1be4 + 5a46a06 commit c395080
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/Integrations/Laravel/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public function get()
*/
public function count($column = '*')
{
$result = $this->getCountQuery($column)->get();
$builder = $this->getCountQuery($column);
$result = $builder->get();

return $result[0]['count'] ?? 0;
if (!empty($this->groups)) {
return count($result);
} else {
return $result[0]['count'] ?? 0;
}
}

/**
Expand Down
19 changes: 14 additions & 5 deletions src/Query/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,23 @@ public function select(...$columns)
return $this;
}

/**
* Returns query for count total rows without limit
*
* @param string $column
*
* @return static
*/
public function getCountQuery($column = '*')
{
$builder = $this->cloneWithout(['columns'])->select(raw('count('.$column.') as `count`'));
$without = ['columns' => [], 'limit' => null];

if (empty($builder->groups)) {
$builder->orders = [];
if (empty($this->groups)) {
$without['orders'] = [];
}

$builder = $this->cloneWithout($without)->select(raw('count('.$column.') as `count`'));

return $builder;
}

Expand All @@ -165,8 +174,8 @@ public function getCountQuery($column = '*')
public function cloneWithout(array $except)
{
return tap(clone $this, function ($clone) use ($except) {
foreach ($except as $property) {
$clone->{$property} = null;
foreach ($except as $property => $value) {
$clone->{$property} = $value;
}
});
}
Expand Down
9 changes: 7 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ public function get()
*/
public function count($column = '*')
{
$result = $this->getCountQuery($column)->get();
$builder = $this->getCountQuery($column);
$result = $builder->get();

return $result[0]['count'] ?? 0;
if (!empty($this->groups)) {
return count($result);
} else {
return $result[0]['count'] ?? 0;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@ public function test_where_dict()

public function test_count()
{
$builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->orderBy('column1')->getCountQuery('*');
$builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->orderBy('column1')->limit(10)->getCountQuery('*');
$this->assertEquals('SELECT count(*) as `count` FROM `table`', $builder->toSql());

$builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->groupBy('column2')->orderBy('column1')->getCountQuery('*');
$builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->groupBy('column2')->orderBy('column1')->limit(10)->getCountQuery('*');
$this->assertEquals('SELECT count(*) as `count` FROM `table` GROUP BY `column2` ORDER BY `column1` ASC', $builder->toSql());
}

Expand Down

0 comments on commit c395080

Please sign in to comment.