From 38aae04e008e01c8098bb8d7bf83cc73df8ee0fc Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Thu, 25 Jun 2020 16:04:58 +0300 Subject: [PATCH 1/4] Add alias to join --- src/Query/JoinClause.php | 39 +++++++++++++++++++++- src/Query/Traits/JoinComponentCompiler.php | 4 +++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Query/JoinClause.php b/src/Query/JoinClause.php index 6032a06..4d62cea 100644 --- a/src/Query/JoinClause.php +++ b/src/Query/JoinClause.php @@ -58,6 +58,13 @@ class JoinClause */ private $subQuery; + /** + * Join alias + * + * @var \Tinderbox\ClickhouseBuilder\Query\Identifier + */ + private $alias; + /** * JoinClause constructor. * @@ -237,13 +244,33 @@ public function query($query = null) /** * Get sub-query builder. * + * @param string|null $alias + * * @return BaseBuilder */ - public function subQuery(): BaseBuilder + public function subQuery(string $alias = null): BaseBuilder { + if ($alias) { + $this->as($alias); + } + return $this->subQuery = $this->query->newQuery(); } + /** + * Set join alias + * + * @param string $alias + * + * @return $this + */ + public function as(string $alias) + { + $this->alias = new Identifier($alias); + + return $this; + } + /** * Get using columns. * @@ -304,6 +331,16 @@ public function getTable() return $this->table; } + /** + * Get alias. + * + * @return Identifier + */ + public function getAlias() : ?Identifier + { + return $this->alias; + } + /** * Converts strings to Identifier objects. * diff --git a/src/Query/Traits/JoinComponentCompiler.php b/src/Query/Traits/JoinComponentCompiler.php index 3c1672d..8e196bc 100644 --- a/src/Query/Traits/JoinComponentCompiler.php +++ b/src/Query/Traits/JoinComponentCompiler.php @@ -36,6 +36,10 @@ protected function compileJoinComponent(Builder $query, JoinClause $join) : stri $result[] = 'JOIN'; $result[] = $this->wrap($join->getTable()); + if ($join->getAlias()) { + $result[] = 'AS'; + $result[] = $this->wrap($join->getAlias()); + } $result[] = 'USING'; $result[] = implode(', ', array_map(function ($column) { return $this->wrap($column); From 6197f58c21a7c08041c6b44b5e1e216a0bf41830 Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Thu, 25 Jun 2020 16:50:13 +0300 Subject: [PATCH 2/4] Add tests and style fixes --- src/Query/JoinClause.php | 6 +++--- src/Query/Traits/JoinComponentCompiler.php | 6 +++--- tests/GrammarTest.php | 21 +++++++++++++-------- tests/JoinClauseTest.php | 8 ++++++++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Query/JoinClause.php b/src/Query/JoinClause.php index 4d62cea..2bb003b 100644 --- a/src/Query/JoinClause.php +++ b/src/Query/JoinClause.php @@ -59,7 +59,7 @@ class JoinClause private $subQuery; /** - * Join alias + * Join alias. * * @var \Tinderbox\ClickhouseBuilder\Query\Identifier */ @@ -258,7 +258,7 @@ public function subQuery(string $alias = null): BaseBuilder } /** - * Set join alias + * Set join alias. * * @param string $alias * @@ -336,7 +336,7 @@ public function getTable() * * @return Identifier */ - public function getAlias() : ?Identifier + public function getAlias(): ?Identifier { return $this->alias; } diff --git a/src/Query/Traits/JoinComponentCompiler.php b/src/Query/Traits/JoinComponentCompiler.php index 8e196bc..ccf7077 100644 --- a/src/Query/Traits/JoinComponentCompiler.php +++ b/src/Query/Traits/JoinComponentCompiler.php @@ -16,7 +16,7 @@ trait JoinComponentCompiler * * @return string */ - protected function compileJoinComponent(Builder $query, JoinClause $join) : string + protected function compileJoinComponent(Builder $query, JoinClause $join): string { $this->verifyJoin($join); @@ -26,11 +26,11 @@ protected function compileJoinComponent(Builder $query, JoinClause $join) : stri $result[] = 'GLOBAL'; } - if (! is_null($join->getStrict())) { + if (!is_null($join->getStrict())) { $result[] = $join->getStrict(); } - if (! is_null($join->getType())) { + if (!is_null($join->getType())) { $result[] = $join->getType(); } diff --git a/tests/GrammarTest.php b/tests/GrammarTest.php index 071f93c..f9e6157 100644 --- a/tests/GrammarTest.php +++ b/tests/GrammarTest.php @@ -205,7 +205,7 @@ public function testCompileSelect() $select = $grammar->compileSelect($this->getBuilder()->from('table')->groupBy([])); $this->assertEquals('SELECT * FROM `table`', $select); - + $select = $grammar->compileSelect($this->getBuilder()->from('table')->groupBy('*')); $this->assertEquals('SELECT * FROM `table`', $select); @@ -265,12 +265,17 @@ public function testCompileSelect() $select = $grammar->compileSelect($this->getBuilder()->anyLeftJoin('table', ['column'], true)); $this->assertEquals('SELECT * GLOBAL ANY LEFT JOIN `table` USING `column`', $select); - + $select = $grammar->compileSelect($this->getBuilder()->anyLeftJoin(function(JoinClause $join) { $join->table($this->getBuilder()->table('test')->select('column')); }, ['column'], true)); $this->assertEquals('SELECT * GLOBAL ANY LEFT JOIN (SELECT `column` FROM `test`) USING `column`', $select); + $select = $grammar->compileSelect($this->getBuilder()->anyLeftJoin(function (JoinClause $join) { + $join->subQuery('test')->table('table'); + }, ['column'])); + $this->assertEquals('SELECT * ANY LEFT JOIN (SELECT * FROM `table`) AS `test` USING `column`', $select); + /* * With complex two elements logic expressions */ @@ -343,20 +348,20 @@ public function testCompileDelete() $sql = $grammar->compileDelete($builder); $this->assertEquals('ALTER TABLE `table` DELETE WHERE `column` = 1', $sql); - + $builder = $this->getBuilder(); $builder->from('table')->where('column', 1)->onCluster('test'); - + $sql = $grammar->compileDelete($builder); - + $this->assertEquals('ALTER TABLE `table` ON CLUSTER test DELETE WHERE `column` = 1', $sql); - + $builder = $this->getBuilder(); $builder->from('table'); - + $this->expectException(GrammarException::class); $this->expectExceptionMessage('Missed where section for delete statement.'); - + $grammar->compileDelete($builder); } } diff --git a/tests/JoinClauseTest.php b/tests/JoinClauseTest.php index d71402a..9223d64 100644 --- a/tests/JoinClauseTest.php +++ b/tests/JoinClauseTest.php @@ -61,6 +61,14 @@ public function testSettersGetters() $join->distributed(true); $this->assertTrue($join->isDistributed()); + + $alias = 'test'; + $join->as($alias); + $this->assertEquals($join->getAlias(), $alias); + + $alias = 'test1'; + $join->subQuery($alias); + $this->assertEquals($join->getAlias(), $alias); } public function testQuery() From c011a732b1da5d7f1335ebc927a21cbf2896bb5d Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Thu, 25 Jun 2020 16:53:46 +0300 Subject: [PATCH 3/4] Style fixes --- tests/GrammarTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/GrammarTest.php b/tests/GrammarTest.php index f9e6157..583f1ac 100644 --- a/tests/GrammarTest.php +++ b/tests/GrammarTest.php @@ -24,7 +24,7 @@ class GrammarTest extends TestCase { use MockeryPHPUnitIntegration; - public function getBuilder() : Builder + public function getBuilder(): Builder { return new Builder(m::mock(Client::class)); } @@ -266,7 +266,7 @@ public function testCompileSelect() $select = $grammar->compileSelect($this->getBuilder()->anyLeftJoin('table', ['column'], true)); $this->assertEquals('SELECT * GLOBAL ANY LEFT JOIN `table` USING `column`', $select); - $select = $grammar->compileSelect($this->getBuilder()->anyLeftJoin(function(JoinClause $join) { + $select = $grammar->compileSelect($this->getBuilder()->anyLeftJoin(function (JoinClause $join) { $join->table($this->getBuilder()->table('test')->select('column')); }, ['column'], true)); $this->assertEquals('SELECT * GLOBAL ANY LEFT JOIN (SELECT `column` FROM `test`) USING `column`', $select); From ee3b703cbb616905c71d9e1a9e370984a0153935 Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Thu, 25 Jun 2020 16:54:39 +0300 Subject: [PATCH 4/4] Style fixes --- tests/GrammarTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/GrammarTest.php b/tests/GrammarTest.php index 583f1ac..3274e11 100644 --- a/tests/GrammarTest.php +++ b/tests/GrammarTest.php @@ -6,7 +6,6 @@ use Mockery as m; use PHPUnit\Framework\TestCase; use Tinderbox\Clickhouse\Client; -use Tinderbox\ClickhouseBuilder\Exceptions\BuilderException; use Tinderbox\ClickhouseBuilder\Exceptions\GrammarException; use Tinderbox\ClickhouseBuilder\Query\Builder; use Tinderbox\ClickhouseBuilder\Query\Column;