From 5c1ebc12a29f7920558938f106e978604811689f Mon Sep 17 00:00:00 2001 From: masterWeber Date: Thu, 7 Jan 2021 14:43:57 +0300 Subject: [PATCH] Added 'HAVING' --- src/Block/HavingBlock.php | 17 +++++++++++++++++ src/Clause/From.php | 2 ++ src/Clause/GroupBy.php | 2 ++ src/Clause/Having.php | 19 +++++++++++++++++++ tests/Clause/HavingTest.php | 21 +++++++++++++++++++++ tests/SqlBuilderTest.php | 10 ++++++---- 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/Block/HavingBlock.php create mode 100644 src/Clause/Having.php create mode 100644 tests/Clause/HavingTest.php diff --git a/src/Block/HavingBlock.php b/src/Block/HavingBlock.php new file mode 100644 index 0000000..0b64874 --- /dev/null +++ b/src/Block/HavingBlock.php @@ -0,0 +1,17 @@ +parent . ' ' . self::STATEMENT . ' ' . $this->buildExpressions()); + } +} \ No newline at end of file diff --git a/tests/Clause/HavingTest.php b/tests/Clause/HavingTest.php new file mode 100644 index 0000000..258bc55 --- /dev/null +++ b/tests/Clause/HavingTest.php @@ -0,0 +1,21 @@ +equal('col',5); + + $this->assertEquals( + 'HAVING `col` = 5', + $having->__toString() + ); + } +} diff --git a/tests/SqlBuilderTest.php b/tests/SqlBuilderTest.php index 6bdbb8e..6dfe25c 100644 --- a/tests/SqlBuilderTest.php +++ b/tests/SqlBuilderTest.php @@ -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']) @@ -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()