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 Query.php |
33
|
|
|
* |
34
|
|
|
* The database Query base 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
|
|
|
use Platine\Database\Connection; |
51
|
|
|
use Platine\Database\ResultSet; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @class Query |
55
|
|
|
* @package Platine\Database\Query |
56
|
|
|
*/ |
57
|
|
|
class Query extends BaseStatement |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* @var Connection |
61
|
|
|
*/ |
62
|
|
|
protected Connection $connection; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string|array<string> |
66
|
|
|
*/ |
67
|
|
|
protected string|array $tables; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Query constructor. |
71
|
|
|
* @param Connection $connection |
72
|
|
|
* @param string|array<string> $tables |
73
|
|
|
* @param QueryStatement|null $queryStatement |
74
|
|
|
*/ |
75
|
|
|
public function __construct( |
76
|
|
|
Connection $connection, |
77
|
|
|
string|array $tables, |
78
|
|
|
?QueryStatement $queryStatement = null |
79
|
|
|
) { |
80
|
|
|
parent::__construct($queryStatement); |
81
|
|
|
|
82
|
|
|
$this->connection = $connection; |
83
|
|
|
$this->tables = $tables; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param bool $value |
88
|
|
|
* |
89
|
|
|
* @return Select|SelectStatement |
90
|
|
|
*/ |
91
|
|
|
public function distinct(bool $value = true): Select|SelectStatement |
92
|
|
|
{ |
93
|
|
|
return $this->buildSelect()->distinct($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string|array<int, string>|Closure|Expression $columns |
98
|
|
|
* |
99
|
|
|
* @return Select|SelectStatement |
100
|
|
|
*/ |
101
|
|
|
public function groupBy(string|array|Closure|Expression $columns): Select|SelectStatement |
102
|
|
|
{ |
103
|
|
|
return $this->buildSelect()->groupBy($columns); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string|Expression $column |
108
|
|
|
* @param Closure|null $value |
109
|
|
|
* |
110
|
|
|
* @return Select|SelectStatement |
111
|
|
|
*/ |
112
|
|
|
public function having(string|Expression $column, ?Closure $value = null): Select|SelectStatement |
113
|
|
|
{ |
114
|
|
|
return $this->buildSelect()->having($column, $value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string|Expression $column |
119
|
|
|
* @param Closure|null $value |
120
|
|
|
* |
121
|
|
|
* @return Select|SelectStatement |
122
|
|
|
*/ |
123
|
|
|
public function orHaving(string|Expression $column, ?Closure $value = null): Select|SelectStatement |
124
|
|
|
{ |
125
|
|
|
return $this->buildSelect()->orHaving($column, $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string|string[]|Closure|Closure[]|Expression|Expression[] $columns |
130
|
|
|
* @param string $order |
131
|
|
|
* |
132
|
|
|
* @return Select|SelectStatement |
133
|
|
|
*/ |
134
|
|
|
public function orderBy( |
135
|
|
|
string|array|Closure|Expression $columns, |
136
|
|
|
string $order = 'ASC' |
137
|
|
|
): Select|SelectStatement { |
138
|
|
|
return $this->buildSelect()->orderBy($columns, $order); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $value |
143
|
|
|
* |
144
|
|
|
* @return Select|SelectStatement |
145
|
|
|
*/ |
146
|
|
|
public function limit(int $value): Select|SelectStatement |
147
|
|
|
{ |
148
|
|
|
return $this->buildSelect()->limit($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param int $value |
153
|
|
|
* |
154
|
|
|
* @return Select|SelectStatement |
155
|
|
|
*/ |
156
|
|
|
public function offset(int $value): Select|SelectStatement |
157
|
|
|
{ |
158
|
|
|
return $this->buildSelect()->offset($value); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $table |
163
|
|
|
* @return Select|SelectStatement |
164
|
|
|
*/ |
165
|
|
|
public function into(string $table): Select|SelectStatement |
166
|
|
|
{ |
167
|
|
|
return $this->buildSelect()->into($table); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string|string[]|Expression|Expression[]|Closure|Closure[] $columns |
172
|
|
|
* |
173
|
|
|
* @return ResultSet |
174
|
|
|
*/ |
175
|
|
|
public function select(string|array|Expression|Closure $columns = []): ResultSet |
176
|
|
|
{ |
177
|
|
|
return $this->buildSelect()->select($columns); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string|array<string> $tables |
182
|
|
|
* @return int |
183
|
|
|
*/ |
184
|
|
|
public function delete(string|array $tables): int |
185
|
|
|
{ |
186
|
|
|
return $this->buildDelete()->delete($tables); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string|Closure|Expression $name |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
|
|
public function column(string|Closure|Expression $name): mixed |
195
|
|
|
{ |
196
|
|
|
return $this->buildSelect()->column($name); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string|Expression|Closure $column |
201
|
|
|
* @param bool $distinct |
202
|
|
|
* |
203
|
|
|
* @return int |
204
|
|
|
*/ |
205
|
|
|
public function count(string|Expression|Closure $column = '*', bool $distinct = false): int |
206
|
|
|
{ |
207
|
|
|
return $this->buildSelect()->count($column, $distinct); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string|Expression|Closure $column |
212
|
|
|
* @param bool $distinct |
213
|
|
|
* |
214
|
|
|
* @return int|float |
215
|
|
|
*/ |
216
|
|
|
public function avg(string|Expression|Closure $column, bool $distinct = false): int|float |
217
|
|
|
{ |
218
|
|
|
return $this->buildSelect()->avg($column, $distinct); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string|Expression|Closure $column |
223
|
|
|
* @param bool $distinct |
224
|
|
|
* |
225
|
|
|
* @return int|float |
226
|
|
|
*/ |
227
|
|
|
public function sum(string|Expression|Closure $column, bool $distinct = false): int|float |
228
|
|
|
{ |
229
|
|
|
return $this->buildSelect()->sum($column, $distinct); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string|Expression|Closure $column |
234
|
|
|
* @param bool $distinct |
235
|
|
|
* |
236
|
|
|
* @return mixed |
237
|
|
|
*/ |
238
|
|
|
public function min(string|Expression|Closure $column, bool $distinct = false): mixed |
239
|
|
|
{ |
240
|
|
|
return $this->buildSelect()->min($column, $distinct); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string|Expression|Closure $column |
245
|
|
|
* @param bool $distinct |
246
|
|
|
* |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public function max(string|Expression|Closure $column, bool $distinct = false) |
250
|
|
|
{ |
251
|
|
|
return $this->buildSelect()->max($column, $distinct); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return Select |
256
|
|
|
*/ |
257
|
|
|
protected function buildSelect(): Select |
258
|
|
|
{ |
259
|
|
|
return new Select( |
260
|
|
|
$this->connection, |
261
|
|
|
$this->tables, |
262
|
|
|
$this->queryStatement |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return Delete |
268
|
|
|
*/ |
269
|
|
|
protected function buildDelete(): Delete |
270
|
|
|
{ |
271
|
|
|
return new Delete( |
272
|
|
|
$this->connection, |
273
|
|
|
$this->tables, |
274
|
|
|
$this->queryStatement |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|