aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Sql/Internal/Update.php
blob: a9e981666c09f5967dcc3c9ad83d1172520f2d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

declare(strict_types=1);

namespace Nsfisis\Albatross\Sql\Internal;

use Nsfisis\Albatross\Exceptions\InvalidSqlException;
use Nsfisis\Albatross\Sql\QueryBuilder;

final class Update
{
    /**
     * @var ?array<string, string|int>
     */
    private ?array $set;

    private string $where = '';

    /**
     * @internal
     */
    public function __construct(
        private readonly QueryBuilder $sql,
        private readonly string $table,
    ) {
    }

    /**
     * @param array<string, string|int> $set
     */
    public function set(array $set): self
    {
        $this->set = $set;
        return $this;
    }

    public function where(string $where): self
    {
        $this->where = $where;
        return $this;
    }

    /**
     * @param array<string, string|int> $params
     */
    public function execute(array $params = []): void
    {
        $this->sql->_executeUpdate($this, $params);
    }

    /**
     * @internal
     */
    public function _getTable(): string
    {
        return $this->table;
    }

    /**
     * @internal
     */
    public function _getWhere(): string
    {
        return $this->where;
    }

    /**
     * @internal
     * @return array<string, string|int>
     */
    public function _getSet(): array
    {
        if (!isset($this->set)) {
            throw new InvalidSqlException('UPDATE: $set must be set before calling execute()');
        }
        return $this->set;
    }
}