1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace PhpBoot\DB\rules\update; |
3
|
|
|
use PhpBoot\DB\rules\basic\BasicRule; |
4
|
|
|
use PhpBoot\DB\rules\basic\WhereRule; |
5
|
|
|
use PhpBoot\DB\impls\UpdateSetImpl; |
6
|
|
|
use PhpBoot\DB\impls\UpdateImpl; |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
require_once dirname(__DIR__).'/impls.php'; |
10
|
1 |
|
require_once __DIR__.'/basic.php'; |
11
|
|
|
|
12
|
|
|
class UpdateRule extends BasicRule |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* update('table')->set('a', 1) => "UPDATE table SET a=1" |
16
|
|
|
* @param string $table |
17
|
|
|
* @return \PhpBoot\DB\rules\update\UpdateSetRule |
18
|
|
|
*/ |
19
|
8 |
|
public function update($table) { |
20
|
8 |
|
UpdateImpl::update($this->context, $table); |
21
|
8 |
|
return new UpdateSetRule($this->context); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
class UpdateSetRule extends WhereRule |
|
|
|
|
26
|
|
|
{ |
27
|
8 |
|
public function __construct($context){ |
28
|
8 |
|
parent::__construct($context); |
29
|
8 |
|
$this->impl = new UpdateSetImpl(); |
30
|
8 |
|
} |
31
|
|
|
/** |
32
|
|
|
* update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
33
|
|
|
* update('table')->set(['a'=>1])->set(['b', DB::raw('now()')]) => "UPDATE table SET a=1,b=now()" |
34
|
|
|
* |
35
|
|
|
* update('table')->set('a=?',1) => "UPDATE table SET a=1" |
36
|
|
|
* @param array|string $expr |
37
|
|
|
* @param mixed $_ |
38
|
|
|
* @return \PhpBoot\DB\rules\update\UpdateSetRule |
39
|
|
|
*/ |
40
|
8 |
|
public function set($expr, $_=null) { |
|
|
|
|
41
|
8 |
|
$this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
42
|
8 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
private $impl; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
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.