├── .github └── workflows │ └── ci.yml ├── .styleci.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── docker-compose.yml ├── docker └── Dockerfile ├── duster.json ├── pint.json └── src ├── ClickhouseBuilder.php ├── ClickhouseConnection.php ├── ClickhouseGrammar.php ├── ClickhouseModel.php ├── ClickhouseProcessor.php ├── LaravelClickhouse.php ├── LaravelClickhouseServiceProvider.php └── Traits └── HasBindings.php /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - '**' 7 | 8 | jobs: 9 | main: 10 | name: Lint and Test 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout code 15 | uses: actions/checkout@v4 16 | with: 17 | ref: ${{ github.event.pull_request.head.ref }} 18 | repository: ${{ github.event.pull_request.head.repo.full_name }} 19 | 20 | - name: "Duster Lint" 21 | uses: tighten/duster-action@v3 22 | with: 23 | args: lint 24 | 25 | - name: Set up PHP 26 | uses: shivammathur/setup-php@v2 27 | with: 28 | php-version: '8.3' 29 | 30 | - name: Install ClickHouse 31 | run: | 32 | sudo apt-get update 33 | sudo apt-get install -y apt-transport-https ca-certificates curl gnupg 34 | curl -fsSL 'https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key' | sudo gpg --dearmor -o /usr/share/keyrings/clickhouse-keyring.gpg 35 | echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg] https://packages.clickhouse.com/deb stable main" | sudo tee /etc/apt/sources.list.d/clickhouse.list 36 | sudo apt-get update 37 | sudo apt-get install -y clickhouse-server clickhouse-client 38 | sudo service clickhouse-server start 39 | 40 | - name: Install SeasClick extension 41 | run: | 42 | sudo apt-get install -y php-pear php-dev gcc make 43 | sudo pecl install seasclick 44 | env: 45 | ACCEPT_PECL_LICENSE: 'yes' 46 | 47 | - name: Enable SeasClick extension 48 | run: echo "extension=SeasClick.so" | sudo tee /etc/php/8.3/cli/conf.d/30-seasclick.ini 49 | 50 | - name: Install dependencies 51 | run: composer install --prefer-dist --no-progress --no-suggest 52 | 53 | - name: Run PHPUnit 54 | env: 55 | APP_ENV: ci 56 | run: vendor/bin/phpunit 57 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Patrique Ouimet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Clickhouse 2 | 3 | An Eloquent model and Query builder with support for Clickhouse using the [SeasClick](https://github.com/seasx/seasclick) extension. 4 | 5 | ## ⚠️ WARNING ⚠️ 6 | 7 | **This package/repository is in active development, use at your own risk.** 8 | 9 | ## Requirements 10 | 11 | [SeasClick](https://github.com/seasx/seasclick) extension 12 | 13 | ## Installation 14 | 15 | You can install the package via composer: 16 | 17 | ```json 18 | { 19 | "repositories": [ 20 | { 21 | "type": "vcs", 22 | "url": "https://github.com/patoui/laravel-clickhouse" 23 | } 24 | ], 25 | "require": { 26 | "patoui/laravel-clickhouse": "dev-main" 27 | } 28 | } 29 | ``` 30 | 31 | Add service provider 32 | 33 | ``` php 34 | 'providers' => [ 35 | // Other Service Providers 36 | Patoui\LaravelClickhouse\LaravelClickhouseServiceProvider::class, 37 | ], 38 | ``` 39 | 40 | Add connection details 41 | 42 | ```php 43 | 'connections' => [ 44 | 'clickhouse' => [ 45 | 'host' => '127.0.0.1', 46 | 'port' => '9000', 47 | 'username' => 'default', 48 | 'password' => '', 49 | ] 50 | ], 51 | ``` 52 | 53 | ## Usage 54 | 55 | Use as you normally would an eloquent model or query builder. 56 | 57 | See `tests` directory for additional examples 58 | 59 | Below is a list of currently untested methods or functionality. This does not mean that they won't work, just that there is currently no test coverage. 60 | 61 | - `union` 62 | - `whereJsonContains` 63 | - `whereJsonLength` 64 | - `whereBetween` 65 | - `whereNotBetween` 66 | - `orWhereColumn` 67 | - Subquery Where Clauses 68 | - Upserts 69 | - `updateOrInsert` 70 | - Updating JSON Columns `->update(['options->enabled' => true]);` 71 | - `increment` or `decrement` 72 | 73 | ```php 74 | DB::connection('clickhouse')->insert( 75 | 'analytics', 76 | ['ts' => time(), 'analytic_id' => mt_rand(1000, 9999), 'status' => mt_rand(200, 599)] 77 | ); 78 | 79 | DB::connection('clickhouse')->table('analytics')->insert([ 80 | 'ts' => time(), 81 | 'analytic_id' => 321, 82 | 'status' => 204, 83 | ]); 84 | 85 | DB::connection('clickhouse') 86 | ->table('analytics') 87 | ->where('ts', '>', strtotime('-1 day')) 88 | ->count(); 89 | 90 | class Analytic extends ClickhouseModel 91 | { 92 | public $guarded = []; // optional, added for brevity 93 | } 94 | 95 | Analytic::create(['ts' => time(), 'analytic_id' => mt_rand(1000, 9999), 'status' => 204, 'name' => 'page_view']); 96 | 97 | Analytic::where('ts', '>', strtotime('-1 day'))->count(); 98 | 99 | Analytic::where('name', 'page_view')->update(['name' => 'page_visit']); 100 | ``` 101 | 102 | ### Testing 103 | 104 | Testing is done within docker to simplify setting up Clickhouse 105 | 106 | ```bash 107 | composer up <-- starts the docker containers, PHP and ClickHouse 108 | ``` 109 | 110 | Run the tests: 111 | 112 | ```bash 113 | composer test 114 | ``` 115 | 116 | ### Caveats 117 | 118 | - the underlying `SeasClick` extension does not support all Clickhouse features 119 | - it does not support the `Bool` type, a `UInt8` can be used instead 120 | 121 | ## Contributing 122 | 123 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 124 | 125 | ### Local testing 126 | 127 | **Requirements** 128 | 129 | - [Docker](https://www.docker.com/) 130 | 131 | Once Docker is running, run the following commands: 132 | 133 | ``` 134 | composer up <-- starts the docker containers, PHP and ClickHouse 135 | composer in <-- install composer dependencies in the PHP container 136 | composer test <-- run the test suite 137 | ``` 138 | 139 | ### Security 140 | 141 | If you discover any security related issues, please email patrique.ouimet@gmail.com instead of using the issue tracker. 142 | 143 | ## Credits 144 | 145 | - [Patrique Ouimet](https://github.com/patoui) 146 | - [All Contributors](../../contributors) 147 | 148 | ## License 149 | 150 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 151 | 152 | ## Laravel Package Boilerplate 153 | 154 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 155 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "patoui/laravel-clickhouse", 3 | "description": "Laravel SeasClick Clickhouse", 4 | "keywords": [ 5 | "patoui", 6 | "laravel-clickhouse" 7 | ], 8 | "homepage": "https://github.com/patoui/laravel-clickhouse", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Patrique Ouimet", 14 | "email": "patrique.ouimet@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "8.x", 20 | "ext-SeasClick": "*", 21 | "illuminate/support": "^10.0|^11.0", 22 | "illuminate/container": "^10.0|^11.0", 23 | "illuminate/database": "^10.0|^11.0" 24 | }, 25 | "require-dev": { 26 | "orchestra/testbench": "^9.0", 27 | "phpunit/phpunit": "^11.0", 28 | "tightenco/duster": "^3.0" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Patoui\\LaravelClickhouse\\": "src" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Patoui\\LaravelClickhouse\\Tests\\": "tests" 38 | } 39 | }, 40 | "scripts": { 41 | "lint": "vendor/bin/duster lint", 42 | "fix": "vendor/bin/duster fix", 43 | "up": "docker compose up -d", 44 | "down": "docker compose down", 45 | "in": "docker exec -it lc_php composer install", 46 | "test": "docker exec -it lc_php vendor/bin/phpunit \"$@\"", 47 | "test-coverage": "docker exec -it lc_php vendor/bin/phpunit --coverage-html coverage", 48 | "clickhouse": "docker exec -it lc_clickhouse clickhouse --client" 49 | }, 50 | "config": { 51 | "sort-packages": true 52 | }, 53 | "extra": { 54 | "laravel": { 55 | "providers": [ 56 | "Patoui\\LaravelClickhouse\\LaravelClickhouseServiceProvider" 57 | ] 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | 3 | app: 4 | build: 5 | context: . 6 | dockerfile: ./docker/Dockerfile 7 | image: library/php 8 | container_name: lc_php 9 | restart: unless-stopped 10 | tty: true 11 | environment: 12 | SERVICE_NAME: app 13 | SERVICE_TAGS: dev 14 | working_dir: /var/www 15 | volumes: 16 | - ./:/var/www 17 | networks: 18 | - app-network 19 | 20 | clickhouse: 21 | container_name: lc_clickhouse 22 | image: clickhouse/clickhouse-server:latest 23 | ports: 24 | - "8123:8123" # HTTP interface 25 | - "9000:9000" # Native client interface 26 | volumes: 27 | - ./clickhouse:/var/lib/clickhouse 28 | networks: 29 | - app-network 30 | environment: 31 | - CLICKHOUSE_SKIP_USER_SETUP=1 32 | 33 | #Docker Networks 34 | networks: 35 | app-network: 36 | driver: bridge 37 | 38 | volumes: 39 | lc_clickhouse: 40 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.3-fpm 2 | 3 | # Copy composer.lock and composer.json 4 | COPY composer.lock composer.json /var/www/ 5 | 6 | # Set working directory 7 | WORKDIR /var/www 8 | 9 | # Install dependencies 10 | RUN apt-get update && apt-get install -y \ 11 | build-essential \ 12 | libzip-dev \ 13 | libpng-dev \ 14 | libjpeg62-turbo-dev \ 15 | libfreetype6-dev \ 16 | locales \ 17 | zip \ 18 | jpegoptim optipng pngquant gifsicle \ 19 | vim \ 20 | unzip \ 21 | git \ 22 | curl 23 | 24 | RUN git clone https://github.com/SeasX/SeasClick.git && cd SeasClick && \ 25 | phpize && ./configure && make && make install 26 | 27 | # Clear cache 28 | RUN apt-get clean && rm -rf /var/lib/apt/lists/* 29 | 30 | # Install extensions 31 | RUN docker-php-ext-install zip exif pcntl pdo pdo_mysql 32 | 33 | RUN docker-php-ext-enable SeasClick 34 | 35 | # Install composer 36 | RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer 37 | 38 | # Copy existing application directory contents 39 | COPY . /var/www 40 | 41 | # Expose port 9000 and start php-fpm server 42 | EXPOSE 9000 43 | CMD ["php-fpm"] 44 | -------------------------------------------------------------------------------- /duster.json: -------------------------------------------------------------------------------- 1 | { 2 | "exclude": [ 3 | "tests/TestCase.php" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "laravel", 3 | "rules": { 4 | "blank_line_between_import_groups": true, 5 | "concat_space": { 6 | "spacing": "one" 7 | }, 8 | "class_attributes_separation": { 9 | "elements": { 10 | "method": "one" 11 | } 12 | }, 13 | "curly_braces_position": { 14 | "control_structures_opening_brace": "same_line", 15 | "functions_opening_brace": "next_line_unless_newline_at_signature_end", 16 | "anonymous_functions_opening_brace": "same_line", 17 | "classes_opening_brace": "next_line_unless_newline_at_signature_end", 18 | "anonymous_classes_opening_brace": "next_line_unless_newline_at_signature_end", 19 | "allow_single_line_empty_anonymous_classes": true, 20 | "allow_single_line_anonymous_functions": false 21 | }, 22 | "explicit_string_variable": true, 23 | "global_namespace_import": { 24 | "import_classes": true, 25 | "import_constants": true, 26 | "import_functions": true 27 | }, 28 | "new_with_braces": { 29 | "named_class": false, 30 | "anonymous_class": false 31 | }, 32 | "ordered_imports": { 33 | "sort_algorithm": "alpha", 34 | "imports_order": [ 35 | "const", 36 | "class", 37 | "function" 38 | ] 39 | }, 40 | "simple_to_complex_string_variable": true 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/ClickhouseBuilder.php: -------------------------------------------------------------------------------- 1 | grammar->resetBindingKeys(); 33 | } 34 | 35 | /** 36 | * Retrieve the "count" result of the query. 37 | * 38 | * @param string $columns 39 | */ 40 | public function count($columns = null): int 41 | { 42 | return parent::count($columns ?: []); 43 | } 44 | 45 | public function getBindings(): array 46 | { 47 | return $this->flattenWithKeys($this->bindings); 48 | } 49 | 50 | /** 51 | * Insert a new record into the database. 52 | */ 53 | public function insert(array $values): bool 54 | { 55 | // Since every insert gets treated like a batch insert, we will make sure the 56 | // bindings are structured in a way that is convenient when building these 57 | // inserts statements by verifying these elements are actually an array. 58 | if (empty($values)) { 59 | return true; 60 | } 61 | 62 | if (! is_array(reset($values))) { 63 | $values = [$values]; 64 | } 65 | 66 | // Here, we will sort the insert keys for every record so that each insert is 67 | // in the same order for the record. We need to make sure this is the case 68 | // so there are not any errors or problems when inserting these records. 69 | else { 70 | foreach ($values as $key => $value) { 71 | ksort($value); 72 | 73 | $values[$key] = $value; 74 | } 75 | } 76 | 77 | // Finally, we will run this query against the database connection and return 78 | // the results. We will need to also flatten these bindings before running 79 | // the query so they are all in one huge, flattened array for execution. 80 | return $this->connection->insert( 81 | $this->grammar->compileInsert($this, $values), 82 | $this->cleanBindings(array_reduce($values, 'array_merge', [])) 83 | ); 84 | } 85 | 86 | /** 87 | * Update a record in the database. 88 | */ 89 | public function update(array $values): int 90 | { 91 | return $this->connection->update( 92 | $this->grammar->compileUpdate($this, $values), 93 | $this->cleanBindings( 94 | $this->grammar->prepareBindingsForUpdate($this->bindings, $values) 95 | ) 96 | ); 97 | } 98 | 99 | /** 100 | * Remove all of the expressions from a list of bindings. 101 | */ 102 | public function cleanBindings(array $bindings): array 103 | { 104 | return array_filter($bindings, static function ($binding) { 105 | return ! $binding instanceof Expression; 106 | }); 107 | } 108 | 109 | /** 110 | * Add a binding to the query. 111 | * 112 | * @param mixed $value 113 | * @param string $type 114 | * @return $this 115 | * 116 | * @throws InvalidArgumentException 117 | */ 118 | public function addBinding($value, $type = 'where'): self 119 | { 120 | if (! array_key_exists($type, $this->bindings)) { 121 | throw new InvalidArgumentException("Invalid binding type: {$type}."); 122 | } 123 | 124 | $values = is_array($value) ? $value : [$value]; 125 | 126 | foreach ($values as $value) { 127 | $this->bindings[$type][$this->nextBindingKey($value)] = $this->castBinding($value); 128 | } 129 | 130 | return $this; 131 | } 132 | 133 | /** 134 | * Add a "where month" statement to the query. 135 | * 136 | * @param \Illuminate\Contracts\Database\Query\Expression|string $column 137 | * @param DateTimeInterface|string|int|null $operator 138 | * @param DateTimeInterface|string|int|null $value 139 | * @param string $boolean 140 | * @return $this 141 | */ 142 | public function whereMonth($column, $operator, $value = null, $boolean = 'and') 143 | { 144 | [$value, $operator] = $this->prepareValueAndOperator( 145 | $value, $operator, func_num_args() === 2 146 | ); 147 | 148 | // If the given operator is not found in the list of valid operators we will 149 | // assume that the developer is just short-cutting the '=' operators and 150 | // we will set the operators to '=' and set the values appropriately. 151 | if ($this->invalidOperator($operator)) { 152 | [$value, $operator] = [$operator, '=']; 153 | } 154 | 155 | $value = $this->flattenValue($value); 156 | 157 | if ($value instanceof DateTimeInterface) { 158 | // modified, no leading zero 159 | $value = $value->format('n'); 160 | } 161 | 162 | if (! $value instanceof Expression) { 163 | // modified, no leading zero 164 | $value = sprintf('%d', $value); 165 | } 166 | 167 | return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean); 168 | } 169 | 170 | /** 171 | * Add a "where day" statement to the query. 172 | * 173 | * @param \Illuminate\Contracts\Database\Query\Expression|string $column 174 | * @param DateTimeInterface|string|int|null $operator 175 | * @param DateTimeInterface|string|int|null $value 176 | * @param string $boolean 177 | * @return $this 178 | */ 179 | public function whereDay($column, $operator, $value = null, $boolean = 'and') 180 | { 181 | [$value, $operator] = $this->prepareValueAndOperator( 182 | $value, $operator, func_num_args() === 2 183 | ); 184 | 185 | // If the given operator is not found in the list of valid operators we will 186 | // assume that the developer is just short-cutting the '=' operators and 187 | // we will set the operators to '=' and set the values appropriately. 188 | if ($this->invalidOperator($operator)) { 189 | [$value, $operator] = [$operator, '=']; 190 | } 191 | 192 | $value = $this->flattenValue($value); 193 | 194 | if ($value instanceof DateTimeInterface) { 195 | // modified, no leading zero 196 | $value = $value->format('j'); 197 | } 198 | 199 | if (! $value instanceof Expression) { 200 | // modified, no leading zero 201 | $value = sprintf('%d', $value); 202 | } 203 | 204 | return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /src/ClickhouseConnection.php: -------------------------------------------------------------------------------- 1 | config = $config; 28 | 29 | $this->db = new SeasClick($config); 30 | 31 | $this->useDefaultPostProcessor(); 32 | $this->useDefaultSchemaGrammar(); 33 | $this->useDefaultQueryGrammar(); 34 | } 35 | 36 | /** 37 | * Get SeasClick client 38 | */ 39 | public function getClient(): SeasClick 40 | { 41 | return $this->db; 42 | } 43 | 44 | /** 45 | * Begin a fluent query against a database table. 46 | * 47 | * @param Closure|Builder|string $table 48 | * @param string|null $as 49 | * @return Builder 50 | */ 51 | // public function table($table, $as = null): Builder 52 | // { 53 | // $this->notImplementedException(); 54 | // } 55 | 56 | /** 57 | * Get a new query builder instance. 58 | */ 59 | public function query(): ClickhouseBuilder 60 | { 61 | return new ClickhouseBuilder( 62 | $this, $this->getQueryGrammar(), $this->getPostProcessor() 63 | ); 64 | } 65 | 66 | /** 67 | * Get the query post processor used by the connection. 68 | */ 69 | public function getDefaultPostProcessor(): Processor 70 | { 71 | return new ClickhouseProcessor; 72 | } 73 | 74 | /** 75 | * Run a select statement and return a single result. 76 | * 77 | * @param string $query 78 | * @param array $bindings 79 | * @param bool $useReadPdo 80 | * @return mixed 81 | */ 82 | public function selectOne($query, $bindings = [], $useReadPdo = true) 83 | { 84 | $records = $this->db->select($query, $bindings); 85 | 86 | return array_shift($records); 87 | } 88 | 89 | /** 90 | * Run a select statement against the database. 91 | * 92 | * @param string $query 93 | * @param array $bindings 94 | * @param bool $useReadPdo 95 | */ 96 | public function select($query, $bindings = [], $useReadPdo = true): array 97 | { 98 | return $this->db->select($query, $bindings); 99 | } 100 | 101 | /** 102 | * Run a select statement against the database and returns a generator. 103 | * 104 | * @param string $query 105 | * @param array $bindings 106 | * @param bool $useReadPdo 107 | */ 108 | public function cursor($query, $bindings = [], $useReadPdo = true): Generator 109 | { 110 | $this->notImplementedException(); 111 | } 112 | 113 | /** 114 | * Run an insert statement against the database. 115 | * 116 | * @param string $query Query is table name 117 | * @param array $bindings 118 | */ 119 | public function insert($query, $bindings = []): bool 120 | { 121 | [$keys, $values] = $this->parseBindings($bindings); 122 | 123 | return $this->db->insert($query, $keys, $values); 124 | } 125 | 126 | /** 127 | * Run an update statement against the database. 128 | * 129 | * @param string $query 130 | * @param array $bindings 131 | */ 132 | public function update($query, $bindings = []): int 133 | { 134 | // TODO: remove hack and properly determine how many records will be updated 135 | return (int) $this->db->execute($query, $bindings); 136 | } 137 | 138 | /** 139 | * Run a delete statement against the database. 140 | * 141 | * @param string $query 142 | * @param array $bindings 143 | */ 144 | public function delete($query, $bindings = []): int 145 | { 146 | // TODO: determine how many records will be deleted 147 | return (int) $this->db->execute($query, $bindings); 148 | } 149 | 150 | /** 151 | * Execute an SQL statement and return the boolean result. 152 | * 153 | * @param string $query 154 | * @param array $bindings 155 | */ 156 | public function statement($query, $bindings = []): bool 157 | { 158 | return $this->db->execute($query, $bindings); 159 | } 160 | 161 | /** 162 | * Run an SQL statement and get the number of rows affected. 163 | * 164 | * @param string $query 165 | * @param array $bindings 166 | */ 167 | public function affectingStatement($query, $bindings = []): int 168 | { 169 | $this->notImplementedException(); 170 | } 171 | 172 | /** 173 | * Run a raw, unprepared query against the PDO connection. 174 | * 175 | * @param string $query 176 | */ 177 | public function unprepared($query): bool 178 | { 179 | return $this->db->execute($query); 180 | } 181 | 182 | /** 183 | * Prepare the query bindings for execution. 184 | * 185 | * @return array 186 | */ 187 | public function prepareBindings(array $bindings) 188 | { 189 | $this->notImplementedException(); 190 | } 191 | 192 | /** 193 | * Execute a Closure within a transaction. 194 | * 195 | * @param callable|Closure $callback 196 | * @param int $attempts 197 | * @return mixed 198 | * 199 | * @throws Throwable 200 | */ 201 | public function transaction($callback, $attempts = 1) 202 | { 203 | $this->noTransactionException(); 204 | } 205 | 206 | /** 207 | * Start a new database transaction. 208 | */ 209 | public function beginTransaction(): void 210 | { 211 | $this->noTransactionException(); 212 | } 213 | 214 | /** 215 | * Commit the active database transaction. 216 | */ 217 | public function commit(): void 218 | { 219 | $this->noTransactionException(); 220 | } 221 | 222 | /** 223 | * Rollback the active database transaction. 224 | * 225 | * @param int|null $toLevel 226 | */ 227 | public function rollBack($toLevel = null): void 228 | { 229 | $this->noTransactionException(); 230 | } 231 | 232 | /** 233 | * Get the number of active transactions. 234 | * 235 | * @return int 236 | */ 237 | public function transactionLevel() 238 | { 239 | $this->noTransactionException(); 240 | } 241 | 242 | /** 243 | * Execute the given callback in "dry run" mode. 244 | */ 245 | public function pretend(Closure $callback): array 246 | { 247 | $this->notImplementedException(); 248 | } 249 | 250 | /** 251 | * Get the default query grammar instance. 252 | */ 253 | protected function getDefaultQueryGrammar(): ClickhouseGrammar 254 | { 255 | return new ClickhouseGrammar; 256 | } 257 | 258 | /** 259 | * @param array $bindings i.e. [['name' => 'John', 'user_id' => 321]] 260 | * @return array [['name', 'user_id'], [['John', 321]]] 261 | */ 262 | private function parseBindings(array $bindings): array 263 | { 264 | if (! $bindings) { 265 | return [[], []]; 266 | } 267 | 268 | if (! is_string(current(array_keys($bindings)))) { 269 | throw new InvalidArgumentException( 270 | "Keys must be strings, i.e. ['name' => 'John', 'user_id' => 321]" 271 | ); 272 | } 273 | 274 | return [array_keys($bindings), [array_values($bindings)]]; 275 | } 276 | 277 | /** 278 | * Helper method to throw exception for not implemented functionality 279 | */ 280 | private function notImplementedException(): void 281 | { 282 | throw new RuntimeException('Not currently implemented'); 283 | } 284 | 285 | private function noTransactionException(): void 286 | { 287 | throw new RuntimeException('Clickhouse does not currently support transactions'); 288 | } 289 | } 290 | -------------------------------------------------------------------------------- /src/ClickhouseGrammar.php: -------------------------------------------------------------------------------- 1 | wrapTable($query->from); 23 | } 24 | 25 | /** 26 | * Get the appropriate query parameter place-holder for a value. 27 | * 28 | * @param mixed $value 29 | * @param null $key 30 | */ 31 | public function parameter($value, $key = null): string 32 | { 33 | $parsedValue = $this->isExpression($value) ? $this->getValue($value) : $value; 34 | 35 | $param = '{' . $this->nextBindingKey($parsedValue) . '}'; 36 | 37 | return is_string($parsedValue) ? $this->quoteString($param) : $param; 38 | } 39 | 40 | /** 41 | * Prepare the bindings for an update statement. 42 | * 43 | * @return array 44 | */ 45 | public function prepareBindingsForUpdate(array $bindings, array $values) 46 | { 47 | $cleanBindings = Arr::except($bindings, ['select', 'join']); 48 | 49 | $values = Arr::flatten(array_map(fn ($value) => value($value), $values)); 50 | 51 | $mergedBindings = array_values(array_merge(Arr::flatten($cleanBindings), $values)); 52 | 53 | $keyedBindings = []; 54 | 55 | foreach ($mergedBindings as $value) { 56 | $keyedBindings[$this->nextBindingKey($value)] = $value; 57 | } 58 | 59 | return $keyedBindings; 60 | } 61 | 62 | /** 63 | * Compile a raw where clause. 64 | * 65 | * @param array $where 66 | * @return string 67 | */ 68 | protected function whereRaw(Builder $query, $where) 69 | { 70 | return $where['sql']; 71 | } 72 | 73 | /** 74 | * Compile a basic where clause. 75 | * 76 | * @param array $where 77 | * @return string 78 | */ 79 | protected function whereBasic(Builder $query, $where) 80 | { 81 | $value = $this->parameter($where['value']); 82 | 83 | return $this->wrap($where['column']) . ' ' . $where['operator'] . ' ' . $value; 84 | } 85 | 86 | /** 87 | * Compile a "where in" clause. 88 | * 89 | * @param array $where 90 | * @return string 91 | */ 92 | protected function whereIn(Builder $query, $where) 93 | { 94 | if (! empty($where['values'])) { 95 | return $this->wrap($where['column']) . ' in (' . $this->parameterize($where['values'], $where['column']) . ')'; 96 | } 97 | 98 | return '0 = 1'; 99 | } 100 | 101 | /** 102 | * Compile a "where not in" clause. 103 | * 104 | * @param array $where 105 | * @return string 106 | */ 107 | protected function whereNotIn(Builder $query, $where) 108 | { 109 | if (! empty($where['values'])) { 110 | return $this->wrap($where['column']) . ' not in (' . $this->parameterize($where['values'], $where['column']) . ')'; 111 | } 112 | 113 | return '1 = 1'; 114 | } 115 | 116 | /** 117 | * Compile a "where not in raw" clause. 118 | * 119 | * For safety, whereIntegerInRaw ensures this method is only used with integer values. 120 | * 121 | * @param array $where 122 | * @return string 123 | */ 124 | protected function whereNotInRaw(Builder $query, $where) 125 | { 126 | if (! empty($where['values'])) { 127 | return $this->wrap($where['column']) . ' not in (' . implode(', ', $where['values']) . ')'; 128 | } 129 | 130 | return '1 = 1'; 131 | } 132 | 133 | /** 134 | * Compile a "where in raw" clause. 135 | * 136 | * For safety, whereIntegerInRaw ensures this method is only used with integer values. 137 | * 138 | * @param array $where 139 | * @return string 140 | */ 141 | protected function whereInRaw(Builder $query, $where) 142 | { 143 | if (! empty($where['values'])) { 144 | return $this->wrap($where['column']) . ' in (' . implode(', ', $where['values']) . ')'; 145 | } 146 | 147 | return '0 = 1'; 148 | } 149 | 150 | /** 151 | * Compile a "where null" clause. 152 | * 153 | * @param array $where 154 | * @return string 155 | */ 156 | protected function whereNull(Builder $query, $where) 157 | { 158 | return sprintf('isNull(%s)', $this->wrap($where['column'])); 159 | } 160 | 161 | /** 162 | * Compile a "where not null" clause. 163 | * 164 | * @param array $where 165 | * @return string 166 | */ 167 | protected function whereNotNull(Builder $query, $where) 168 | { 169 | return sprintf('isNotNull(%s)', $this->wrap($where['column'])); 170 | } 171 | 172 | /** 173 | * Compile a "between" where clause. 174 | * 175 | * @param array $where 176 | * @return string 177 | */ 178 | protected function whereBetween(Builder $query, $where) 179 | { 180 | $between = $where['not'] ? 'not between' : 'between'; 181 | 182 | $min = $this->parameter(reset($where['values'])); 183 | 184 | $max = $this->parameter(end($where['values'])); 185 | 186 | return $this->wrap($where['column']) . ' ' . $between . ' ' . $min . ' and ' . $max; 187 | } 188 | 189 | /** 190 | * Compile a "between" where clause. 191 | * 192 | * @param array $where 193 | * @return string 194 | */ 195 | protected function whereBetweenColumns(Builder $query, $where) 196 | { 197 | $between = $where['not'] ? 'not between' : 'between'; 198 | 199 | $min = $this->wrap(reset($where['values'])); 200 | 201 | $max = $this->wrap(end($where['values'])); 202 | 203 | return $this->wrap($where['column']) . ' ' . $between . ' ' . $min . ' and ' . $max; 204 | } 205 | 206 | /** 207 | * Compile a "where date" clause. 208 | * 209 | * @param array $where 210 | * @return string 211 | */ 212 | protected function whereDate(Builder $query, $where) 213 | { 214 | return $this->dateBasedWhere('date', $query, $where); 215 | } 216 | 217 | /** 218 | * Compile a "where time" clause. 219 | * 220 | * @param array $where 221 | * @return string 222 | */ 223 | protected function whereTime(Builder $query, $where) 224 | { 225 | $value = $this->parameter($where['value']); 226 | 227 | return sprintf( 228 | "formatDateTime(%s, '%%H:%%i:%%s') %s %s", 229 | $this->wrap($where['column']), 230 | $where['operator'], 231 | $value 232 | ); 233 | } 234 | 235 | /** 236 | * Compile a "where day" clause. 237 | * 238 | * @param array $where 239 | * @return string 240 | */ 241 | protected function whereDay(Builder $query, $where) 242 | { 243 | return $this->dateBasedWhere('day', $query, $where); 244 | } 245 | 246 | /** 247 | * Compile a "where month" clause. 248 | * 249 | * @param array $where 250 | * @return string 251 | */ 252 | protected function whereMonth(Builder $query, $where) 253 | { 254 | return $this->dateBasedWhere('month', $query, $where); 255 | } 256 | 257 | /** 258 | * Compile a "where year" clause. 259 | * 260 | * @param array $where 261 | * @return string 262 | */ 263 | protected function whereYear(Builder $query, $where) 264 | { 265 | return $this->dateBasedWhere('year', $query, $where); 266 | } 267 | 268 | /** 269 | * Compile an update statement without joins into SQL. 270 | * 271 | * @param string $table 272 | * @param string $columns 273 | * @param string $where 274 | */ 275 | protected function compileUpdateWithoutJoins(Builder $query, $table, $columns, $where): string 276 | { 277 | return "alter table {$table} update {$columns} {$where}"; 278 | } 279 | 280 | /** 281 | * Wrap the given JSON selector. 282 | * 283 | * @param string $value 284 | * @return string 285 | */ 286 | protected function wrapJsonSelector($value) 287 | { 288 | [$field, $path] = $this->wrapJsonFieldAndPath($value); 289 | 290 | return 'simpleJSONExtractString(' . $field . $path . ')'; 291 | } 292 | 293 | /** 294 | * Wrap the given JSON path. 295 | * 296 | * @param string $value 297 | * @param string $delimiter 298 | * @return string 299 | */ 300 | protected function wrapJsonPath($value, $delimiter = '->') 301 | { 302 | $value = preg_replace("/([\\\\]+)?\\'/", "''", $value); 303 | 304 | $jsonPath = collect(explode($delimiter, $value)) 305 | ->map(fn ($segment) => $this->wrapJsonPathSegment($segment)) 306 | ->join('.'); 307 | 308 | return sprintf("'%s'", $jsonPath); 309 | } 310 | 311 | /** 312 | * Wrap the given JSON path segment. 313 | * 314 | * @param string $segment 315 | * @return string 316 | */ 317 | protected function wrapJsonPathSegment($segment) 318 | { 319 | if (preg_match('/(\[[^\]]+\])+$/', $segment, $parts)) { 320 | $key = Str::beforeLast($segment, $parts[0]); 321 | 322 | if (! empty($key)) { 323 | return $key . $parts[0]; 324 | } 325 | 326 | return $parts[0]; 327 | } 328 | 329 | return $segment; 330 | } 331 | } 332 | -------------------------------------------------------------------------------- /src/ClickhouseModel.php: -------------------------------------------------------------------------------- 1 | app['db']); 16 | } 17 | 18 | /** 19 | * Register the application services. 20 | */ 21 | public function register(): void 22 | { 23 | // Add database driver. 24 | $this->app->resolving('db', function ($db) { 25 | $db->extend('clickhouse', function ($config) { 26 | return new ClickhouseConnection($config); 27 | }); 28 | }); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Traits/HasBindings.php: -------------------------------------------------------------------------------- 1 | binding_key = null; 15 | $this->binding_keys = null; 16 | } 17 | 18 | protected function nextBindingKey(mixed $value): string 19 | { 20 | $hash = md5(json_encode($value)); 21 | 22 | if (! isset($this->binding_keys)) { 23 | $this->binding_keys = []; 24 | } 25 | 26 | if (isset($this->binding_keys[$hash])) { 27 | return $this->binding_keys[$hash]; 28 | } 29 | 30 | $this->binding_keys[$hash] = $this->nextKey(); 31 | 32 | return $this->binding_keys[$hash]; 33 | } 34 | 35 | protected function nextKey(): string 36 | { 37 | if ($this->binding_key === null) { 38 | $this->binding_key = 'a'; 39 | } else { 40 | $this->binding_key++; 41 | } 42 | 43 | return $this->binding_key; 44 | } 45 | 46 | protected function flattenWithKeys(array $data): array 47 | { 48 | $result = []; 49 | 50 | foreach ($data as $key => $value) { 51 | if (is_array($value)) { 52 | $result = array_merge($result, $this->flattenWithKeys($value)); 53 | } else { 54 | $result[$key] = $value; 55 | } 56 | } 57 | 58 | return $result; 59 | } 60 | } 61 | --------------------------------------------------------------------------------