blob: cf3f77a1b21a808ecab4bd86ee5f96e7cb78c31f (
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<?php
declare(strict_types=1);
namespace Nsfisis\Albatross\Sql\Internal;
use Nsfisis\Albatross\Exceptions\InvalidSqlException;
use Nsfisis\Albatross\Sql\QueryBuilder;
/**
* @internal
*/
final class Select
{
/**
* @var ?list<string>
*/
private ?array $fields;
private ?Join $join = null;
private string $where = '';
/**
* @var list<array{string, string}>
*/
private array $orderBy = [];
/**
* @var ?positive-int
*/
private ?int $limit = null;
public function __construct(
private readonly QueryBuilder $sql,
private readonly string $table,
) {
}
public function leftJoin(string $table, string $on): self
{
$this->join = new Join('LEFT JOIN', $table, $on);
return $this;
}
/**
* @param list<string> $fields
*/
public function fields(array $fields): self
{
$this->fields = $fields;
return $this;
}
public function where(string $where): self
{
$this->where = $where;
return $this;
}
/**
* @param list<array{string, string}> $orderBy
*/
public function orderBy(array $orderBy): self
{
$this->orderBy = $orderBy;
return $this;
}
/**
* @param positive-int $limit
*/
public function limit(int $limit): self
{
$this->limit = $limit;
return $this;
}
public function first(): SelectFirst
{
$this->limit = 1;
return new SelectFirst($this);
}
/**
* @param array<string, string|int> $params
* @return list<array<string, string>>
*/
public function execute(array $params = []): array
{
return $this->sql->_executeSelect($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('SELECT: $fields must be set before calling execute()');
}
return $this->fields;
}
/**
* @internal
*/
public function _getJoin(): ?Join
{
return $this->join;
}
/**
* @internal
*/
public function _getWhere(): string
{
return $this->where;
}
/**
* @internal
* @return list<array{string, string}>
*/
public function _getOrderBy(): array
{
return $this->orderBy;
}
/**
* @return ?positive-int
*/
public function _getLimit(): ?int
{
return $this->limit;
}
}
|