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