|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace QB\PostgreSQL\Statement; |
|
6
|
|
|
|
|
7
|
|
|
use QB\Generic\Statement\Insert as GenericInsert; |
|
8
|
|
|
|
|
9
|
|
|
class Insert extends GenericInsert |
|
10
|
|
|
{ |
|
11
|
|
|
public const DEFAULT_VALUES = 'DEFAULT VALUES'; |
|
12
|
|
|
|
|
13
|
|
|
public const CONFLICT_DO_NOTHING = 'DO NOTHING'; |
|
14
|
|
|
public const CONFLICT_DO_UPDATE = 'DO UPDATE'; |
|
15
|
|
|
|
|
16
|
|
|
/** @var array<int,string> */ |
|
17
|
|
|
protected array $onConflict = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array<int,string> */ |
|
20
|
|
|
protected array $doUpdate = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var bool */ |
|
23
|
|
|
protected bool $doNothing = false; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array<int,string> */ |
|
26
|
|
|
protected array $returning = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string ...$columns |
|
30
|
|
|
* |
|
31
|
|
|
* @return $this |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function setOnConflict(string ...$columns): static |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->onConflict = $columns; |
|
36
|
|
|
|
|
37
|
1 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string ...$columns |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function setDoUpdate(string ...$columns): static |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->doNothing = false; |
|
48
|
1 |
|
$this->doUpdate = $columns; |
|
49
|
|
|
|
|
50
|
1 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setDoNothing(): static |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->doNothing = true; |
|
59
|
1 |
|
$this->doUpdate = []; |
|
60
|
|
|
|
|
61
|
1 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string ...$columns |
|
66
|
|
|
* |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function setReturning(string ...$columns): static |
|
70
|
|
|
{ |
|
71
|
2 |
|
$this->returning = $columns; |
|
72
|
|
|
|
|
73
|
2 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
8 |
|
public function __toString(): string |
|
80
|
|
|
{ |
|
81
|
8 |
|
$parts = array_merge( |
|
82
|
8 |
|
[parent::__toString()], |
|
83
|
7 |
|
$this->getOnConflict(), |
|
84
|
7 |
|
$this->getReturning(), |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
7 |
|
return implode(PHP_EOL, $parts); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
protected function getOnConflict(): array |
|
91
|
|
|
{ |
|
92
|
7 |
|
if (!$this->doNothing && count($this->doUpdate) == 0) { |
|
93
|
5 |
|
return []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
$parts = []; |
|
97
|
|
|
|
|
98
|
2 |
|
$action = $this->doNothing ? static::CONFLICT_DO_NOTHING : static::CONFLICT_DO_UPDATE; |
|
99
|
2 |
|
if (count($this->onConflict) > 0) { |
|
100
|
1 |
|
$parts[] = sprintf('ON CONFLICT (%s) %s', implode(', ', $this->onConflict), $action); |
|
101
|
|
|
} else { |
|
102
|
1 |
|
$parts[] = sprintf('ON CONFLICT %s', $action); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
if (count($this->doUpdate) > 0) { |
|
106
|
1 |
|
$parts[] = sprintf('SET %s', implode(', ', $this->doUpdate)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
return $parts; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
7 |
|
protected function getReturning(): array |
|
113
|
|
|
{ |
|
114
|
7 |
|
if (count($this->returning) == 0) { |
|
115
|
5 |
|
return []; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
return [sprintf('RETURNING %s', implode(',', $this->returning))]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
8 |
|
public function isValid(): bool |
|
125
|
|
|
{ |
|
126
|
8 |
|
return count($this->tables) === 1; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string[] |
|
131
|
|
|
*/ |
|
132
|
7 |
|
protected function getRawValues(): array |
|
133
|
|
|
{ |
|
134
|
7 |
|
if (count($this->rawValues) == 0) { |
|
135
|
1 |
|
return [self::DEFAULT_VALUES]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
return parent::getRawValues(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|