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
|
|
|
public function setOnConflict(string ...$columns): static |
34
|
|
|
{ |
35
|
|
|
$this->onConflict = $columns; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string ...$columns |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function setDoUpdate(string ...$columns): static |
46
|
|
|
{ |
47
|
|
|
$this->doNothing = false; |
48
|
|
|
$this->doUpdate = $columns; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function setDoNothing(): static |
57
|
|
|
{ |
58
|
|
|
$this->doNothing = true; |
59
|
|
|
$this->doUpdate = []; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string ...$columns |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setReturning(string ...$columns): static |
70
|
|
|
{ |
71
|
|
|
$this->returning = $columns; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function __toString(): string |
80
|
|
|
{ |
81
|
|
|
$parts = [parent::__toString()]; |
82
|
|
|
|
83
|
|
|
if ($this->doNothing || $this->doUpdate) { |
|
|
|
|
84
|
|
|
$action = $this->doNothing ? static::CONFLICT_DO_NOTHING : static::CONFLICT_DO_UPDATE; |
85
|
|
|
if ($this->onConflict) { |
|
|
|
|
86
|
|
|
$parts[] = sprintf('ON CONFLICT (%s) %s', implode(', ', $this->onConflict), $action); |
87
|
|
|
} else { |
88
|
|
|
$parts[] = sprintf('ON CONFLICT %s', $action); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->doUpdate) { |
|
|
|
|
92
|
|
|
$parts[] = sprintf('SET %s', implode(', ', $this->doUpdate)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($this->returning) { |
|
|
|
|
97
|
|
|
$parts[] = sprintf('RETURNING %s', implode(',', $this->returning)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return implode(PHP_EOL, $parts); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function isValid(): bool |
104
|
|
|
{ |
105
|
|
|
return count($this->tables) === 1; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function values(): array |
109
|
|
|
{ |
110
|
|
|
if (!$this->values) { |
|
|
|
|
111
|
|
|
return [self::DEFAULT_VALUES]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return parent::values(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.