diff --git a/README.md b/README.md index 9282756..97f43cf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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( + <<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 diff --git a/config/clickhouse.php b/config/clickhouse.php index 531fc0d..2aa6d50 100644 --- a/config/clickhouse.php +++ b/config/clickhouse.php @@ -33,6 +33,10 @@ 'timeout' => 1, 'connectTimeOut' => 2, ], + 'settings' => [ + // ClickHouse session settings + // Example: 'allow_experimental_object_type' => 1, + ], ], /* diff --git a/src/Factory/ClickhouseClientFactory.php b/src/Factory/ClickhouseClientFactory.php index da8ead7..fcc2358 100644 --- a/src/Factory/ClickhouseClientFactory.php +++ b/src/Factory/ClickhouseClientFactory.php @@ -42,6 +42,7 @@ public function create( } $options = []; + $settings = []; if (isset($config['options'])) { $options = $config['options']; @@ -49,6 +50,12 @@ public function create( unset($config['options']); } + if (isset($config['settings'])) { + $settings = $config['settings']; + + unset($config['settings']); + } + $client = new Client($config); foreach ($options as $option => $value) { @@ -57,6 +64,10 @@ public function create( $client->$method($value); } + foreach ($settings as $setting => $value) { + $client->settings()->set($setting, $value); + } + return $client; } diff --git a/tests/Factory/ClickhouseClientFactoryTest.php b/tests/Factory/ClickhouseClientFactoryTest.php index 33313f4..20fdce4 100644 --- a/tests/Factory/ClickhouseClientFactoryTest.php +++ b/tests/Factory/ClickhouseClientFactoryTest.php @@ -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')); + } }