| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* MVC_Database |
| 5 |
* |
| 6 |
* @version 1.0 |
| 7 |
* |
| 8 |
* @author Sandi Winter, simplified CodeIgniter DB query builder library |
| 9 |
* @link https://github.com/sandiwinter/winter_mvc |
| 10 |
*/ |
| 11 |
if ( ! class_exists( 'MVC_Database' ) ): |
| 12 |
|
| 13 |
class MVC_Database { |
| 14 |
|
| 15 |
/** |
| 16 |
* wpdb |
| 17 |
* |
| 18 |
* @var object |
| 19 |
*/ |
| 20 |
protected $wpdb = NULL; |
| 21 |
|
| 22 |
protected $query_array = array(); |
| 23 |
|
| 24 |
public $prefix = ''; |
| 25 |
|
| 26 |
public static function instance() |
| 27 |
{ |
| 28 |
static $inst = null; |
| 29 |
if ($inst === null) { |
| 30 |
$inst = new MVC_Database(); |
| 31 |
} |
| 32 |
return $inst; |
| 33 |
} |
| 34 |
|
| 35 |
public function __construct() |
| 36 |
{ |
| 37 |
global $wpdb; |
| 38 |
|
| 39 |
$this->wpdb = &$wpdb; |
| 40 |
|
| 41 |
$this->prefix = $wpdb->prefix; |
| 42 |
} |
| 43 |
|
| 44 |
public function last_query() |
| 45 |
{ |
| 46 |
// Print last SQL query string |
| 47 |
return $this->wpdb->last_query; |
| 48 |
} |
| 49 |
|
| 50 |
public function last_result() |
| 51 |
{ |
| 52 |
// Print last SQL query result |
| 53 |
return $this->wpdb->last_result; |
| 54 |
} |
| 55 |
|
| 56 |
public function results() |
| 57 |
{ |
| 58 |
// Print last SQL query result |
| 59 |
return $this->wpdb->last_result; |
| 60 |
} |
| 61 |
|
| 62 |
public function row() |
| 63 |
{ |
| 64 |
if(!isset($this->wpdb->last_result[0]))return NULL; |
| 65 |
|
| 66 |
// Print last SQL query result |
| 67 |
return $this->wpdb->last_result[0]; |
| 68 |
} |
| 69 |
|
| 70 |
public function last_error() |
| 71 |
{ |
| 72 |
// Print last SQL query error |
| 73 |
return $this->wpdb->last_error; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Select |
| 78 |
* |
| 79 |
* Generates the SELECT portion of the query |
| 80 |
* |
| 81 |
* @param string |
| 82 |
* @param mixed |
| 83 |
*/ |
| 84 |
public function select($select = '', $escape = NULL) |
| 85 |
{ |
| 86 |
$this->query_array['select'][$select] = $escape; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* GROUP BY |
| 91 |
* |
| 92 |
* Sets a flag which tells the query string compiler to add DISTINCT |
| 93 |
* |
| 94 |
* @param string $val - column name |
| 95 |
*/ |
| 96 |
public function group_by($val = TRUE) |
| 97 |
{ |
| 98 |
$this->query_array['group_by'][$val] = NULL; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* DISTINCT |
| 103 |
* |
| 104 |
* Sets a flag which tells the query string compiler to add DISTINCT |
| 105 |
* |
| 106 |
* @param bool $val |
| 107 |
*/ |
| 108 |
public function distinct($val = TRUE) |
| 109 |
{ |
| 110 |
$this->query_array['distinct'][$val] = NULL; |
| 111 |
} |
| 112 |
|
| 113 |
// -------------------------------------------------------------------- |
| 114 |
|
| 115 |
/** |
| 116 |
* From |
| 117 |
* |
| 118 |
* Generates the FROM portion of the query |
| 119 |
* |
| 120 |
* @param mixed $from can be a string or array |
| 121 |
*/ |
| 122 |
public function from($from) |
| 123 |
{ |
| 124 |
$this->query_array['from'][$from] = $from; |
| 125 |
} |
| 126 |
|
| 127 |
// -------------------------------------------------------------------- |
| 128 |
|
| 129 |
/** |
| 130 |
* JOIN |
| 131 |
* |
| 132 |
* Generates the JOIN portion of the query |
| 133 |
* |
| 134 |
* @param string |
| 135 |
* @param string the join condition |
| 136 |
* @param string the type of join |
| 137 |
* @param string whether not to try to escape identifiers |
| 138 |
*/ |
| 139 |
public function join($sql_part, $escape = NULL, $condition = '') |
| 140 |
{ |
| 141 |
$this->query_array['join'][' '.$condition.' JOIN '.$sql_part] = $escape; |
| 142 |
} |
| 143 |
|
| 144 |
// -------------------------------------------------------------------- |
| 145 |
|
| 146 |
/** |
| 147 |
* WHERE |
| 148 |
* |
| 149 |
* Generates the WHERE portion of the query. |
| 150 |
* Separates multiple calls with 'AND'. |
| 151 |
* |
| 152 |
* @param mixed |
| 153 |
* @param mixed |
| 154 |
*/ |
| 155 |
public function where($key, $value = NULL) |
| 156 |
{ |
| 157 |
if(is_array($key)) |
| 158 |
{ |
| 159 |
foreach($key as $key_1=>$val_1) |
| 160 |
{ |
| 161 |
$this->query_array['where'][$key_1] = $val_1; |
| 162 |
} |
| 163 |
|
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if(!is_string($key))return; |
| 168 |
|
| 169 |
$this->query_array['where'][$key] = $value; |
| 170 |
} |
| 171 |
|
| 172 |
// -------------------------------------------------------------------- |
| 173 |
|
| 174 |
/** |
| 175 |
* OR WHERE |
| 176 |
* |
| 177 |
* Generates the WHERE portion of the query. |
| 178 |
* Separates multiple calls with 'OR'. |
| 179 |
* |
| 180 |
* @param mixed |
| 181 |
* @param mixed |
| 182 |
*/ |
| 183 |
public function or_where($sql_part, $escape = NULL) |
| 184 |
{ |
| 185 |
$this->query_array['where']['OR '.$sql_part] = $escape; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* LIKE |
| 190 |
* |
| 191 |
* Generates a %LIKE% portion of the query. |
| 192 |
* Separates multiple calls with 'AND'. |
| 193 |
* |
| 194 |
* @param mixed $field |
| 195 |
* @param string $match |
| 196 |
* @param string $side |
| 197 |
* @param bool $escape |
| 198 |
*/ |
| 199 |
public function like($sql_part, $escape = NULL) |
| 200 |
{ |
| 201 |
$this->query_array['like'][$sql_part] = $escape; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* OR LIKE |
| 207 |
* |
| 208 |
* Generates a %LIKE% portion of the query. |
| 209 |
* Separates multiple calls with 'OR'. |
| 210 |
* |
| 211 |
* @param mixed $field |
| 212 |
* @param string $match |
| 213 |
* @param string $side |
| 214 |
* @param bool $escape |
| 215 |
*/ |
| 216 |
public function or_like($sql_part, $escape = NULL) |
| 217 |
{ |
| 218 |
$this->query_array['like']['OR '.$sql_part] = $escape; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* LIMIT |
| 223 |
* |
| 224 |
* @param int $value LIMIT value |
| 225 |
* @param int $offset OFFSET value |
| 226 |
*/ |
| 227 |
public function limit($limit, $offset = 0) |
| 228 |
{ |
| 229 |
$this->query_array['limit'] = $limit; |
| 230 |
$this->query_array['offset'] = $offset; |
| 231 |
} |
| 232 |
|
| 233 |
// -------------------------------------------------------------------- |
| 234 |
|
| 235 |
/** |
| 236 |
* Sets the OFFSET value |
| 237 |
* |
| 238 |
* @param int $offset OFFSET value |
| 239 |
*/ |
| 240 |
public function offset($offset) |
| 241 |
{ |
| 242 |
$this->query_array['offset'] = $offset; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Sets the ORDER_BY value |
| 247 |
* |
| 248 |
* @param string ORDER_BY value |
| 249 |
*/ |
| 250 |
public function order_by($sql_part) |
| 251 |
{ |
| 252 |
if(empty($this->query_array['order_by'])) |
| 253 |
{ |
| 254 |
$this->query_array['order_by'] = $sql_part; |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
$this->query_array['order_by'] .= ', '.$sql_part; |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
/** |
| 265 |
* Get |
| 266 |
* |
| 267 |
* Compiles the select statement based on the other functions called |
| 268 |
* and runs the query |
| 269 |
* |
| 270 |
* @param string the table |
| 271 |
* @param string the limit clause |
| 272 |
* @param string the offset clause |
| 273 |
* @return CI_DB_result |
| 274 |
*/ |
| 275 |
public function get($table = '', $limit = NULL, $offset = NULL) |
| 276 |
{ |
| 277 |
if ($table !== '') |
| 278 |
{ |
| 279 |
$this->from($table); |
| 280 |
} |
| 281 |
|
| 282 |
if ( ! empty($limit)) |
| 283 |
{ |
| 284 |
$this->limit($limit, $offset); |
| 285 |
} |
| 286 |
|
| 287 |
$result = $this->query($this->_compile_select()); |
| 288 |
$this->_reset_select(); |
| 289 |
return $result; |
| 290 |
} |
| 291 |
|
| 292 |
public function delete($table = '') |
| 293 |
{ |
| 294 |
$sql_query = 'DELETE FROM '.$table.' '; |
| 295 |
|
| 296 |
if(isset($this->query_array['where'])) |
| 297 |
{ |
| 298 |
$sql_query .= 'WHERE '; |
| 299 |
|
| 300 |
foreach($this->query_array['where'] as $col=>$val) |
| 301 |
{ |
| 302 |
if(empty($val)) |
| 303 |
{ |
| 304 |
$sql_query .= ' '.$col.' AND '; |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
if(substr($col, -2) == ' <') |
| 309 |
{ |
| 310 |
$sql_query .= '`'.substr($col, 0, -2).'` < \''.$val.'\' AND '; |
| 311 |
} |
| 312 |
elseif(substr($col, -2) == ' >') |
| 313 |
{ |
| 314 |
$sql_query .= '`'.substr($col, 0, -2).'` > \''.$val.'\' AND '; |
| 315 |
} |
| 316 |
elseif(substr($col, -3) == ' !=') |
| 317 |
{ |
| 318 |
$sql_query .= '`'.substr($col, 0, -3).'` != \''.$val.'\' AND '; |
| 319 |
} |
| 320 |
else |
| 321 |
{ |
| 322 |
$sql_query .= '`'.$col.'` = \''.$val.'\' AND '; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
$sql_query = substr($sql_query, 0, -4); |
| 328 |
} |
| 329 |
|
| 330 |
$result = $this->query($sql_query); |
| 331 |
$this->_reset_select(); |
| 332 |
return $result; |
| 333 |
} |
| 334 |
|
| 335 |
private function _compile_select() |
| 336 |
{ |
| 337 |
$sql_query = ''; |
| 338 |
|
| 339 |
if(isset($this->query_array['select'])) |
| 340 |
{ |
| 341 |
$values_array = array_keys($this->query_array['select']); |
| 342 |
$sql_query .= 'SELECT '.implode(",", $values_array).' '; |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
$sql_query .= 'SELECT * '; |
| 347 |
} |
| 348 |
|
| 349 |
if(isset($this->query_array['from'])) |
| 350 |
{ |
| 351 |
$values_array = array_keys($this->query_array['from']); |
| 352 |
$sql_query .= 'FROM '.implode(",", $values_array).' '; |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
echo 'FROM part missing'; |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
if(isset($this->query_array['join'])) |
| 361 |
{ |
| 362 |
$values_array = array_keys($this->query_array['join']); |
| 363 |
$sql_query .= implode(" ", $values_array).' '; |
| 364 |
} |
| 365 |
|
| 366 |
if(isset($this->query_array['where'])) |
| 367 |
{ |
| 368 |
$sql_query .= 'WHERE '; |
| 369 |
|
| 370 |
foreach($this->query_array['where'] as $col=>$val) |
| 371 |
{ |
| 372 |
if(empty($val)) |
| 373 |
{ |
| 374 |
if(substr($col,0,3) == 'OR ') // remove AND from before if exists |
| 375 |
{ |
| 376 |
if(substr($sql_query, -5) == ' AND ') |
| 377 |
{ |
| 378 |
$sql_query = substr($sql_query, 0, -5); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
$sql_query .= ' '.$col.' AND '; |
| 383 |
} |
| 384 |
else |
| 385 |
{ |
| 386 |
if(substr($col, -2) == ' <') |
| 387 |
{ |
| 388 |
$sql_query .= '`'.substr($col, 0, -2).'` < \''.$val.'\' AND '; |
| 389 |
} |
| 390 |
elseif(substr($col, -2) == ' >') |
| 391 |
{ |
| 392 |
$sql_query .= '`'.substr($col, 0, -2).'` > \''.$val.'\' AND '; |
| 393 |
} |
| 394 |
elseif(substr($col, -3) == ' !=') |
| 395 |
{ |
| 396 |
$sql_query .= '`'.substr($col, 0, -3).'` != \''.$val.'\' AND '; |
| 397 |
} |
| 398 |
else |
| 399 |
{ |
| 400 |
$sql_query .= '`'.$col.'` = \''.$val.'\' AND '; |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
$sql_query = substr($sql_query, 0, -4); |
| 406 |
} |
| 407 |
|
| 408 |
if(isset($this->query_array['like'])) |
| 409 |
{ |
| 410 |
if(strpos($sql_query, 'WHERE') === FALSE) |
| 411 |
{ |
| 412 |
$sql_query .= 'WHERE '; |
| 413 |
} |
| 414 |
else |
| 415 |
{ |
| 416 |
$sql_query .= ' AND '; |
| 417 |
} |
| 418 |
|
| 419 |
foreach($this->query_array['like'] as $col=>$val) |
| 420 |
{ |
| 421 |
if(empty($val)) |
| 422 |
{ |
| 423 |
$sql_query .= ' '.$col.' AND '; |
| 424 |
} |
| 425 |
else |
| 426 |
{ |
| 427 |
$sql_query .= '`'.$col.'` LIKE \''.$val.'\' AND '; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
$sql_query = substr($sql_query, 0, -4); |
| 432 |
} |
| 433 |
|
| 434 |
if(isset($this->query_array['group_by'])) |
| 435 |
{ |
| 436 |
$sql_query .= 'GROUP BY '.implode(',', array_keys($this->query_array['group_by'])).' '; |
| 437 |
} |
| 438 |
|
| 439 |
if(isset($this->query_array['order_by'])) |
| 440 |
{ |
| 441 |
$sql_query .= 'ORDER BY '.$this->query_array['order_by'].' '; |
| 442 |
} |
| 443 |
|
| 444 |
if(isset($this->query_array['limit'])) |
| 445 |
{ |
| 446 |
$sql_query .= 'LIMIT '.$this->query_array['limit'].' '; |
| 447 |
} |
| 448 |
|
| 449 |
if(isset($this->query_array['offset']) && isset($this->query_array['limit'])) |
| 450 |
{ |
| 451 |
$sql_query .= 'OFFSET '.$this->query_array['offset'].' '; |
| 452 |
} |
| 453 |
|
| 454 |
if(isset($this->query_array['distinct'])) { |
| 455 |
$pos = strpos($sql_query, 'SELECT'); |
| 456 |
if ($pos !== false) { |
| 457 |
$sql_query = substr_replace($sql_query, 'SELECT DISTINCT '.implode(',', array_keys($this->query_array['distinct'])).',', $pos, strlen('SELECT')); |
| 458 |
} |
| 459 |
} |
| 460 |
//wmvc_dump($sql_query); |
| 461 |
|
| 462 |
return $sql_query; |
| 463 |
} |
| 464 |
|
| 465 |
private function _reset_select() |
| 466 |
{ |
| 467 |
$this->query_array = array(); |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Determines if a query is a "write" type. |
| 472 |
* |
| 473 |
* @param string An SQL query string |
| 474 |
* @return bool |
| 475 |
*/ |
| 476 |
public function is_write_type($sql) |
| 477 |
{ |
| 478 |
return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX|MERGE)\s/i', $sql); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Query |
| 483 |
* |
| 484 |
* @param string the sql query |
| 485 |
* @return mixed |
| 486 |
*/ |
| 487 |
public function query($sql) |
| 488 |
{ |
| 489 |
return $this->_execute($sql); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
/** |
| 494 |
* Execute the query |
| 495 |
* |
| 496 |
* @param string $sql an SQL query |
| 497 |
* @return mixed |
| 498 |
*/ |
| 499 |
protected function _execute($sql) |
| 500 |
{ |
| 501 |
global $wpdb; |
| 502 |
return $wpdb->query($sql); |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Number of rows in the result set |
| 507 |
* |
| 508 |
* @return int |
| 509 |
*/ |
| 510 |
public function num_rows() |
| 511 |
{ |
| 512 |
global $wpdb; |
| 513 |
return $wpdb->num_rows; |
| 514 |
} |
| 515 |
|
| 516 |
public function list_fields($table_name) |
| 517 |
{ |
| 518 |
$sql = 'SHOW COLUMNS FROM '.$table_name.';'; |
| 519 |
|
| 520 |
$this->_execute($sql); |
| 521 |
|
| 522 |
$results = $this->results(); |
| 523 |
|
| 524 |
$fields = array(); |
| 525 |
|
| 526 |
foreach($results as $row) |
| 527 |
{ |
| 528 |
$fields[$row->Field] = $row; |
| 529 |
} |
| 530 |
|
| 531 |
return $fields; |
| 532 |
} |
| 533 |
|
| 534 |
//UPDATE MyGuests SET lastname='Doe' WHERE id=2 |
| 535 |
public function update($table_name, $data = array(), $id = NULL, $primary_field = 'id') |
| 536 |
{ |
| 537 |
if(count($data) == 0)return; |
| 538 |
if(empty($id))return; |
| 539 |
if(empty($table_name))return; |
| 540 |
if(empty($primary_field))return; |
| 541 |
|
| 542 |
$query = ''; |
| 543 |
$query.= 'UPDATE '.$table_name.' SET '; |
| 544 |
|
| 545 |
foreach($data as $key=>$val) |
| 546 |
{ |
| 547 |
if($val === NULL) |
| 548 |
{ |
| 549 |
$query.= ' `'.$key.'` = NULL ,'; |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
$query.= ' `'.$key.'` = \''.$val.'\' ,'; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
$query = substr($query, 0, -1); |
| 558 |
|
| 559 |
if(is_array($id)) |
| 560 |
{ |
| 561 |
$where_part = array(); |
| 562 |
foreach($id as $key=>$val) |
| 563 |
{ |
| 564 |
$where_part[] = ' '.$key.' = \''.$val.'\' '; |
| 565 |
} |
| 566 |
|
| 567 |
$query.= 'WHERE '.join(' AND ', $where_part).';'; |
| 568 |
} |
| 569 |
else |
| 570 |
{ |
| 571 |
$query.= 'WHERE '.$primary_field.'='.$id.';'; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
//echo $query;exit(); |
| 576 |
|
| 577 |
$this->query($query); |
| 578 |
$this->_reset_select(); |
| 579 |
|
| 580 |
return $id; |
| 581 |
} |
| 582 |
|
| 583 |
public function insert($table_name, $data) |
| 584 |
{ |
| 585 |
global $wpdb; |
| 586 |
|
| 587 |
if(empty($table_name)) |
| 588 |
{ |
| 589 |
echo 'Missing table name for insert'; |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
if(!is_array($data) || count($data) == 0) |
| 594 |
{ |
| 595 |
echo 'Missing data for insert'; |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
$wpdb->insert( |
| 600 |
$table_name, |
| 601 |
$data |
| 602 |
); |
| 603 |
|
| 604 |
return $wpdb->insert_id; |
| 605 |
} |
| 606 |
|
| 607 |
/* |
| 608 |
|
| 609 |
UPDATE mytable SET title = CASE |
| 610 |
WHEN id = 1 THEN ‘Great Expectations’ |
| 611 |
WHEN id = 2 THEN ‘War and Peace’ |
| 612 |
... |
| 613 |
END |
| 614 |
WHERE id IN (1,2,...) |
| 615 |
|
| 616 |
*/ |
| 617 |
|
| 618 |
public function updateBatch( $table_name, $values, $index) |
| 619 |
{ |
| 620 |
$ids = []; |
| 621 |
$final = []; |
| 622 |
|
| 623 |
foreach ($values as $val) |
| 624 |
{ |
| 625 |
$ids[] = $val[$index]; |
| 626 |
|
| 627 |
foreach (array_keys($val) as $field) |
| 628 |
{ |
| 629 |
if ($field !== $index) |
| 630 |
{ |
| 631 |
$final[$field][] = 'WHEN ' . $index . ' = ' . $val[$index] . ' THEN ' . $val[$field]; |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
$cases = ''; |
| 637 |
foreach ($final as $k => $v) |
| 638 |
{ |
| 639 |
$cases .= $k . " = CASE \n" |
| 640 |
. implode("\n", $v) . "\n" |
| 641 |
. 'ELSE ' . $k . ' END, '; |
| 642 |
} |
| 643 |
|
| 644 |
$this->where($index . ' IN(' . implode(',', $ids) . ')', null, false); |
| 645 |
|
| 646 |
$query = 'UPDATE ' . '' . $table_name . ' SET ' . substr($cases, 0, -2) . ''; |
| 647 |
|
| 648 |
$this->query($query); |
| 649 |
$this->_reset_select(); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
} |
| 655 |
|
| 656 |
endif; |
| 657 |
|
| 658 |
?> |
| 659 |
|