-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdate.php
107 lines (88 loc) · 2.48 KB
/
Update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace System\Database\MyQuery;
use System\Database\MyPDO;
use System\Database\MyQuery\Join\AbstractJoin;
use System\Database\MyQuery\Traits\ConditionTrait;
use System\Database\MyQuery\Traits\SubQueryTrait;
class Update extends Execute
{
use ConditionTrait;
use SubQueryTrait;
public function __construct(string $table_name, MyPDO $PDO)
{
$this->_table = $table_name;
$this->PDO = $PDO;
}
public function __toString()
{
return $this->builder();
}
/**
* Insert set value (single).
*
* @param array<string, string|int|bool|null> $values Array of bing and value
*
* @return self
*/
public function values($values)
{
foreach ($values as $key => $value) {
$this->value($key, $value);
}
return $this;
}
/**
* Insert set value (single).
*
* @param string $bind Pdo bind
* @param string|int|bool|null $value Value of the bind
*
* @return self
*/
public function value(string $bind, $value)
{
$this->_binds[] = Bind::set($bind, $value, $bind)->prefixBind(':bind_');
return $this;
}
/**
* Join statment:
* - inner join
* - left join
* - right join
* - full join.
*/
public function join(AbstractJoin $ref_table): self
{
// overide master table
$ref_table->table($this->_table);
$this->_join[] = $ref_table->stringJoin();
$binds = (fn () => $this->{'sub_query'})->call($ref_table);
if (null !== $binds) {
$this->_binds = array_merge($this->_binds, $binds->getBind());
}
return $this;
}
private function getJoin(): string
{
return 0 === count($this->_join)
? ''
: implode(' ', $this->_join)
;
}
protected function builder(): string
{
$setter = [];
foreach ($this->_binds as $bind) {
if ($bind->hasColumName()) {
$setter[] = $bind->getColumnName() . ' = ' . $bind->getBind();
}
}
$build = [];
$build['join'] = $this->getJoin();
$build[] = 'SET ' . implode(', ', $setter);
$build['where'] = $this->getWhere();
$query_parts = implode(' ', array_filter($build, fn ($item) => $item !== ''));
return $this->_query = "UPDATE {$this->_table} {$query_parts}";
}
}