Skip to content

Commit

Permalink
Adding the ability to work with tags servers and the ability to set a…
Browse files Browse the repository at this point in the history
…lias for joined table (#30)

* allowed to use proxy servers and to set alias for join

* added more tests, dev dependency and refactored some php docs

* fix travis and code style fixes

* another style fixes

* another code style fixes

* fix test for getting last query statistics from builder

* update readme

* fix readme

* refactor for using server with tags instead of proxy servers

* fix onCluster function

* some small refactor

* change clickhouse client version
  • Loading branch information
romanpravda authored Sep 22, 2020
1 parent 66a4afc commit 2d021de
Show file tree
Hide file tree
Showing 38 changed files with 1,014 additions and 704 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ before_script:
- composer install --prefer-source --dev
- docker run -p 9000:9000 -p 8123:8123 -d --name some-clickhouse-server --ulimit nofile=262144:262144 yandex/clickhouse-server

script: phpunit --coverage-clover ./tests/logs/clover.xml
script: vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml

after_script:
- php vendor/bin/php-coveralls -v
Expand Down
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ I think there no need for additional words)
### Joins

```php
$builder->from('table')->join('another_table', 'any', 'left', ['column1', 'column2'], true);
$builder->from('table')->join('another_table', 'any', 'left', ['column1', 'column2'], true, 'alias');
```

```sql
SELECT * FROM `table` GLOBAL ANY LEFT JOIN `another_table` USING `column1`, `column2`
SELECT * FROM `table` GLOBAL ANY LEFT JOIN `another_table` AS `alias` USING `column1`, `column2`
```

For performing subquery as first argument you can pass closure or builder.
Expand Down Expand Up @@ -567,6 +567,43 @@ Example with cluster:
]
```

Example with server with tag:

```php
'connections' => [
'clickhouse' => [
'driver' => 'clickhouse',
'servers' => [
[
'host' => 'ch-00.domain.com',
'port' => '',
'database' => '',
'username' => '',
'password' => '',
'options' => [
'timeout' => 10,
'protocol' => 'https',
'tags' => [
'tag'
],
],
],
[
'host' => 'ch-01.domain.com',
'port' => '',
'database' => '',
'username' => '',
'password' => '',
'options' => [
'timeout' => 10,
'protocol' => 'https'
],
],
],
],
]
```

Choose server without cluster:

```php
Expand All @@ -585,6 +622,12 @@ Choose cluster:
DB::connection('clickhouse')->onCluster('test')->select(...);
```

Use server with tag:

```php
DB::connection('clickhouse')->usingServerWithTag('tag')->select(...);
```

You can use both `servers` and `clusters` config directives and choose on which server
query should be executed via `onCluster` and `using` methods. If you want to choose
server outside cluster, you should just call `onCluster(null)` and then call `using` method. You can
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
"require": {
"php": "~7.1",
"myclabs/php-enum": "^1.5",
"the-tinderbox/clickhouse-php-client": "~2.0.0"
"the-tinderbox/clickhouse-php-client": "~2.1.0"
},
"require-dev": {
"illuminate/database": "5.*",
"phpunit/phpunit": "^6.1",
"mockery/mockery": "^0.9.9",
"illuminate/events": "5.*",
"illuminate/config": "5.*"
"illuminate/config": "5.*",
"illuminate/pagination": "5.*"
},
"extra": {
"laravel": {
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/BuilderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static function cannotDetermineAliasForColumn()
{
return new static('Cannot determine alias for the column');
}

public static function noTableStructureProvided()
{
return new static("No structure provided for insert in memory table");
return new static('No structure provided for insert in memory table');
}
}
35 changes: 21 additions & 14 deletions src/Integrations/Laravel/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function __construct(Connection $connection)
*/
public function get()
{
if (! empty($this->async)) {
if (!empty($this->async)) {
return $this->connection->selectAsync($this->toAsyncQueries());
} else {
return $this->connection->select($this->toSql(), [], $this->getFiles());
}
}

/**
* Returns Query instance
* Returns Query instance.
*
* @param array $settings
*
Expand All @@ -67,7 +67,7 @@ public function toQuery(array $settings = []): Query
* Performs compiled sql for count rows only. May be used for pagination
* Works only without async queries.
*
* @param string $column Column to pass into count() aggregate function
* @throws \Tinderbox\Clickhouse\Exceptions\ClientException
*
* @return int|mixed
*/
Expand All @@ -76,15 +76,17 @@ public function count()
$builder = $this->getCountQuery();
$result = $builder->get();

if (! empty($this->groups)) {
if (!empty($this->groups)) {
return count($result);
} else {
return $result[0]['count'] ?? 0;
}
}

/**
* Perform query and get first row
* Perform query and get first row.
*
* @throws \Tinderbox\Clickhouse\Exceptions\ClientException
*
* @return mixed|null|\Tinderbox\Clickhouse\Query\Result
*/
Expand All @@ -100,7 +102,7 @@ public function first()
*
* @return self
*/
public function newQuery() : Builder
public function newQuery(): Builder
{
return new static($this->connection);
}
Expand All @@ -117,9 +119,9 @@ public function newQuery() : Builder
*/
public function insertFiles(array $columns, array $files, string $format = Format::CSV, int $concurrency = 5): array
{
return $this->connection->insertFiles((string)$this->getFrom()->getTable(), $columns, $files, $format, $concurrency);
return $this->connection->insertFiles((string) $this->getFrom()->getTable(), $columns, $files, $format, $concurrency);
}

/**
* Insert in table data from files.
*
Expand All @@ -132,9 +134,9 @@ public function insertFiles(array $columns, array $files, string $format = Forma
public function insertFile(array $columns, $file, string $format = Format::CSV): bool
{
$file = $this->prepareFile($file);

$result = $this->connection->insertFiles($this->getFrom()->getTable(), $columns, [$file], $format);

return $result[0][0];
}

Expand All @@ -143,6 +145,8 @@ public function insertFile(array $columns, $file, string $format = Format::CSV):
*
* @param array $values
*
* @throws \Tinderbox\ClickhouseBuilder\Exceptions\GrammarException
*
* @return bool
*/
public function insert(array $values)
Expand All @@ -151,7 +155,7 @@ public function insert(array $values)
return false;
}

if (! is_array(reset($values))) {
if (!is_array(reset($values))) {
$values = [$values];
} /*
* Here, we will sort the insert keys for every record so that each insert is
Expand All @@ -174,6 +178,8 @@ public function insert(array $values)
/**
* Performs ALTER TABLE `table` DELETE query.
*
* @throws \Tinderbox\ClickhouseBuilder\Exceptions\GrammarException
*
* @return int
*/
public function delete()
Expand All @@ -188,6 +194,7 @@ public function delete()
* @param int $perPage
*
* @throws \Tinderbox\Clickhouse\Exceptions\ClientException
*
* @return LengthAwarePaginator
*/
public function paginate(int $page = 1, int $perPage = 15): LengthAwarePaginator
Expand All @@ -214,7 +221,7 @@ public function paginate(int $page = 1, int $perPage = 15): LengthAwarePaginator
*
* @return QueryStatistic
*/
public function getLastQueryStatistics() : QueryStatistic
public function getLastQueryStatistics(): QueryStatistic
{
return $this->getConnection()->getLastQueryStatistic();
}
Expand All @@ -224,7 +231,7 @@ public function getLastQueryStatistics() : QueryStatistic
*
* @return Connection
*/
public function getConnection() : Connection
public function getConnection(): Connection
{
return $this->connection;
}
Expand Down
Loading

0 comments on commit 2d021de

Please sign in to comment.