register($config); return self::$instance; } /** * __construct * * @return mixd */ public function __construct($config) { $this->register($config); } /** * register * * @return mixd */ private function register($config) { $this->connect = $this->update = Connect::getInstance($config); } /** * table * * @return object */ public function table($table) { if(defined('MAZE_PROJECT') && MAZE_PROJECT != 'default') { $table = MAZE_PROJECT . '_' . $table; } $this->table = $this->connect->table($table); return $this; } /** * file * * @return mixed */ private function file() { //$file = Path::create(MAZE_PATH . 'data/database/', $this->table . '.php'); $file = Path::create(MAZE_PATH . 'data/database/', $this->table); return $file; } /** * create * * @return mixed */ public function create($struct) { if(isset(Config::$global['base']['create'])) { return false; } $file = $this->file(); if(file_exists($file)) { return false; } $data['time'] = MAZE_TIME; $data['table'] = $this->table; $data['create'] = $struct; $this->log($data); file_put_contents($file, 'file(); if(!file_exists($file)) { return false; } $data = include($file); if(isset($index['version'])) { $version = $index['version']; unset($index['version']); } else { $version = 1; } if(empty($data['index']) || (isset($data['index']) && $data['index'] < $version)) { $return = $this->table->ensureIndex($index, array('name' => '')); $this->log($index); $data['index'] = $version; file_put_contents($file, 'file(); if(!file_exists($file)) { return false; } if(isset($value['col']) && isset($value['value'])) { $col = explode(',', $value['col']); $value = explode(',', $value['value']); foreach($col as $k => $v) { $this->value['add'][$v] = $value[$k]; $this->insert(); } $this->log($value); $data = include($file); $data['insert'] = $value; file_put_contents($file, 'select($col, 'find'); $result = array(); if($data) { foreach($data as $k => $v) { $v['id'] = (array) $v['_id']; $v['id'] = $v['id']['$id']; if(isset($v[$key])) { if(isset($array[3]) && isset($v[$array[2]])) { $result[$v[$key]][$v[$array[2]]] = $v; } elseif(isset($array[2]) && isset($v[$array[2]])) { $result[$v[$key]] = $v[$array[2]]; } elseif(isset($array[2])) { $result[$v[$key]][] = $v; } else { $result[$v[$key]] = $v; } } else { $result[] = $v; } } } return $result; } /** * one * * @return array */ public function one($col = '') { $data = $this->select($col, 'findOne'); if($data) { $data['id'] = (array) $data['_id']; $data['id'] = $data['id']['$id']; } return $data; } /** * count * * @return array */ public function count($col = 'clear') { return $this->select($col, 'count'); } /** * insert * * @return int */ public function insert() { $insert = $this->value['add']; $return = $this->table->insert($insert); $this->log($this->value); $this->value = array(); return $insert['_id']; } /** * update * * @return int */ public function update() { $method = '$set'; $return = $this->table->update($this->value['where'], array($method => $this->value['set'])); $this->log($this->value); $this->value = array(); return $return; } /** * delete * * @return int */ public function delete() { $this->update('$unset'); return $result; } /** * select * * @return array */ private function select($col = '', $method = 'find') { if(isset($this->value['where'])) { //print_r($this->value['where']); $return = $cursor = $this->table->$method($this->value['where']); } else { $return = $cursor = $this->table->$method(); } if($method != 'count') { if(isset($this->value['order'])) { $return = $cursor->sort($this->value['order']); } if(isset($this->value['limit'])) { foreach($this->value['limit'] as $k => $v) { $return = $cursor->limit($k)->skip($v); } } if($col && $col != '*' && $col != 'clear') { if(is_string($col)) { $temp = explode(',', $col); $col = array(); foreach($temp as $k => $v) { $col[$v] = true; } } $return = $cursor->fields($col); } } $this->log($this->value); if($col != 'clear') { $this->value = array(); } return $return; } /** * page * * @return object */ public function page($num, $config = array()) { empty($config[0]) && $config[0] = 'list'; empty($config[1]) && $config[1] = 'current'; empty($config[2]) && $config[2] = ''; $page = Page::getInstance($config[1]); $page->template($config[0]); $page->link($config[2]); $page->total($this->count()); $this->limit($num, $page->offset($num)); return $this; } /** * __call * * @return object */ public function __call($method, $param) { if(is_array($param[0])) { foreach($param[0] as $k => $v) { if($method == 'order') { $this->call($method, array($k, $v)); } else { $this->call($method, $v); } } } else { $this->call($method, $param); } return $this; } /** * call * * @return mixd */ private function call($method, $param) { if(is_array($param) && isset($param[0])) { if(is_string($param[0]) && strpos($param[0], ',')) { $temp = explode(',', str_replace('`', '', $param[0])); $index = count($temp)-1; foreach($temp as $k => $v) { if($k == $index) { $param[0] = $v; } else { $array = explode(' ', $v); $this->call($method, $array); } } } if($param[0] == 'id') { $param[0] = '_id'; } $func = 'convert_' . $method; if(method_exists($this, $func)) { $this->$func($param); } $this->value[$method][$param[0]] = $param[1]; } else { $this->value[$method] = $param; } } /** * convert_order * * @return mixed */ private function convert_order(&$param) { switch($param[1]) { case 'desc': $param[1] = -1; break; case 'asc': $param[1] = 1; break; } } /** * convert_group * * @return mixed */ private function convert_group(&$param) { print_r($param);die; } /** * convert_where * * @return mixed */ private function convert_where(&$param) { if(isset($param[2])) { $state = true; switch($param[2]) { case 'like': # 模糊查询 if(strpos($param[1], '%') !== false) { $param[1] = str_replace('%', '(.*?)', $param[1]); $param[1] = new \MongoRegex('/'.$param[1].'/i'); } else { $param[1] = new \MongoRegex('/(.*?)'.$param[1].'(.*?)/i'); } $state = false; break; case 'in': case 'nin': # in查询 $param[1] = explode(',', $param[1]); if($param[0] == '_id') { foreach($param[1] as $k => $v) { $param[1][$k] = new \MongoId($v); } } $param[2] = '$' . $param[2]; break; case '>': $param[2] = '$gt'; break; case '>=': $param[2] = '$gte'; break; case '<': $param[2] = '$lt'; break; case '<=': $param[2] = '$lte'; break; case '!=': $param[2] = '$ne'; break; case '%': $param[2] = '$mod'; break; case 'bt': $state = false; $param[1] = array('gt' => $param[1][0], 'lt' => $param[1][1]); break; case 'bte': $state = false; $param[1] = array('gte' => $param[1][0], 'lte' => $param[1][1]); break; default: $param[2] = '$' . $param[2]; break; } if($state == true) { $param[1] = array($param[2] => $param[1]); } } if($param[0] == '_id' && is_string($param[1])) { $param[1] = new \MongoId($param[1]); } } /** * log * * @return log */ private function log($value) { Debug::log(array('value' => $value)); } }