Skip to content
Merged
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Bavix\LaravelClickHouse\ClickHouseServiceProvider::class,
And add new connection into your config/database.php file. Something like this:
```php
'connections' => [
'clickhouse' => [
'driver' => 'clickhouse-ext',
'bavix::clickhouse' => [
'driver' => 'bavix::clickhouse',
'host' => '',
'port' => '',
'database' => '',
Expand All @@ -38,8 +38,8 @@ And add new connection into your config/database.php file. Something like this:
Or like this, if clickhouse runs in cluster
```php
'connections' => [
'clickhouse' => [
'driver' => 'clickhouse-ext',
'bavix::clickhouse' => [
'driver' => 'bavix::clickhouse',
'cluster' => [
'server-1' => [
'host' => '',
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"require": {
"php": ">=7.2",
"laravel/framework": "^6.0|^7.0",
"the-tinderbox/clickhouse-builder": "^3.0"
"the-tinderbox/clickhouse-builder": "^3.0",
"ext-json": "*"
},
"require-dev": {
"infection/infection": "0.15.*|0.16.*",
Expand Down
26 changes: 15 additions & 11 deletions src/ClickHouseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@

namespace Bavix\LaravelClickHouse;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\ServiceProvider;
use Bavix\LaravelClickHouse\Database\Connection;
use Bavix\LaravelClickHouse\Database\Eloquent\Model;

class ClickHouseServiceProvider extends ServiceProvider
{
/**
* @return void
* @throws
*/
public function boot(): void
{
$this->app->resolving('db', function ($db) {
$db->extend('clickhouse-ext', function ($config, $name) {
$config['name'] = $name;
$connection = new Connection($config);
if ($this->app->bound('events')) {
$connection->setEventDispatcher($this->app['events']);
}
Model::setConnectionResolver($this->app['db']);
Model::setEventDispatcher($this->app['events']);
}

return $connection;
/**
* @return void
*/
public function register(): void
{
$this->app->resolving('db', static function (DatabaseManager $db) {
$db->extend('bavix::clickhouse', static function ($config, $name) {
return new Connection(\array_merge($config, [
'name' => $name,
]));
});

Model::setConnectionResolver($db);
});
}
}
19 changes: 19 additions & 0 deletions src/Database/Eloquent/Concerns/Common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bavix\LaravelClickHouse\Database\Eloquent\Concerns;

trait Common
{

/**
* Save the model to the database.
*
* @param array $options
* @return bool
*/
public function save(array $options = []): bool
{
return static::insert($this->toArray());
}

}
8 changes: 5 additions & 3 deletions src/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder;

/**
* @mixin \Eloquent
* Class Model
* @package Bavix\LaravelClickHouse\Database\Eloquent
*/
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
{
use Concerns\HasAttributes,
Concerns\Common,
HasEvents,
HasRelationships,
HidesAttributes,
Expand All @@ -37,7 +39,7 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*
* @var string
*/
protected $connection = 'clickhouse';
protected $connection = 'bavix::clickhouse';

/**
* The table associated with the model.
Expand Down Expand Up @@ -426,7 +428,7 @@ public function setConnection(string $name)
*/
public static function resolveConnection(string $connection = null)
{
return static::$resolver->connection($connection);
return static::getConnectionResolver()->connection($connection);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function testQuery(): void
'database' => 'default',
]);

$this->assertInstanceOf(Builder::class, $connection->query());
self::assertInstanceOf(Builder::class, $connection->query());
}
}
78 changes: 39 additions & 39 deletions tests/Unit/Database/Eloquent/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ public function testWhereKey(): void

$wheres = $this->builder->getQuery()->getWheres();

$this->assertCount(1, $wheres);
self::assertCount(1, $wheres);
/** @var TwoElementsLogicExpression $expression */
$expression = $wheres[0];
$this->assertInstanceOf(TwoElementsLogicExpression::class, $expression);
self::assertInstanceOf(TwoElementsLogicExpression::class, $expression);
/** @var Identifier $first */
$first = $expression->getFirstElement();
$this->assertInstanceOf(Identifier::class, $first);
$this->assertSame($this->model->getTable().'.'.$this->model->getKeyName(), (string) $first);
$this->assertSame($id, $expression->getSecondElement());
self::assertInstanceOf(Identifier::class, $first);
self::assertSame($this->model->getTable().'.'.$this->model->getKeyName(), (string) $first);
self::assertSame($id, $expression->getSecondElement());
$operator = $expression->getOperator();
$this->assertInstanceOf(Operator::class, $operator);
$this->assertSame('=', $operator->getValue());
self::assertInstanceOf(Operator::class, $operator);
self::assertSame('=', $operator->getValue());
}

public function testWhereKeyNot(): void
Expand All @@ -71,20 +71,20 @@ public function testWhereKeyNot(): void

$wheres = $this->builder->getQuery()->getWheres();

$this->assertCount(1, $wheres);
self::assertCount(1, $wheres);
/** @var TwoElementsLogicExpression $expression */
$expression = $wheres[0];
$this->assertInstanceOf(TwoElementsLogicExpression::class, $expression);
self::assertInstanceOf(TwoElementsLogicExpression::class, $expression);
/** @var Identifier $first */
$first = $expression->getFirstElement();
$this->assertInstanceOf(Identifier::class, $first);
$this->assertSame($this->model->getTable().'.'.$this->model->getKeyName(), (string) $first);
self::assertInstanceOf(Identifier::class, $first);
self::assertSame($this->model->getTable().'.'.$this->model->getKeyName(), (string) $first);
/** @var Tuple $second */
$second = $expression->getSecondElement();
$this->assertSame($ids, $second->getElements());
self::assertSame($ids, $second->getElements());
$operator = $expression->getOperator();
$this->assertInstanceOf(Operator::class, $operator);
$this->assertSame('NOT IN', $operator->getValue());
self::assertInstanceOf(Operator::class, $operator);
self::assertSame('NOT IN', $operator->getValue());
}

public function testWhereSimple(): void
Expand All @@ -94,18 +94,18 @@ public function testWhereSimple(): void

$wheres = $this->builder->getQuery()->getWheres();

$this->assertCount(1, $wheres);
self::assertCount(1, $wheres);
/** @var TwoElementsLogicExpression $expression */
$expression = $wheres[0];
$this->assertInstanceOf(TwoElementsLogicExpression::class, $expression);
self::assertInstanceOf(TwoElementsLogicExpression::class, $expression);
/** @var Identifier $first */
$first = $expression->getFirstElement();
$this->assertInstanceOf(Identifier::class, $first);
$this->assertSame('date_column', (string) $first);
$this->assertSame($date, $expression->getSecondElement());
self::assertInstanceOf(Identifier::class, $first);
self::assertSame('date_column', (string) $first);
self::assertSame($date, $expression->getSecondElement());
$operator = $expression->getOperator();
$this->assertInstanceOf(Operator::class, $operator);
$this->assertSame('>', $operator->getValue());
self::assertInstanceOf(Operator::class, $operator);
self::assertSame('>', $operator->getValue());
}

public function testWhereClosure(): void
Expand All @@ -125,7 +125,7 @@ public function testWhereClosure(): void

$sql = $this->builder->toSql();

$this->assertSame('SELECT * FROM `test_table` WHERE (`id` < 10 OR `id` = 15) AND `status` = 100', $sql);
self::assertSame('SELECT * FROM `test_table` WHERE (`id` < 10 OR `id` = 15) AND `status` = 100', $sql);
}

public function testOrWhere(): void
Expand All @@ -136,7 +136,7 @@ public function testOrWhere(): void
$this->builder->orWhere('date_column', '>', $date);

$sql = $this->builder->toSql();
$this->assertSame(
self::assertSame(
'SELECT * FROM `test_table` WHERE `id` = '.$id.' OR `date_column` > \''.$date.'\'',
$sql
);
Expand All @@ -159,9 +159,9 @@ public function testFind(): void

$model = $this->builder->find($id);

$this->assertInstanceOf(EloquentModelCastingTest::class, $model);
$this->assertSame($id, $model->id);
$this->assertSame($stringAttribute, $model->stringAttribute);
self::assertInstanceOf(EloquentModelCastingTest::class, $model);
self::assertSame($id, $model->id);
self::assertSame($stringAttribute, $model->stringAttribute);
}

public function testFindMany(): void
Expand All @@ -184,8 +184,8 @@ public function testFindMany(): void

$models = $this->builder->findMany($ids->toArray());

$this->assertInstanceOf(Collection::class, $models);
$this->assertCount($ids->count(), $models);
self::assertInstanceOf(Collection::class, $models);
self::assertCount($ids->count(), $models);
}

public function testFindOrFail(): void
Expand Down Expand Up @@ -234,18 +234,18 @@ public function testGet(): void

$collection = $this->builder->get();

$this->assertInstanceOf(Collection::class, $collection);
$this->assertCount(1, $collection);
self::assertInstanceOf(Collection::class, $collection);
self::assertCount(1, $collection);

$retrievedModel = $collection[0];
$this->assertSame($connectionResultRow['id'], $retrievedModel->id);
$this->assertSame((int) $connectionResultRow['intAttribute'], $retrievedModel->intAttribute);
$this->assertSame((float) $connectionResultRow['floatAttribute'], $retrievedModel->floatAttribute);
$this->assertSame((string) $connectionResultRow['stringAttribute'], $retrievedModel->stringAttribute);
$this->assertTrue($retrievedModel->boolAttribute);
$this->assertTrue($retrievedModel->booleanAttribute);
$this->assertEquals(json_decode($connectionResultRow['objectAttribute']), $retrievedModel->objectAttribute);
$this->assertSame(json_decode($connectionResultRow['arrayAttribute'], true), $retrievedModel->arrayAttribute);
$this->assertSame($connectionResultRow['arrayAttribute'], $retrievedModel->jsonAttribute);
self::assertSame($connectionResultRow['id'], $retrievedModel->id);
self::assertSame((int) $connectionResultRow['intAttribute'], $retrievedModel->intAttribute);
self::assertSame((float) $connectionResultRow['floatAttribute'], $retrievedModel->floatAttribute);
self::assertSame((string) $connectionResultRow['stringAttribute'], $retrievedModel->stringAttribute);
self::assertTrue($retrievedModel->boolAttribute);
self::assertTrue($retrievedModel->booleanAttribute);
self::assertEquals(json_decode($connectionResultRow['objectAttribute']), $retrievedModel->objectAttribute);
self::assertSame(json_decode($connectionResultRow['arrayAttribute'], true), $retrievedModel->arrayAttribute);
self::assertSame($connectionResultRow['arrayAttribute'], $retrievedModel->jsonAttribute);
}
}
34 changes: 17 additions & 17 deletions tests/Unit/Database/Eloquent/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function testMapModelToModel(): void
return $model;
});

