|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\FluentAQL\Clauses; |
|
6
|
|
|
|
|
7
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException; |
|
8
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\Expression; |
|
9
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class UpsertClause extends Clause |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array<mixed>|string|object $search |
|
15
|
|
|
*/ |
|
16
|
|
|
protected array|string|object $search; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array<mixed>|string|object $insert |
|
20
|
|
|
*/ |
|
21
|
|
|
protected array|string|object $insert; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array<mixed>|object|string $update |
|
25
|
|
|
*/ |
|
26
|
|
|
protected array|string|object $update; |
|
27
|
|
|
|
|
28
|
|
|
protected string|QueryBuilder|Expression $collection; |
|
29
|
|
|
|
|
30
|
|
|
protected bool $replace; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* UpsertClause constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
36
|
|
|
* @param array<mixed>|string|object $search |
|
37
|
|
|
* @param array<mixed>|string|object $insert |
|
38
|
|
|
* @param array<mixed>|string|object $update |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __construct( |
|
41
|
|
|
array|object|string $search, |
|
42
|
|
|
array|object|string $insert, |
|
43
|
|
|
array|object|string $update, |
|
44
|
|
|
string|QueryBuilder|Expression $collection, |
|
45
|
|
|
bool $replace = false |
|
46
|
|
|
) { |
|
47
|
1 |
|
parent::__construct(); |
|
48
|
|
|
|
|
49
|
1 |
|
$this->search = $search; |
|
50
|
1 |
|
$this->insert = $insert; |
|
51
|
1 |
|
$this->update = $update; |
|
52
|
1 |
|
$this->collection = $collection; |
|
53
|
1 |
|
$this->replace = $replace; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @throws ExpressionTypeException |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function compile(QueryBuilder $queryBuilder): string |
|
60
|
|
|
{ |
|
61
|
1 |
|
$this->search = $queryBuilder->normalizeArgument($this->search, ['RegisteredVariable', 'Key', 'Bind']); |
|
62
|
1 |
|
$this->insert = $queryBuilder->normalizeArgument($this->insert, ['RegisteredVariable', 'Key', 'Bind']); |
|
63
|
1 |
|
$this->update = $queryBuilder->normalizeArgument($this->update, ['Object', 'Bind']); |
|
64
|
1 |
|
$this->collection = $queryBuilder->normalizeArgument($this->collection, ['Collection', 'Bind']); |
|
65
|
1 |
|
$queryBuilder->registerCollections($this->collection->compile($queryBuilder)); |
|
66
|
|
|
|
|
67
|
1 |
|
$withClause = 'UPDATE'; |
|
68
|
1 |
|
if ($this->replace) { |
|
69
|
1 |
|
$withClause = 'REPLACE'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return "UPSERT {$this->search->compile($queryBuilder)} " . |
|
73
|
1 |
|
"INSERT {$this->insert->compile($queryBuilder)} $withClause {$this->update->compile($queryBuilder)} " . |
|
74
|
1 |
|
"IN {$this->collection->compile($queryBuilder)}"; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|