Skip to content

Commit

Permalink
Enhance cluster support & add max() support (#67)
Browse files Browse the repository at this point in the history
* Add max function support

* Add getClient() method to Builder

* Add cluster support to create and drop table
  • Loading branch information
leo108 authored Jan 3, 2023
1 parent 05f63ce commit d22c4f1
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 20 deletions.
8 changes: 2 additions & 6 deletions src/Query/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ abstract class BaseBuilder
/**
* Cluster name.
*
* @var string
* @var string|null
*/
protected $onCluster;
protected $onCluster = null;

/**
* File representing values which should be inserted in table.
Expand All @@ -148,10 +148,6 @@ abstract class BaseBuilder
*/
protected $values;

protected $clusterName;

protected $serverHostname;

/**
* Set columns for select statement.
*
Expand Down
16 changes: 12 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function __construct(Client $client)
$this->grammar = new Grammar();
}

/**
* @return \Tinderbox\Clickhouse\Client
*/
public function getClient(): Client
{
return $this->client;
}

/**
* Perform compiled from builder sql query and getting result.
*
Expand Down Expand Up @@ -176,9 +184,9 @@ public function delete()
*
* @return bool
*/
public function createTable($tableName, string $engine, array $structure)
public function createTable($tableName, string $engine, array $structure, ?string $extraOptions = null)
{
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure));
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure, false, $this->getOnCluster(), $extraOptions));
}

/**
Expand All @@ -190,9 +198,9 @@ public function createTable($tableName, string $engine, array $structure)
*
* @return bool
*/
public function createTableIfNotExists($tableName, string $engine, array $structure)
public function createTableIfNotExists($tableName, string $engine, array $structure, ?string $extraOptions = null)
{
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure, true));
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure, true, $this->getOnCluster(), $extraOptions));
}

public function dropTable($tableName)
Expand Down
18 changes: 18 additions & 0 deletions src/Query/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ public function sum($columnName = null): self
return $this;
}

/**
* Apply max function to column.
*
* @param string|Expression|null $columnName
*
* @return $this
*/
public function max($columnName = null): self
{
if ($columnName !== null) {
$this->name($columnName);
}

$this->functions[] = ['function' => 'max'];

return $this;
}

/**
* Apply round function to column.
*
Expand Down
30 changes: 20 additions & 10 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,37 +140,47 @@ public function compileInsert(BaseBuilder $query, $values): string
/**
* Compiles create table query.
*
* @param $tableName
* @param string $engine
* @param array $structure
* @param bool $ifNotExists
* @param $tableName
* @param string $engine
* @param array $structure
* @param bool $ifNotExists
* @param string|null $clusterName
* @param string|null $extraOptions
*
* @return string
*/
public function compileCreateTable($tableName, string $engine, array $structure, $ifNotExists = false): string
public function compileCreateTable($tableName, string $engine, array $structure, bool $ifNotExists = false, ?string $clusterName = null, ?string $extraOptions = null): string
{
if ($tableName instanceof Identifier) {
$tableName = (string) $tableName;
}

return 'CREATE TABLE '.($ifNotExists ? 'IF NOT EXISTS ' : '')."{$tableName} ({$this->compileTableStructure($structure)}) ENGINE = {$engine}";
$onCluster = $clusterName === null ? '' : "ON CLUSTER {$clusterName}";
$extraOptions = $extraOptions ?? '';

return 'CREATE TABLE '
.($ifNotExists ? 'IF NOT EXISTS ' : '')
.rtrim("{$tableName} {$onCluster} ({$this->compileTableStructure($structure)}) ENGINE = {$engine} {$extraOptions}");
}

/**
* Compiles drop table query.
*
* @param $tableName
* @param bool $ifExists
* @param $tableName
* @param bool $ifExists
* @param string|null $clusterName
*
* @return string
*/
public function compileDropTable($tableName, $ifExists = false): string
public function compileDropTable($tableName, bool $ifExists = false, ?string $clusterName = null): string
{
if ($tableName instanceof Identifier) {
$tableName = (string) $tableName;
}

return 'DROP TABLE '.($ifExists ? 'IF EXISTS ' : '')."{$tableName}";
$onCluster = $clusterName === null ? '' : "ON CLUSTER {$clusterName}";

return trim('DROP TABLE '.($ifExists ? 'IF EXISTS ' : '')."{$tableName} {$onCluster}");
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Query/Traits/ColumnCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ private function sum($column)
return "sum({$column})";
}

/**
* Compiles max function on column.
*
* @param $column
*
* @return string
*/
private function max($column)
{
return "max({$column})";
}

/**
* Compiles round function on column.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public function testSum()
], $functions);
}

public function testMax()
{
$column = new Column($this->getBuilder());
$column->name('column');
$column->max();

$functions = $column->getFunctions();

$this->assertEquals([
[
'function' => 'max',
],
], $functions);
}

public function testRound()
{
$column = new Column($this->getBuilder());
Expand Down
25 changes: 25 additions & 0 deletions tests/GrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,29 @@ public function testCompileDelete()

$grammar->compileDelete($builder);
}

public function testCompileCreateTable()
{
$grammar = new Grammar();

$sql = $grammar->compileCreateTable('table', 'MergeTree', ['id' => 'UInt64'], true, 'test');
$this->assertEquals('CREATE TABLE IF NOT EXISTS table ON CLUSTER test (id UInt64) ENGINE = MergeTree', $sql);

$sql = $grammar->compileCreateTable('table', 'MergeTree', ['id' => 'UInt64'], false, 'test');
$this->assertEquals('CREATE TABLE table ON CLUSTER test (id UInt64) ENGINE = MergeTree', $sql);

$sql = $grammar->compileCreateTable('table', 'MergeTree', ['id' => 'UInt64'], false);
$this->assertEquals('CREATE TABLE table (id UInt64) ENGINE = MergeTree', $sql);
}

public function testCompileDropTable()
{
$grammar = new Grammar();

$sql = $grammar->compileDropTable('table', true, 'test');
$this->assertEquals('DROP TABLE IF EXISTS table ON CLUSTER test', $sql);

$sql = $grammar->compileDropTable('table', false);
$this->assertEquals('DROP TABLE table', $sql);
}
}

0 comments on commit d22c4f1

Please sign in to comment.