diff --git a/README.md b/README.md index 35131d7..4aab658 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Query/ArrayJoinClause.php b/src/Query/ArrayJoinClause.php index cadde21..15adaad 100644 --- a/src/Query/ArrayJoinClause.php +++ b/src/Query/ArrayJoinClause.php @@ -2,6 +2,8 @@ namespace Tinderbox\ClickhouseBuilder\Query; +use Tinderbox\ClickhouseBuilder\Query\Enums\JoinType; + class ArrayJoinClause { /** @@ -18,6 +20,13 @@ class ArrayJoinClause */ private $query; + /** + * Join type. + * + * @var JoinType|null + */ + private $type; + /** * JoinClause constructor. * @@ -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. * @@ -55,4 +88,14 @@ public function getArrayIdentifier() { return $this->arrayIdentifier; } + + /** + * Get join type. + * + * @return JoinType|null + */ + public function getType(): ?JoinType + { + return $this->type; + } } diff --git a/src/Query/BaseBuilder.php b/src/Query/BaseBuilder.php index f063a88..1b224d6 100644 --- a/src/Query/BaseBuilder.php +++ b/src/Query/BaseBuilder.php @@ -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. * diff --git a/src/Query/Traits/ArrayJoinComponentCompiler.php b/src/Query/Traits/ArrayJoinComponentCompiler.php index 750cc4e..4b8cc3d 100644 --- a/src/Query/Traits/ArrayJoinComponentCompiler.php +++ b/src/Query/Traits/ArrayJoinComponentCompiler.php @@ -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()); diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 9458b99..2cf0a2a 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -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();