-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-update-helper.php
79 lines (64 loc) · 1.59 KB
/
data-update-helper.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
<?php
require_once(dirname(__FILE__) . '/data-condition-helper.php');
class DataUpdateHelper extends DataConditionHelper {
private $updates = '';
private function setValue($field, $value) {
if ($this->updates) {
$this->updates .= ',';
}
$this->updates .= $field . '=' . $value;
}
public function setString($field, $value) {
$this->setValue(
self::filterKeyword($field),
self::singleQuote(self::filterString($value))
);
}
public function setKeyword($field, $value) {
$this->setValue(
self::filterKeyword($field),
self::filterKeyword($value)
);
}
public function setInteger($field, $value) {
$this->setValue(
self::filterKeyword($field),
self::filterInteger($value)
);
}
public function setFloat($field, $value) {
$this->setValue(
self::filterKeyword($field),
self::filterFloat($value)
);
}
public function setValues($arrValues) {
if (!is_array($arrValues)) return;
foreach ($arrValues as $field => $value) {
if (is_string($value)) {
$this->setString($field, $value);
} elseif (is_float($value)) {
$this->setFloat($field, $value);
} elseif (is_integer($value)) {
$this->setInteger($field, $value);
} else {
$this->setKeyword($field, $value);
}
}
}
protected function generateSQL() {
$table = &$this->table;
$conditions =& $this->conditions;
$updates =& $this->updates;
$sql = "UPDATE {$table} SET {$updates}";
if ($this->conditions) {
$sql .= ' WHERE ' . $conditions;
}
$sql .= ';';
return $sql;
}
public function execute() {
$sql = $this->generateSQL();
return $this->db->execute($sql);
}
}