1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Umbrellio\TableSync\Integration\Laravel\Receive\Upserter; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Umbrellio\TableSync\Integration\Laravel\Receive\MessageData\MessageData; |
10
|
|
|
use Umbrellio\TableSync\Integration\Laravel\Receive\Upserter\ConflictConditionResolverContract; |
11
|
|
|
|
12
|
|
|
class Upserter |
13
|
|
|
{ |
14
|
|
|
private $conflictConditionResolver; |
15
|
|
|
|
16
|
2 |
|
public function __construct(ConflictConditionResolverContract $conflictConditionResolver) |
17
|
|
|
{ |
18
|
2 |
|
$this->conflictConditionResolver = $conflictConditionResolver; |
19
|
2 |
|
} |
20
|
|
|
|
21
|
|
|
// todo via modern expressive not existing query builder |
22
|
2 |
|
public function upsert(MessageData $messageData, float $version): void |
23
|
|
|
{ |
24
|
2 |
|
if (empty($messageData->getData())) { |
25
|
1 |
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
$data = $this->addVersionToItems($messageData->getData(), $version); |
29
|
|
|
|
30
|
1 |
|
$columns = array_keys($data[0]); |
31
|
1 |
|
$columnString = implode(',', array_map(function (string $column): string { |
32
|
1 |
|
return "\"{$column}\""; |
33
|
1 |
|
}, $columns)); |
34
|
|
|
|
35
|
1 |
|
$values = $this->convertInsertValues($data); |
36
|
1 |
|
$target = $this->conflictConditionResolver->resolve($messageData); |
37
|
|
|
|
38
|
1 |
|
$valueBindings = Arr::flatten($data); |
39
|
|
|
|
40
|
1 |
|
$updateColumns = array_diff($columns, $messageData->getTargetKeys()); |
41
|
1 |
|
$updateSpecString = $this->updateSpec($updateColumns); |
42
|
|
|
|
43
|
|
|
$sql = <<<CODE_SAMPLE |
44
|
1 |
|
INSERT INTO {$messageData->getTable()} ({$columnString}) VALUES {$values} |
45
|
1 |
|
ON CONFLICT {$target} |
46
|
|
|
DO UPDATE |
47
|
1 |
|
SET {$updateSpecString} |
48
|
1 |
|
WHERE {$messageData->getTable()}.version < ? |
49
|
|
|
CODE_SAMPLE; |
50
|
|
|
|
51
|
1 |
|
DB::statement($sql, array_merge($valueBindings, [$version])); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
private function addVersionToItems(array $items, float $version): array |
55
|
|
|
{ |
56
|
1 |
|
return array_map(function ($item) use ($version) { |
57
|
1 |
|
return array_merge($item, compact('version')); |
58
|
1 |
|
}, $items); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
private function convertInsertValues(array $items): string |
62
|
|
|
{ |
63
|
1 |
|
$values = array_map(function (array $item) { |
64
|
1 |
|
$item = $this->implodedPlaceholders($item); |
65
|
1 |
|
return "({$item})"; |
66
|
1 |
|
}, $items); |
67
|
|
|
|
68
|
1 |
|
return implode(',', $values); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
private function updateSpec(array $columns): string |
72
|
|
|
{ |
73
|
1 |
|
$values = array_map(function (string $column) { |
74
|
1 |
|
return "\"{$column}\" = EXCLUDED.{$column}"; |
75
|
1 |
|
}, $columns); |
76
|
|
|
|
77
|
1 |
|
return implode(',', $values); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
private function implodedPlaceholders(array $items, string $placeholder = '?'): string |
81
|
|
|
{ |
82
|
1 |
|
return implode(',', array_map(function () use ($placeholder) { |
83
|
1 |
|
return $placeholder; |
84
|
1 |
|
}, $items)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|