Skip to content

Commit 4ac95e4

Browse files
authored
Merge pull request #19 from sartor/batch-insert
2 parents 91a5110 + 563b3b6 commit 4ac95e4

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

Command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace bashkarev\clickhouse;
99

10-
use ClickHouseDB\Exception\DatabaseException;
10+
use ClickHouseDB\Exception\QueryException;
1111
use ClickHouseDB\Statement;
1212
use Yii;
1313
use yii\db\Exception;
@@ -57,7 +57,7 @@ protected function queryInternal($method, $fetchMode = null)
5757
}
5858
$result = call_user_func_array([$this, $method], [$statement, $fetchMode]);
5959
Yii::endProfile($token, 'bashkarev\clickhouse\Command::query');
60-
} catch (DatabaseException $e) {
60+
} catch (QueryException $e) {
6161
Yii::endProfile($token, 'bashkarev\clickhouse\Command::query');
6262
throw new Exception($e->getMessage());
6363
} catch (\Exception $e) {
@@ -91,7 +91,7 @@ public function execute()
9191
$statement = $this->db->execute($rawSql);
9292
$this->refreshTableSchema();
9393
return (int)(!$statement->isError());
94-
} catch (DatabaseException $e) {
94+
} catch (QueryException $e) {
9595
Yii::endProfile($token, __METHOD__);
9696
throw new Exception($e->getMessage());
9797
} catch (\Exception $e) {

Connection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ public function getIsActive()
148148
return ($this->_client !== null);
149149
}
150150

151+
public function getClient(): Client
152+
{
153+
return $this->_client;
154+
}
155+
151156
/**
152157
* @inheritdoc
153158
* @throws NotSupportedException

README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ Extension ClickHouse for Yii 2
22
==============================
33

44
This extension provides the [ClickHouse](https://clickhouse.yandex/) integration for the [Yii framework 2.0](http://www.yiiframework.com).
5+
Main features:
6+
- SQL commands
7+
- Query builder
8+
- Schema builder
9+
- Migrations
10+
- Batch Insert
11+
- Parallel insert from large CSV files
12+
- Valid handling of UInt64 type in PHP
13+
- Supports Decimals and Nullable fields
514

615
[![Build Status](https://travis-ci.org/bashkarev/clickhouse.svg?branch=master)](https://travis-ci.org/bashkarev/clickhouse)
716

@@ -84,23 +93,27 @@ yii clickhouse-migrate
8493
# reverts the last applied migration
8594
yii clickhouse-migrate/down
8695
```
96+
Access to native SMI2 ClickHouse client
97+
---------------------------------------
98+
```php
99+
$client = \Yii::$app->clickhouse->getClient();
100+
```
87101

88102
Insert csv files
89103
----------------
90104

91105
> Files are uploaded in parallel.
92106
93107
```php
94-
/**
95-
* @var \bashkarev\clickhouse\InsertFiles $insert
96-
*/
97-
$insert = Yii::$app->clickhouse->createCommand()->batchInsertFiles('csv',[
98-
'@vendor/bashkarev/clickhouse/tests/data/csv/e1e747f9901e67ca121768b36921fbae.csv',
99-
'@vendor/bashkarev/clickhouse/tests/data/csv/ebe191dfc36d73aece91e92007d24e3e.csv',
100-
]);
101-
$insert
102-
->setFiles(fopen('/csv/ebe191dfc36d73aece91e92007d24e3e.csv','rb'))
103-
->setChunkSize(8192) // default 4096 bytes
104-
->execute();
108+
$db = \Yii::$app->clickhouse;
109+
$client = $db->getClient();
110+
111+
$results = $client->insertBatchFiles('table_name', ['file_with_data.csv']);
112+
113+
$state = $results['file_with_data.csv'];
114+
$isSuccess = !$state->isError();
115+
$uploadInfo = $state->responseInfo();
116+
117+
print_r($uploadInfo);
105118
```
106119

tests/ConnectionTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
namespace bashkarev\clickhouse\tests;
99

1010
use bashkarev\clickhouse\Connection;
11+
use bashkarev\clickhouse\Query;
12+
use ClickHouseDB\Statement;
1113
use yii\base\InvalidConfigException;
1214
use yii\base\NotSupportedException;
13-
use yii\db\Exception;
1415

1516
/**
1617
* @author Dmitry Bashkarev <[email protected]>
@@ -144,4 +145,28 @@ public function testTransaction()
144145
$this->expectException(NotSupportedException::class);
145146
$this->getConnection()->beginTransaction();
146147
}
148+
149+
public function testBatchInsert()
150+
{
151+
$connection = $this->getConnection();
152+
$client = $connection->getClient();
153+
154+
$files = [
155+
\Yii::getAlias('@data/csv/e1e747f9901e67ca121768b36921fbae.csv'),
156+
\Yii::getAlias('@data/csv/ebe191dfc36d73aece91e92007d24e3e.csv'),
157+
\Yii::getAlias('@data/csv/empty.csv'),
158+
];
159+
160+
$result = $client->insertBatchFiles('csv', $files);
161+
$count = (new Query)
162+
->from('csv')
163+
->count('*', $connection);
164+
165+
foreach ($result as $filename => $state) {
166+
/** @var Statement $state */
167+
$this->assertEquals($state->isError(), false);
168+
}
169+
170+
$this->assertContains('2000', $count);
171+
}
147172
}

tests/QueryTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,28 @@ public function testSubQuery()
117117
public function testUnionAll()
118118
{
119119
$db = $this->getConnection();
120-
$secondQuery = (new Query())
120+
$query1 = (new Query())
121121
->select('name')
122122
->from('{{customer}}')
123123
->where([
124124
'id' => [1]
125125
]);
126126

127-
$rows = (new Query())
127+
$query2 = (new Query())
128128
->select('name')
129129
->from('{{customer}}')
130130
->where([
131131
'id' => [2]
132-
])
133-
->union($secondQuery, true)
132+
]);
133+
134+
$rows = (new Query())
135+
->select('name')
136+
->from(['result' => $query1->union($query2, true)])
134137
->orderBy(['name' => SORT_ASC])
135138
->all($db);
136139

140+
// Using Subquery, because of ClickHouse does not sort result
141+
137142
$this->assertEquals([
138143
['name' => 'user1'],
139144
['name' => 'user2']

0 commit comments

Comments
 (0)