-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelcache.php
91 lines (78 loc) · 2.42 KB
/
modelcache.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
<?
class ModelCache {
protected $modelStructure = null;
protected $modelSource = null;
public function initializeData(&$data) {
$data = array();
foreach(array_keys($this->modelStructure) as $key) {
$data[$key] = null;
if($this->modelStructure[$key]['default'] != null) {
$data[$key] = $this->modelStructure[$key]['default'];
}
}
}
public function isNumeric($fld) {
if($this->modelStructure[$fld]['type'] == 'int')
return true;
if($this->modelStructure[$fld]['type'] == 'tinyint')
return true;
if($this->modelStructure[$fld]['type'] == 'smallint')
return true;
if($this->modelStructure[$fld]['type'] == 'bit')
return true;
if($this->modelStructure[$fld]['type'] == 'short')
return true;
return false;
}
public function getFieldsForQuery() {
$fields = array();
foreach(array_keys($this->modelStructure) as $fld) {
if($this->modelStructure[$fld]["type"] == "timestamp") {
$fields[] = "UNIX_TIMESTAMP($fld) as $fld";
} else {
$fields[] = $fld;
}
}
return $fields;
}
public function getFields() {
return array_keys($this->modelStructure);
}
public function hasField($fld) {
return in_array($fld, $this->getFields());
}
public function sanitize($fld, $value) {
if(preg_match("#text$#", $this->modelStructure[$fld]["type"]) ||
preg_match("#char$#", $this->modelStructure[$fld]["type"]) ||
$this->modelStructure[$fld]["type"] == "enum" ||
$this->modelStructure[$fld]["type"] == "date" ||
$this->modelStructure[$fld]["type"] == "timestamp") {
$value = "'" . Datasource::escape($value) . "'";
} else if($this->modelStructure[$fld]["type"] == "tinyint") {
if(!(intval($value) == 0 || intval($value) == 1)) {
SwissMVCErrors::generalError("Given value ($value) is not a valid boolean value");
}
} else {
// Assume to be numeric
if(!is_numeric($value) && $value != 'NULL') {
SwissMVCErrors::generalError("$value given as numeric database input (" . $this->modelSource . ".$fld)");
}
}
return $value;
}
public function getPrimaryKey() {
foreach(array_keys($this->modelStructure) as $key) {
if($this->modelStructure[$key]['is_id']) {
return $key;
}
}
return NULL;
}
public function getModelStructure() {
return $this->modelStructure;
}
public function getModelSource() {
return $this->modelSource;
}
}
?>