Download the PHP package the-tinderbox/clickhouse-builder without Composer
On this page you can find all versions of the php package the-tinderbox/clickhouse-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download the-tinderbox/clickhouse-builder
More information about the-tinderbox/clickhouse-builder
Files in the-tinderbox/clickhouse-builder
Package clickhouse-builder
Short Description Clickhouse sql query builder
License
Informations about the package clickhouse-builder
Clickhouse Query Builder
Requirements
php 7.1+
Install
Via composer
Usage
For working query builder we must previously instantiate and pass in constructor the-tinderbox/clickhouse-php-client
.
After that we can build and perform sql queries.
Select columns
All this calls will be transformed into next sql:
Also, as a column we can pass closure. In this case in closure will be passed instance of Column class, inside which we can setup column how we want. This can be useful for difficult expressions with many functions, subqueries and etc.
Will be compiled in:
Will be compiled in:
Same behavior can be also achieved by any of the following approaches:
Notice! Functions on columns is not stable and under development.
From
Produce the following query:
Also can be passed closure or builder as argument for performing sub query.
or
or
or
It is all variants of the same sql query which was listed above.
Sample coefficient
I think there no need for additional words)
Joins
For performing subquery as first argument you can pass closure or builder.
Also there are many helper functions with hardcoded arguments, like strict or type and they combinations.
You can use array join as well.
Temporary tables usage
There are some cases when you need to filter f.e. users by their ids, but amount of ids is huge. You can store users ids in local file, upload it to server and use it as temporary table.
Read more about local files here in section Using local files
.
Select
You should pass instance of TempTable
with declared table structure to attach file to query.
If you want tables to be detected automatically, call addFile
method before calling whereIn
.
You can use local files in whereIn
, prewhereIn
, havingIn
and join
statements of query builder.
Insert
If you want to insert file or files into Clickhouse, you could use insertFile
and insertFiles
methods.
Or you can pass batch of files into insertFiles
method and all of them will be inserted
asynchronously.
Also, you can use helper and insert data to temporary table with engine Memory.
Helper will drop temporary table with name test
and creates table with declared structure, engine Memory
and inserts data from test.tsv
file into just created table.
It's helpful if you want to fill some table with data to execute query and then drop it.
Prewhere, where, having
All example will be about where, but same behavior also is for prewhere and having.
All string values will be wrapped with single quotes.
If operator is not provided =
will be used.
If operator is not provided and value is an array, then IN
will be used.
If in the first argument was passed closure, then all wheres statements from inside will be wrapped with parenthesis.
But if on that builder (inside closure) will be specified from
then it will be transformed into subquery.
Almost same is for value parameter, except wrapping into parenthesis. Any closure or builder instance passed as value will be converted into subquery.
Also you can pass internal representation of this statement and it will be used. I will no talk about this with deeper explanation because its not preferable way to use this.
Like joins there are many helpers with hardcoded parameters.
Also there is method to make where by dictionary:
If you want to use complex key, you may pass an array as $key
, then array will be converted to tuple. By default all strings will be escaped by single quotes, but you may pass an Identifier
instance to pass for example column name:
Will produce:
Group By
Works like select.
Final query will be like:
Order By
In the example above, third argument is optional
Aliases:
For column there are same behaviour like in select method.
Limit
There are two types of limit. Limit and limit n by.
Limit n by:
Will produce:
Simple limit:
Will produce:
Union ALL
In unionAll
method can be passed closure or builder instance. In case of closure inside will be passed
builder instance.
Performing request and getting result.
After building request you must call get()
method for sending request to the server.
Also there has opportunity to make asynchronous requests. Its works almost like unionAll
.
This callings will produce the same behavior. Two queries which will be executed asynchronous.
Now, if you call get()
method, as result will be returned array, where numeric index correspond to the result of
request with this number.
Integrations
Laravel or Lumen < 5.5
You can use this builder in Laravel/Lumen applications.
Laravel
In config/app.php
add:
Lumen
In bootstrap/app.php
add:
Connection configures via config/database.php
.
Example with alone server:
Get a new builder:
or
Example with cluster:
Example with server with tag:
Choose server without cluster:
Or execute each new query on random server:
Choose cluster:
Use server with tag:
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
call usingRandomServer
and using
methods with selected cluster or not.
All versions of clickhouse-builder with dependencies
myclabs/php-enum Version ^1.5
the-tinderbox/clickhouse-php-client Version ^3.0