1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\DB\impls; |
4
|
|
|
use PhpBoot\DB\DB; |
5
|
|
|
use PhpBoot\DB\NestedStringCut; |
6
|
|
|
use PhpBoot\DB\Raw; |
7
|
|
|
use PhpBoot\DB\rules\basic\BasicRule; |
8
|
|
|
use PhpBoot\DB\Context; |
9
|
|
|
|
10
|
|
|
class ExecResult{ |
11
|
22 |
|
public function __construct($success, $pdo, $st){ |
12
|
22 |
|
$this->pdo = $pdo; |
13
|
22 |
|
$this->st = $st; |
14
|
22 |
|
$this->success = $success; |
15
|
22 |
|
$this->rows = $this->st->rowCount(); |
16
|
22 |
|
} |
17
|
1 |
|
public function lastInsertId($name=null){ |
18
|
1 |
|
return $this->pdo->lastInsertId($name); |
19
|
|
|
} |
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
* true on success or false on failure. |
23
|
|
|
*/ |
24
|
|
|
public $success; |
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
* the number of rows. |
28
|
|
|
*/ |
29
|
|
|
public $rows; |
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var \PDO |
33
|
|
|
*/ |
34
|
|
|
public $pdo; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \PDOStatement |
38
|
|
|
*/ |
39
|
|
|
public $st; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
class SelectImpl |
|
|
|
|
43
|
|
|
{ |
44
|
20 |
|
static public function select($context, $columns){ |
|
|
|
|
45
|
20 |
|
$context->appendSql("SELECT $columns"); |
46
|
20 |
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
class FromImpl |
|
|
|
|
50
|
|
|
{ |
51
|
19 |
|
static public function from($context, $tables,$as=null){ |
|
|
|
|
52
|
19 |
|
if($tables instanceof BasicRule){ |
53
|
|
|
$context->appendSql("FROM (".$tables->context->sql.')'); |
54
|
|
|
$context->params = array_merge($context->params,$tables->context->params); |
55
|
|
|
}else { |
56
|
19 |
|
$context->appendSql("FROM ".DB::wrap($tables)); |
57
|
|
|
} |
58
|
19 |
|
if($as){ |
59
|
|
|
$context->appendSql("as ".DB::wrap($as)); |
60
|
|
|
} |
61
|
19 |
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
class DeleteImpl |
|
|
|
|
65
|
|
|
{ |
66
|
7 |
|
static public function deleteFrom($context, $from) |
|
|
|
|
67
|
|
|
{ |
68
|
7 |
|
$context->appendSql("DELETE FROM ".DB::wrap($from)); |
69
|
7 |
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
class JoinImpl |
|
|
|
|
73
|
|
|
{ |
74
|
5 |
|
static public function join($context, $type, $table) { |
|
|
|
|
75
|
5 |
|
$table = DB::wrap($table); |
76
|
5 |
|
if($type){ |
77
|
3 |
|
$context->appendSql("$type JOIN $table"); |
78
|
3 |
|
}else{ |
79
|
2 |
|
$context->appendSql("JOIN $table"); |
80
|
|
|
} |
81
|
5 |
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
class JoinOnImpl |
|
|
|
|
85
|
|
|
{ |
86
|
5 |
|
static public function on($context, $condition) { |
|
|
|
|
87
|
5 |
|
$context->appendSql("ON $condition"); |
88
|
5 |
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
class ForUpdateImpl |
|
|
|
|
92
|
|
|
{ |
93
|
2 |
|
static public function forUpdate($context){ |
|
|
|
|
94
|
2 |
|
$context->appendSql("FOR UPDATE"); |
95
|
2 |
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
class ForUpdateOfImpl |
|
|
|
|
99
|
|
|
{ |
100
|
1 |
|
static public function of($context, $column){ |
|
|
|
|
101
|
1 |
|
$column = DB::wrap($column); |
102
|
1 |
|
$context->appendSql("OF $column"); |
103
|
1 |
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
class InsertImpl |
|
|
|
|
107
|
|
|
{ |
108
|
5 |
|
static public function insertInto($context, $table) { |
|
|
|
|
109
|
5 |
|
$table = DB::wrap($table); |
110
|
5 |
|
$context->appendSql("INSERT INTO $table"); |
111
|
5 |
|
} |
112
|
|
|
} |
113
|
|
|
class ReplaceImpl |
|
|
|
|
114
|
|
|
{ |
115
|
2 |
|
static public function replaceInto($context, $table) { |
|
|
|
|
116
|
2 |
|
$table = DB::wrap($table); |
117
|
2 |
|
$context->appendSql("REPLACE INTO $table"); |
118
|
2 |
|
} |
119
|
|
|
} |
120
|
|
|
class ValuesImpl |
|
|
|
|
121
|
|
|
{ |
122
|
7 |
|
static public function values($context, $values){ |
|
|
|
|
123
|
7 |
|
$params = []; |
124
|
7 |
|
$stubs = []; |
125
|
7 |
|
foreach ($values as $v){ |
126
|
7 |
|
if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
127
|
4 |
|
$stubs[]=$v->get(); |
128
|
4 |
|
}else{ |
129
|
7 |
|
$stubs[]='?'; |
130
|
7 |
|
$params[] = $v; |
131
|
|
|
} |
132
|
7 |
|
} |
133
|
7 |
|
$stubs = implode(',', $stubs); |
134
|
|
|
|
135
|
7 |
|
if(array_keys($values) === range(0, count($values) - 1)){ |
136
|
|
|
//VALUES(val0, val1, val2) |
|
|
|
|
137
|
4 |
|
$context->appendSql("VALUES($stubs)"); |
138
|
|
|
|
139
|
4 |
|
}else{ |
140
|
|
|
//(col0, col1, col2) VALUES(val0, val1, val2) |
|
|
|
|
141
|
|
|
$columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
142
|
3 |
|
$context->appendSql("($columns) VALUES($stubs)",false); |
143
|
|
|
} |
144
|
7 |
|
$context->appendParams($params); |
145
|
7 |
|
} |
146
|
|
|
private $sql = null; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
class UpdateImpl |
|
|
|
|
150
|
|
|
{ |
151
|
8 |
|
static public function update($context, $table){ |
|
|
|
|
152
|
8 |
|
$table = DB::wrap($table); |
153
|
8 |
|
$context->appendSql("UPDATE $table"); |
154
|
8 |
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
class UpdateSetImpl |
|
|
|
|
158
|
|
|
{ |
159
|
8 |
|
public function set(Context $context, $expr, $args){ |
160
|
8 |
|
if(is_string($expr)){ |
161
|
|
|
return $this->setExpr($context, $expr, $args); |
162
|
|
|
}else{ |
163
|
8 |
|
return $this->setArgs($context, $expr); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function setExpr(Context $context, $expr, $args){ |
168
|
|
|
if($this->first){ |
169
|
|
|
$this->first = false; |
170
|
|
|
$prefix = 'SET '; |
171
|
|
|
}else{ |
172
|
|
|
$prefix = ','; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$context->appendSql("$prefix$expr",$prefix == 'SET '); |
176
|
|
|
$context->appendParams($args); |
177
|
|
|
|
178
|
|
|
} |
179
|
8 |
View Code Duplication |
public function setArgs(Context $context, $values){ |
|
|
|
|
180
|
8 |
|
$set = []; |
181
|
8 |
|
$params = []; |
182
|
8 |
|
foreach ($values as $k=>$v){ |
183
|
8 |
|
$k = DB::wrap($k); |
184
|
8 |
|
if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
185
|
1 |
|
$set[]= "$k=".$v->get(); |
186
|
1 |
|
}else{ |
187
|
8 |
|
$set[]= "$k=?"; |
188
|
8 |
|
$params[]=$v; |
189
|
|
|
} |
190
|
8 |
|
} |
191
|
8 |
|
if($this->first){ |
192
|
8 |
|
$this->first = false; |
193
|
8 |
|
$context->appendSql('SET '.implode(',', $set)); |
194
|
8 |
|
$context->appendParams($params); |
195
|
8 |
|
}else{ |
196
|
1 |
|
$context->appendSql(','.implode(',', $set),false); |
197
|
1 |
|
$context->appendParams($params); |
198
|
|
|
} |
199
|
8 |
|
} |
200
|
|
|
private $first=true; |
201
|
|
|
} |
202
|
|
|
class OrderByImpl |
|
|
|
|
203
|
|
|
{ |
204
|
7 |
|
public function orderByArgs(Context $context, $orders){ |
205
|
7 |
|
if(empty($orders)){ |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
7 |
|
$params = array(); |
209
|
7 |
|
foreach ($orders as $k=>$v){ |
210
|
7 |
|
if(is_integer($k)){ |
211
|
6 |
|
$params[] = DB::wrap($v); |
212
|
6 |
|
}else{ |
213
|
2 |
|
$k = DB::wrap($k); |
214
|
|
|
|
215
|
2 |
|
$v = strtoupper($v); |
216
|
2 |
|
($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
|
|
|
217
|
|
|
|
218
|
2 |
|
$params[] = "$k $v"; |
219
|
|
|
} |
220
|
7 |
|
} |
221
|
7 |
|
if($this->first){ |
222
|
7 |
|
$this->first = false; |
223
|
7 |
|
$context->appendSql('ORDER BY '.implode(',', $params)); |
224
|
7 |
|
}else{ |
225
|
1 |
|
$context->appendSql(','.implode(',', $params),false); |
226
|
|
|
} |
227
|
7 |
|
return $this; |
228
|
|
|
} |
229
|
7 |
|
public function orderBy(Context $context, $column, $order=null){ |
230
|
7 |
|
if(is_string($column)){ |
231
|
7 |
|
if($order === null){ |
232
|
6 |
|
$column = [$column]; |
233
|
6 |
|
}else{ |
234
|
2 |
|
$column = [$column=>$order]; |
235
|
|
|
} |
236
|
7 |
|
} |
237
|
7 |
|
return $this->orderByArgs($context, $column); |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
private $first=true; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
class LimitImpl |
|
|
|
|
245
|
|
|
{ |
246
|
3 |
|
static public function limit(Context $context, $size){ |
|
|
|
|
247
|
3 |
|
$intSize = intval($size); |
248
|
3 |
|
strval($intSize) == $size or \PhpBoot\abort( |
|
|
|
|
249
|
|
|
new \InvalidArgumentException("invalid params for limit($size)")); |
250
|
3 |
|
$context->appendSql("LIMIT $size"); |
251
|
3 |
|
} |
252
|
1 |
|
static public function limitWithOffset(Context $context, $start, $size){ |
|
|
|
|
253
|
1 |
|
$intStart = intval($start); |
254
|
1 |
|
$intSize = intval($size); |
255
|
1 |
|
strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
|
|
|
|
256
|
|
|
new \InvalidArgumentException("invalid params for limit($start, $size)")); |
257
|
1 |
|
$context->appendSql("LIMIT $start,$size"); |
258
|
1 |
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
class WhereImpl{ |
|
|
|
|
262
|
|
|
|
263
|
1 |
|
static private function findQ($str,$offset = 0,$no=0){ |
|
|
|
|
264
|
1 |
|
$found = strpos($str, '?', $offset); |
265
|
1 |
|
if($no == 0 || $found === false){ |
266
|
1 |
|
return $found; |
267
|
|
|
} |
268
|
1 |
|
return self::findQ($str, $found+1, $no-1); |
269
|
|
|
} |
270
|
2 |
View Code Duplication |
static public function having(Context $context, $expr, $args){ |
|
|
|
|
271
|
2 |
|
if(is_string($expr)){ |
272
|
2 |
|
self::condition($context, 'HAVING', $expr, $args); |
273
|
2 |
|
}else{ |
274
|
|
|
self::conditionArgs($context, 'HAVING', $expr); |
275
|
|
|
} |
276
|
|
|
//TODO 支持 OR 、 闭包 |
277
|
|
|
|
278
|
2 |
|
} |
279
|
19 |
View Code Duplication |
static public function where(Context $context, $expr, $args){ |
|
|
|
|
280
|
19 |
|
if(empty($expr)){ |
281
|
1 |
|
return; |
282
|
|
|
} |
283
|
18 |
|
if (is_string($expr)){ |
284
|
13 |
|
self::condition($context, 'WHERE', $expr, $args); |
285
|
13 |
|
}else{ |
286
|
6 |
|
self::conditionArgs($context, 'WHERE', $expr); |
287
|
|
|
} |
288
|
|
|
//TODO 支持 OR 、 闭包 |
289
|
18 |
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* find like Mongodb query glossary |
293
|
|
|
* whereArray( |
294
|
|
|
* [ |
295
|
|
|
* 'id'=>['>'=>1], |
296
|
|
|
* 'name'=>'cym', |
297
|
|
|
* ] |
298
|
|
|
* ) |
299
|
|
|
* 支持的操作符有 |
300
|
|
|
* = 'id'=>['=' => 1] |
301
|
|
|
* > 'id'=>['>' => 1] |
302
|
|
|
* < 'id'=>['<' => 1] |
303
|
|
|
* <> 'id'=>['<>' => 1] |
304
|
|
|
* >= 'id'=>['>=' => 1] |
305
|
|
|
* <= 'id'=>['<=' => 1] |
306
|
|
|
* BETWEEN 'id'=>['BETWEEN' => [1 ,2]] |
307
|
|
|
* LIKE 'id'=>['LIKE' => '1%'] |
308
|
|
|
* IN 'id'=>['IN' => [1,2,3]] |
309
|
|
|
* NOT IN 'id'=>['NOT IN' => [1,2,3]] |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
6 |
|
static public function conditionArgs(Context $context, $prefix, $args=[]){ |
|
|
|
|
313
|
6 |
|
if($args ===null){ |
314
|
|
|
return ; |
315
|
|
|
} |
316
|
6 |
|
$exprs = array(); |
317
|
6 |
|
$params = array(); |
318
|
6 |
|
foreach ($args as $k => $v){ |
319
|
6 |
|
$k = DB::wrap($k); |
320
|
6 |
|
if(is_array($v)){ |
321
|
1 |
|
$ops = ['=', '>', '<', '<>', '>=', '<=', 'IN', 'NOT IN', 'BETWEEN', 'LIKE']; |
322
|
1 |
|
$op = array_keys($v)[0]; |
323
|
1 |
|
$op = strtoupper($op); |
324
|
|
|
|
325
|
1 |
|
false !== array_search($op, $ops) or \PhpBoot\abort( |
|
|
|
|
326
|
|
|
new \InvalidArgumentException("invalid param $op for whereArgs")); |
327
|
|
|
|
328
|
1 |
|
$var = array_values($v)[0]; |
329
|
1 |
|
if($op == 'IN' || $op == 'NOT IN'){ |
330
|
1 |
|
$stubs = []; |
331
|
1 |
View Code Duplication |
foreach ($var as $i){ |
|
|
|
|
332
|
1 |
|
if(is_a($i, Raw::class)){ |
333
|
1 |
|
$stubs[]=strval($i); |
334
|
1 |
|
}else{ |
335
|
1 |
|
$stubs[]='?'; |
336
|
1 |
|
$params[] = $i; |
337
|
|
|
} |
338
|
1 |
|
} |
339
|
1 |
|
$stubs = implode(',', $stubs); |
340
|
1 |
|
$exprs[] = "$k $op ($stubs)"; |
341
|
1 |
|
}else if($op == 'BETWEEN'){ |
342
|
1 |
|
$cond = "$k BETWEEN"; |
343
|
1 |
View Code Duplication |
if(is_a($var[0], Raw::class)){ |
|
|
|
|
344
|
|
|
$cond = "$cond ".strval($var[0]); |
345
|
|
|
}else{ |
346
|
1 |
|
$cond = "$cond ?"; |
347
|
1 |
|
$params[] = $var[0]; |
348
|
|
|
} |
349
|
1 |
View Code Duplication |
if(is_a($var[1], Raw::class)){ |
|
|
|
|
350
|
1 |
|
$cond = "$cond AND ".strval($var[1]); |
351
|
1 |
|
}else{ |
352
|
|
|
$cond = "$cond AND ?"; |
353
|
|
|
$params[] = $var[1]; |
354
|
|
|
} |
355
|
1 |
|
$exprs[] = $cond; |
356
|
1 |
|
}else{ |
357
|
1 |
|
if(is_a($var, Raw::class)){ |
358
|
|
|
$exprs[] = "$k $op ".strval($var); |
359
|
|
|
}else{ |
360
|
1 |
|
$exprs[] = "$k $op ?"; |
361
|
1 |
|
$params[] = $var; |
362
|
|
|
} |
363
|
|
|
} |
364
|
1 |
|
}else{ |
365
|
6 |
|
if(is_a($v, Raw::class)){ |
366
|
1 |
|
$exprs[] = "$k = ".strval($v); |
367
|
|
|
|
368
|
1 |
|
}else{ |
369
|
6 |
|
$exprs[] = "$k = ?"; |
370
|
6 |
|
$params[] = $v; |
371
|
|
|
} |
372
|
|
|
} |
373
|
6 |
|
} |
374
|
|
|
|
375
|
6 |
|
self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
376
|
6 |
|
} |
377
|
19 |
|
static public function condition(Context $context, $prefix, $expr, $args){ |
|
|
|
|
378
|
19 |
|
if(!empty($expr)){ |
379
|
19 |
|
if($args){ |
380
|
|
|
//因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
381
|
|
|
//也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
|
|
|
|
382
|
18 |
|
$cutted = null; |
383
|
18 |
|
$cut = null; |
384
|
18 |
|
$toReplace = array(); |
385
|
|
|
|
386
|
18 |
|
$newArgs=array(); |
387
|
|
|
//找到所有数组对应的?符位置 |
388
|
18 |
|
foreach ($args as $k =>$arg){ |
389
|
18 |
|
if(is_array($arg) || is_a($arg, Raw::class)){ |
390
|
1 |
|
if(!$cutted){ |
|
|
|
|
391
|
1 |
|
$cut = new NestedStringCut($expr); |
392
|
1 |
|
$cutted = $cut->getText(); |
393
|
1 |
|
} |
394
|
|
|
//找到第$k个?符 |
395
|
1 |
|
$pos = self::findQ($cutted, 0, $k); |
396
|
1 |
|
$pos = $cut->mapPos($pos); |
397
|
1 |
|
$pos !== false or \PhpBoot\abort( |
|
|
|
|
398
|
|
|
new \InvalidArgumentException("unmatched params and ? @ $expr")); |
399
|
|
|
|
400
|
1 |
|
if(is_array($arg)){ |
401
|
1 |
|
$stubs = []; |
402
|
1 |
View Code Duplication |
foreach ($arg as $i){ |
|
|
|
|
403
|
1 |
|
if(is_a($i, Raw::class)){ |
404
|
1 |
|
$stubs[] = strval($i); |
405
|
1 |
|
}else{ |
406
|
1 |
|
$stubs[] = '?'; |
407
|
1 |
|
$newArgs[] = $i; |
408
|
|
|
} |
409
|
1 |
|
} |
410
|
1 |
|
$stubs = implode(',', $stubs); |
411
|
1 |
|
}else{ |
412
|
1 |
|
$stubs = strval($arg); |
413
|
|
|
} |
414
|
1 |
|
$toReplace[] = [$pos, $stubs]; |
415
|
|
|
|
416
|
1 |
|
}else{ |
417
|
18 |
|
$newArgs[]=$arg; |
418
|
|
|
} |
419
|
18 |
|
} |
420
|
|
|
|
421
|
18 |
|
if(count($toReplace)){ |
422
|
1 |
|
$toReplace = array_reverse($toReplace); |
423
|
1 |
|
foreach ($toReplace as $i){ |
424
|
1 |
|
list($pos, $v) = $i; |
425
|
1 |
|
$expr = substr($expr, 0, $pos).$v. substr($expr, $pos+1); |
426
|
1 |
|
} |
427
|
1 |
|
$args = $newArgs; |
428
|
1 |
|
} |
429
|
18 |
|
} |
430
|
19 |
|
$context->appendSql($prefix.' '.$expr); |
431
|
19 |
|
if($args){ |
432
|
18 |
|
$context->appendParams($args); |
433
|
18 |
|
} |
434
|
19 |
|
} |
435
|
19 |
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
class GroupByImpl{ |
|
|
|
|
439
|
3 |
|
static public function groupBy(Context $context, $column){ |
|
|
|
|
440
|
3 |
|
$column = DB::wrap($column); |
441
|
3 |
|
$context->appendSql("GROUP BY $column"); |
442
|
3 |
|
} |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
class ExecImpl |
|
|
|
|
446
|
|
|
{ |
447
|
|
|
/** |
448
|
|
|
* |
449
|
|
|
* @param Context $context |
450
|
|
|
* @param $exceOnError boolean whether throw exceptions |
451
|
|
|
* @return ExecResult |
452
|
|
|
*/ |
453
|
22 |
|
static public function exec($context) { |
|
|
|
|
454
|
22 |
|
$st = $context->connection->prepare($context->sql); |
455
|
22 |
|
$success = $st->execute($context->params); |
456
|
22 |
|
return new ExecResult($success, $context->connection, $st); |
457
|
|
|
} |
458
|
|
|
/** |
459
|
|
|
* |
460
|
|
|
* @param Context $context |
461
|
|
|
* @param string|false $asDict return as dict or array |
|
|
|
|
462
|
|
|
* @return false|array |
463
|
|
|
*/ |
464
|
20 |
|
static public function get($context, $dictAs=false){ |
|
|
|
|
465
|
|
|
|
466
|
20 |
|
$st = $context->connection->prepare($context->sql); |
467
|
20 |
|
if($st->execute($context->params)){ |
468
|
|
|
$res = $st->fetchAll(\PDO::FETCH_ASSOC); |
469
|
|
|
if ($dictAs){ |
470
|
|
|
$dict= []; |
471
|
|
|
foreach ($res as $i){ |
472
|
|
|
$dict[$i[$dictAs]]=$i; |
473
|
|
|
} |
474
|
|
|
return $context->handleResult($dict); |
475
|
|
|
} |
476
|
|
|
return $context->handleResult($res); |
477
|
|
|
}else{ |
478
|
20 |
|
return false; |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @param Context $context |
484
|
|
|
* @return int|false |
485
|
|
|
*/ |
486
|
|
|
static public function count($context){ |
|
|
|
|
487
|
|
|
|
488
|
|
|
$found = []; |
489
|
|
|
if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
490
|
|
|
count($found)==0){ |
491
|
|
|
\PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
492
|
|
|
} |
493
|
|
|
list($chars, $columnBegin) = $found[0]; |
|
|
|
|
494
|
|
|
$columnBegin = $columnBegin + strlen('select')+1; |
495
|
|
|
|
496
|
|
|
$columnEnd = 0; |
497
|
|
|
$found = []; |
498
|
|
|
if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
499
|
|
|
count($found)==0){ |
500
|
|
|
$columnEnd = strlen($context->sql); |
501
|
|
|
}else{ |
502
|
|
|
list($chars, $columnEnd) = $found[0]; |
|
|
|
|
503
|
|
|
} |
504
|
|
|
$sql = substr($context->sql, 0, $columnBegin); |
505
|
|
|
$sql .= ' COUNT(*) as `count` '; |
506
|
|
|
$sql .= substr($context->sql, $columnEnd); |
507
|
|
|
|
508
|
|
|
$st = $context->connection->prepare($sql); |
509
|
|
|
if($st->execute($context->params)){ |
510
|
|
|
$res = $st->fetchAll(\PDO::FETCH_ASSOC); |
511
|
|
|
return $res[0]['count']; |
512
|
|
|
}else{ |
513
|
|
|
return false; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
class OnDuplicateKeyUpdateImpl |
|
|
|
|
519
|
|
|
{ |
520
|
1 |
|
public function set($context, $column, $value){ |
521
|
1 |
|
if(is_string($column)){ |
522
|
1 |
|
$this->setExpr($context, $column, $value); |
523
|
1 |
|
}else{ |
524
|
1 |
|
$this->setArgs($context, $column); |
525
|
|
|
} |
526
|
1 |
|
} |
527
|
|
|
|
528
|
1 |
|
public function setExpr($context, $expr, $args){ |
529
|
1 |
|
$prefix = ''; |
|
|
|
|
530
|
1 |
|
if($this->first){ |
531
|
1 |
|
$this->first = false; |
532
|
1 |
|
$prefix = 'ON DUPLICATE KEY UPDATE '; |
533
|
1 |
|
}else{ |
534
|
|
|
$prefix = ','; |
535
|
|
|
} |
536
|
|
|
|
537
|
1 |
|
$context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
538
|
1 |
|
$context->appendParams($args); |
539
|
|
|
|
540
|
1 |
|
} |
541
|
1 |
View Code Duplication |
public function setArgs($context, $values){ |
|
|
|
|
542
|
1 |
|
$set = []; |
543
|
1 |
|
$params = []; |
544
|
1 |
|
foreach ($values as $k=>$v){ |
545
|
1 |
|
$k = DB::wrap($k); |
546
|
1 |
|
if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
547
|
1 |
|
$set[]= "$k=".$v->get(); |
548
|
1 |
|
}else{ |
549
|
|
|
$set[]= "$k=?"; |
550
|
|
|
$params[]=$v; |
551
|
|
|
} |
552
|
1 |
|
} |
553
|
1 |
|
if($this->first){ |
554
|
1 |
|
$this->first = false; |
555
|
1 |
|
$context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
556
|
1 |
|
$context->appendParams($params); |
557
|
1 |
|
}else{ |
558
|
|
|
$context->appendSql(','.implode(',', $set),false); |
559
|
|
|
$context->appendParams($params); |
560
|
|
|
} |
561
|
1 |
|
} |
562
|
|
|
private $first=true; |
563
|
|
|
} |
564
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.