1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace PhpBoot\DB\rules\select; |
3
|
|
|
use PhpBoot\DB\rules\basic\BasicRule; |
4
|
|
|
use PhpBoot\DB\impls\ExecImpl; |
5
|
|
|
use PhpBoot\DB\impls\SelectImpl; |
6
|
|
|
use PhpBoot\DB\impls\FromImpl; |
7
|
|
|
use PhpBoot\DB\impls\JoinImpl; |
8
|
|
|
use PhpBoot\DB\impls\JoinOnImpl; |
9
|
|
|
use PhpBoot\DB\impls\WhereImpl; |
10
|
|
|
use PhpBoot\DB\impls\GroupByImpl; |
11
|
|
|
use PhpBoot\DB\impls\OrderByImpl; |
12
|
|
|
use PhpBoot\DB\impls\LimitImpl; |
13
|
|
|
use PhpBoot\DB\impls\ForUpdateOfImpl; |
14
|
|
|
use PhpBoot\DB\impls\ForUpdateImpl; |
15
|
|
|
|
16
|
1 |
|
require_once dirname(__DIR__).'/impls.php'; |
17
|
1 |
|
require_once __DIR__.'/basic.php'; |
18
|
|
|
|
19
|
|
|
class SelectRule extends BasicRule |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* select('column0, column1') => "SELECT column0, column1" |
23
|
|
|
* select('column0', 'column1') => "SELECT column0, column1" |
24
|
|
|
* @param string $columns |
25
|
|
|
* @return \PhpBoot\DB\rules\select\FromRule |
26
|
|
|
*/ |
27
|
20 |
|
public function select($columns) { |
28
|
20 |
|
SelectImpl::select($this->context, $columns); |
29
|
20 |
|
return new FromRule($this->context); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
class GetRule extends BasicRule |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Execute sql and get responses |
37
|
|
|
* @param string|false $asDict |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
19 |
|
public function get($asDict=false) { |
41
|
19 |
|
return ExecImpl::get($this->context, $asDict); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return int|false |
46
|
|
|
*/ |
47
|
|
|
public function count() { |
48
|
|
|
return ExecImpl::count($this->context); |
49
|
|
|
} |
50
|
|
|
/** |
51
|
|
|
* Execute sql and get one response |
52
|
|
|
* @return null |
53
|
|
|
*/ |
54
|
1 |
|
public function getFirst(){ |
55
|
1 |
|
$res = ExecImpl::get($this->context); |
56
|
1 |
|
if(count($res)){ |
57
|
1 |
|
return $res[0]; |
58
|
|
|
} |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
class FromRule extends GetRule |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* from('table') => "FROM table" |
66
|
|
|
* @param string $table |
67
|
|
|
* @return \PhpBoot\DB\rules\select\JoinRule |
68
|
|
|
*/ |
69
|
19 |
|
public function from($table, $as=null){ |
70
|
19 |
|
FromImpl::from($this->context, $table,$as); |
71
|
19 |
|
return new JoinRule($this->context); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
class ForUpdateOfRule extends GetRule |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* forUpdate()->of('column') => 'FOR UPDATE OF column' |
78
|
|
|
* @param string $column |
79
|
|
|
* @return \PhpBoot\DB\rules\select\GetRule |
80
|
|
|
*/ |
81
|
1 |
|
public function of($column){ |
82
|
1 |
|
ForUpdateOfImpl::of($this->context, $column); |
83
|
1 |
|
return new GetRule($this->context); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
class ForUpdateRule extends GetRule |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
/** |
89
|
|
|
* forUpdate() => 'FOR UPDATE' |
90
|
|
|
* @return \PhpBoot\DB\rules\select\ForUpdateOfRule |
91
|
|
|
*/ |
92
|
2 |
|
public function forUpdate(){ |
93
|
2 |
|
ForUpdateImpl::forUpdate($this->context); |
94
|
2 |
|
return new ForUpdateOfRule($this->context); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
class LimitRule extends ForUpdateRule |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
/** |
101
|
|
|
* limit(0,1) => "LIMIT 0,1" |
102
|
|
|
* @param int $start |
103
|
|
|
* @param int $size |
104
|
|
|
* @return \PhpBoot\DB\rules\select\ForUpdateRule |
105
|
|
|
*/ |
106
|
1 |
|
public function limit($start, $size) { |
107
|
1 |
|
LimitImpl::limitWithOffset($this->context, $start, $size); |
108
|
1 |
|
return new ForUpdateRule($this->context); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
class OrderByRule extends LimitRule |
|
|
|
|
113
|
|
|
{ |
114
|
19 |
|
public function __construct($context){ |
115
|
19 |
|
parent::__construct($context); |
116
|
19 |
|
$this->order = new OrderByImpl(); |
117
|
19 |
|
} |
118
|
|
|
/** |
119
|
|
|
* orderBy('column') => "ORDER BY column" |
120
|
|
|
* orderBy('column', Sql::ORDER_BY_ASC) => "ORDER BY column ASC" |
121
|
|
|
* orderBy('column0')->orderBy('column1') => "ORDER BY column0, column1" |
122
|
|
|
* |
123
|
|
|
* orderBy(['column0', 'column1'=>Sql::ORDER_BY_ASC]) => "ORDER BY column0,column1 ASC" |
124
|
|
|
* |
125
|
|
|
* @param string $column |
126
|
|
|
* @param string $order Sql::ORDER_BY_ASC or Sql::ORDER_BY_DESC |
127
|
|
|
* @return \PhpBoot\DB\rules\select\OrderByRule |
128
|
|
|
*/ |
129
|
3 |
|
public function orderBy($column, $order=null) { |
130
|
3 |
|
$this->order->orderBy($this->context, $column, $order); |
131
|
3 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
// /** |
|
|
|
|
134
|
|
|
// * orderByArgs(['column0', 'column1'=>Sql::ORDER_BY_ASC]) => "ORDER BY column0,column1 ASC" |
135
|
|
|
// * @param array $args |
136
|
|
|
// * @return \PhpBoot\DB\rules\select\OrderByRule |
137
|
|
|
// */ |
138
|
|
|
// public function orderByArgs($args) { |
139
|
|
|
// $this->order->orderByArgs($this->context, $args); |
140
|
|
|
// return $this; |
141
|
|
|
// } |
142
|
|
|
/** |
143
|
|
|
* @var OrderByImpl |
144
|
|
|
*/ |
145
|
|
|
private $order; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
class HavingRule extends OrderByRule |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
/** |
151
|
|
|
* |
152
|
|
|
* having('SUM(a)=?', 1) => "HAVING SUM(a)=1" |
153
|
|
|
* having('a>?', Sql::raw('now()')) => "HAVING a>now()" |
154
|
|
|
* having('a IN (?)', [1, 2]) => "HAVING a IN (1,2)" |
155
|
|
|
* |
156
|
|
|
* having([ |
157
|
|
|
* 'a'=>1, |
158
|
|
|
* 'b'=>['IN'=>[1,2]] |
159
|
|
|
* 'c'=>['BETWEEN'=>[1,2]] |
160
|
|
|
* 'd'=>['<>'=>1] |
161
|
|
|
* ]) |
162
|
|
|
* |
163
|
|
|
* => |
164
|
|
|
* "HAVING a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
165
|
|
|
* |
166
|
|
|
* @param string|array $expr |
167
|
|
|
* @param string $_ |
168
|
|
|
* @return \PhpBoot\DB\rules\select\OrderByRule |
169
|
|
|
*/ |
170
|
2 |
|
public function having($expr, $_=null) { |
|
|
|
|
171
|
2 |
|
WhereImpl::having($this->context, $expr, array_slice(func_get_args(), 1)); |
172
|
2 |
|
return new OrderByRule($this->context); |
173
|
|
|
} |
174
|
|
|
// /** |
|
|
|
|
175
|
|
|
// * |
176
|
|
|
// * |
177
|
|
|
// * |
178
|
|
|
// * @param array $args |
179
|
|
|
// * @return \PhpBoot\DB\rules\select\OrderByRule |
180
|
|
|
// */ |
181
|
|
|
// public function havingArgs($args) { |
182
|
|
|
// WhereImpl::havingArgs($this->context, $args); |
183
|
|
|
// return new OrderByRule($this->context); |
184
|
|
|
// } |
185
|
|
|
} |
186
|
|
|
class GroupByRule extends OrderByRule |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
/** |
189
|
|
|
* groupBy('column') => "GROUP BY column" |
190
|
|
|
* @param string $column |
191
|
|
|
* @return \PhpBoot\DB\rules\select\HavingRule |
192
|
|
|
*/ |
193
|
3 |
|
public function groupBy($column) { |
194
|
3 |
|
GroupByImpl::groupBy($this->context, $column); |
195
|
3 |
|
return new HavingRule($this->context); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
class WhereRule extends GroupByRule |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
/** |
201
|
|
|
* where('a=?', 1) => "WHERE a=1" |
202
|
|
|
* where('a=?', Sql::raw('now()')) => "WHERE a=now()" |
203
|
|
|
* where('a IN (?)', [1, 2]) => "WHERE a IN (1,2)" |
204
|
|
|
* where([ |
205
|
|
|
* 'a'=>1, |
206
|
|
|
* 'b'=>['IN'=>[1,2]] |
207
|
|
|
* 'c'=>['BETWEEN'=>[1,2]] |
208
|
|
|
* 'd'=>['<>'=>1] |
209
|
|
|
* ]) |
210
|
|
|
* => |
211
|
|
|
* "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
212
|
|
|
* @param string|array $conditions |
213
|
|
|
* //TODO where(callable $query) |
214
|
|
|
* @param mixed $_ |
215
|
|
|
* @return \PhpBoot\DB\rules\select\GroupByRule |
216
|
|
|
*/ |
217
|
6 |
|
public function where($conditions=null, $_=null) { |
|
|
|
|
218
|
6 |
|
WhereImpl::where($this->context, $conditions, array_slice(func_get_args(), 1)); |
219
|
6 |
|
return new GroupByRule($this->context); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
class JoinRule extends WhereRule |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
/** |
226
|
|
|
* join('table1')->on('table0.id=table1.id') => "JOIN table1 ON table0.id=table1.id" |
227
|
|
|
* @param string $table |
228
|
|
|
* @return \PhpBoot\DB\rules\select\JoinOnRule |
229
|
|
|
*/ |
230
|
2 |
|
public function join($table){ |
231
|
2 |
|
JoinImpl::join($this->context,null, $table); |
232
|
2 |
|
return new JoinOnRule($this->context); |
233
|
|
|
} |
234
|
|
|
/** |
235
|
|
|
* leftJoin('table1')->on('table0.id=table1.id') => "LEFT JOIN table1 ON table0.id=table1.id" |
236
|
|
|
* @param string $table |
237
|
|
|
* @return \PhpBoot\DB\rules\select\JoinOnRule |
238
|
|
|
*/ |
239
|
1 |
|
public function leftJoin($table){ |
240
|
1 |
|
JoinImpl::join($this->context,'LEFT', $table); |
241
|
1 |
|
return new JoinOnRule($this->context); |
242
|
|
|
} |
243
|
|
|
/** |
244
|
|
|
* rightJoin('table1')->on('table0.id=table1.id') => "RIGHT JOIN table1 ON table0.id=table1.id" |
245
|
|
|
* @param string $table |
246
|
|
|
* @return \PhpBoot\DB\rules\select\JoinOnRule |
247
|
|
|
*/ |
248
|
1 |
|
public function rightJoin($table) { |
249
|
1 |
|
JoinImpl::join($this->context,'RIGHT', $table); |
250
|
1 |
|
return new JoinOnRule($this->context); |
251
|
|
|
} |
252
|
|
|
/** |
253
|
|
|
* innerJoin('table1')->on('table0.id=table1.id') => "INNER JOIN table1 ON table0.id=table1.id" |
254
|
|
|
* @param string $table |
255
|
|
|
* @return \PhpBoot\DB\rules\select\JoinOnRule |
256
|
|
|
*/ |
257
|
1 |
|
public function innerJoin($table) { |
258
|
1 |
|
JoinImpl::join($this->context,'INNER', $table); |
259
|
1 |
|
return new JoinOnRule($this->context); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
class JoinOnRule extends BasicRule |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
/** |
266
|
|
|
* join('table1')->on('table0.id=table1.id') => "JOIN table1 ON table0.id=table1.id" |
267
|
|
|
* @param string $condition |
268
|
|
|
* @return \PhpBoot\DB\rules\select\JoinRule |
269
|
|
|
*/ |
270
|
5 |
|
public function on($condition){ |
271
|
5 |
|
JoinOnImpl::on($this->context, $condition); |
272
|
5 |
|
return new JoinRule($this->context); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.