Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Clickhouse/Clause/ApplyClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

use AlephTools\SqlBuilder\Sql\Expression\ValueListExpression;

trait ApplyClause
{
protected array $applies = [];

public function apply(mixed $value): static
{
$this->applies[] = (new ValueListExpression())->append($value);
$this->built = false;
return $this;
}

protected function cloneApply(mixed $copy): void
{
foreach ($this->applies as $apply) {
$copy->apply(clone $apply);
}
}

public function cleanApply(): void
{
$this->applies = [];
}

protected function buildApply(): void
{
foreach ($this->applies as $apply) {
$this->sql .= " APPLY($apply)";
}
}
}
48 changes: 48 additions & 0 deletions src/Clickhouse/Clause/ArrayJoinClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

use AlephTools\SqlBuilder\Sql\Expression\FromExpression;

trait ArrayJoinClause
{
protected ?FromExpression $arrayJoin = null;

public function arrayJoin(mixed $column, mixed $alias): static
{
$this->arrayJoin ??= $this->createArrayJoinExpression();
$this->arrayJoin->append($column, $alias);
$this->built = false;
return $this;
}

private function createArrayJoinExpression(): FromExpression
{
return new FromExpression();
}

public function leftArrayJoin(): static
{
return $this;
}

public function cloneArrayJoin(mixed $copy): void
{
$copy->arrayJoin = $this->arrayJoin ? clone $this->arrayJoin : null;
}

public function cleanArrayJoin(): void
{
$this->arrayJoin = null;
}

public function buildArrayJoin(): void
{
if ($this->arrayJoin) {
$this->sql .= " ARRAY JOIN $this->arrayJoin";
$this->addParams($this->arrayJoin->getParams());
}
}
}
43 changes: 43 additions & 0 deletions src/Clickhouse/Clause/ExceptClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

use AlephTools\SqlBuilder\Sql\Expression\SelectExpression;

trait ExceptClause
{
protected SelectExpression|null $except = null;

public function except(mixed $except): static
{
$this->except ??= $this->createSelectExpression();
$this->except->append($except);
$this->built = false;
return $this;
}

protected function createExceptSelectExpression(): SelectExpression
{
return new SelectExpression();
}

protected function cloneExcept(mixed $copy): void
{
$copy->except = $this->except ? clone $this->except : null;
}

public function cleanExcept(): void
{
$this->except = null;
}

protected function buildExcept(): void
{
if ($this->except !== null) {
$this->sql .= " EXCEPT ($this->except)";
$this->addParams($this->except->getParams());
}
}
}
51 changes: 51 additions & 0 deletions src/Clickhouse/Clause/ExceptQueryClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

use AlephTools\SqlBuilder\Sql\Expression\SelectExpression;

trait ExceptQueryClause
{
protected SelectExpression|null $exceptQuery = null;
protected bool $exceptQueryDistinct = false;

public function exceptQuery(mixed $except, bool $distinct = false): static
{
$this->exceptQuery ??= $this->createSelectExpression();
$this->exceptQuery->append($except);
$this->exceptQueryDistinct = $distinct;
$this->built = false;
return $this;
}

protected function createExceptQuerySelectExpression(): SelectExpression
{
return new SelectExpression();
}

protected function cloneExceptQuery(mixed $copy): void
{
$copy->exceptQuery = $this->exceptQuery ? clone $this->exceptQuery : null;
$copy->exceptQueryDistinct = $this->exceptQueryDistinct;
}

public function cleanExceptQuery(): void
{
$this->exceptQuery = null;
$this->exceptQueryDistinct = false;
}

protected function buildExceptQuery(): void
{
if ($this->exceptQuery !== null) {
$this->sql .= ' EXCEPT';
if ($this->exceptQueryDistinct) {
$this->sql .= ' DISTINCT';
}
$this->sql .= " $this->exceptQuery";
$this->addParams($this->exceptQuery->getParams());
}
}
}
34 changes: 34 additions & 0 deletions src/Clickhouse/Clause/FormatClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

trait FormatClause
{
protected ?string $format = null;

public function format(mixed $format): static
{
$this->format = $format;
$this->built = false;
return $this;
}

public function cleanFormat(): void
{
$this->format = null;
}

protected function cloneFormat(mixed $copy): void
{
$copy->format = $this->format;
}

protected function buildFormat(): void
{
if ($this->format !== null) {
$this->sql .= " FORMAT $this->format";
}
}
}
44 changes: 44 additions & 0 deletions src/Clickhouse/Clause/IntersectClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

use AlephTools\SqlBuilder\Clickhouse\SelectStatement;

trait IntersectClause
{
protected SelectStatement|null $intersect = null;
protected bool $intersectDistinct = false;

public function intersect(SelectStatement $statement, bool $distinct = false): static
{
$this->intersect = $statement;
$this->intersectDistinct = $distinct;
$this->built = false;
return $this;
}

protected function cloneIntersect(mixed $copy): void
{
$copy->intersect = $this->intersect ? clone $this->intersect : null;
$copy->intersectDistinct = $this->intersectDistinct;
}

public function cleanIntersect(): void
{
$this->intersect = null;
}

protected function buildIntersect(): void
{
if ($this->intersect !== null) {
$this->sql .= ' INTERSECT';
if ($this->intersectDistinct) {
$this->sql .= ' DISTINCT';
}
$this->sql .= " $this->intersect";
$this->addParams($this->intersect->getParams());
}
}
}
91 changes: 91 additions & 0 deletions src/Clickhouse/Clause/IntoOutfileClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace AlephTools\SqlBuilder\Clickhouse\Clause;

trait IntoOutfileClause
{
protected ?string $fileName = null;
protected bool $existed = false;
protected bool $stdout = false;
protected bool $truncate = false;
protected ?string $compression = null;
protected ?int $compressionLevel = null;

public function intoOutfile(
string $file,
bool $withStdout = false,
?string $compression = null,
?int $compressionLevel = null
): static {
$this->existed = false;
$this->fileName = $file;
$this->stdout = $withStdout;
$this->compression = $compression;
$this->compressionLevel = $compressionLevel;
$this->built = false;

return $this;
}

public function intoExistedOutfile(
string $file,
bool $withStdout = false,
bool $truncate = false,
?string $compression = null,
?int $compressionLevel = null
): static {
$this->existed = true;
$this->fileName = $file;
$this->stdout = $withStdout;
$this->truncate = $truncate;
$this->compression = $compression;
$this->compressionLevel = $compressionLevel;
$this->built = false;

return $this;
}

protected function cloneIntoOutfile(mixed $copy): void
{
$copy->existed = $this->existed;
$copy->fileName = $this->fileName;
$copy->stdout = $this->stdout;
$copy->truncate = $this->truncate;
$copy->compression = $this->compression;
$copy->compressionLevel = $this->compressionLevel;
}

public function cleanIntoOutfile(): void
{
$this->fileName = null;
$this->stdout = false;
$this->truncate = false;
$this->compression = null;
$this->compressionLevel = null;
}

protected function buildIntoOutfile(): void
{
if ($this->fileName !== null) {
$this->sql .= " INTO OUTFILE '$this->fileName'";
if ($this->stdout) {
$this->sql .= " AND STDOUT";
}
if ($this->existed) {
if ($this->truncate) {
$this->sql .= " TRUNCATE";
} else {
$this->sql .= " APPEND";
}
}
if ($this->compression !== null) {
$this->sql .= " COMPRESSION $this->compression";
if ($this->compressionLevel !== null) {
$this->sql .= " LEVEL $this->compressionLevel";
}
}
}
}
}
Loading
Loading