Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix on cluster create and drop table #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
53 changes: 35 additions & 18 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

use Tinderbox\Clickhouse\Client;
use Tinderbox\Clickhouse\Common\Format;
use Tinderbox\Clickhouse\Interfaces\FileInterface;
use Tinderbox\Clickhouse\Query;
use Tinderbox\Clickhouse\Query\Result;
use Tinderbox\ClickhouseBuilder\Exceptions\GrammarException;

class Builder extends BaseBuilder
{
/**
* Client which is used to perform queries.
*
* @var \Tinderbox\Clickhouse\Client
* @var Client
*/
protected $client;

Expand All @@ -31,7 +34,7 @@ public function __construct(Client $client)
*
* @param array $settings
*
* @return \Tinderbox\Clickhouse\Query\Result|\Tinderbox\Clickhouse\Query\Result[]
* @return Result|Result[]
*/
public function get(array $settings = [])
{
Expand Down Expand Up @@ -105,10 +108,10 @@ public function insertFiles(array $columns, array $files, string $format = Forma
/**
* Insert in table data from files.
*
* @param array $columns
* @param string|\Tinderbox\Clickhouse\Interfaces\FileInterface $file
* @param string $format
* @param array $settings
* @param array $columns
* @param string|FileInterface $file
* @param string $format
* @param array $settings
*
* @return bool
*/
Expand All @@ -126,11 +129,11 @@ public function insertFile(array $columns, $file, string $format = Format::CSV,
*
* @param array $values
*
* @throws \Tinderbox\ClickhouseBuilder\Exceptions\GrammarException
* @throws GrammarException
*
* @return bool
*/
public function insert(array $values)
public function insert(array $values): bool
{
if (empty($values)) {
return false;
Expand All @@ -156,11 +159,11 @@ public function insert(array $values)
/**
* Performs ALTER TABLE `table` DELETE query.
*
* @throws \Tinderbox\ClickhouseBuilder\Exceptions\GrammarException
* @throws GrammarException
*
* @return bool
*/
public function delete()
public function delete(): bool
{
return $this->client->writeOne(
$this->grammar->compileDelete($this)
Expand All @@ -176,9 +179,9 @@ public function delete()
*
* @return bool
*/
public function createTable($tableName, string $engine, array $structure)
public function createTable($tableName, string $engine, array $structure): bool
{
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure));
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure, $this->getOnCluster()));
}

/**
Expand All @@ -190,18 +193,32 @@ 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): bool
{
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure, true));
return $this->client->writeOne($this->grammar->compileCreateTable($tableName, $engine, $structure, $this->getOnCluster(), true));
}

public function dropTable($tableName)
/**
* Executes query to drop table.
*
* @param $tableName
*
* @return bool
*/
public function dropTable($tableName): bool
{
return $this->client->writeOne($this->grammar->compileDropTable($tableName));
return $this->client->writeOne($this->grammar->compileDropTable($tableName, $this->getOnCluster()));
}

public function dropTableIfExists($tableName)
/**
* Executes query to drop table if table exists.
*
* @param $tableName
*
* @return bool
*/
public function dropTableIfExists($tableName): bool
{
return $this->client->writeOne($this->grammar->compileDropTable($tableName, true));
return $this->client->writeOne($this->grammar->compileDropTable($tableName, $this->getOnCluster(), true));
}
}
22 changes: 12 additions & 10 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,37 +140,39 @@ 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 string|null $clusterName
* @param bool $ifNotExists
*
* @return string
*/
public function compileCreateTable($tableName, string $engine, array $structure, $ifNotExists = false): string
public function compileCreateTable($tableName, string $engine, array $structure, string $clusterName = null, $ifNotExists = false): string
{
if ($tableName instanceof Identifier) {
$tableName = (string) $tableName;
}

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

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

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

/**
Expand Down