diff --git a/src/Query/BaseBuilder.php b/src/Query/BaseBuilder.php index 57bc70a..890877a 100644 --- a/src/Query/BaseBuilder.php +++ b/src/Query/BaseBuilder.php @@ -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. @@ -148,10 +148,6 @@ abstract class BaseBuilder */ protected $values; - protected $clusterName; - - protected $serverHostname; - /** * Set columns for select statement. * diff --git a/src/Query/Builder.php b/src/Query/Builder.php index f9cab5a..3d38d34 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -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. * @@ -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)); } /** @@ -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) diff --git a/src/Query/Column.php b/src/Query/Column.php index fe4819a..45637f2 100644 --- a/src/Query/Column.php +++ b/src/Query/Column.php @@ -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. * diff --git a/src/Query/Grammar.php b/src/Query/Grammar.php index c922bdc..023abf4 100644 --- a/src/Query/Grammar.php +++ b/src/Query/Grammar.php @@ -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}"); } /** diff --git a/src/Query/Traits/ColumnCompiler.php b/src/Query/Traits/ColumnCompiler.php index 90997c4..202c213 100644 --- a/src/Query/Traits/ColumnCompiler.php +++ b/src/Query/Traits/ColumnCompiler.php @@ -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. * diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 5b31290..89d92fa 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -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()); diff --git a/tests/GrammarTest.php b/tests/GrammarTest.php index 8dcbeab..4616f88 100644 --- a/tests/GrammarTest.php +++ b/tests/GrammarTest.php @@ -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); + } }