register($config); return self::$instance; } /** * __construct * * @return mixd */ public function __construct($config) { $this->register($config); $this->sql = Sql::getInstance(); } /** * register * * @return mixd */ private function register($config) { # read update if(strpos($config['host'], ',') !== false) { $host = explode(',', $config['host']); $config['host'] = $host[0]; $this->read = Connect::getInstance($config); $config['host'] = $host[1]; $this->update = Connect::getInstance($config); } else { $this->read = $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 = $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 include($file); } $sql = $this->sql->create($this->table, $struct); $this->update->query($sql); $this->log($sql, 'create'); $data['time'] = MAZE_TIME; $data['table'] = $this->table; $data['create'] = $sql; $data['struct'] = array_flip(array_keys($struct)); 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)) { $sql = $this->sql->showIndex($this->table); $handle = $this->update->query($sql); $info = $handle->fetchAll(); if($info) { foreach($info as $k => $v) { if($v['Key_name'] != 'PRIMARY') { $sql = $this->sql->dropIndex($this->table, $v['Key_name']); $this->update->query($sql); } } } $sql = $this->sql->index($this->table, $index); $this->update->query($sql); $this->log($sql, 'index'); $data['index'] = $version; file_put_contents($file, 'file(); if(!file_exists($file)) { return false; } $data = include($file); if($struct) { $sql = $this->sql->alter($this->table, $alter); $this->update->query($sql); $this->log($sql, 'alter'); $data['struct'] = array_flip(array_keys($struct)); file_put_contents($file, 'sql->alter($this->table, $alter[$version]); $this->update->query($sql); $this->log($sql, 'alter'); $data['alter'] = $version; file_put_contents($file, 'file(); if(!file_exists($file)) { return false; } if(isset($value['col']) && isset($value['value'])) { $sql = $this->sql->inserts($this->table, $value['col'], $value['value']); $this->update->query($sql); $this->log($sql, 'inserts'); $data = include($file); $data['insert'] = $sql; file_put_contents($file, 'select($col, 'fetchAll'); if($data && $key) { $result = array(); foreach($data as $k => $v) { 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; } } } return $result; } return $data; } /** * one * * @return array */ public function one($col) { return $this->select($col); } /** * count * * @return array */ public function count($col = 'clear') { return $this->select($col, 'fetchColumn', 'count'); } /** * insert * * @return int */ public function insert() { $sql = $this->sql->insert($this->table); $handle = $this->update->prepare($sql); $handle->execute($this->value); $id = $this->update->id(); $this->log($sql, $this->value); $this->value = array(); return $id; } /** * update * * @return int */ public function update() { $sql = $this->sql->update($this->table); $result = false; if($sql) { $handle = $this->update->prepare($sql); $handle->execute($this->value); $result = $handle->rowCount(); $this->log($sql, $this->value); } $this->value = array(); return $result; } /** * delete * * @return int */ public function delete() { $sql = $this->sql->delete($this->table); $result = false; if($sql) { $handle = $this->update->prepare($sql); $handle->execute($this->value); $result = $handle->rowCount(); $this->log($sql, $this->value); } $this->value = array(); return $result; } /** * select * * @return array */ private function select($col = '', $method = 'fetch', $type = 'select') { $sql = $this->sql->{$type}($this->table, $col); if($type == 'count' && strpos($sql, 'group by `')) { $method = 'fetchAll'; } $handle = $this->read->prepare($sql); $handle->execute($this->value); $data = $handle->$method(); $this->log($sql, $this->value); if($col != 'clear') { $this->value = array(); } return $data; } /** * page * * @return object */ public function page($num, $config = array()) { $this->reset('limit'); 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]) && $method != 'order') { foreach($param[0] as $k => $v) { $this->call($method, $v); } } else { $this->call($method, $param); } return $this; } /** * call * * @return mixd */ private function call($method, $param) { if($method == 'where' || $method == 'set' || $method == 'add') { # 特殊处理in if(isset($param[2]) && $param[2] == 'in') { if(is_string($param[1])) { $param[1] = explode(',', $param[1]); } $prefix = 'in_'; foreach($param[1] as $k => $v) { $k = ':' . $prefix . $k; $key[] = $k; $this->value[$k] = $v; } $param[1] = '(' . implode(',', $key) . ')'; } else { $key = ':' . count($this->value); $this->value[$key] = $param[1]; $param[1] = $key; } } $this->sql->$method($param); } /** * log * * @return log */ private function log($sql, $value) { Debug::log(array('sql' => $sql, 'value' => $value)); } }