Advertisement
stuppid_bot

Untitled

Jun 7th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2.  
  3. class Query {
  4.     protected $db;
  5.     protected $table;
  6.     protected $join = array();
  7.     protected $where = array();
  8.     protected $order = array();
  9.     protected $limit;
  10.     protected $offset;
  11.     protected $values = array();
  12.  
  13.     public function __construct($table)
  14.     {
  15.         $this->db = DB::db();
  16.         $this->table = $table;
  17.     }
  18.  
  19.     public function select($cols = '*', $alias = null)
  20.     {
  21.         $sql = "SELECT {$cols} FROM " .
  22.             $this->table . ($alias ? " AS $alias" : "") .
  23.             $this->getJoin() .
  24.             $this->getWhere() .
  25.             $this->getOrder() .
  26.             $this->getLimit();
  27.         // print dump($this);
  28.         $stmt = $this->db->prepare($sql);
  29.         $stmt->execute($this->values);
  30.         return $stmt;
  31.     }
  32.  
  33.     public function row($cols = '*', $alias = null)
  34.     {
  35.         $stmt = $this->select($cols, $alias);
  36.         return $stmt->fetch(PDO::FETCH_ASSOC);
  37.     }
  38.  
  39.     public function rows($cols = '*', $alias = null)
  40.     {
  41.         $stmt = $this->select($cols, $alias);
  42.         return $stmt->fetchAll(PDO::FETCH_ASSOC);
  43.     }
  44.  
  45.     public function one($field, $colnum = 0)
  46.     {
  47.         $stmt = $this->select($field);
  48.         return $stmt->fetchColumn($colnum);
  49.     }
  50.  
  51.     public function count($field = '*')
  52.     {
  53.         $stmt = $this->select("COUNT($field)");
  54.         return $stmt->fetchColumn($colnum);
  55.     }
  56.  
  57.     public function insert($row)
  58.     {
  59.         $row = (array) $row;
  60.         $sql = 'INSERT INTO ' . $this->table;
  61.         $values = array_values($row);
  62.  
  63.         if ($values != $row) {
  64.             $sql .= ' (' . join(',', array_keys($row)) . ')';
  65.         }
  66.  
  67.         $sql .= ' VALUES(' . rtrim(str_repeat('?,', count($row)), ',') . ')';
  68.         $stmt = $this->db->prepare($sql);
  69.         $stmt->execute($values);
  70.         return $this->db->lastInsertId();
  71.     }
  72.  
  73.     public function update($data)
  74.     {
  75.         $data = (array) $data;
  76.         $sql = "UPDATE {$this->table} SET ";
  77.         $parts = array();
  78.  
  79.         foreach (array_keys($data) as $v) {
  80.             $parts[] = "$v=?";
  81.         }
  82.  
  83.         $sql .= implode(',', $parts) .
  84.             $this->getWhere();
  85.         $values = array_merge(array_values($data), $this->values);
  86.         $stmt = $this->db->prepare($sql);
  87.         $stmt->execute($values);
  88.         return $stmt->rowCount();
  89.     }
  90.  
  91.     public static function delete()
  92.     {
  93.         $sql = 'DELETE FROM ' .
  94.             $this->table .
  95.             $this->getWhere();
  96.         $stmt = $this->db->prepare($sql);
  97.         $stmt->execute($this->values);
  98.         return $stmt->rowCount();
  99.     }
  100.  
  101.     public function join($table, $alias, $field1, $field2, $type = null) {
  102.         $this->join[] = ($type ? "$type " : "") . "JOIN $table AS $alias ON $field1=$field2";
  103.         return $this;
  104.     }
  105.  
  106.     public function where($field, $val, $op = '=')
  107.     {
  108.         $this->where[] = "$field $op ?";
  109.         $this->values[] = $val;
  110.         return $this;
  111.     }
  112.  
  113.     public function order($field, $type = null)
  114.     {
  115.         $this->order[] = $field . ($type ? " $type" : "");
  116.         return $this;
  117.     }
  118.  
  119.     public function limit($limit, $offset = null)
  120.     {
  121.         $this->limit = $limit;
  122.         $this->offset = $offset;
  123.         return $this;
  124.     }
  125.  
  126.     public function page($page, $per_page)
  127.     {
  128.         $page = (int) $page;
  129.         $per_page = (int) $per_page;
  130.         $offset = $per_page * $page - $per_page;
  131.         $this->limit($per_page, $offset);
  132.         return $this;
  133.     }
  134.  
  135.     protected function getJoin()
  136.     {
  137.         return count($this->join) ? ' ' . implode(' ', $this->join) : '';
  138.     }
  139.  
  140.     protected function getWhere()
  141.     {
  142.         return count($this->where) ? ' WHERE ' . implode(' AND ', $this->where) : '';
  143.     }
  144.  
  145.     protected function getOrder()
  146.     {
  147.         return count($this->order) ? ' ORDER BY ' . implode(',', $this->order) : '';
  148.     }
  149.  
  150.     protected function getLimit()
  151.     {
  152.         $l = (int) $this->limit;
  153.         $o = (int) $this->offset;
  154.         return $l || $o ? " LIMIT " . ($o ? "$o, " : "") . $l : "";
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement