Skip to content

Commit

Permalink
Helpers for group by and select
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jan 21, 2020
1 parent 672f991 commit fdd3125
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
23 changes: 1 addition & 22 deletions src/Builder/Traits/GroupByTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,7 @@ public function groupBy($fields)
/**
* @var $this IStatement
*/

$groupClause = new GroupByClause();
if(func_num_args() > 1)
{
foreach(func_get_args() as $field)
{
$groupClause->addField($field);
}
}
else if(is_array($fields))
{
foreach($fields as $field)
{
$groupClause->addField($field);
}
}
else
{
$groupClause->addField($fields);
}

$this->addClause($groupClause);
$this->addClause(GroupByClause::create(...func_get_args()));
return $this;
}
}
24 changes: 24 additions & 0 deletions src/Clause/GroupByClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,28 @@ public function getAction()
{
return 'GROUP BY';
}

public static function create($fields)
{
$groupClause = new static();
if(func_num_args() > 1)
{
foreach(func_get_args() as $field)
{
$groupClause->addField($field);
}
}
else if(is_array($fields))
{
foreach($fields as $field)
{
$groupClause->addField($field);
}
}
else
{
$groupClause->addField($fields);
}
return $groupClause;
}
}
10 changes: 10 additions & 0 deletions src/Clause/SelectClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class SelectClause implements IClause
protected $_expressions = [];
protected $_distinct = false;

public static function create(...$fields)
{
$select = new static();
foreach($fields as $field)
{
$select->addField($field);
}
return $select;
}

public function setDistinct($distinct = true)
{
$this->_distinct = $distinct;
Expand Down
9 changes: 9 additions & 0 deletions tests/Clause/SelectClauseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,13 @@ public function testGettersAndSetters()
$this->setExpectedException("InvalidArgumentException");
$clause->setExpressions([$field, $now, 'abc']);
}

public function testStatic()
{
$clause = SelectClause::create('first');
$clause->setDistinct(true);
$this->assertEquals('SELECT DISTINCT first', QueryAssembler::stringify($clause));

$this->assertEquals('SELECT *', QueryAssembler::stringify(SelectClause::create()));
}
}

0 comments on commit fdd3125

Please sign in to comment.