$this->assertInstanceOf(Collection::class, $models);
$this->assertCount($connectionResult->count(), $models);
self::assertInstanceOf(Collection::class, $models);
self::assertCount($connectionResult->count(), $models);

$models->each(function (EloquentModelCastingTest $model, int $key) use ($now) {
$this->assertSame($key + 1, $model->id);
$this->assertInstanceOf(Carbon::class, $model->datetimeAttribute);
$this->assertSame($now->toDateTimeString(), $model->datetimeAttribute->toDateTimeString());
self::assertSame($key + 1, $model->id);
self::assertInstanceOf(Carbon::class, $model->datetimeAttribute);
self::assertSame($now->toDateTimeString(), $model->datetimeAttribute->toDateTimeString());
});
}

Expand All @@ -89,13 +89,13 @@ public function testMapModelToArray(): void
];
});

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $collection);
$this->assertCount($connectionResult->count(), $collection);
self::assertInstanceOf(\Illuminate\Support\Collection::class, $collection);
self::assertCount($connectionResult->count(), $collection);

$collection->each(function (array $row, int $key) use ($now) {
$this->assertSame($key + 1, $row['id']);
$this->assertInstanceOf(Carbon::class, $row['datetimeAttribute']);
$this->assertSame($now->toDateTimeString(), $row['datetimeAttribute']->toDateTimeString());
self::assertSame($key + 1, $row['id']);
self::assertInstanceOf(Carbon::class, $row['datetimeAttribute']);
self::assertSame($now->toDateTimeString(), $row['datetimeAttribute']->toDateTimeString());
});
}

Expand All @@ -117,10 +117,10 @@ public function testFind($key): void
$found = EloquentModelCastingTest::all()->find($key);

if (is_array($key)) {
$this->assertInstanceOf(Collection::class, $found);
$this->assertCount(count($key), $found);
self::assertInstanceOf(Collection::class, $found);
self::assertCount(count($key), $found);
} else {
$this->assertInstanceOf(EloquentModelCastingTest::class, $found);
self::assertInstanceOf(EloquentModelCastingTest::class, $found);
}
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public function testContains(bool $expected, $key, $operator = null, $value = nu
$contains = EloquentModelCastingTest::all()->contains($key);
}

$this->assertSame($expected, $contains);
self::assertSame($expected, $contains);
}

public function testGet(): void
Expand All @@ -167,13 +167,13 @@ public function testGet(): void

$models = EloquentModelCastingTest::all();

$this->assertInstanceOf(Collection::class, $models);
$this->assertCount($connectionResult->count(), $models);
self::assertInstanceOf(Collection::class, $models);
self::assertCount($connectionResult->count(), $models);
$models = $models->map(function (EloquentModelCastingTest $model) {
return $model->toArray();
});

$this->assertSame(
self::assertSame(
$connectionResult
->map(function (array $row) {
$row['floatAttribute'] = (float) $row['floatAttribute'];
Expand Down
Loading