Lines 100.00% 3 / 3
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
PostgresLock 100.00% 3 / 3 100.00% 3 / 3 3 100.00% 1 / 1
 __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
1<?php
2
3declare(strict_types=1);
4
5namespace BlueprintAU\Radiant\Database\Locks;
6
7use BlueprintAU\Radiant\Database\Connections\PostgresConnection;
8
9/**
10 * Postgres lock: a session-level advisory lock on a named key.
11 *
12 * `pg_advisory_lock(hashtext(...))` blocks until acquired; the session
13 * keeps the lock until `pg_advisory_unlock` runs on the SAME session —
14 * which is why the adapter pins the lock to the connection instance.
15 *
16 * The lock name is supplied at call time via {@see withLock()} — one name
17 * is one mutual-exclusion domain, so distinct jobs use distinct names.
18 */
19final class PostgresLock extends SqlLock
20{
21    /**
22     * @param  PostgresConnection  $connection
23     */
24    public function __construct(PostgresConnection $connection)
25    {
26        parent::__construct($connection);
27    }
28
29    /**
30     * The lock acquisition statement — blocks until acquired.
31     *
32     * @return string
33     */
34    #[\Override]
35    protected function lockStatement(): string
36    {
37        return 'SELECT pg_advisory_lock(hashtext(?))';
38    }
39
40    /**
41     * The lock release statement.
42     *
43     * @return string
44     */
45    #[\Override]
46    protected function unlockStatement(): string
47    {
48        return 'SELECT pg_advisory_unlock(hashtext(?))';
49    }
50}