Skip to content

Commit

Permalink
Merge pull request #27 from AndrewZoh/add_alias_to_join
Browse files Browse the repository at this point in the history
Add alias to join
  • Loading branch information
evsign authored Jun 25, 2020
2 parents a5d31a4 + ee3b703 commit 3b2f9d5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
39 changes: 38 additions & 1 deletion src/Query/JoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class JoinClause
*/
private $subQuery;

/**
* Join alias.
*
* @var \Tinderbox\ClickhouseBuilder\Query\Identifier
*/
private $alias;

/**
* JoinClause constructor.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
10 changes: 7 additions & 3 deletions src/Query/Traits/JoinComponentCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -26,16 +26,20 @@ 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();
}

$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);
Expand Down
26 changes: 15 additions & 11 deletions tests/GrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,7 @@ class GrammarTest extends TestCase
{
use MockeryPHPUnitIntegration;

public function getBuilder() : Builder
public function getBuilder(): Builder
{
return new Builder(m::mock(Client::class));
}
Expand Down Expand Up @@ -205,7 +204,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);

Expand Down Expand Up @@ -265,12 +264,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) {

$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
*/
Expand Down Expand Up @@ -343,20 +347,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);
}
}
8 changes: 8 additions & 0 deletions tests/JoinClauseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3b2f9d5

Please sign in to comment.