Skip to content

Commit c150b08

Browse files
committed
Add delete method for BaseModel
1 parent a3c845c commit c150b08

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
## 1.12.0 [2022-01-19]
2+
3+
### Features
4+
1. Add delete method for BaseModel
5+
16
## 1.11.0 [2022-01-09]
27

38
### Features
4-
1. Fix issue #8: Migrations on standalone Clickhouse database
9+
1. Fix issue #8: Migrations on standalone Clickhouse database
510

611
## 1.10.0 [2021-09-20]
712

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,40 @@ class MyTable extends BaseModel
235235
}
236236
```
237237

238+
### OPTIMIZE Statement
239+
240+
See https://clickhouse.com/docs/ru/sql-reference/statements/optimize/
241+
242+
```php
243+
MyTable::optimize($final = false, $partition = null);
244+
```
245+
246+
### Deletions
247+
248+
See https://clickhouse.com/docs/en/sql-reference/statements/alter/delete/
249+
250+
```php
251+
MyTable::where('field_one', 123)->delete();
252+
```
253+
254+
Using buffer engine and performing OPTIMIZE or ALTER TABLE DELETE
255+
256+
```php
257+
<?php
258+
259+
namespace App\Models\Clickhouse;
260+
261+
use PhpClickHouseLaravel\BaseModel;
262+
263+
class MyTable extends BaseModel
264+
{
265+
// All SELECT's and INSERT's on $table
266+
protected $table = 'my_table_buffer';
267+
// OPTIMIZE and DELETE on $tableSources
268+
protected $tableSources = 'my_table';
269+
}
270+
```
271+
238272
### Helpers for inserting different data types
239273

240274
```php

src/BaseModel.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
99
use Illuminate\Support\Facades\DB;
1010
use Illuminate\Support\Str;
11+
use Tinderbox\ClickhouseBuilder\Query\Enums\Operator;
12+
use Tinderbox\ClickhouseBuilder\Query\TwoElementsLogicExpression;
1113

1214
class BaseModel
1315
{
@@ -29,6 +31,13 @@ class BaseModel
2931
*/
3032
protected $tableForInserts;
3133

34+
/**
35+
* Use this field for OPTIMIZE TABLE OR ALTER TABLE (also DELETE) queries
36+
*
37+
* @var string
38+
*/
39+
protected $tableSources;
40+
3241
/**
3342
* Indicates if the model exists.
3443
*
@@ -56,6 +65,15 @@ public function getTableForInserts()
5665
return $this->tableForInserts ?? $this->getTable();
5766
}
5867

68+
/**
69+
* Use this field for OPTIMIZE TABLE OR ALTER TABLE (also DELETE) queries
70+
* @return string
71+
*/
72+
public function getTableSources()
73+
{
74+
return $this->tableSources ?? $this->getTable();
75+
}
76+
5977
/**
6078
* @return Client
6179
*/
@@ -268,15 +286,54 @@ public function __set($key, $value)
268286
* Optimize table. Using for ReplacingMergeTree, etc.
269287
* @source https://clickhouse.tech/docs/ru/sql-reference/statements/optimize/
270288
* @param bool $final
289+
* @param string|null $partition
271290
* @return \ClickHouseDB\Statement
272291
*/
273-
public static function optimize($final = false)
292+
public static function optimize($final = false, $partition = null)
274293
{
275-
$sql = "OPTIMIZE TABLE " . (new static)->getTable();
294+
$sql = "OPTIMIZE TABLE " . (new static)->getTableSources();
295+
if ($partition) {
296+
$sql .= " PARTITION $partition";
297+
}
276298
if ($final) {
277299
$sql .= " FINAL";
278300
}
279301
return static::getClient()->write($sql);
280302
}
281303

304+
/**
305+
* @param TwoElementsLogicExpression|string|Closure $column
306+
* @param string|null $operator
307+
* @param int|float|string|null $value
308+
* @param string $concatOperator Operator::AND for example
309+
* @return Builder
310+
*/
311+
public static function where($column, $operator = null, $value = null, string $concatOperator = Operator::AND)
312+
{
313+
$static = new static;
314+
$builder = (new Builder)->select(['*'])
315+
->from($static->getTable())
316+
->setSourcesTable($static->getTableSources());
317+
if (is_null($value)) {
318+
// Fix func_num_args() in where clause in BaseBuilder
319+
$builder->where($column, $operator);
320+
} else {
321+
$builder->where($column, $operator, $value, $concatOperator);
322+
}
323+
return $builder;
324+
}
325+
326+
/**
327+
* @param string $expression
328+
* @return Builder
329+
*/
330+
public static function whereRaw(string $expression)
331+
{
332+
$static = new static;
333+
return (new Builder)->select(['*'])
334+
->from($static->getTable())
335+
->setSourcesTable($static->getTableSources())
336+
->whereRaw($expression);
337+
}
338+
282339
}

src/Builder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
class Builder extends BaseBuilder
1414
{
1515

16+
/** @var string */
17+
protected $tableSources;
18+
1619
public function __construct()
1720
{
1821
$this->grammar = new Grammar();
@@ -52,4 +55,28 @@ public function chunk(int $count, callable $callback)
5255
} while ($rows);
5356
}
5457

58+
/**
59+
* For delete query
60+
* @param string $table
61+
* @return $this
62+
*/
63+
public function setSourcesTable(string $table)
64+
{
65+
$this->tableSources = $table;
66+
return $this;
67+
}
68+
69+
/**
70+
* Note! This is a heavy operation not designed for frequent use.
71+
* @return Statement
72+
*/
73+
public function delete()
74+
{
75+
$table = $this->tableSources ?? $this->getFrom()->getTable();
76+
$sql = "ALTER TABLE $table DELETE " . $this->grammar->compileWheresComponent($this, $this->getWheres());
77+
/** @var Client $db */
78+
$db = DB::connection('clickhouse')->getClient();
79+
return $db->write($sql);
80+
}
81+
5582
}

0 commit comments

Comments
 (0)