Skip to content

Commit

Permalink
Left array join support (#50)
Browse files Browse the repository at this point in the history
* Added left array join

* Readme updated

Co-authored-by: Igor Suvorov <[email protected]>
  • Loading branch information
suvorovis and Igor Suvorov authored Sep 10, 2021
1 parent 185b3b2 commit 1a68a1e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ $buulder->from('table')->leftJoin('table', 'any', ['column']);
$buulder->from('table')->innerJoin('table', 'all', ['column']);
```

You can use array join as well.

```php
$builder->from('test')->arrayJoin('someArr');
$builder->from('test')->leftArrayJoin('someArr');
```

```sql
SELECT * FROM `test` ARRAY JOIN `someArr`
SELECT * FROM `test` LEFT ARRAY JOIN `someArr`
```

### Temporary tables usage

There are some cases when you need to filter f.e. users by their ids, but amount of ids is huge. You can
Expand Down
43 changes: 43 additions & 0 deletions src/Query/ArrayJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tinderbox\ClickhouseBuilder\Query;

use Tinderbox\ClickhouseBuilder\Query\Enums\JoinType;

class ArrayJoinClause
{
/**
Expand All @@ -18,6 +20,13 @@ class ArrayJoinClause
*/
private $query;

/**
* Join type.
*
* @var JoinType|null
*/
private $type;

/**
* JoinClause constructor.
*
Expand All @@ -28,6 +37,30 @@ public function __construct(BaseBuilder $query)
$this->query = $query;
}

/**
* Set LEFT join type.
*
* @return ArrayJoinClause
*/
public function left(): self
{
return $this->type(JoinType::LEFT);
}

/**
* Set join type.
*
* @param string $type
*
* @return ArrayJoinClause
*/
public function type(string $type): self
{
$this->type = new JoinType(strtoupper($type));

return $this;
}

/**
* Set array identifier for join.
*
Expand Down Expand Up @@ -55,4 +88,14 @@ public function getArrayIdentifier()
{
return $this->arrayIdentifier;
}

/**
* Get join type.
*
* @return JoinType|null
*/
public function getType(): ?JoinType
{
return $this->type;
}
}
15 changes: 15 additions & 0 deletions src/Query/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ public function arrayJoin($arrayIdentifier)
return $this;
}

/**
* Add left array join to query.
*
* @param string|Expression $arrayIdentifier
*
* @return static
*/
public function leftArrayJoin($arrayIdentifier)
{
$this->arrayJoin = new ArrayJoinClause($this);
$this->arrayJoin->left()->array($arrayIdentifier);

return $this;
}

/**
* Add join to query.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Query/Traits/ArrayJoinComponentCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ trait ArrayJoinComponentCompiler
protected function compileArrayJoinComponent(Builder $query, ArrayJoinClause $join): string
{
$result = [];

if (!is_null($join->getType())) {
$result[] = $join->getType();
}

$result[] = 'ARRAY JOIN';
$result[] = $this->wrap($join->getArrayIdentifier());

Expand Down
8 changes: 8 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,14 @@ public function testArrayJoin()
$this->assertEquals('SELECT * FROM `test` ARRAY JOIN `someArr`', $builder->toSql());
}

public function testLeftArrayJoin()
{
$builder = $this->getBuilder();
$builder->table('test')->leftArrayJoin('someArr');

$this->assertEquals('SELECT * FROM `test` LEFT ARRAY JOIN `someArr`', $builder->toSql());
}

public function testAddFile()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 1a68a1e

Please sign in to comment.