1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Sql\Query; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\GroupedSubQuery; |
21
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
22
|
|
|
use ArekX\PQL\Query; |
23
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConditionTrait; |
24
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConfigureTrait; |
25
|
|
|
use ArekX\PQL\Sql\Query\Traits\JoinTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class representing a select query for retrieving data from |
29
|
|
|
* database. |
30
|
|
|
*/ |
31
|
|
|
class Select extends Query implements GroupedSubQuery |
32
|
|
|
{ |
33
|
|
|
use ConditionTrait; |
34
|
|
|
use JoinTrait; |
35
|
|
|
use ConfigureTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets a data source from which to pull the data from |
39
|
|
|
* such as a table of data, depending on the driver. |
40
|
|
|
* |
41
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
42
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
43
|
|
|
* and escape query use Raw query. |
44
|
|
|
* |
45
|
|
|
* If the source is passed as an array then the key of this source |
46
|
|
|
* is used as an alias if the driver supports it and values is used |
47
|
|
|
* as the source name. |
48
|
|
|
* |
49
|
|
|
* If null, this query part is not to be used. |
50
|
|
|
* |
51
|
|
|
* @param array|string|StructuredQuery|null $source Source to be passed |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
24 |
|
public function from(StructuredQuery|array|string|null $source): self |
55
|
|
|
{ |
56
|
24 |
|
$this->use('from', $source); |
57
|
24 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set columns to be retrieved from data source |
62
|
|
|
* |
63
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
64
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
65
|
|
|
* and escape query use Raw query. |
66
|
|
|
* |
67
|
|
|
* If columns are passed as an array then the key of the array |
68
|
|
|
* is used as an alias if the driver supports it and values is used |
69
|
|
|
* as the column name. |
70
|
|
|
* |
71
|
|
|
* If null, this query part is not to be used. |
72
|
|
|
* |
73
|
|
|
* @param array|string|StructuredQuery|null $columns |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
34 |
|
public function columns(StructuredQuery|array|string|null $columns): static |
77
|
|
|
{ |
78
|
34 |
|
$this->use('columns', $columns); |
79
|
34 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add a column to the columns list. |
84
|
|
|
* |
85
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
86
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
87
|
|
|
* and escape query use Raw query. |
88
|
|
|
* |
89
|
|
|
* If passed column is an array it will be merged |
90
|
|
|
* with the current column list. |
91
|
|
|
* |
92
|
|
|
* @param array|string|StructuredQuery $column Column to be added |
93
|
|
|
* @return $this |
94
|
|
|
* @see Select::columns() |
95
|
|
|
*/ |
96
|
3 |
|
public function addColumns(StructuredQuery|array|string $column): static |
97
|
|
|
{ |
98
|
3 |
|
$previousColumns = $this->get('columns') ?? []; |
99
|
|
|
|
100
|
3 |
|
if (!is_array($previousColumns)) { |
101
|
3 |
|
$previousColumns = [$previousColumns]; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
if (is_array($column)) { |
105
|
2 |
|
foreach ($column as $key => $value) { |
106
|
2 |
|
$previousColumns[$key] = $value; |
107
|
|
|
} |
108
|
|
|
} else { |
109
|
2 |
|
$previousColumns[] = $column; |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
$this->use('columns', $previousColumns); |
113
|
3 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Order results by a specific value. |
118
|
|
|
* |
119
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
120
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
121
|
|
|
* and escape query use Raw query. |
122
|
|
|
* |
123
|
|
|
* If the array is passed this function expects an associative array where key is the |
124
|
|
|
* column to sort by and value is how to sort by. |
125
|
|
|
* Format using strings is: |
126
|
|
|
* ```php |
127
|
|
|
* [ |
128
|
|
|
* 'ascending column' => 'asc', |
129
|
|
|
* 'descending column' => 'desc' |
130
|
|
|
* ] |
131
|
|
|
* ``` |
132
|
|
|
* Format using constants is: |
133
|
|
|
* ```php |
134
|
|
|
* [ |
135
|
|
|
* 'ascending column' => SORT_ASC, |
136
|
|
|
* 'descending column' => SORT_DESC |
137
|
|
|
* ] |
138
|
|
|
* ``` |
139
|
|
|
* |
140
|
|
|
* If structured query is passed it is processed as is. |
141
|
|
|
* |
142
|
|
|
* @param array|StructuredQuery|null $orderBy |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
2 |
|
public function orderBy(StructuredQuery|array|null $orderBy): static |
146
|
|
|
{ |
147
|
2 |
|
$this->use('orderBy', $orderBy); |
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Group results by specified value. |
153
|
|
|
* |
154
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
155
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
156
|
|
|
* and escape query use Raw query. |
157
|
|
|
* |
158
|
|
|
* If array is passed here, every element of the array is used as the grouping column. |
159
|
|
|
* |
160
|
|
|
* If structured query is passed it is processed as is. |
161
|
|
|
* |
162
|
|
|
* @param array|StructuredQuery|null $groupBy |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
2 |
|
public function groupBy(StructuredQuery|array|null $groupBy): static |
166
|
|
|
{ |
167
|
2 |
|
$this->use('groupBy', $groupBy); |
168
|
2 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Add a HAVING condition to the query. |
174
|
|
|
* |
175
|
|
|
* Condition logic is the same as with where method. |
176
|
|
|
* |
177
|
|
|
* @param array|StructuredQuery|null $having |
178
|
|
|
* @return $this |
179
|
|
|
* @see ConditionTrait::where() |
180
|
|
|
*/ |
181
|
2 |
|
public function having(StructuredQuery|array|null $having): static |
182
|
|
|
{ |
183
|
2 |
|
$this->use('having', $having); |
184
|
2 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Append having to the existing one by AND-ing the previous having |
189
|
|
|
* filter to the value specified in having part. |
190
|
|
|
* |
191
|
|
|
* Condition accepts the same format as where method. |
192
|
|
|
* |
193
|
|
|
* If the current condition in part is an `['and', condition]`, new condition |
194
|
|
|
* will be appended to it, otherwise the previous condition and this condition will |
195
|
|
|
* be wrapped as `['and', previous, current]` |
196
|
|
|
* |
197
|
|
|
* @param array|StructuredQuery $having |
198
|
|
|
* @return $this |
199
|
|
|
* @see ConditionTrait::appendConditionPart() |
200
|
|
|
* @see ConditionTrait::where() |
201
|
|
|
*/ |
202
|
1 |
|
public function andHaving(StructuredQuery|array $having): static |
203
|
|
|
{ |
204
|
1 |
|
return $this->appendConditionPart('having', 'and', $having); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Append having to the existing one by OR-ing the previous having |
209
|
|
|
* filter to the value specified in having part. |
210
|
|
|
* |
211
|
|
|
* Condition accepts the same format as where method. |
212
|
|
|
* |
213
|
|
|
* If the current condition in part is an `['or', condition]`, new condition |
214
|
|
|
* will be appended to it, otherwise the previous condition and this condition will |
215
|
|
|
* be wrapped as `['or', previous, current]` |
216
|
|
|
* |
217
|
|
|
* @param array|StructuredQuery $having |
218
|
|
|
* @return $this |
219
|
|
|
* @see ConditionTrait::appendConditionPart() |
220
|
|
|
* @see ConditionTrait::where() |
221
|
|
|
*/ |
222
|
1 |
|
public function orHaving(StructuredQuery|array $having): static |
223
|
|
|
{ |
224
|
1 |
|
return $this->appendConditionPart('having', 'or', $having); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|