aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Sql/Internal/InsertFromSelect.php
blob: c003aa48cd07456def9a7b0b9aa3e2b933688ee7 (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 InsertFromSelect
{
    /**
     * @var ?list<string>
     */
    private ?array $fields;

    private ?Select $from;

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

    /**
     * @param list<string> $fields
     */
    public function fields(array $fields): self
    {
        $this->fields = $fields;
        return $this;
    }

    public function from(Select $from): self
    {
        $this->from = $from;
        return $this;
    }

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

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

    /**
     * @internal
     * @return list<string>
     */
    public function _getFields(): array
    {
        if (!isset($this->fields)) {
            throw new InvalidSqlException('INSERT SELECT: $fields must be set before calling execute()');
        }
        return $this->fields;
    }

    public function _getFrom(): Select
    {
        if (!isset($this->from)) {
            throw new InvalidSqlException('INSERT SELECT: $from must be set before calling execute()');
        }
        return $this->from;
    }
}