1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class SelectAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource SelectAbstract.php |
6
|
|
|
* @created 03.06.2017 |
7
|
|
|
* @package chillerlan\Database\Query\Dialects |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Query\Traits\WhereTrait; |
16
|
|
|
|
17
|
|
|
abstract class SelectAbstract extends StatementAbstract implements SelectInterface{ |
18
|
|
|
use WhereTrait; |
19
|
|
|
|
20
|
|
|
protected $distinct = false; |
21
|
|
|
|
22
|
|
|
protected $cols = []; |
23
|
|
|
protected $from = []; |
24
|
|
|
protected $orderby = []; |
25
|
|
|
protected $groupby = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return \chillerlan\Database\Query\SelectInterface |
29
|
|
|
*/ |
30
|
|
|
public function distinct():SelectInterface{ |
31
|
|
|
$this->distinct = true; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $expr1 |
38
|
|
|
* @param null $expr2 |
39
|
|
|
* @param null $func |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
protected function addColumn($expr1, $expr2 = null, $func = null){ |
44
|
|
|
// @todo: quotes |
45
|
|
|
switch(true){ |
46
|
|
|
case $expr2 && $func: |
|
|
|
|
47
|
|
|
$col = sprintf('%s(%s) AS %s', strtoupper($func), $expr1, $expr2); break; |
|
|
|
|
48
|
|
|
case $expr2 && !$func: |
|
|
|
|
49
|
|
|
$col = sprintf('%s AS %s', $expr1, $expr2); break; |
|
|
|
|
50
|
|
|
case !$expr2 && $func: |
51
|
|
|
$col = sprintf('%s(%s)', strtoupper($func), $expr1); break; |
|
|
|
|
52
|
|
|
case !$expr2 && !$func: |
53
|
|
|
default: |
54
|
|
|
$col = $expr1; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->cols[$expr2 ?? $expr1] = $col; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $expressions |
62
|
|
|
* |
63
|
|
|
* @return \chillerlan\Database\Query\SelectInterface |
64
|
|
|
*/ |
65
|
|
|
public function cols(array $expressions):SelectInterface{ |
66
|
|
|
|
67
|
|
|
foreach($expressions as $k => $ref){ |
68
|
|
|
|
69
|
|
|
if(is_string($k)){ |
70
|
|
|
is_array($ref) |
71
|
|
|
? $this->addColumn($k, $ref[0], $ref[1] ?? null) |
72
|
|
|
: $this->addColumn($k, $ref); |
73
|
|
|
} |
74
|
|
|
else{ |
75
|
|
|
is_array($ref) |
76
|
|
|
? $this->addColumn($ref[0], null, $ref[1] ?? null) |
77
|
|
|
: $this->addColumn($ref); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $table |
87
|
|
|
* @param string|null $ref |
88
|
|
|
*/ |
89
|
|
|
protected function _addFrom(string $table, string $ref = null){ |
90
|
|
|
$from = $table; |
91
|
|
|
|
92
|
|
|
if($ref){ |
|
|
|
|
93
|
|
|
$from = sprintf('%s AS %s', $table, $ref);// @todo: index hint |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->from[$ref ?? $table] = $from; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $expressions |
101
|
|
|
* |
102
|
|
|
* @return \chillerlan\Database\Query\SelectInterface |
103
|
|
|
*/ |
104
|
|
|
public function from(array $expressions):SelectInterface{ |
105
|
|
|
|
106
|
|
|
foreach($expressions as $k => $ref){ |
107
|
|
|
|
108
|
|
|
if(is_string($k)){ |
109
|
|
|
$this->_addFrom($k, $ref); |
110
|
|
|
} |
111
|
|
|
else{ |
112
|
|
|
$x = explode(' ', $ref); |
113
|
|
|
|
114
|
|
|
if(count($x) === 2){ |
115
|
|
|
$this->_addFrom($x[0], $x[1]); |
116
|
|
|
} |
117
|
|
|
else{ |
118
|
|
|
$this->_addFrom($ref); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function where($val1, $val2, $operator = '=', $bind = true, $join = 'AND'):SelectInterface{ |
128
|
|
|
return $this->_addWhere($val1, $val2, $operator, $bind, $join); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function openBracket($join = null):SelectInterface{ |
132
|
|
|
return $this->_openBracket($join); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function closeBracket():SelectInterface{ |
136
|
|
|
return $this->_closeBracket(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $limit |
141
|
|
|
* |
142
|
|
|
* @return \chillerlan\Database\Query\SelectInterface |
143
|
|
|
*/ |
144
|
|
|
public function limit(int $limit):SelectInterface{ |
145
|
|
|
$this->limit = $limit; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $offset |
152
|
|
|
* |
153
|
|
|
* @return \chillerlan\Database\Query\SelectInterface |
154
|
|
|
*/ |
155
|
|
|
public function offset(int $offset):SelectInterface{ |
156
|
|
|
$this->offset = $offset; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $expressions |
163
|
|
|
* |
164
|
|
|
* @return \chillerlan\Database\Query\SelectInterface |
165
|
|
|
*/ |
166
|
|
|
public function orderby(array $expressions):SelectInterface{ |
167
|
|
|
|
168
|
|
|
foreach($expressions as $k => $expression){ |
169
|
|
|
|
170
|
|
|
if(is_string($k)){ |
171
|
|
|
$orderby = $k; |
172
|
|
|
$expression = strtoupper($expression); |
173
|
|
|
|
174
|
|
|
if(in_array($expression, ['ASC', 'DESC'])){ |
175
|
|
|
$orderby .= ' '.$expression; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->orderby[] = $orderby; |
179
|
|
|
} |
180
|
|
|
else{ |
181
|
|
|
$this->orderby[] = $expression; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function groupBy(array $expressions):SelectInterface{ |
190
|
|
|
|
191
|
|
|
foreach($expressions as $expression){ |
192
|
|
|
$this->groupby[] = $expression; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function union():SelectInterface{ |
199
|
|
|
// TODO: Implement union() method. |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
As per the PSR-2 coding standard, there must be a space after the
case
keyword, instead of the test immediately following it.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.