-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbRec.class.php
More file actions
183 lines (137 loc) · 4.71 KB
/
DbRec.class.php
File metadata and controls
183 lines (137 loc) · 4.71 KB
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
176
177
178
179
180
181
182
183
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/*
* This class is not intended as part of the ogerlibphp12 and
* should go into the application specific class directory later.
* For now it is guest here to share basic development between
* apps in early stage and at the same time being tested in
* mature apps for later inclusion.
*/
/**
* WORK IN PROGRESS
* This class has core function on one side and at the same time
* is the playground for other database related functions.
* Including ones that may go into other classes when final.
* So be careful when using across multiple projects.
*/
/**
* Base class for handling one record of a database table.
* Mainly a collection of static methods.
*/
class DbRec {
public static $tableName;
/**
* Filter out values from an array where the keys of the values array
* have to match a column name of the assiciated table.
* @values: associative array with fieldname (key) value pairs.
*/
public static function filterColValues($values = array()) {
$newVals = array();
foreach ((array)$values as $key => $value) {
if (Dbw::isColumn(static::$tableName, $key)) {
$newVals[$key] = $value;
}
}
return $newVals;
} // eo filter
/**
* Get sql template for given target.
*/
public static function getSqlTpl($target, &$opts = array()) {
return $target;
} // eo get sql tpl
/**
* Get prepared sql string and fill sele vals
*/
public static function getSql($target, &$seleVals = array(), $req = null, &$tplOpts = array()) {
$tpl = static::getSqlTpl($target, $tplOpts);
switch ($tplOpts['parser']) {
case "php-sql":
$tpl = OgerExtjs::extjSqlClearCurlyTags($tpl);
$sqlTpl = new OgerExtjSqlTpl($req);
$sql = $sqlTpl->prepare($tpl, null, $tplOpts);
$seleVals = $sqlTpl->getParamValues();
break;
default:
$sql = OgerExtjs::extjSql($tpl, $seleVals, $req);
}
// postprocess template
if ($tplOpts['str_replace']) {
foreach ((array)$tplOpts['str_replace'] as $search => $replace) {
if (!$search) {
continue;
}
$sql = str_replace($search, $replace, $sql);
}
}
// format / beautify sql
if (!$tplOpts['skip-format']) {
$sql = SqlFormatter::format($sql);
}
return $sql;
} // eo get sql
/**
* Write record values to db.
* Accepts old style WHERE values (as array) too.
*/
public static function store($storeAction, $values, $where = null, $opts = array()) {
// @file_put_contents("debug.localonly", "\n-----\nvalues1=" . var_export($values, true) . "\n\n", FILE_APPEND);
$values = static::filterColValues($values);
// @file_put_contents("debug.localonly", "\n-----\nvalues2=" . var_export($values, true) . "\n\n", FILE_APPEND);
// sanity check - do not update without WHERE clause
if ($storeAction == "UPDATE") {
if (!$where) {
throw new Exception("Update without WHERE clause refused.");
}
} // eo update WHERE check
$stmt = Dbw::getStoreStmt($storeAction, static::$tableName, $values, $where);
// @file_put_contents("debug.localonly", "\n-----\nsql=\n$stmt\n\nvalues=\n" . var_export($values, true) . "\n\nwhere=\n" . var_export($where, true) . "\n\n", FILE_APPEND);
$pstmt = Dbw::$conn->prepare($stmt);
// cleanup where array if containes nested info
if (is_array($where)) {
foreach ($where as $key => $val) {
if (is_array($val)) {
unset($where[$key]);
reset($val);
$key = key($val);
$where[$key] = $val[$key];
}
}
}
// on update merge the where-values in, but preserve
// data-values if already present
if ($storeAction == "UPDATE" && is_array($where)) {
$values = array_merge($where, $values);
}
Dbw::checkStmtParams($stmt, $values);
// return raw sql and values data for debugging
if ($opts['getDebugInfo']) {
return array("stmt" => $stmt, "values" => $values);
}
// execute now
$result = $pstmt->execute($values);
$pstmt->closeCursor();
// return pstmt for using outside (e.g. counting affected rows)
// ATTENTION: mysql returns 0 if records are found but nothing changed!!!
return $pstmt;
} // eo store to db
/**
* Prepare date fields for output.
* Only used for php internals, because extjs converts on the fly
*/
public static function prepDateOut($values, $dateFieldNames) {
foreach ($dateFieldNames as $fieldName) {
if (substr($values[$fieldName], 0, 10) == '0000-00-00') {
$values[$fieldName] = '';
}
}
return $values;
} // eo order by template
} // end of class