Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Query {
- protected $db;
- protected $table;
- protected $join = array();
- protected $where = array();
- protected $order = array();
- protected $limit;
- protected $offset;
- protected $values = array();
- public function __construct($table)
- {
- $this->db = DB::db();
- $this->table = $table;
- }
- public function select($cols = '*', $alias = null)
- {
- $sql = "SELECT {$cols} FROM " .
- $this->table . ($alias ? " AS $alias" : "") .
- $this->getJoin() .
- $this->getWhere() .
- $this->getOrder() .
- $this->getLimit();
- // print dump($this);
- $stmt = $this->db->prepare($sql);
- $stmt->execute($this->values);
- return $stmt;
- }
- public function row($cols = '*', $alias = null)
- {
- $stmt = $this->select($cols, $alias);
- return $stmt->fetch(PDO::FETCH_ASSOC);
- }
- public function rows($cols = '*', $alias = null)
- {
- $stmt = $this->select($cols, $alias);
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- public function one($field, $colnum = 0)
- {
- $stmt = $this->select($field);
- return $stmt->fetchColumn($colnum);
- }
- public function count($field = '*')
- {
- $stmt = $this->select("COUNT($field)");
- return $stmt->fetchColumn($colnum);
- }
- public function insert($row)
- {
- $row = (array) $row;
- $sql = 'INSERT INTO ' . $this->table;
- $values = array_values($row);
- if ($values != $row) {
- $sql .= ' (' . join(',', array_keys($row)) . ')';
- }
- $sql .= ' VALUES(' . rtrim(str_repeat('?,', count($row)), ',') . ')';
- $stmt = $this->db->prepare($sql);
- $stmt->execute($values);
- return $this->db->lastInsertId();
- }
- public function update($data)
- {
- $data = (array) $data;
- $sql = "UPDATE {$this->table} SET ";
- $parts = array();
- foreach (array_keys($data) as $v) {
- $parts[] = "$v=?";
- }
- $sql .= implode(',', $parts) .
- $this->getWhere();
- $values = array_merge(array_values($data), $this->values);
- $stmt = $this->db->prepare($sql);
- $stmt->execute($values);
- return $stmt->rowCount();
- }
- public static function delete()
- {
- $sql = 'DELETE FROM ' .
- $this->table .
- $this->getWhere();
- $stmt = $this->db->prepare($sql);
- $stmt->execute($this->values);
- return $stmt->rowCount();
- }
- public function join($table, $alias, $field1, $field2, $type = null) {
- $this->join[] = ($type ? "$type " : "") . "JOIN $table AS $alias ON $field1=$field2";
- return $this;
- }
- public function where($field, $val, $op = '=')
- {
- $this->where[] = "$field $op ?";
- $this->values[] = $val;
- return $this;
- }
- public function order($field, $type = null)
- {
- $this->order[] = $field . ($type ? " $type" : "");
- return $this;
- }
- public function limit($limit, $offset = null)
- {
- $this->limit = $limit;
- $this->offset = $offset;
- return $this;
- }
- public function page($page, $per_page)
- {
- $page = (int) $page;
- $per_page = (int) $per_page;
- $offset = $per_page * $page - $per_page;
- $this->limit($per_page, $offset);
- return $this;
- }
- protected function getJoin()
- {
- return count($this->join) ? ' ' . implode(' ', $this->join) : '';
- }
- protected function getWhere()
- {
- return count($this->where) ? ' WHERE ' . implode(' AND ', $this->where) : '';
- }
- protected function getOrder()
- {
- return count($this->order) ? ' ORDER BY ' . implode(',', $this->order) : '';
- }
- protected function getLimit()
- {
- $l = (int) $this->limit;
- $o = (int) $this->offset;
- return $l || $o ? " LIMIT " . ($o ? "$o, " : "") . $l : "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement