Skip to content

Commit

Permalink
Merge pull request #4 from the-tinderbox/103-added_add_prefixed_methods
Browse files Browse the repository at this point in the history
added addGroupBy method, added count method, added getCountQuery method
  • Loading branch information
TinderBox authored Jun 26, 2017
2 parents dcf603d + 42e8375 commit 77f1be4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Integrations/Laravel/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ public function get()
}
}

/**
* Performs compiled sql for count rows only. May be used for pagination
* Works only without async queries.
*
* @param string $column Column to pass into count() aggregate function
*
* @return int|mixed
*/
public function count($column = '*')
{
$result = $this->getCountQuery($column)->get();

return $result[0]['count'] ?? 0;
}

/**
* Perform query and get first row
*
Expand Down
46 changes: 46 additions & 0 deletions src/Query/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ public function select(...$columns)
return $this;
}

public function getCountQuery($column = '*')
{
$builder = $this->cloneWithout(['columns'])->select(raw('count('.$column.') as `count`'));

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

return $builder;
}

/**
* Clone the query without the given properties.
*
* @param array $except
* @return static
*/
public function cloneWithout(array $except)
{
return tap(clone $this, function ($clone) use ($except) {
foreach ($except as $property) {
$clone->{$property} = null;
}
});
}

/**
* Add columns to exist select statement.
*
Expand Down Expand Up @@ -1576,6 +1602,26 @@ public function groupBy(...$columns)
return $this;
}

/**
* Add group by statement to exist group statements
*
* @param $columns
*
* @return static
*/
public function addGroupBy(...$columns)
{
$columns = isset($columns[0]) && is_array($columns[0]) ? $columns[0] : $columns;

if (empty($columns)) {
$columns[] = '*';
}

$this->groups = array_merge($this->groups, $this->processColumns($columns, false));

return $this;
}

/**
* Add order by statement.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function get()
}
}

/**
* Performs compiled sql for count rows only. May be used for pagination
* Works only without async queries.
*
* @param string $column Column to pass into count() aggregate function
*
* @return int|mixed
*/
public function count($column = '*')
{
$result = $this->getCountQuery($column)->get();

return $result[0]['count'] ?? 0;
}

/**
* Makes clean instance of builder.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,15 @@ public function test_where_dict()
$this->assertEquals("SELECT dictGetString('dictionary', 'attribute', tuple('key', 1)) as `attribute`, dictGetString('dictionary2', 'attribute2', 5) as `attribute2` FROM `table` WHERE `attribute` != 'value' OR `attribute2` = 5", $builder->toSql());
}

public function test_count()
{
$builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->orderBy('column1')->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('*');
$this->assertEquals('SELECT count(*) as `count` FROM `table` GROUP BY `column2` ORDER BY `column1` ASC', $builder->toSql());
}

public function test_group_by()
{
$builder = $this->getBuilder()->from('table')->groupBy('column');
Expand All @@ -672,6 +681,15 @@ public function test_group_by()
$this->assertEquals('SELECT * FROM `table` GROUP BY `column`, `column2`', $builder->toSql());
}

public function test_add_group_by()
{
$builder = $this->getBuilder()->from('table')->groupBy('column')->addGroupBy('column2', 'column3');
$this->assertEquals('SELECT * FROM `table` GROUP BY `column`, `column2`, `column3`', $builder->toSql());

$builder = $this->getBuilder()->from('table')->addGroupBy(['column', 'column2' => 'a']);
$this->assertEquals('SELECT * FROM `table` GROUP BY `column`, `column2`', $builder->toSql());
}

public function test_havings()
{
$builder = $this->getBuilder()->from('table')->groupBy('column')->having('column', 1);
Expand Down

0 comments on commit 77f1be4

Please sign in to comment.