1
|
|
|
<?php |
2
|
|
|
namespace PhpBoot\DB\rules\update; |
3
|
|
|
use PhpBoot\DB\Context; |
4
|
|
|
use PhpBoot\DB\rules\basic\BasicRule; |
5
|
|
|
use PhpBoot\DB\rules\basic\WhereRule; |
6
|
|
|
use PhpBoot\DB\impls\UpdateSetImpl; |
7
|
|
|
use PhpBoot\DB\impls\UpdateImpl; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
require_once dirname(__DIR__).'/impls.php'; |
11
|
|
|
require_once __DIR__.'/basic.php'; |
12
|
|
|
|
13
|
|
|
class UpdateRule extends BasicRule |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* update('table')->set('a', 1) => "UPDATE table SET a=1" |
17
|
|
|
* @param string $table |
18
|
|
|
* @return \PhpBoot\DB\rules\update\UpdateSetRule |
19
|
|
|
*/ |
20
|
11 |
|
public function update($table) { |
21
|
11 |
|
UpdateImpl::update($this->context, $table); |
22
|
11 |
|
return new UpdateSetRule($this->context); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
class UpdateSetRule extends BasicRule |
|
|
|
|
27
|
|
|
{ |
28
|
11 |
|
public function __construct($context){ |
29
|
11 |
|
parent::__construct($context); |
30
|
11 |
|
$this->impl = new UpdateSetImpl(); |
31
|
11 |
|
} |
32
|
|
|
/** |
33
|
|
|
* update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
34
|
|
|
|
35
|
|
|
* update('table')->set('a=?',1) => "UPDATE table SET a=1" |
36
|
|
|
* @param array|string $expr |
37
|
|
|
* @param mixed $_ |
38
|
|
|
* @return UpdateSetWhereRule |
39
|
|
|
*/ |
40
|
11 |
|
public function set($expr, $_=null) { |
|
|
|
|
41
|
11 |
|
$this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
42
|
11 |
|
return new UpdateSetWhereRule($this->context, $this->impl); |
43
|
|
|
} |
44
|
|
|
private $impl; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
class UpdateSetWhereRule extends WhereRule |
|
|
|
|
48
|
|
|
{ |
49
|
11 |
|
public function __construct(Context $context, UpdateSetImpl $impl){ |
50
|
11 |
|
parent::__construct($context); |
51
|
11 |
|
$this->impl = $impl; |
52
|
11 |
|
} |
53
|
|
|
/** |
54
|
|
|
* update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
55
|
|
|
* update('table')->set('a=?',1) => "UPDATE table SET a=1" |
56
|
|
|
* @param array|string $expr |
57
|
|
|
* @param mixed $_ |
58
|
|
|
* @return UpdateSetWhereRule |
59
|
|
|
*/ |
60
|
|
|
public function set($expr, $_=null) { |
|
|
|
|
61
|
|
|
$this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
62
|
|
|
return new UpdateSetWhereRule($this->context, $this->impl); |
63
|
|
|
} |
64
|
|
|
private $impl; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.