-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSelectQuery.php
More file actions
147 lines (132 loc) · 3.59 KB
/
SelectQuery.php
File metadata and controls
147 lines (132 loc) · 3.59 KB
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
147
<?php
namespace Aternos\Model\Query;
/**
* Class SelectQuery
*
* @package Aternos\Model\Query
*/
class SelectQuery extends Query
{
/**
* @var SelectField[]
*/
protected ?array $fields = null;
protected ?array $group = null;
/**
* SelectQuery constructor.
*
* @param array|WhereCondition|WhereGroup|null $where
* @param array|null $order
* @param array|null $fields
* @param array|int|Limit|null $limit
* @param array|GroupField[]|string[]|null $group
* @param bool $saveResultsToRegistry Whether results of this query should be saved in the model registry.
* @param bool $distinct only select unique rows
*/
public function __construct(null|WhereCondition|array|WhereGroup $where = null,
null|array $order = null,
null|array $fields = null,
null|Limit|array|int $limit = null,
null|array $group = null,
protected bool $saveResultsToRegistry = true,
protected bool $distinct = false,
)
{
if ($where) {
$this->where($where);
}
if ($order) {
$this->orderBy($order);
}
if ($fields) {
$this->fields($fields);
}
if ($limit) {
$this->limit($limit);
}
if ($group) {
$this->groupBy($group);
}
}
/**
* Set fields
*
* @param array $fields
* @return $this
*/
public function fields(array $fields): static
{
$this->fields = [];
foreach ($fields as $field) {
if ($field instanceof SelectField) {
$this->fields[] = $field;
} else if (is_string($field)) {
$this->fields[] = new SelectField($field);
}
}
return $this;
}
/**
* Set group by fields
*
* @param array|GroupField[]|string[] $fields
* @return $this
*/
public function groupBy(array $fields): static
{
$this->group = null;
foreach ($fields as $field) {
if ($field instanceof GroupField) {
$this->group[] = $field;
} else if (is_string($field)) {
$this->group[] = new GroupField($field);
}
}
return $this;
}
/**
* @return array|null|GroupField[]
*/
public function getGroup(): ?array
{
return $this->group;
}
/**
* @param bool $distinct
* @return $this
*/
public function distinct(bool $distinct = true): static
{
$this->distinct = $distinct;
return $this;
}
/**
* Get whether this query should be distinct
*
* @return bool
*/
public function isDistinct(): bool
{
return $this->distinct;
}
/**
* Set whether results of this query should be saved in the model registry
*
* @param bool $saveResultsToRegistry
* @return $this
*/
public function saveResultsToRegistry(bool $saveResultsToRegistry = true): static
{
$this->saveResultsToRegistry = $saveResultsToRegistry;
return $this;
}
/**
* Whether results of this query should be saved in the model registry
*
* @return bool
*/
public function shouldSaveResultsToRegistry(): bool
{
return $this->saveResultsToRegistry;
}
}