Advertisement
stuppid_bot

Untitled

Jun 13th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. <?php
  2.  
  3. class DB_Query {
  4.     protected $table;
  5.     protected $columns;
  6.     protected $joins;
  7.     protected $conditions;
  8.     protected $order;
  9.     protected $offset;
  10.     protected $limit;
  11.     protected $values;
  12.  
  13.     public function __construct($table, $columns = null) {
  14.         $this->table = $table;
  15.         $this->columns($columns);
  16.     }
  17.  
  18.     public function columns($columns) {
  19.         $this->columns = $columns;
  20.     }
  21.  
  22.     public function where($field, $value, $op = '=') {
  23.         $this->conditions[] = "$field $op ?";
  24.         $this->values[] = $value;
  25.         return $this;
  26.     }
  27.  
  28.     public function like($field, $value) {
  29.         return $this->where($field, "%$value%", "LIKE");
  30.     }
  31.  
  32.     public function orderBy($order) {
  33.         $this->order = $order;
  34.         return $this;
  35.     }
  36.  
  37.     public function join($table, $alias, $condition, $type = null) {
  38.         $this->joins[] = ($type ? "$type " : "") . "JOIN $table AS $alias ON $condition";
  39.     }
  40.  
  41.     public function limit($offset, $limit) {
  42.         $this->offset = (int) $offset;
  43.         $this->limit = (int) $limit;
  44.     }
  45.  
  46.     public function page($page, $per_page) {
  47.         $page = (int) $page;
  48.         $per_page = (int) $per_page;
  49.         $offset = $per_page * $page - $per_page;
  50.         $this->limit($offset, $per_page);
  51.         return $this;
  52.     }
  53.  
  54.     public function execute() {
  55.         $t = $this;
  56.         $sql = 'SELECT ' . (is_null($t->columns) ? '*' : $t->columns) . ' FROM ' . $t->table;
  57.         if ($t->joins) {
  58.             $sql .= ' ' . implode(' ', $t->joins);
  59.         }
  60.         if ($t->conditions) {
  61.             $sql .= ' WHERE ' . implode(' AND ', $t->conditions);
  62.         }
  63.         if ($t->order) {
  64.             $sql .= " ORDER BY {$t->order}";
  65.         }
  66.         if ($t->limit) {
  67.             $sql .= " LIMIT {$t->offset},{$t->limit}";
  68.         }
  69.         // echo $sql . '<br>';
  70.         $stm = db::prepare($sql);
  71.         $stm->execute($t->values);
  72.         return $stm;
  73.     }
  74.  
  75.     public function reset() {
  76.         foreach (array('columns', 'joins', 'conditions', 'order', 'offset', 'limit', 'values') as $p) {
  77.             $this->{$p} = null;
  78.         }
  79.     }
  80.  
  81.     public function __call($method, $args) {
  82.         if (preg_match('/^fetch(all|column|object)?$/i', $method)) {
  83.             $stm = $this->execute();
  84.             $callback = array($stm, $method);
  85.             return call_user_func_array($callback, $args);
  86.         }
  87.         throw new BadMethodCallException("Method $method not exists");
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement