Skip to content

Commit

Permalink
Added 'HAVING'
Browse files Browse the repository at this point in the history
  • Loading branch information
masterWeber committed Jan 7, 2021
1 parent 102eb26 commit 5c1ebc1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/Block/HavingBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace SQLBuilder\Block;


use SQLBuilder\Clause\Having;
use SQLBuilder\Stringable_;

trait HavingBlock
{
public function having($expression = null): Having
{
/** @var Stringable_ $this */
return new Having($expression, $this);
}
}
2 changes: 2 additions & 0 deletions src/Clause/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use SQLBuilder\Block\GroupByBlock;
use SQLBuilder\Block\HavingBlock;
use SQLBuilder\Block\OrderByBlock;
use SQLBuilder\Block\WhereBlock;
use SQLBuilder\Helper;
Expand All @@ -20,6 +21,7 @@ class From implements Stringable_
use WhereBlock;
use OrderByBlock;
use GroupByBlock;
use HavingBlock;

/**
* @param array|string $table
Expand Down
2 changes: 2 additions & 0 deletions src/Clause/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace SQLBuilder\Clause;


use SQLBuilder\Block\HavingBlock;
use SQLBuilder\Block\LimitBlock;
use SQLBuilder\Helper;
use SQLBuilder\Stringable_;
Expand All @@ -22,6 +23,7 @@ class GroupBy implements Stringable_
protected array $expressionList;
protected ?Stringable_ $parent;

use HavingBlock;
use LimitBlock;

public function __construct($expression, ?string $modifier = null, ?Stringable_ $parent = null)
Expand Down
19 changes: 19 additions & 0 deletions src/Clause/Having.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace SQLBuilder\Clause;


use SQLBuilder\Block\LimitBlock;
use SQLBuilder\Helper;
use SQLBuilder\Stringable_;

class Having extends Condition
{
const STATEMENT = 'HAVING';

public function __toString(): string
{
return trim($this->parent . ' ' . self::STATEMENT . ' ' . $this->buildExpressions());
}
}
21 changes: 21 additions & 0 deletions tests/Clause/HavingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Clause;

use SQLBuilder\Clause\Having;
use PHPUnit\Framework\TestCase;

class HavingTest extends TestCase
{

public function testToString()
{
$having = new Having();
$having->equal('col',5);

$this->assertEquals(
'HAVING `col` = 5',
$having->__toString()
);
}
}
10 changes: 6 additions & 4 deletions tests/SqlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ public function testSelect(): void

$sql = $sqlBuilder->select()
->from('table_name')
->groupBy('col');
->groupBy('col')
->having()
->lessThan('col', 5);

$this->assertEquals(
'SELECT * FROM `table_name` GROUP BY `col`',
'SELECT * FROM `table_name` GROUP BY `col` HAVING `col` < 5',
$sql->__toString()
);

$sql = $sqlBuilder->select(['t1.column' => 'col1','t2.column' => 'col2'])
$sql = $sqlBuilder->select(['t1.column' => 'col1', 't2.column' => 'col2'])
->distinct()
->from(['table1' => 't1'])
->join(['table2' => 't2'])
Expand All @@ -69,7 +71,7 @@ public function testSelect(): void

$this->assertEquals(
"SELECT DISTINCT `t1`.`column` AS `col1`, `t2`.`column` AS `col2`"
." FROM `table1` AS `t1` " .
. " FROM `table1` AS `t1` " .
"RIGHT JOIN `table2` AS `t2` ON (`col1` = `t2`.`col3`)"
. " WHERE `col1` IS NOT NULL ORDER BY `col2` DESC LIMIT 12745",
$sql->__toString()
Expand Down

0 comments on commit 5c1ebc1

Please sign in to comment.