Lines
100.00%
6 / 6
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| lockStatement | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| unlockStatement | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Locks\SqlLock] withLock | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| 21 | final class MySqlLock extends SqlLock | |
| 22 | { | |
| 23 | /** | |
| 24 | * @param MySqlConnection $connection | |
| 25 | */ | |
| 26 | public function __construct(MySqlConnection $connection) | |
| 27 | { | |
| 28 | parent::__construct($connection); | |
| 29 | } | |
| 30 | ||
| 31 | /** | |
| 32 | * The lock acquisition statement — blocks up to 30s, then fails. | |
| 33 | * | |
| 34 | * GET_LOCK returns 0 on timeout and NULL on error; the SELECT wraps the | |
| 35 | * call in a signal expression that raises an error via a division by | |
| 36 | * zero on a non-1 result. | |
| 37 | * | |
| 38 | * @return string | |
| 39 | */ | |
| 40 | #[\Override] | |
| 41 | protected function lockStatement(): string | |
| 42 | { | |
| 43 | return "SELECT IF(GET_LOCK(?, 30) = 1, 1, crc32('lock-timeout') DIV 0)"; | |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * The lock release statement. | |
| 48 | * | |
| 49 | * @return string | |
| 50 | */ | |
| 51 | #[\Override] | |
| 52 | protected function unlockStatement(): string | |
| 53 | { | |
| 54 | return 'DO RELEASE_LOCK(?)'; | |
| 55 | } | |
| 56 | } |
Inherited from BlueprintAU\Radiant\Database\Locks\SqlLock
| 53 | final public function withLock(callable $callback, string $name): mixed | |
| 54 | { | |
| 55 | // The lock statement runs with the name bound (no interpolation); a | |
| 56 | // failure to acquire must surface as an exception, not a silent | |
| 57 | // no-lock run. | |
| 58 | $this->connection->statement($this->lockStatement(), [$name]); | |
| 59 | try { | |
| 60 | return $callback(); | |
| 61 | } finally { | |
| 62 | $this->connection->statement($this->unlockStatement(), [$name]); | |
| 63 | } | |
| 64 | } |