wfDB.php
7.68 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
class wfDB {
public $errorMsg = false;
public static function shared() {
static $_shared = null;
if ($_shared === null) {
$_shared = new wfDB();
}
return $_shared;
}
/**
* Returns the table prefix for the main site on multisites and the site itself on single site installations.
*
* @return string
*/
public static function networkPrefix() {
global $wpdb;
return $wpdb->base_prefix;
}
/**
* Returns the table with the site (single site installations) or network (multisite) prefix added.
*
* @param string $table
* @param bool $applyCaseConversion Whether or not to convert the table case to what is actually in use.
* @return string
*/
public static function networkTable($table, $applyCaseConversion = true) {
if (wfSchema::usingLowercase() && $applyCaseConversion) {
$table = strtolower($table);
}
return self::networkPrefix() . $table;
}
/**
* Returns the table prefix for the given blog ID. On single site installations, this will be equivalent to wfDB::networkPrefix().
*
* @param int $blogID
* @return string
*/
public static function blogPrefix($blogID) {
global $wpdb;
return $wpdb->get_blog_prefix($blogID);
}
/**
* Returns the table with the site (single site installations) or blog-specific (multisite) prefix added.
*
* @param string $table
* @param bool $applyCaseConversion Whether or not to convert the table case to what is actually in use.
* @return string
*/
public static function blogTable($table, $blogID, $applyCaseConversion = true) {
if (wfSchema::usingLowercase() && $applyCaseConversion) {
$table = strtolower($table);
}
return self::blogPrefix($blogID) . $table;
}
public function querySingle(){
global $wpdb;
if(func_num_args() > 1){
$args = func_get_args();
return $wpdb->get_var(call_user_func_array(array($wpdb, 'prepare'), $args));
} else {
return $wpdb->get_var(func_get_arg(0));
}
}
public function querySingleRec(){ //queryInSprintfFormat, arg1, arg2, ... :: Returns a single assoc-array or null if nothing found.
global $wpdb;
if(func_num_args() > 1){
$args = func_get_args();
return $wpdb->get_row(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A);
} else {
return $wpdb->get_row(func_get_arg(0), ARRAY_A);
}
}
public function queryWrite(){
global $wpdb;
if(func_num_args() > 1){
$args = func_get_args();
return $wpdb->query(call_user_func_array(array($wpdb, 'prepare'), $args));
} else {
return $wpdb->query(func_get_arg(0));
}
}
public function flush(){ //Clear cache
global $wpdb;
$wpdb->flush();
}
public function querySelect(){ //sprintfString, arguments :: always returns array() and will be empty if no results.
global $wpdb;
if(func_num_args() > 1){
$args = func_get_args();
return $wpdb->get_results(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A);
} else {
return $wpdb->get_results(func_get_arg(0), ARRAY_A);
}
}
public function queryWriteIgnoreError(){ //sprintfString, arguments
global $wpdb;
$oldSuppress = $wpdb->suppress_errors(true);
$args = func_get_args();
call_user_func_array(array($this, 'queryWrite'), $args);
$wpdb->suppress_errors($oldSuppress);
}
public function columnExists($table, $col){
$table = wfDB::networkTable($table);
$q = $this->querySelect("desc $table");
foreach($q as $row){
if($row['Field'] == $col){
return true;
}
}
return false;
}
public function dropColumn($table, $col){
$table = wfDB::networkTable($table);
$this->queryWrite("alter table $table drop column $col");
}
public function createKeyIfNotExists($table, $col, $keyName){
$table = wfDB::networkTable($table);
$exists = $this->querySingle(<<<SQL
SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME='%s'
SQL
, $table);
$keyFound = false;
if($exists){
$q = $this->querySelect("show keys from $table");
foreach($q as $row){
if($row['Key_name'] == $keyName){
$keyFound = true;
}
}
}
if(! $keyFound){
$this->queryWrite("alter table $table add KEY $keyName($col)");
}
}
public function getMaxAllowedPacketBytes(){
$rec = $this->querySingleRec("show variables like 'max_allowed_packet'");
return intval($rec['Value']);
}
public function getMaxLongDataSizeBytes() {
$rec = $this->querySingleRec("show variables like 'max_long_data_size'");
return $rec['Value'];
}
public function truncate($table){ //Ensures everything is deleted if user is using MySQL >= 5.1.16 and does not have "drop" privileges
$this->queryWrite("truncate table $table");
$this->queryWrite("delete from $table");
}
public function getLastError(){
global $wpdb;
return $wpdb->last_error;
}
public function realEscape($str){
global $wpdb;
return $wpdb->_real_escape($str);
}
}
abstract class wfModel {
private $data;
private $db;
private $dirty = false;
/**
* Column name of the primary key field.
*
* @return string
*/
abstract public function getIDColumn();
/**
* Table name.
*
* @return mixed
*/
abstract public function getTable();
/**
* Checks if this is a valid column in the table before setting data on the model.
*
* @param string $column
* @return boolean
*/
abstract public function hasColumn($column);
/**
* wfModel constructor.
* @param array|int|string $data
*/
public function __construct($data = array()) {
if (is_array($data) || is_object($data)) {
$this->setData($data);
} else if (is_numeric($data)) {
$this->fetchByID($data);
}
}
public function fetchByID($id) {
$id = absint($id);
$data = $this->getDB()->get_row($this->getDB()->prepare('SELECT * FROM ' . $this->getTable() .
' WHERE ' . $this->getIDColumn() . ' = %d', $id));
if ($data) {
$this->setData($data);
return true;
}
return false;
}
/**
* @return bool
*/
public function save() {
if (!$this->dirty) {
return false;
}
$this->dirty = ($this->getPrimaryKey() ? $this->update() : $this->insert()) === false;
return !$this->dirty;
}
/**
* @return false|int
*/
public function insert() {
$data = $this->getData();
unset($data[$this->getPrimaryKey()]);
$rowsAffected = $this->getDB()->insert($this->getTable(), $data);
$this->setPrimaryKey($this->getDB()->insert_id);
return $rowsAffected;
}
/**
* @return false|int
*/
public function update() {
return $this->getDB()->update($this->getTable(), $this->getData(), array(
$this->getIDColumn() => $this->getPrimaryKey(),
));
}
/**
* @param $name string
* @return mixed
*/
public function __get($name) {
if (!$this->hasColumn($name)) {
return null;
}
return array_key_exists($name, $this->data) ? $this->data[$name] : null;
}
/**
* @param $name string
* @param $value mixed
*/
public function __set($name, $value) {
if (!$this->hasColumn($name)) {
return;
}
$this->data[$name] = $value;
$this->dirty = true;
}
/**
* @return array
*/
public function getData() {
return $this->data;
}
/**
* @param array $data
* @param bool $flagDirty
*/
public function setData($data, $flagDirty = true) {
$this->data = array();
foreach ($data as $column => $value) {
if ($this->hasColumn($column)) {
$this->data[$column] = $value;
$this->dirty = (bool) $flagDirty;
}
}
}
/**
* @return wpdb
*/
public function getDB() {
if ($this->db === null) {
global $wpdb;
$this->db = $wpdb;
}
return $this->db;
}
/**
* @param wpdb $db
*/
public function setDB($db) {
$this->db = $db;
}
/**
* @return int
*/
public function getPrimaryKey() {
return $this->{$this->getIDColumn()};
}
/**
* @param int $value
*/
public function setPrimaryKey($value) {
$this->{$this->getIDColumn()} = $value;
}
}