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 onConflict(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 doUpdate(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 doNothing(): 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 returning(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
|
|
|
/** |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
7 |
|
protected function getOnConflict(): array |
94
|
|
|
{ |
95
|
7 |
|
if (!$this->doNothing && count($this->doUpdate) == 0) { |
96
|
5 |
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$parts = []; |
100
|
|
|
|
101
|
2 |
|
$action = $this->doNothing ? static::CONFLICT_DO_NOTHING : static::CONFLICT_DO_UPDATE; |
102
|
2 |
|
if (count($this->onConflict) > 0) { |
103
|
1 |
|
$parts[] = sprintf('ON CONFLICT (%s) %s', implode(', ', $this->onConflict), $action); |
104
|
|
|
} else { |
105
|
1 |
|
$parts[] = sprintf('ON CONFLICT %s', $action); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
if (count($this->doUpdate) > 0) { |
109
|
1 |
|
$parts[] = sprintf('SET %s', implode(', ', $this->doUpdate)); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
return $parts; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string[] |
117
|
|
|
*/ |
118
|
7 |
|
protected function getReturning(): array |
119
|
|
|
{ |
120
|
7 |
|
if (count($this->returning) == 0) { |
121
|
5 |
|
return []; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return [sprintf('RETURNING %s', implode(',', $this->returning))]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
8 |
|
public function isValid(): bool |
131
|
|
|
{ |
132
|
8 |
|
return !empty($this->table); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string[] |
137
|
|
|
*/ |
138
|
7 |
|
protected function getRawValues(): array |
139
|
|
|
{ |
140
|
7 |
|
if (count($this->rawValues) == 0) { |
141
|
1 |
|
return [self::DEFAULT_VALUES]; |
142
|
|
|
} |
143
|
|
|
|
144
|
6 |
|
return parent::getRawValues(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|