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 Select.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
|
|
|
use Platine\Database\Connection; |
51
|
|
|
use Platine\Database\ResultSet; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @class Select |
55
|
|
|
* @package Platine\Database\Query |
56
|
|
|
*/ |
57
|
|
|
class Select extends SelectStatement |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* @var Connection |
61
|
|
|
*/ |
62
|
|
|
protected Connection $connection; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Select constructor. |
66
|
|
|
* @param Connection $connection |
67
|
|
|
* @param string|array<int, string> $tables |
68
|
|
|
* @param QueryStatement|null $queryStatement |
69
|
|
|
*/ |
70
|
|
|
public function __construct( |
71
|
|
|
Connection $connection, |
72
|
|
|
string|array $tables, |
73
|
|
|
?QueryStatement $queryStatement = null |
74
|
|
|
) { |
75
|
|
|
parent::__construct($tables, $queryStatement); |
76
|
|
|
$this->connection = $connection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|Expression|Closure|string[]|Expression[]|Closure[] $columns |
81
|
|
|
* |
82
|
|
|
* @return ResultSet |
83
|
|
|
*/ |
84
|
|
|
public function select(string|Expression|Closure|array $columns = []): ResultSet |
85
|
|
|
{ |
86
|
|
|
parent::select($columns); |
87
|
|
|
$driver = $this->connection->getDriver(); |
88
|
|
|
|
89
|
|
|
return $this->connection->query( |
90
|
|
|
$driver->select($this->queryStatement), |
91
|
|
|
$driver->getParams() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string|Expression|Closure $name |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function column(string|Expression|Closure $name): mixed |
101
|
|
|
{ |
102
|
|
|
parent::column($name); |
103
|
|
|
|
104
|
|
|
return $this->getColumnResult(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string|Expression|Closure $column |
109
|
|
|
* @param bool $distinct |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function count( |
114
|
|
|
string|Expression|Closure $column = '*', |
115
|
|
|
bool $distinct = false |
116
|
|
|
): int { |
117
|
|
|
parent::count($column, $distinct); |
118
|
|
|
return (int) $this->getColumnResult(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string|Expression|Closure $column |
123
|
|
|
* @param bool $distinct |
124
|
|
|
* |
125
|
|
|
* @return int|float |
126
|
|
|
*/ |
127
|
|
|
public function avg( |
128
|
|
|
string|Expression|Closure $column, |
129
|
|
|
bool $distinct = false |
130
|
|
|
): int|float { |
131
|
|
|
parent::avg($column, $distinct); |
132
|
|
|
return $this->getColumnResult(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string|Expression|Closure $column |
137
|
|
|
* @param bool $distinct |
138
|
|
|
* |
139
|
|
|
* @return int|float |
140
|
|
|
*/ |
141
|
|
|
public function sum( |
142
|
|
|
string|Expression|Closure $column, |
143
|
|
|
bool $distinct = false |
144
|
|
|
): int|float { |
145
|
|
|
parent::sum($column, $distinct); |
146
|
|
|
return $this->getColumnResult(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string|Expression|Closure $column |
151
|
|
|
* @param bool $distinct |
152
|
|
|
* |
153
|
|
|
* @return int|float |
154
|
|
|
*/ |
155
|
|
|
public function min( |
156
|
|
|
string|Expression|Closure $column, |
157
|
|
|
bool $distinct = false |
158
|
|
|
): int|float { |
159
|
|
|
parent::min($column, $distinct); |
160
|
|
|
return $this->getColumnResult(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string|Expression|Closure $column |
165
|
|
|
* @param bool $distinct |
166
|
|
|
* |
167
|
|
|
* @return int|float |
168
|
|
|
*/ |
169
|
|
|
public function max( |
170
|
|
|
string|Expression|Closure $column, |
171
|
|
|
bool $distinct = false |
172
|
|
|
): int|float { |
173
|
|
|
parent::max($column, $distinct); |
174
|
|
|
return $this->getColumnResult(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return the result set for column |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
protected function getColumnResult(): mixed |
182
|
|
|
{ |
183
|
|
|
$driver = $this->connection->getDriver(); |
184
|
|
|
|
185
|
|
|
return $this->connection->column( |
186
|
|
|
$driver->select($this->queryStatement), |
187
|
|
|
$driver->getParams() |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|