1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace PhpBoot\DB\rules\insert; |
3
|
|
|
|
4
|
|
|
use PhpBoot\DB\DB; |
5
|
|
|
use PhpBoot\DB\impls\OnDuplicateKeyUpdateImpl; |
6
|
|
|
use PhpBoot\DB\rules\basic\BasicRule; |
7
|
|
|
use PhpBoot\DB\rules\basic\ExecRule; |
8
|
|
|
use PhpBoot\DB\impls\InsertImpl; |
9
|
|
|
use PhpBoot\DB\impls\ValuesImpl; |
10
|
|
|
|
11
|
|
|
require_once dirname(__DIR__).'/impls.php'; |
12
|
|
|
require_once __DIR__.'/basic.php'; |
13
|
|
|
|
14
|
|
|
class InsertRule extends BasicRule |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* insertInto('table')->values([1,2]) => "INSERT INTO table VALUES(1,2)" |
19
|
|
|
* @param string $table |
20
|
|
|
* @return \PhpBoot\DB\rules\insert\ValuesRule |
21
|
|
|
*/ |
22
|
8 |
|
public function insertInto($table) { |
23
|
8 |
|
InsertImpl::insertInto($this->context, $table); |
24
|
8 |
|
return new ValuesRule($this->context); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
class ValuesRule extends BasicRule |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* insertInto('table')->values([1,2]) => "INSERT INTO table VALUES(1,2)" |
32
|
|
|
* insertInto('table')->values(['a'=>1, 'b'=>Sql::raw('now()')]) => "INSERT INTO table(a,b) VALUES(1,now())" |
33
|
|
|
* @param array $values |
34
|
|
|
* @return \PhpBoot\DB\rules\insert\OnDuplicateKeyUpdateRule |
35
|
|
|
*/ |
36
|
5 |
|
public function values(array $values) { |
37
|
5 |
|
ValuesImpl::values($this->context, $values); |
38
|
5 |
|
return new OnDuplicateKeyUpdateRule($this->context); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* insertInto('table')->batchValues([[1,2],[3,4]]) => "INSERT INTO table VALUES(1,2), (3,2)" |
43
|
|
|
* |
44
|
|
|
* @param array $values |
45
|
|
|
* @return \PhpBoot\DB\rules\insert\OnDuplicateKeyUpdateRule |
46
|
|
|
*/ |
47
|
3 |
|
public function batchValues(array $values){ |
48
|
3 |
|
ValuesImpl::batchValues($this->context, $values); |
49
|
3 |
|
return new OnDuplicateKeyUpdateRule($this->context); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
class OnDuplicateKeyUpdateRule extends ExecRule |
|
|
|
|
54
|
|
|
{ |
55
|
8 |
|
public function __construct($context) |
56
|
|
|
{ |
57
|
8 |
|
parent::__construct($context); |
58
|
8 |
|
$this->impl = new OnDuplicateKeyUpdateImpl(); |
59
|
8 |
|
} |
60
|
|
|
|
61
|
|
|
// /** |
|
|
|
|
62
|
|
|
// * |
63
|
|
|
// * insertInto('table') |
64
|
|
|
// * ->values(['a'=>1, 'b'=>Sql::raw('now()')]) |
65
|
|
|
// * ->onDuplicateKeyUpdate('a', Sql::raw('a+1')) |
66
|
|
|
// * => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
67
|
|
|
// * |
68
|
|
|
// * @param string $column |
69
|
|
|
// * @param mixed $value |
70
|
|
|
// * @return \PhpBoot\DB\rules\basic\ExecRule |
71
|
|
|
// */ |
72
|
|
|
// public function onDuplicateKeyUpdate($column, $value) { |
73
|
|
|
// $this->impl->set($this->context, $column, $value); |
74
|
|
|
// return new ExecRule($this->context); |
75
|
|
|
// } |
76
|
|
|
|
77
|
|
|
// /** |
|
|
|
|
78
|
|
|
// * |
79
|
|
|
// * insertInto('table') |
80
|
|
|
// * ->values(['a'=>1, 'b'=>Sql::raw('now()')]) |
81
|
|
|
// * ->onDuplicateKeyUpdateArgs(['a'=>Sql::raw('a+1')]) |
82
|
|
|
// * => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
83
|
|
|
// * |
84
|
|
|
// * @param string $column |
85
|
|
|
// * @param mixed $value |
86
|
|
|
// * @return \PhpBoot\DB\rules\basic\ExecRule |
87
|
|
|
// */ |
88
|
|
|
// public function onDuplicateKeyUpdateArgs($values) { |
89
|
|
|
// $this->impl->setArgs($this->context, $values); |
90
|
|
|
// return new ExecRule($this->context); |
91
|
|
|
// } |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* insertInto('table') |
96
|
|
|
* ->values(['a'=>1, 'b'=>Sql::raw('now()')]) |
97
|
|
|
* ->onDuplicateKeyUpdate(['a'=>Sql::raw('a+1')]) |
98
|
|
|
* => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
99
|
|
|
* |
100
|
|
|
* insertInto('table') |
101
|
|
|
* ->values(['a'=>1, 'b'=>Sql::raw('now()')]) |
102
|
|
|
* ->onDuplicateKeyUpdate('a=a+1') |
103
|
|
|
* => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
104
|
|
|
* |
105
|
|
|
* @param string $column |
|
|
|
|
106
|
|
|
* @param mixed $value |
|
|
|
|
107
|
|
|
* @return \PhpBoot\DB\rules\basic\ExecRule |
108
|
|
|
*/ |
109
|
1 |
|
public function onDuplicateKeyUpdate($expr, $_=null) { |
|
|
|
|
110
|
1 |
|
$this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
111
|
1 |
|
return new ExecRule($this->context); |
112
|
|
|
} |
113
|
|
|
private $impl; |
114
|
|
|
} |
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.