| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
if (!class_exists('WPRWPDb')) : |
| 5 |
|
| 6 |
class WPRWPDb { |
| 7 |
public function dbprefix() { |
| 8 |
global $wpdb; |
| 9 |
$prefix = $wpdb->base_prefix ? $wpdb->base_prefix : $wpdb->prefix; |
| 10 |
return $prefix; |
| 11 |
} |
| 12 |
|
| 13 |
public function prepare($query, $args) { |
| 14 |
global $wpdb; |
| 15 |
return $wpdb->prepare($query, $args); |
| 16 |
} |
| 17 |
|
| 18 |
public function getSiteId() { |
| 19 |
global $wpdb; |
| 20 |
return $wpdb->siteid; |
| 21 |
} |
| 22 |
|
| 23 |
public function getResult($query, $obj = ARRAY_A) { |
| 24 |
global $wpdb; |
| 25 |
return $wpdb->get_results($query, $obj); |
| 26 |
} |
| 27 |
|
| 28 |
public function query($query) { |
| 29 |
global $wpdb; |
| 30 |
return $wpdb->query($query); |
| 31 |
} |
| 32 |
|
| 33 |
public function getVar($query, $col = 0, $row = 0) { |
| 34 |
global $wpdb; |
| 35 |
return $wpdb->get_var($query, $col, $row); |
| 36 |
} |
| 37 |
|
| 38 |
public function getCol($query, $col = 0) { |
| 39 |
global $wpdb; |
| 40 |
return $wpdb->get_col($query, $col); |
| 41 |
} |
| 42 |
|
| 43 |
public function tableName($table) { |
| 44 |
return $table[0]; |
| 45 |
} |
| 46 |
|
| 47 |
public function showTables() { |
| 48 |
$tables = $this->getResult("SHOW TABLES", ARRAY_N); |
| 49 |
return array_map(array($this, 'tableName'), $tables); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
public function showTableStatus() { |
| 54 |
return $this->getResult("SHOW TABLE STATUS"); |
| 55 |
} |
| 56 |
|
| 57 |
public function tableKeys($table) { |
| 58 |
return $this->getResult("SHOW KEYS FROM $table;"); |
| 59 |
} |
| 60 |
|
| 61 |
public function showDbVariables($variable) { |
| 62 |
$variables = $this->getResult("Show variables like '%$variable%' ;"); |
| 63 |
$result = array(); |
| 64 |
foreach ($variables as $variable) { |
| 65 |
$result[$variable["Variable_name"]] = $variable["Value"]; |
| 66 |
} |
| 67 |
return $result; |
| 68 |
} |
| 69 |
|
| 70 |
public function describeTable($table) { |
| 71 |
return $this->getResult("DESCRIBE $table;"); |
| 72 |
} |
| 73 |
|
| 74 |
public function showTableIndex($table) { |
| 75 |
return $this->getResult("SHOW INDEX FROM $table"); |
| 76 |
} |
| 77 |
|
| 78 |
public function checkTable($table, $type) { |
| 79 |
return $this->getResult("CHECK TABLE $table $type;"); |
| 80 |
} |
| 81 |
|
| 82 |
public function repairTable($table) { |
| 83 |
return $this->getResult("REPAIR TABLE $table;"); |
| 84 |
} |
| 85 |
|
| 86 |
public function showTableCreate($table) { |
| 87 |
return $this->getVar("SHOW CREATE TABLE $table;", 1); |
| 88 |
} |
| 89 |
|
| 90 |
public function rowsCount($table) { |
| 91 |
$count = $this->getVar("SELECT COUNT(*) FROM $table;"); |
| 92 |
return intval($count); |
| 93 |
} |
| 94 |
|
| 95 |
public function createTable($query, $name, $usedbdelta = false) { |
| 96 |
$table = $this->getBVTable($name); |
| 97 |
if (!$this->isTablePresent($table)) { |
| 98 |
if ($usedbdelta) { |
| 99 |
if (!function_exists('dbDelta')) |
| 100 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 101 |
dbDelta($query); |
| 102 |
} else { |
| 103 |
$this->query($query); |
| 104 |
} |
| 105 |
} |
| 106 |
return $this->isTablePresent($table); |
| 107 |
} |
| 108 |
|
| 109 |
public function createTables($tables, $usedbdelta = false) { |
| 110 |
$result = array(); |
| 111 |
foreach ($tables as $table => $query) { |
| 112 |
$result[$table] = $this->createTable($query, $table, $usedbdelta); |
| 113 |
} |
| 114 |
return $result; |
| 115 |
} |
| 116 |
|
| 117 |
public function alterBVTable($query, $name) { |
| 118 |
$resp = false; |
| 119 |
$table = $this->getBVTable($name); |
| 120 |
if ($this->isTablePresent($table)) { |
| 121 |
$resp = $this->query($query); |
| 122 |
} |
| 123 |
return $resp; |
| 124 |
} |
| 125 |
|
| 126 |
public function alterTables($tables) { |
| 127 |
$result = array(); |
| 128 |
foreach ($tables as $table => $query) { |
| 129 |
$result[$table] = $this->alterBVTable($query, $table); |
| 130 |
} |
| 131 |
return $result; |
| 132 |
} |
| 133 |
|
| 134 |
public function getTableContent($table, $fields = '*', $filter = '', $limit = 0, $offset = 0) { |
| 135 |
$query = "SELECT $fields from $table $filter"; |
| 136 |
if ($limit > 0) |
| 137 |
$query .= " LIMIT $limit"; |
| 138 |
if ($offset > 0) |
| 139 |
$query .= " OFFSET $offset"; |
| 140 |
$rows = $this->getResult($query); |
| 141 |
return $rows; |
| 142 |
} |
| 143 |
|
| 144 |
public function isTablePresent($table) { |
| 145 |
return ($this->getVar("SHOW TABLES LIKE '$table'") === $table); |
| 146 |
} |
| 147 |
|
| 148 |
public function getCharsetCollate() { |
| 149 |
global $wpdb; |
| 150 |
return $wpdb->get_charset_collate(); |
| 151 |
} |
| 152 |
|
| 153 |
public function getWPTable($name) { |
| 154 |
return ($this->dbprefix() . $name); |
| 155 |
} |
| 156 |
|
| 157 |
public function getBVTable($name) { |
| 158 |
return ($this->getWPTable("bv_" . $name)); |
| 159 |
} |
| 160 |
|
| 161 |
public function truncateBVTable($name) { |
| 162 |
$table = $this->getBVTable($name); |
| 163 |
if ($this->isTablePresent($table)) { |
| 164 |
return $this->query("TRUNCATE TABLE $table;"); |
| 165 |
} else { |
| 166 |
return false; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public function deleteBVTableContent($name, $filter = "") { |
| 171 |
$table = $this->getBVTable($name); |
| 172 |
if ($this->isTablePresent($table)) { |
| 173 |
return $this->query("DELETE FROM $table $filter;"); |
| 174 |
} else { |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public function dropBVTable($name) { |
| 180 |
$table = $this->getBVTable($name); |
| 181 |
if ($this->isTablePresent($table)) { |
| 182 |
$this->query("DROP TABLE IF EXISTS $table;"); |
| 183 |
} |
| 184 |
return !$this->isTablePresent($table); |
| 185 |
} |
| 186 |
|
| 187 |
public function dropTables($tables) { |
| 188 |
$result = array(); |
| 189 |
foreach ($tables as $table) { |
| 190 |
$result[$table] = $this->dropBVTable($table); |
| 191 |
} |
| 192 |
return $result; |
| 193 |
} |
| 194 |
|
| 195 |
public function truncateTables($tables) { |
| 196 |
$result = array(); |
| 197 |
foreach ($tables as $table) { |
| 198 |
$result[$table] = $this->truncateBVTable($table); |
| 199 |
} |
| 200 |
return $result; |
| 201 |
} |
| 202 |
|
| 203 |
public function deleteRowsFromtable($name, $count = 1) { |
| 204 |
$table = $this->getBVTable($name); |
| 205 |
if ($this->isTablePresent($table)) { |
| 206 |
return $this->getResult("DELETE FROM $table LIMIT $count;"); |
| 207 |
} else { |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
public function replaceIntoBVTable($name, $value) { |
| 213 |
global $wpdb; |
| 214 |
$table = $this->getBVTable($name); |
| 215 |
return $wpdb->replace($table, $value); |
| 216 |
} |
| 217 |
|
| 218 |
public function insertIntoBVTable($name, $value) { |
| 219 |
global $wpdb; |
| 220 |
$table = $this->getBVTable($name); |
| 221 |
return $wpdb->insert($table, $value); |
| 222 |
} |
| 223 |
|
| 224 |
public function tinfo($name) { |
| 225 |
$result = array(); |
| 226 |
$table = $this->getBVTable($name); |
| 227 |
|
| 228 |
$result['name'] = $table; |
| 229 |
|
| 230 |
if ($this->isTablePresent($table)) { |
| 231 |
$result['exists'] = true; |
| 232 |
$result['createquery'] = $this->showTableCreate($table); |
| 233 |
} |
| 234 |
|
| 235 |
return $result; |
| 236 |
} |
| 237 |
|
| 238 |
public function getMysqlVersion() { |
| 239 |
return $this->showDbVariables('version')['version']; |
| 240 |
} |
| 241 |
} |
| 242 |
endif; |