Skip to content
Draft
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
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ php artisan vendor:publish --provider="Cog\Laravel\Clickhouse\ClickhouseServiceP

Edit `config/clickhouse.php` file.

### ClickHouse settings

You can configure ClickHouse session settings in the configuration file. These settings will be applied to all queries made through the client.

For example, to enable nested JSON storage using the experimental Object type:

```php
'connection' => [
// ... other connection settings
'settings' => [
'allow_experimental_object_type' => 1,
],
],
```

Common settings you might want to configure:
- `allow_experimental_object_type` - Enable JSON/Object type for nested data structures
- `max_execution_time` - Maximum query execution time in seconds
- `max_memory_usage` - Maximum memory usage for query execution

See [ClickHouse settings documentation](https://clickhouse.com/docs/en/operations/settings/settings) for all available settings.

## Usage

### ClickHouse client
Expand All @@ -65,6 +87,45 @@ app(\ClickHouseDB\Client::class)->write(
);
```

#### Example: Working with nested JSON data

After enabling `allow_experimental_object_type` in your configuration, you can create tables with JSON columns and store nested data:

```php
// In your migration
$this->clickhouseClient->write(
<<<SQL
CREATE TABLE IF NOT EXISTS {$this->databaseName}.users (
id UInt64,
name String,
metadata JSON
) ENGINE = MergeTree()
ORDER BY id
SQL
);

// Inserting nested JSON data
app(\ClickHouseDB\Client::class)->insert('users', [
[
'id' => 1,
'name' => 'John Doe',
'metadata' => json_encode([
'address' => [
'street' => '123 Main St',
'city' => 'New York',
],
'preferences' => [
'theme' => 'dark',
'language' => 'en',
],
]),
],
]);

// Reading nested JSON data
$users = app(\ClickHouseDB\Client::class)->select('SELECT * FROM users')->rows();
```

### ClickHouse database migration

#### Create migration
Expand Down
4 changes: 4 additions & 0 deletions config/clickhouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
'timeout' => 1,
'connectTimeOut' => 2,
],
'settings' => [
// ClickHouse session settings
// Example: 'allow_experimental_object_type' => 1,
],
],

/*
Expand Down
11 changes: 11 additions & 0 deletions src/Factory/ClickhouseClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ public function create(
}

$options = [];
$settings = [];

if (isset($config['options'])) {
$options = $config['options'];

unset($config['options']);
}

if (isset($config['settings'])) {
$settings = $config['settings'];

unset($config['settings']);
}

$client = new Client($config);

foreach ($options as $option => $value) {
Expand All @@ -57,6 +64,10 @@ public function create(
$client->$method($value);
}

foreach ($settings as $setting => $value) {
$client->settings()->set($setting, $value);
}

return $client;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Factory/ClickhouseClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,33 @@ public function testInitializationWithNonExistsOption(): void
self::assertSame(ClickhouseConfigException::class, get_class($exception));
}
}

public function testInitializationWithSettings(): void
{
$clickhouse = new ClickhouseClientFactory(
[
'host' => 'example.com',
'port' => '9000',
'username' => 'test_user',
'password' => 'secret',
'options' => [
'database' => 'test_database',
],
'settings' => [
'max_execution_time' => 60,
'allow_experimental_object_type' => 1,
],
]
);

$client = $clickhouse->create();

self::assertSame('example.com', $client->getConnectHost());
self::assertSame('9000', $client->getConnectPort());
self::assertSame('test_user', $client->getConnectUsername());
self::assertSame('secret', $client->getConnectPassword());
self::assertSame('test_database', $client->settings()->getDatabase());
self::assertSame(60, $client->settings()->getSetting('max_execution_time'));
self::assertSame(1, $client->settings()->getSetting('allow_experimental_object_type'));
}
}