Add update rows ability

This commit is contained in:
Denis Glushkov
2022-12-27 18:03:00 +05:00
parent de06085480
commit 6ac294dd95
6 changed files with 72 additions and 7 deletions
+5
View File
@@ -1,3 +1,8 @@
## 1.16 [2022-12-27]
### Features
1. Added update rows ability
## 1.15 [2022-08-15]
### Features
+12 -2
View File
@@ -178,7 +178,6 @@ $rows = MyTable::select(['field_one', new RawColumn('sum(field_two)', 'field_two
Events work just like an [eloquent model events](https://laravel.com/docs/9.x/eloquent#events)
Available events: **creating**, **created**, **saved**
### Retries
You may enable ability to retry requests while received not 200 response, maybe due network connectivity problems.
@@ -285,9 +284,20 @@ class MyTable extends BaseModel
}
```
### Updates
See https://clickhouse.com/docs/ru/sql-reference/statements/alter/update/
```php
MyTable::where('field_one', 123)->update(['field_two' => 'new_val']);
// or expression
MyTable::where('field_one', 123)
->update(['field_two' => new RawColumn("concat(field_two,'new_val')")]);
```
### Helpers for inserting different data types
```php
// Array data type
MyTable::insertAssoc([[1,'str',new InsertArray(['a','b'])]]);
MyTable::insertAssoc([[1, 'str', new InsertArray(['a','b'])]]);
```
+14 -5
View File
@@ -80,13 +80,22 @@ class Builder extends BaseBuilder
}
/**
* Makes clean instance of builder.
*
* @return self
* Note! This is a heavy operation not designed for frequent use.
* @return Statement
*/
public function newQuery(): self
public function update(array $values): Statement
{
return new static($this->client);
if (empty($values)) {
throw QueryException::cannotUpdateEmptyValues();
}
$table = $this->tableSources ?? $this->getFrom()->getTable();
$set = [];
foreach ($values as $key => $value) {
$set[] = "`$key` = " . $this->grammar->wrap($value);
}
$sql = "ALTER TABLE $table UPDATE " . implode(', ', $set) . ' '
. $this->grammar->compileWheresComponent($this, $this->getWheres());
return $this->client->write($sql);
}
}
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace PhpClickHouseLaravel;
class QueryException extends \ClickHouseDB\Exception\QueryException
{
public static function cannotUpdateEmptyValues(): self
{
return new self('Error updating empty values');
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Tests;
use PhpClickHouseLaravel\RawColumn;
use Tests\Models\Example;
class UpdateTest extends TestCase
{
public function testUpdate()
{
Example::truncate();
Example::insertAssoc([['f_int' => 1, 'f_int2' => 2, 'f_string' => 'a']]);
Example::where('f_int', 1)->update([
'f_int2' => 3,
'f_string' => 'b',
'created_at' => new RawColumn('created_at + INTERVAL 1 YEAR')
]);
usleep(3e4); // some lag in clickhouse server
$rows = Example::where('f_int', 1)->getRows();
$this->assertCount(1, $rows);
$this->assertEquals(3, $rows[0]['f_int2']);
$this->assertEquals('b', $rows[0]['f_string']);
$this->assertEquals(date('Y') + 1, substr($rows[0]['created_at'], 0, 4));
}
}
+1
View File
@@ -13,6 +13,7 @@ return new class extends \PhpClickHouseLaravel\Migration {
CREATE TABLE examples (
created_at DateTime64 DEFAULT now64(),
f_int Int64,
f_int2 Int64,
f_string String
)
ENGINE = MergeTree()