1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Database |
5
|
|
|
* |
6
|
|
|
* Platine Database is the abstraction layer using PDO with support of query and schema builder |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Database |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file SelectStatement.php |
33
|
|
|
* |
34
|
|
|
* The select statement class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Database\Query |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Database\Query; |
48
|
|
|
|
49
|
|
|
use Closure; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @class SelectStatement |
53
|
|
|
* @package Platine\Database\Query |
54
|
|
|
*/ |
55
|
|
|
class SelectStatement extends BaseStatement |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* @var HavingStatement |
59
|
|
|
*/ |
60
|
|
|
protected HavingStatement $havingStatement; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* SelectStatement constructor. |
64
|
|
|
* @param string|array<int, string> $tables |
65
|
|
|
* @param QueryStatement|null $queryStatement |
66
|
|
|
*/ |
67
|
|
|
public function __construct(string|array $tables, ?QueryStatement $queryStatement = null) |
68
|
|
|
{ |
69
|
|
|
parent::__construct($queryStatement); |
70
|
|
|
|
71
|
|
|
if (!is_array($tables)) { |
|
|
|
|
72
|
|
|
$tables = [$tables]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->queryStatement->addTables($tables); |
76
|
|
|
$this->havingStatement = new HavingStatement($this->queryStatement); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $table |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function into(string $table): self |
84
|
|
|
{ |
85
|
|
|
$this->queryStatement->setInto($table); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param bool $value |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function distinct(bool $value = true): self |
95
|
|
|
{ |
96
|
|
|
$this->queryStatement->setDistinct($value); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string|Expression|Closure|string[]|Expression[]|Closure[] $columns |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function groupBy(string|Expression|Closure|array $columns): self |
106
|
|
|
{ |
107
|
|
|
if (!is_array($columns)) { |
|
|
|
|
108
|
|
|
$columns = [$columns]; |
109
|
|
|
} |
110
|
|
|
$this->queryStatement->addGroupBy($columns); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string|Expression|Closure $column |
117
|
|
|
* @param Closure|null $closure |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function having(string|Expression|Closure $column, ?Closure $closure = null): self |
121
|
|
|
{ |
122
|
|
|
$this->havingStatement->having($column, $closure); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string|Expression|Closure $column |
129
|
|
|
* @param Closure|null $closure |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function orHaving( |
133
|
|
|
string|Expression|Closure $column, |
134
|
|
|
?Closure $closure = null |
135
|
|
|
): self { |
136
|
|
|
$this->havingStatement->orHaving($column, $closure); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string|Closure|Expression|string[]|Expression[]|Closure[] $columns |
143
|
|
|
* @param string $order |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function orderBy( |
147
|
|
|
string|Closure|Expression|array $columns, |
148
|
|
|
string $order = 'ASC' |
149
|
|
|
): self { |
150
|
|
|
if (!is_array($columns)) { |
|
|
|
|
151
|
|
|
$columns = [$columns]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->queryStatement->addOrder($columns, $order); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $value |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function limit(int $value): self |
164
|
|
|
{ |
165
|
|
|
$this->queryStatement->setLimit($value); |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int $value |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function offset(int $value): self |
175
|
|
|
{ |
176
|
|
|
$this->queryStatement->setOffset($value); |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string|Expression|Closure|string[]|Expression[]|Closure[] $columns |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
public function select(string|Expression|Closure|array $columns = []): mixed |
186
|
|
|
{ |
187
|
|
|
$expr = new ColumnExpression($this->queryStatement); |
188
|
|
|
|
189
|
|
|
if ($columns instanceof Closure) { |
|
|
|
|
190
|
|
|
$columns($expr); |
191
|
|
|
} else { |
192
|
|
|
if (!is_array($columns)) { |
|
|
|
|
193
|
|
|
$columns = [$columns]; |
194
|
|
|
} |
195
|
|
|
$expr->columns($columns); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string|Expression|Closure $name |
203
|
|
|
* @return mixed |
204
|
|
|
*/ |
205
|
|
|
public function column(string|Expression|Closure $name): mixed |
206
|
|
|
{ |
207
|
|
|
(new ColumnExpression($this->queryStatement))->column($name); |
208
|
|
|
|
209
|
|
|
return true; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string|Expression|Closure $column |
214
|
|
|
* @param bool $distinct |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
public function count( |
218
|
|
|
string|Expression|Closure $column = '*', |
219
|
|
|
bool $distinct = false |
220
|
|
|
): mixed { |
221
|
|
|
(new ColumnExpression($this->queryStatement))->count($column, null, $distinct); |
222
|
|
|
|
223
|
|
|
return true; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string|Expression|Closure $column |
228
|
|
|
* @param bool $distinct |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
public function avg( |
232
|
|
|
string|Expression|Closure $column, |
233
|
|
|
bool $distinct = false |
234
|
|
|
): mixed { |
235
|
|
|
(new ColumnExpression($this->queryStatement))->avg($column, null, $distinct); |
236
|
|
|
|
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param string|Expression|Closure $column |
242
|
|
|
* @param bool $distinct |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public function sum(string|Expression|Closure $column, bool $distinct = false): mixed |
246
|
|
|
{ |
247
|
|
|
(new ColumnExpression($this->queryStatement))->sum($column, null, $distinct); |
248
|
|
|
|
249
|
|
|
return true; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string|Expression|Closure $column |
254
|
|
|
* @param bool $distinct |
255
|
|
|
* @return mixed |
256
|
|
|
*/ |
257
|
|
|
public function min(string|Expression|Closure $column, bool $distinct = false): mixed |
258
|
|
|
{ |
259
|
|
|
(new ColumnExpression($this->queryStatement))->min($column, null, $distinct); |
260
|
|
|
|
261
|
|
|
return true; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string|Expression|Closure $column |
266
|
|
|
* @param bool $distinct |
267
|
|
|
* @return mixed |
268
|
|
|
*/ |
269
|
|
|
public function max(string|Expression|Closure $column, bool $distinct = false): mixed |
270
|
|
|
{ |
271
|
|
|
(new ColumnExpression($this->queryStatement))->max($column, null, $distinct); |
272
|
|
|
|
273
|
|
|
return true; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @inheritDoc |
278
|
|
|
*/ |
279
|
|
|
public function __clone(): void |
280
|
|
|
{ |
281
|
|
|
parent::__clone(); |
282
|
|
|
$this->havingStatement = new HavingStatement($this->queryStatement); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|