| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace WY\app\libs;
- use WY\app\Config;
- use WY\app\libs\Db;
- use WY\app\libs\Res;
- use WY\app\libs\Req;
- use WY\app\libs\Log;
- if (!defined('WY_ROOT')) {
- exit;
- }
- class Model
- {
- public $limits = '';
- public $limit = false;
- public $offset = false;
- public $orderby = '';
- public $where = [];
- public $fields = '';
- public $insertData = [];
- public $updateSet = [];
- private $db;
- public $in = false;
- public $left = '';
- public $right = '';
- public $inner = '';
- public $join = '';
- public $on = '';
- public $groupby = '';
- function __construct()
- {
- $this->prefix = Config::db()['prefix'];
- $this->db = Db::getInstance();
- $this->db->connect();
- $this->res = new Res();
- $this->req = new Req();
- }
- function select($fields = '*')
- {
- $this->fields = $fields;
- return $this;
- }
- function from($table)
- {
- $this->table = $this->prefix . $table;
- return $this;
- }
- function limit($limit)
- {
- $this->limit = $limit;
- return $this;
- }
- function offset($offset)
- {
- $this->offset = $offset;
- return $this;
- }
- function orderby($orderby)
- {
- if ($orderby) {
- $this->orderby = 'ORDER BY ' . $orderby;
- }
- return $this;
- }
- function groupby($groupby)
- {
- $this->groupby = 'GROUP BY ' . $groupby;
- return $this;
- }
- function where($where)
- {
- $this->where = $where;
- return $this;
- }
- function sums($fields)
- {
- $this->sums = $fields;
- return $this;
- }
- function in()
- {
- if ($this->where) {
- $this->in = true;
- }
- return $this;
- }
- function on($on)
- {
- $this->on = $on;
- return $this;
- }
- function join()
- {
- if ($this->left) {
- $this->join = 'LEFT JOIN ' . $this->left . ' ON ' . $this->on;
- }
- if ($this->right) {
- $this->join = 'RIGHT JOIN ' . $this->right . ' ON ' . $this->on;
- }
- if ($this->inner) {
- $this->join = 'INNER JOIN ' . $this->inner . ' ON ' . $this->on;
- }
- return $this;
- }
- function left($table)
- {
- $this->left = $this->prefix . $table;
- return $this;
- }
- function inner($table)
- {
- $this->inner = $this->prefix . $table;
- return $this;
- }
- function right($table)
- {
- $this->right = $this->prefix . $table;
- return $this;
- }
- function toSql()
- {
- if (false !== $this->limit) {
- if (false !== $this->offset) {
- $this->limits = 'LIMIT ' . $this->offset . ',' . $this->limit;
- } else {
- $this->limits = 'LIMIT ' . $this->limit;
- }
- }
- $sql = "select " . $this->fields . " from " . $this->table . " " . $this->join . " {where} " . $this->groupby . " " . $this->orderby . " " . $this->limits;
- return $sql;
- }
- function fetchAll()
- {
- $sql = $this->toSql();
-
- $data = array();
- if ($this->where) {
- $data = $this->db->parseWhere($this->where);
- $sql = str_replace('{where}', $data['where'], $sql);
- } else {
- $sql = str_replace('{where}', '', $sql);
- }
- $stm = $this->db->prepare($sql);
- if ($data) {
- $this->db->bindValue($stm, $data['values']);
- }
- $this->db->execute($stm);
- if ($stm->rowCount()) {
- return $this->db->fetchAll($stm);
- }
- return false;
- }
- function fetchRow()
- {
- $sql = $this->toSql();
- $data = array();
- if ($this->where) {
- $data = $this->db->parseWhere($this->where);
- $sql = str_replace('{where}', $data['where'], $sql);
- } else {
- $sql = str_replace('{where}', '', $sql);
- }
- $stm = $this->db->prepare($sql);
- if ($data) {
- $this->db->bindValue($stm, $data['values']);
- }
- $this->db->execute($stm);
- if ($stm->rowCount()) {
- return $this->db->fetchRow($stm);
- }
- return false;
- }
- function insertData(array $insertData)
- {
- $this->insertData = $insertData;
- return $this;
- }
- function updateSet(array $updateSet)
- {
- $this->updateSet = $updateSet;
- return $this;
- }
- function insert()
- {
- if (!$this->insertData) {
- return false;
- }
- $result = $this->db->insert($this->table, $this->insertData);
- return $result;
- }
- function count()
- {
- $result = $this->db->count($this->table, $this->where);
- return $result;
- }
- function sum()
- {
- $result = $this->db->sum($this->table, $this->fields, $this->where);
- return $result;
- }
- function delete()
- {
- if ($this->in) {
- $result = $this->db->deleteIn($this->table, $this->where);
- } else {
- $result = $this->db->delete($this->table, $this->where);
- }
- return $result;
- }
- function update()
- {
- if (!$this->updateSet) {
- return false;
- }
-
- $result = $this->db->update($this->table, $this->updateSet, $this->where);
- return $result;
- }
- protected function model()
- {
- return new Model();
- }
- }
- ?>
|