forked from Runalyze/Runalyze
/
Updater.php
175 lines (150 loc) · 3.15 KB
/
Updater.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* This file contains class::Updater
* @package Runalyze\Model
*/
namespace Runalyze\Model;
/**
* Update entity in database
*
* @author Hannes Christiansen
* @package Runalyze\Model
*/
abstract class Updater {
/**
* PDO
* @var \PDO
*/
protected $PDO;
/**
* Old object
* @var \Runalyze\Model\Entity
*/
protected $OldObject;
/**
* New object
* @var \Runalyze\Model\Entity
*/
protected $NewObject;
/**
* Construct updater
* @param \PDO $connection
* @param \Runalyze\Model\Entity $newObject [optional]
* @param \Runalyze\Model\Entity $oldObject [optional]
*/
public function __construct(\PDO $connection, Entity $newObject = null, Entity $oldObject = null) {
$this->PDO = $connection;
$this->NewObject = $newObject;
$this->OldObject = $oldObject;
}
/**
* Tablename without prefix
* @return string
*/
abstract protected function table();
/**
* Where clause
* @return string
*/
abstract protected function where();
/**
* Keys to update
* @return array
*/
abstract protected function keys();
/**
* Ignore specific key
*
* This method may be overwritten in the subclass to ignore some specific keys,
* e.g. don't update `A` if `B` has a specific value.
* @param string $key
* @return boolean
*/
protected function ignore($key) {
return false;
}
/**
* Update
* @param \Runalyze\Model\Entity $object [optional]
* @param array|boolean $keys [optional]
* @throws \RuntimeException
*/
final public function update(Entity $object = null, $keys = false) {
if (!is_null($object)) {
$this->NewObject = $object;
}
if (is_null($this->NewObject)) {
throw new \RuntimeException('The updater does not have any object to update.');
}
if ($keys === false) {
$keys = $this->keys();
}
$this->before();
$this->runUpdate($keys);
$this->after();
}
/**
* Run update
* @param array $keys
*/
private function runUpdate($keys) {
$set = array();
foreach ($keys as $key) {
if (!$this->ignore($key) && $this->hasChanged($key)) {
$value = $this->value($key);
$quoted = is_null($value) ? 'NULL' : $this->PDO->quote($value);
$set[] = '`'.$key.'` = '.$quoted;
}
}
if (!empty($set)) {
$this->PDO->exec(
'UPDATE `'.PREFIX.$this->table().'`
SET '.implode(', ', $set).'
WHERE '.$this->where()
);
}
}
/**
* Knows old object
* @return boolean
*/
protected function knowsOldObject() {
return (null !== $this->OldObject);
}
/**
* Has the value changed?
* @param string $key
* @return boolean
*/
protected function hasChanged($key) {
if ($this->knowsOldObject()) {
return ($this->OldObject->get($key) !== $this->NewObject->get($key));
}
return true;
}
/**
* Value for key
* @param string $key
* @return string
*/
protected function value($key) {
$value = $this->NewObject->get($key);
if (is_array($value)) {
return Entity::implode($value);
}
return $value;
}
/**
* Tasks before update
*/
protected function before() {
if ($this->knowsOldObject()) {
$this->OldObject->synchronize();
}
$this->NewObject->synchronize();
}
/**
* Tasks after update
*/
protected function after() {}
}