| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disallow direct access. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
class CJTMYSQLQueueDriver extends CJTHookableClass { |
| 13 |
|
| 14 |
/** |
| 15 |
* put your comment there... |
| 16 |
* |
| 17 |
* @var mixed |
| 18 |
*/ |
| 19 |
protected $queue = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
protected $onexec = array('parameters' => array('param')); |
| 27 |
|
| 28 |
/** |
| 29 |
* put your comment there... |
| 30 |
* |
| 31 |
* @var mixed |
| 32 |
*/ |
| 33 |
protected $onprocessqueue = array('parameters' => array('queue')); |
| 34 |
|
| 35 |
/** |
| 36 |
* put your comment there... |
| 37 |
* |
| 38 |
* @var mixed |
| 39 |
*/ |
| 40 |
protected $onprocesscommand = array('parameters' => array('command')); |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
* @var mixed |
| 46 |
*/ |
| 47 |
protected $onqueue = array('parameters' => array('query')); |
| 48 |
|
| 49 |
/** |
| 50 |
* put your comment there... |
| 51 |
* |
| 52 |
* @var mixed |
| 53 |
*/ |
| 54 |
protected $onqueuereturn = array('parameters' => array('driver')); |
| 55 |
|
| 56 |
/** |
| 57 |
* put your comment there... |
| 58 |
* |
| 59 |
* @var mixed |
| 60 |
*/ |
| 61 |
protected $onselect = array('parameters' => array('param')); |
| 62 |
|
| 63 |
/** |
| 64 |
* put your comment there... |
| 65 |
* |
| 66 |
* @var mixed |
| 67 |
*/ |
| 68 |
private $wpdb = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* put your comment there... |
| 72 |
* |
| 73 |
*/ |
| 74 |
public function __construct($mysqlDriver) { |
| 75 |
// Hookable! |
| 76 |
parent::__construct(); |
| 77 |
// Internal DB engine! |
| 78 |
$this->wpdb = $mysqlDriver; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* put your comment there... |
| 83 |
* |
| 84 |
* @param mixed $query |
| 85 |
*/ |
| 86 |
protected function addQueue($query) { |
| 87 |
$query = $this->resolveTableName($query); |
| 88 |
$key = md5($query); |
| 89 |
if ($query = $this->onqueue($query)) { |
| 90 |
$this->queue[$key] = $query; |
| 91 |
} |
| 92 |
return $this->onqueuereturn($this); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* put your comment there... |
| 97 |
* |
| 98 |
*/ |
| 99 |
public function clear() { |
| 100 |
$this->queue = array(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Put your comments here... |
| 105 |
* |
| 106 |
* |
| 107 |
* @return |
| 108 |
*/ |
| 109 |
public function commit() { |
| 110 |
$this->addQueue('COMMIT;'); |
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* put your comment there... |
| 116 |
* |
| 117 |
* @param mixed $query |
| 118 |
*/ |
| 119 |
public function delete($query) { |
| 120 |
return $this->addQueue($query); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* put your comment there... |
| 125 |
* |
| 126 |
* @param string $data |
| 127 |
* @param mixed $field |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
public function escapeValue($data, $field) { |
| 131 |
switch ($field->numeric) { |
| 132 |
case 0: |
| 133 |
$data = esc_sql($data); |
| 134 |
$data = "'{$data}'"; |
| 135 |
break; |
| 136 |
case 1: |
| 137 |
$data = (int) $data; |
| 138 |
break; |
| 139 |
} |
| 140 |
return $data; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* put your comment there... |
| 145 |
* |
| 146 |
* @param mixed $query |
| 147 |
*/ |
| 148 |
public function exec($query) { |
| 149 |
// Initialize! |
| 150 |
$query = $this->resolveTableName($query); |
| 151 |
$resultSet = array(); |
| 152 |
// Filtering! |
| 153 |
extract($this->onexec(compact('query', 'resultSet'))); |
| 154 |
// filter can controller the returned value or customize the query! |
| 155 |
if ($query && empty($result)) { |
| 156 |
$result = $this->wpdb->query($query); |
| 157 |
} |
| 158 |
return $result; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* put your comment there... |
| 163 |
* |
| 164 |
* @param mixed $table |
| 165 |
*/ |
| 166 |
public function getColumns($table) { |
| 167 |
$columns = array(); |
| 168 |
$this->select("SELECT * FROM {$table} WHERE 1!=1;"); |
| 169 |
// Use field name as element key. |
| 170 |
foreach ($this->wpdb->col_info as $index => $column) { |
| 171 |
$columns[$column->name] = $column; |
| 172 |
} |
| 173 |
return $columns; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* put your comment there... |
| 178 |
* |
| 179 |
*/ |
| 180 |
public function getInsertId () { |
| 181 |
return $this->wpdb->insert_id; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* put your comment there... |
| 186 |
* |
| 187 |
* @param mixed $query |
| 188 |
* @param mixed $returnType |
| 189 |
* @param mixed $default |
| 190 |
*/ |
| 191 |
public function getRow($query, $returnType = OBJECT_K, $default = null) { |
| 192 |
$row = null; |
| 193 |
// Fetch al result set! |
| 194 |
$result = $this->select($query, $returnType); |
| 195 |
if ($result) { |
| 196 |
$row = reset($result); |
| 197 |
} |
| 198 |
return $row; |
| 199 |
} |
| 200 |
/** |
| 201 |
* put your comment there... |
| 202 |
* |
| 203 |
*/ |
| 204 |
public function getTableName($table) { |
| 205 |
return "{$this->wpdb->prefix}{$table}"; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* put your comment there... |
| 210 |
* |
| 211 |
*/ |
| 212 |
public function getVar($query, $row = 0, $column = 0) { |
| 213 |
return $this->wpdb->get_var($query, $row, $column); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* put your comment there... |
| 218 |
* |
| 219 |
* @param mixed $query |
| 220 |
*/ |
| 221 |
public function insert($query) { |
| 222 |
return $this->addQueue($query); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Put your comments here... |
| 227 |
* |
| 228 |
* |
| 229 |
* @return |
| 230 |
*/ |
| 231 |
public function merge($driver) { |
| 232 |
// Put target driver queue at the end of our queue. |
| 233 |
$this->queue = array_merge($this->queue, $driver->queue); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* put your comment there... |
| 238 |
* |
| 239 |
* @param mixed $parameters |
| 240 |
* @param mixed $operators |
| 241 |
* @param mixed $defaultOperator |
| 242 |
* @param mixed $excludeNulls |
| 243 |
*/ |
| 244 |
public function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) { |
| 245 |
$prepared = array(); |
| 246 |
// For every parameter esacape name value. |
| 247 |
foreach ($parameters as $name => $value) { |
| 248 |
if (!$excludeNulls || ($value !== null)) { |
| 249 |
if (array_key_exists($name, $this->fields) === FALSE) { |
| 250 |
throw new Exception("Field:{$name} is not found!"); |
| 251 |
} |
| 252 |
else { |
| 253 |
$field = $this->fields[$name]; |
| 254 |
// Escape field name and value. |
| 255 |
$value = $this->escapeValue($value, $field); |
| 256 |
// Get name-value operator. |
| 257 |
$operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator; |
| 258 |
$prepared[] = "`{$name}`{$operator}{$value}"; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
return $prepared; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* put your comment there... |
| 267 |
* |
| 268 |
*/ |
| 269 |
public function processQueue() { |
| 270 |
$queue = $this->onprocessqueue($this->queue); |
| 271 |
// Collect queue commands. |
| 272 |
foreach ($queue as $command) { |
| 273 |
$this->wpdb->query($this->onprocesscommand($command)); |
| 274 |
} |
| 275 |
// Clear queue. |
| 276 |
$this->clear(); |
| 277 |
// Chain. |
| 278 |
return $this; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* put your comment there... |
| 283 |
* |
| 284 |
* @param mixed $query |
| 285 |
*/ |
| 286 |
public function resolveTableName($query) { |
| 287 |
// Define list of Prefixes to be replaced with Wordpress table prefix! |
| 288 |
$keywords = array('#__wordpress_' => '', '#__cjtoolbox_' => 'cjtoolbox_'); |
| 289 |
// Replace each keyword with Wordpress table prefix |
| 290 |
// + if there is any prefix defined for the keyword itself! |
| 291 |
foreach ($keywords as $search => $prefix) { |
| 292 |
$query = str_replace($search, "{$this->wpdb->prefix}{$prefix}", $query); |
| 293 |
} |
| 294 |
// Return new query with table names resolved. |
| 295 |
return $query; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Put your comments here... |
| 300 |
* |
| 301 |
* |
| 302 |
* @return |
| 303 |
*/ |
| 304 |
public function rollback() { |
| 305 |
$this->addQueue('ROLLBACK;'); |
| 306 |
return $this; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* put your comment there... |
| 311 |
* |
| 312 |
*/ |
| 313 |
public function row($query) { |
| 314 |
return $this->wpdb->get_row($query); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* put your comment there... |
| 319 |
* |
| 320 |
* @param mixed $query |
| 321 |
*/ |
| 322 |
public function select($query, $returnType = OBJECT_K) { |
| 323 |
// Initialize! |
| 324 |
$query = $this->resolveTableName($query); |
| 325 |
$resultSet = array(); |
| 326 |
// Filtering! |
| 327 |
extract($this->onselect(compact('query', 'resultSet'))); |
| 328 |
// filter can controller the returned value or customize the query! |
| 329 |
if ($query && empty($resultSet)) { |
| 330 |
$resultSet = $this->wpdb->get_results($query, $returnType); |
| 331 |
} |
| 332 |
return $resultSet; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Put your comments here... |
| 337 |
* |
| 338 |
* |
| 339 |
* @return |
| 340 |
*/ |
| 341 |
public function startTransaction() { |
| 342 |
$this->addQueue('BEGIN WORK;'); |
| 343 |
return $this; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* put your comment there... |
| 348 |
* |
| 349 |
* @param mixed $query |
| 350 |
*/ |
| 351 |
public function update($query) { |
| 352 |
return $this->addQueue($query); |
| 353 |
} |
| 354 |
|
| 355 |
} // End class. |
| 356 |
|
| 357 |
// Hooking! |
| 358 |
CJTMYSQLQueueDriver::define('CJTMYSQLQueueDriver', array('hookType' => CJTWordpressEvents::HOOK_FILTER)); |