1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace QB\MySQL\Statement; |
6
|
|
|
|
7
|
|
|
use QB\Generic\IQueryPart; |
8
|
|
|
use QB\Generic\Statement\Insert as GenericInsert; |
9
|
|
|
|
10
|
|
|
class Insert extends GenericInsert |
11
|
|
|
{ |
12
|
|
|
public const LOW_PRIORITY = 'LOW_PRIORITY'; |
13
|
|
|
public const HIGH_PRIORITY = 'HIGH_PRIORITY'; |
14
|
|
|
public const DELAYED = 'DELAYED'; |
15
|
|
|
public const IGNORE = 'IGNORE'; |
16
|
|
|
|
17
|
|
|
/** @var array<int,string> */ |
18
|
|
|
protected array $modifiers = []; |
19
|
|
|
|
20
|
|
|
protected ?IQueryPart $onDuplicateKeyUpdate = null; |
21
|
|
|
|
22
|
|
|
protected ?Select $select = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string ...$modifiers |
26
|
|
|
* |
27
|
|
|
* @return $this |
28
|
|
|
*/ |
29
|
2 |
|
public function modifier(string ...$modifiers): static |
30
|
|
|
{ |
31
|
2 |
|
foreach ($modifiers as $modifier) { |
32
|
|
|
switch ($modifier) { |
33
|
2 |
|
case static::LOW_PRIORITY: |
34
|
2 |
|
case static::DELAYED: |
35
|
2 |
|
case static::HIGH_PRIORITY: |
36
|
1 |
|
$this->modifiers[0] = $modifier; |
37
|
1 |
|
break; |
38
|
1 |
|
case static::IGNORE: |
39
|
1 |
|
$this->modifiers[1] = $modifier; |
40
|
1 |
|
break; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param IQueryPart $onDuplicateKeyUpdate |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
1 |
|
public function setOnDuplicateKeyUpdate(IQueryPart $onDuplicateKeyUpdate): static |
53
|
|
|
{ |
54
|
1 |
|
$this->onDuplicateKeyUpdate = $onDuplicateKeyUpdate; |
55
|
|
|
|
56
|
1 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Select $select |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
1 |
|
public function select(Select $select): static |
65
|
|
|
{ |
66
|
1 |
|
$this->select = $select; |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
4 |
|
public function __toString(): string |
75
|
|
|
{ |
76
|
4 |
|
$parts = [parent::__toString()]; |
77
|
|
|
|
78
|
3 |
|
if ($this->onDuplicateKeyUpdate) { |
79
|
1 |
|
$parts[] = sprintf('ON DUPLICATE KEY UPDATE %s', (string)$this->onDuplicateKeyUpdate); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
return implode(PHP_EOL, $parts); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
public function isValid(): bool |
86
|
|
|
{ |
87
|
4 |
|
return !empty($this->table) && (count($this->rawValues) > 0 || $this->select !== null); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
protected function getRawValues(): array |
91
|
|
|
{ |
92
|
3 |
|
if ($this->select !== null) { |
93
|
1 |
|
return [(string)$this->select]; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return parent::getRawValues(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|