|
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 ColumnExpression.php |
|
33
|
|
|
* |
|
34
|
|
|
* The class for column Expression |
|
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 ColumnExpression |
|
53
|
|
|
* @package Platine\Database\Query |
|
54
|
|
|
*/ |
|
55
|
|
|
class ColumnExpression |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* ColumnExpression constructor. |
|
59
|
|
|
* @param QueryStatement $queryStatement |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(protected QueryStatement $queryStatement) |
|
62
|
|
|
{ |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string|Expression|Closure $name |
|
67
|
|
|
* @param string|null $alias |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function column(string|Expression|Closure $name, ?string $alias = null): self |
|
71
|
|
|
{ |
|
72
|
|
|
$this->queryStatement->addColumn($name, $alias); |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add multiple columns |
|
79
|
|
|
* @param array<int|string, mixed> $columns |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function columns(array $columns): self |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($columns as $name => $alias) { |
|
85
|
|
|
if (!is_string($name)) { |
|
86
|
|
|
$this->column($alias, null); |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (is_string($alias)) { |
|
91
|
|
|
$this->column($name, $alias); |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->column($alias, $name); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string|array<int, mixed>|Expression|Closure $column |
|
102
|
|
|
* @param string|null $alias |
|
103
|
|
|
* @param bool $distinct |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
public function count( |
|
107
|
|
|
string|array|Expression|Closure $column = '*', |
|
108
|
|
|
?string $alias = null, |
|
109
|
|
|
bool $distinct = false |
|
110
|
|
|
): self { |
|
111
|
|
|
return $this->column( |
|
112
|
|
|
(new Expression())->count($column, $distinct), |
|
113
|
|
|
$alias |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string|Expression|Closure $column |
|
119
|
|
|
* @param string|null $alias |
|
120
|
|
|
* @param bool $distinct |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
|
|
public function avg( |
|
124
|
|
|
string|Expression|Closure $column, |
|
125
|
|
|
?string $alias = null, |
|
126
|
|
|
bool $distinct = false |
|
127
|
|
|
): self { |
|
128
|
|
|
return $this->column((new Expression())->avg($column, $distinct), $alias); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string|Expression|Closure $column |
|
133
|
|
|
* @param string|null $alias |
|
134
|
|
|
* @param bool $distinct |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
|
|
public function sum( |
|
138
|
|
|
string|Expression|Closure $column, |
|
139
|
|
|
?string $alias = null, |
|
140
|
|
|
bool $distinct = false |
|
141
|
|
|
): self { |
|
142
|
|
|
return $this->column((new Expression())->sum($column, $distinct), $alias); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string|Expression|Closure $column |
|
147
|
|
|
* @param string|null $alias |
|
148
|
|
|
* @param bool $distinct |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
|
|
public function min( |
|
152
|
|
|
string|Expression|Closure $column, |
|
153
|
|
|
?string $alias = null, |
|
154
|
|
|
bool $distinct = false |
|
155
|
|
|
): self { |
|
156
|
|
|
return $this->column((new Expression())->min($column, $distinct), $alias); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string|Expression|Closure $column |
|
161
|
|
|
* @param string|null $alias |
|
162
|
|
|
* @param bool $distinct |
|
163
|
|
|
* @return $this |
|
164
|
|
|
*/ |
|
165
|
|
|
public function max( |
|
166
|
|
|
string|Expression|Closure $column, |
|
167
|
|
|
?string $alias = null, |
|
168
|
|
|
bool $distinct = false |
|
169
|
|
|
): self { |
|
170
|
|
|
return $this->column((new Expression())->max($column, $distinct), $alias); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function __clone(): void |
|
174
|
|
|
{ |
|
175
|
|
|
$this->queryStatement = clone $this->queryStatement; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|