|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace QB\MySQL\Clause; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class Lock |
|
10
|
|
|
{ |
|
11
|
|
|
public const LOCK_IN_SHARE_MODE = 'LOCK IN SHARE MODE'; |
|
12
|
|
|
|
|
13
|
|
|
public const FOR_UPDATE = 'UPDATE'; |
|
14
|
|
|
public const FOR_SHARE = 'SHARE'; |
|
15
|
|
|
|
|
16
|
|
|
public const MODIFIER_NOWAIT = 'NOWAIT'; |
|
17
|
|
|
public const MODIFIER_SKIP_LOCKED = 'SKIP LOCKED'; |
|
18
|
|
|
|
|
19
|
|
|
protected const VALID_FOR = [null, self::LOCK_IN_SHARE_MODE, self::FOR_UPDATE, self::FOR_SHARE]; |
|
20
|
|
|
protected const VALID_MODIFIERS = [null, self::MODIFIER_NOWAIT, self::MODIFIER_SKIP_LOCKED]; |
|
21
|
|
|
|
|
22
|
|
|
protected string $for = self::LOCK_IN_SHARE_MODE; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string[] */ |
|
25
|
|
|
protected array $tables = []; |
|
26
|
|
|
|
|
27
|
|
|
protected ?string $modifier = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Lock constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|null $for |
|
33
|
|
|
* @param string[] $tables |
|
34
|
|
|
* @param string|null $modifier |
|
35
|
|
|
*/ |
|
36
|
27 |
|
public function __construct(?string $for = null, array $tables = [], ?string $modifier = null) |
|
37
|
|
|
{ |
|
38
|
27 |
|
if (!$this->isValid($for, $modifier)) { |
|
39
|
5 |
|
$data = (string)print_r([$for, $modifier], true); |
|
40
|
5 |
|
throw new InvalidArgumentException( |
|
41
|
5 |
|
sprintf('invalid arguments for %s. arguments: %s', __CLASS__, $data) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
22 |
|
if ($for === null) { |
|
46
|
1 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
21 |
|
$this->for = $for; |
|
50
|
21 |
|
$this->tables = $tables; |
|
51
|
21 |
|
$this->modifier = $modifier; |
|
52
|
21 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string|null $for |
|
56
|
|
|
* @param string|null $modifier |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
27 |
|
private function isValid(?string $for = null, ?string $modifier = null): bool |
|
61
|
|
|
{ |
|
62
|
27 |
|
if (!in_array($for, static::VALID_FOR, true)) { |
|
63
|
3 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
24 |
|
if (!in_array($modifier, static::VALID_MODIFIERS, true)) { |
|
67
|
2 |
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
22 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
22 |
|
public function __toString(): string |
|
74
|
|
|
{ |
|
75
|
22 |
|
if ($this->for === self::LOCK_IN_SHARE_MODE) { |
|
76
|
2 |
|
return self::LOCK_IN_SHARE_MODE; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
20 |
|
$parts = []; |
|
80
|
20 |
|
$parts[] = 'FOR ' . $this->for; |
|
81
|
20 |
|
if (count($this->tables) > 0) { |
|
82
|
5 |
|
$parts[] = 'OF ' . implode(', ', $this->tables); |
|
83
|
|
|
} |
|
84
|
20 |
|
if ($this->modifier) { |
|
85
|
11 |
|
$parts[] = $this->modifier; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
20 |
|
return implode(' ', $parts); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|