rabin 1 年之前
父节点
当前提交
9a1ba458f7

+ 14 - 15
dever.php

@@ -3,6 +3,11 @@ class Dever
 {
     public static $data = array();
     private static $define = array();
+    public static $string = 'Dever\\String\\Helper';
+    public static $date = 'Dever\\Support\\Date';
+    public static $env = 'Dever\\String\\Env';
+    public static $path = 'Dever\\Path';
+    public static $page = 'Dever\\Paginator';
     public static function run()
     {
         $route = Dever\Route::get();
@@ -42,14 +47,14 @@ class Dever
         }
         return self::$data[$index]->loadDevelop();
     }
-    public static function db($table, $app = '', $path = 'table')
+    public static function db($table, $app = '', $store = 'default', $partition = false, $path = 'table')
     {
         if (!$app) {
             $app = DEVER_APP_NAME;
         }
         $index = $app . $path . $table;
         if (empty(self::$data[$index])) {
-            self::$data[$index] = new Dever\Model($table, $app, $path);
+            self::$data[$index] = new Dever\Model($table, $app, $store, $partition, $path);
         }
         return self::$data[$index];
     }
@@ -101,22 +106,16 @@ class Dever
     {
         return Dever\Session::oper(...func_get_args());
     }
-    public static function string()
-    {
-        return Dever\String\Helper;
-    }
-    public static function env()
-    {
-        return Dever\String\Env;
-    }
-    public static function path()
-    {
-        return Dever\Path;
-    }
     public static function shell($value)
     {
         return Dever::check(Dever\Route::input('shell'), $value);
     }
+    public static function store($store = 'default', $partition = false)
+    {
+        $setting = Dever\Config::get('setting')['database'][$store];
+        $class = 'Dever\\Store\\' . $setting['type'];
+        return $class::getInstance($store, $setting, $partition);
+    }
     public static function check($var, $find)
     {
         if (is_array($var)) {
@@ -128,7 +127,7 @@ class Dever
     }
     public static function json_encode($value)
     {
-        $value = json_encode($value, JSON_UNESCAPED_UNICODE + JSON_FORCE_OBJECT);
+        $value = json_encode($value, JSON_UNESCAPED_UNICODE);
         return $value;
     }
     public static function json_decode($value)

+ 29 - 44
src/Dever/Debug.php

@@ -2,7 +2,7 @@
 use Dever;
 class Debug
 {
-    private static $data = array();
+    private static $data;
     private static $trace;
     private static $tool;
     private static $time;
@@ -26,15 +26,15 @@ class Debug
     public static function add($msg, $type = 'log')
     {
         if (self::$shell) {
-            $format = array('msg' => $msg);
+            $format = is_array($msg) ? $msg : array('msg' => $msg);
             $format = array_merge($format, self::env());
             self::data($type, Output::format($format));
         }
     }
-    public static function out($data)
+    public static function out($data, $type = 'out')
     {
         if (self::$shell) {
-            self::add($data, 'data');
+            self::add($data, 'out');
             self::runtime();
             $handler = self::$tool->getHandlers();
             foreach (self::$data as $k => $v) {
@@ -45,6 +45,10 @@ class Debug
             throw new Exceptions($title);
         }
     }
+    public static function error($msg, $type = 'error')
+    {
+        self::$shell = true;self::out($msg, $type);
+    }
     public static function lib($class, $method)
     {
         if (self::$shell) {
@@ -77,23 +81,18 @@ class Debug
                 }
             }
         }
+        if (!$trace) {
+            $trace = array_keys((self::$trace));
+            $trace = $trace[0];
+        }
         return $trace;
     }
-    private static function data($method, $msg, $key = false)
+    public static function getTrace()
     {
-        if (self::$shell) {
-            if ($key) {
-                self::$data[$method][$key] = $msg;
-            } else {
-                self::$data[$method][] = $msg;
-            }
+        if (self::$trace) {
+            return array_reverse(array_values(self::$trace));
         }
-    }
-    private static function runtime()
-    {
-        self::data('runtime', Output::format(self::loadfile()), 'Load Files');
-        $msg = Output::format(array('time' => self::time(), 'memory' => self::memory()));
-        self::data('runtime', $msg, 'Total');
+        return array();
     }
     private static function env()
     {
@@ -105,16 +104,25 @@ class Debug
             'trace' => $trace
         );
     }
+    private static function data($method, $msg)
+    {
+        self::$data[$method][] = $msg;
+    }
+    private static function runtime()
+    {
+        self::data('runtime', Output::format(self::load()), 'Load Files');
+        $msg = Output::format(array('time' => self::time(), 'memory' => self::memory()));
+        self::data('runtime', $msg, 'Total');
+    }
     private static function check($value)
     {
         if (isset($value['file']) && strpos($value['file'], DEVER_APP_PATH) !== false) {
-            $config = array('api', 'lib', 'table', 'daemon', 'route', DEVER_ENTRY);
+            $config = array('lib', 'api', 'table');
             foreach ($config as $k => $v) {
                 if (strpos($value['file'], DEVER_APP_PATH . $v) !== false) {
-                    return false;
+                    return true;
                 }
             }
-            return true;
         }
         return false;
     }
@@ -151,7 +159,7 @@ class Debug
         self::$memory = $memory;
         return '[' . ($memory / 1024) . 'KB]';
     }
-    private static function loadfile()
+    private static function load()
     {
         $files = get_included_files();
         $result = array();
@@ -163,27 +171,4 @@ class Debug
         }
         return $result;
     }
-    
-    public static function sql($key = '')
-    {
-        if (Config::get('database')['sql']) {
-            if (is_numeric($key) && isset(Config::get('database')['sql'][$key])) {
-                return Config::get('database')['sql'][$key];
-            }
-            
-            elseif ($key == 'all') {
-                return Config::get('database')['sql'];
-            }
-
-            $num = count(Config::get('database')['sql'])-1;
-            return self::sql($num);
-        }
-    }
-    public static function getTrace()
-    {
-        if (self::$trace) {
-            return array_reverse(array_values(self::$trace));
-        }
-        return array();
-    }
 }

+ 1 - 1
src/Dever/Import.php

@@ -55,7 +55,7 @@ class Import
     private function loadDevelopCommit()
     {
         if (method_exists($this->class, $this->method . '_commit')) {
-            $db = Model::load();
+            $db = \Dever::store();
             try {
                 $db->begin();
                 $data = $this->loadDevelopCall();

+ 156 - 297
src/Dever/Model.php

@@ -1,36 +1,56 @@
 <?php namespace Dever;
+use Dever;
 class Model
 {
     protected $method = '';
     protected $store;
-    protected $config = array();
-    public function __construct($table, $app, $path)
+    protected $partition = false;
+    public $config = array();
+    public function __construct($table, $app, $store, $partition, $path)
     {
-        $store = 'default';
-        if (strpos($table, '.')) {
-            list($table, $this->method) = explode('.', $table);
+        $project = Project::load($app);
+        if ($table) {
+            $file = $path . DIRECTORY_SEPARATOR . $table . '.php';
+            $base = $project['path'] . $file;
+            if (is_file($base)) {
+                $this->config = include $base;
+                if (isset($this->config['partition']) && empty($partition)) {
+                    $partition = $this->config['partition'];
+                }
+                $this->config['app'] = $app;
+                $this->config['table'] = DEVER_PROJECT . '_' . $app . '_' . $table;
+                $this->lang();
+            } else {
+                $this->config['table'] = $table;
+                $this->config['struct'] = false;
+            }
         }
-        if (strpos($table, ':')) {
-            list($table, $store) = explode(':', $table);
+        if ($partition) {
+            $this->partition($partition);
         }
-        $project = Project::load($app);
-        $file = $path . DIRECTORY_SEPARATOR . $table . '.php';
-        $base = $project['path'] . $file;
-        if (is_file($base)) {
-            $this->config = include $base;
+        $this->store = Dever::store($store, $this->partition);
+        if (isset($file)) {
+            $this->init($file);
         }
-        $this->config['name'] = DEVER_PROJECT . '_' . $app . '_' . $table;
-        $this->config['app'] = $app;
-        if (isset($this->config['store'])) {
-            $store = $this->config['store'];
+    }
+    private function partition($partition)
+    {
+        if (is_array($partition)) {
+            $this->partition = $partition;
+        } else {
+            $this->partition = Dever::config('setting')['database']['partition'] ?? false;
+        }
+        if ($this->partition['database']) {
+            foreach ($this->partition as $k => &$v) {
+                $t = Dever::session($k);
+                if (!$t) {
+                    $e = '$v=' . $v . ';';
+                    eval($e);
+                }
+            }
         }
-        $setting = Config::get('setting')['database'][$store];
-        $class = 'Dever\\Store\\' . $setting['type'];
-        $this->store = $class::getInstance($store, $setting);
-        $this->load();
-        $this->init($file);
     }
-    private function load()
+    private function lang()
     {
         if (isset($this->config['lang']) && isset($this->config['struct']) && $pack = Config::get('setting')['lang_pack']) {
             foreach ($this->config['lang'] as $lang) {
@@ -47,6 +67,20 @@ class Model
     private function init($file)
     {
         $data['struct'] = $data['index'] = 0;
+        if ($this->partition) {
+            if (isset($this->partition['table'])) {
+                $this->config['table'] .= '_' . $this->partition['table'];
+                $file = rtrim($file, '.php') . '_';
+                if (isset($this->partition['database'])) {
+                    $file .= $this->partition['database'] . '_';
+                }
+                $file .= $this->partition['table'] . '.php';
+            }
+            if (isset($this->partition['field'])) {
+                $this->partition['field'] = Dever::$date::maketime($this->partition['field']);
+                $data['field'] = 0;
+            }
+        }
         $file = Path::get($file);
         if (is_file($file)) {
             $data = include $file;
@@ -59,334 +93,159 @@ class Model
                     $data[$k] = $num;
                     file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
                 }
+            } elseif ($k == 'field' && $v != $this->partition['field']) {
+                $this->store->partition($this->config, $this->partition['field']);
+                $data['field'] = $this->partition['field'];
+                file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
             }
         }
     }
-
-    public function select($param = array(), $set, $call = false)
+    public function select($param, $set = array())
     {
-        return $this->store->select($this->config['name'], $param, $set);
-        if ($call) {
-
+        if (isset($set['num'])) {
+            $set['limit'] = Paginator::init($set['num'], $set['page'] ?? 1, function()use($param){return $this->count($param);});
         }
-        if ($data) {
-            if (isset($set['reset'])) {
-                $result = $this->reset($data, $set['reset'], $call);
-            } else {
-                if ($call) {
-                    foreach ($data as $row) {
-                        $result[] = $call($row);
-                    }
-                }
-            }
+        $result = $this->store->select($this->config['table'], $param, $set, $this->config['struct']);
+        if (isset($set['num']) && empty($set['page'])) {
+            $result = $result->fetchAll();
+            Paginator::status(empty($result));
         }
         return $result;
     }
-
-    public function find($where, $call = false)
-    {
-        return $this->db()->find($where, $call);
-    }
-
-    public function query($sql, $data = array(), $method = '')
+    public function count($param)
     {
-        return $this->db()->exe($sql, $data, $method);
+        return $this->store->count($this->config['table'], $param, $this->config['struct']);
     }
-
-    public function fetch($sql, $data = array(), $cache = false)
+    public function find($param, $set = array())
     {
-        return $this->fetchAll($sql, $data, false, $cache, 'fetch');
+        return $this->select($param, $set)->fetch();
     }
-
-    public function fetchAll($sql, $data = array(), $page = array(), $cache = false, $method = 'fetchAll')
+    public function insert($data)
     {
-        $this->setTable($sql);
-
-        if (!$cache && !empty(Config::get('cache')->mysql)) {
-            $cache = 'getSql_' . md5($sql . serialize($data));
-            if ($page && $p = Input::get('page')) {
-                $cache .= '_p' . $p;
-            }
-        }
-
-        if ($cache) {
-            $result = $this->db()->cache($cache);
-            if ($result) {
-                return $result;
-            }
-        }
-
-        if ($page) {
-            $result = $this->page($page, $sql, $data);
-        } else {
-            $result = $this->query($sql, $data)->$method();
+        if (empty($data['cdate'])) {
+            $data['cdate'] = DEVER_TIME;
         }
-        
-        if ($cache) {
-            $this->db()->cache($cache, $result);
-        } else {
-            $this->db()->log($this->sql, $data, $result);
-        }
-
-        return $result;
-    }
-
-    public function rowCount($sql, $data = array())
-    {
-        return $this->query($sql, $data)->rowcount();
-    }
-
-    public function row($sql, $data = array())
-    {
-        return $this->query($sql, $data)->rowcount();
+        return $this->store->insert($this->config['table'], $data, $this->config['struct']);
     }
-
-    public function lastId($sql, $data = array())
+    public function update($param, $data)
     {
-        return $this->query($sql, $data)->id();
+        return $this->store->update($this->config['table'], $param, $data, $this->config['struct']);
     }
-
-    public function page($config = array(), $sql = false, $data = array())
+    public function delete($param)
     {
-        return $this->db()->getPageBySql($config, $sql, $data, $this);
+        return $this->store->delete($this->config['table'], $param, $this->config['struct']);
     }
-
-    public function index($index)
-    {
-        return $this->db()->index($index);
-    }
-
     public function begin()
     {
-        return $this->db()->begin();
+        return $this->store->begin();
     }
-
     public function commit()
     {
-        return $this->db()->commit();
+        return $this->store->commit();
     }
-
     public function rollback()
     {
-        return $this->db()->rollback();
+        return $this->store->rollback();
     }
-
-    /**
-     * __call
-     *
-     * @return mixd
-     */
-    public function __call($method, $param)
+    public function query($sql, $bind = array(), $page = array())
     {
-        if (isset($param[0][0]) && is_array($param[0][0])) {
-            $result = array();
-            foreach ($param[0] as $k => $v) {
-                $result[] = $this->$method($v);
-            }
-            
-            return $result;
-        }
-
-        $call = false;
-        if (isset($param[1])) {
-            if (is_object($param[1])) {
-                $call = $param[1];
-            } elseif ($method == 'insert') {
-                $param[0]['insert_value_num'] = $param[1];
-            }
+        if (strpos($sql, '{table}')) {
+            $sql = str_replace('{table}', $this->config['table'], $sql);
         }
-
-        $param = $param ? $this->initParam($param[0], $method) : array();
-
-        $key = $this->table . $method . md5(serialize($param));
-        if (isset($param['clear']) && $param['clear'] && isset($this->data[$key])) {
-            unset($this->data[$key]);
-        }
-
-        $this->compatible($key, $method, $param);
-
-        if (!isset($this->data[$key])) {
-            $this->request($method, $param);
-            if (isset($this->config['request'][$method]['type']) && in_array($this->config['request'][$method]['type'], array('update', 'insert'))) {
-                return $this->callData($method, $param, $call);
+        if (isset($page['num'])) {
+            if (strpos($sql, 'limit')) {
+                $temp = explode('limit', $sql);
+                $sql = $temp[0];
             }
-            $this->data[$key] = $this->callData($method, $param, $call);
+            $sql .= ' limit ' . Paginator::init($page['num'], $page['page'] ?? 1, function()use($sql, $bind){return $this->queryCount($sql, $bind);});
         }
-
-        return $this->data[$key];
-
-    }
-
-    /**
-     * getData
-     *
-     * @return mixd
-     */
-    private function callData($method, $param, $call = false)
-    {
-        $param = $this->search($method, $param);
-        $handle = new Model\Handle($method, $this->config, $param, $call);
-        return $handle->get();
-    }
-
-    /**
-     * compatible
-     *
-     * @return mixd
-     */
-    protected function compatible($key, $method, $param)
-    {
-        if (Config::get('database')->compatible && isset($this->config['project'])) {
-            $file = $this->config['project']['path'] . 'database/' . ucfirst(Config::get('database')->compatible) . '/' . ucfirst($this->config['name']) . '.php';
-            if (is_file($file)) {
-                $class = ucfirst($this->config['project']['name']) . '\\Database\\' . ucfirst(Config::get('database')->compatible) . '\\' . ucfirst($this->config['name']);
-                if (class_exists($class) && method_exists($class, $method)) {
-                    $this->data[$key] = $class::$method($param);
-                }
-            }
+        $result = $this->store->query($sql, $bind);
+        if (isset($page['num']) && empty($page['page'])) {
+            $result = $result->fetchAll();
+            Paginator::status(empty($result));
         }
+        return $result;
     }
-
-    /**
-     * request
-     *
-     * @return mixd
-     */
-    protected function request($method, $param)
+    public function queryCount($sql, $bind)
     {
-        if (!isset($this->config['struct'])) {
-            $this->config['struct'] = array();
-        }
-        if (empty($this->config['request'][$method]) && isset($this->config['struct'])) {
-            $search = '';
-            if (isset($param['search_type'])) {
-                $search = $param['search_type'];
-            }
-
-            $this->config['request'][$method] = Model\Request::get($this->table, $method, $this->config['struct'], $search);
-
-            if ($method == 'list' && isset($this->config['manage']['list_type']) && $this->config['manage']['list_type']) {
-                $this->config['request']['list']['page'][0] = 50000;
-            }
-
-
-            if (isset($this->config['request'][$method]['type']) && isset($this->config['request'][$method]['option']) && $this->config['request'][$method]['type'] == 'all' && isset($this->config['request'][$method . '_option'])) {
-
-                $this->config['request'][$method]['option'] = array_merge($this->config['request'][$method]['option'], $this->config['request'][$method . '_option']);
-            }
+        $sql = mb_strtolower($sql);
+        if (strpos($sql, ' from ')) {
+            $temp = explode(' from ', $sql);
         }
-
-        if (isset($param['config']) && isset($this->config['request'][$method]['config']) && $this->config['request'][$method]['config']) {
-            $this->config['request'][$method] = array_merge($this->config['request'][$method], $param['config']);
-            unset($param['config']);
-            unset($this->config['request'][$method]['config']);
-        }
-
-        if (in_array($method, array('all', 'state', 'total', 'list', 'find')) && isset($param['option'])) {
-            foreach ($param['option'] as $k => $v) {
-                $this->config['request'][$method]['option'][$k] = $v;
+        if (isset($temp[1])) {
+            if (strpos($temp[1], ' order ')) {
+                $temp = explode(' order ', $temp[1]);
+                $sql = $temp[0];
+            } else {
+                $sql = $temp[1];
             }
-        }
-        if ($method == 'list') {
-            $search = Dever::search_button();
-            if ($search) {
-                $this->config['request']['list']['group'] = $search[0];
-                $this->config['request']['list']['col'] = $search[1];
+            if (strpos($sql, 'group')) {
+                $sql = 'SELECT count(1) as num FROM (SELECT count(1) FROM '.$sql.' ) a ';
+            } else {
+                $sql = 'SELECT count(1) as num FROM ' . $sql;
             }
+            return $this->store->query($sql, $bind)->fetchColumn();
         }
-
-        if (isset($param['join'])) {
-            $this->config['request'][$method]['join'] = $param['join'];
-            if (isset($this->config['request'][$method]['option'])) {
-                foreach ($this->config['request'][$method]['option'] as $k => $v) {
-                    if (is_string($v) && !strstr($v, 't_1') && !strstr($k, '-')) {
-                        $this->config['request'][$method]['option'][$k] = 'yes-t_1.' . $k;
-                    }
-                    if (is_array($v) && !strstr($v[0], 't_1')) {
-                        if (strstr($v[0], '-')) {
-                            $temp = explode('-', $v[0]);
-                            $this->config['request'][$method]['option'][$k][0] = 'yes-t_1.' . $temp[1];
-                        } else {
-                            $this->config['request'][$method]['option'][$k][0] = 'yes-t_1.' . $k;
+    }
+    public function __call($method, $data)
+    {
+        if (isset($this->config['request'][$method])) {
+            $method = $this->config['request'][$method];
+            $param = array();
+            if (isset($method['where'])) {
+                $temp = array();
+                foreach ($method['where'] as $k => $v) {
+                    if ($k == 'or' || $k == 'and') {
+                        foreach ($v as $k1 => $v1) {
+                            $this->callCreate($data, $temp, $k1, $v1);
                         }
+                    } else {
+                        $this->callCreate($data, $temp, $k, $v);
                     }
                 }
+                $param[] = $temp;
             }
-        }
-
-        if (DEVER_APP_NAME != 'manage') {
-            $lang = Dever::langConfig();
-            if (Dever::config('base')->lang && $lang && $lang != Dever::config('base')->lang && isset($this->config['manage']['lang']) && $this->config['manage']['lang']) {
-                $col = array();
-                foreach ($this->config['manage']['lang'] as $k => $v) {
-                    $col[] = $lang . '_' . $v . ' as `' . $v . '`';
+            if (isset($method['data'])) {
+                $temp = array();
+                foreach ($method['data'] as $k => $v) {
+                    $this->callCreate($data, $temp, $k, $v);
                 }
-                if (!isset($this->config['request'][$method]['col'])) {
-                    $this->config['request'][$method]['col'] = '*';
-                }
-                $this->config['request'][$method]['col'] .= ',' . implode(',', $col);
+                $param[] = $temp;
+            }
+            if (isset($method['set']) && isset($data[1])) {
+                $param[] = array_merge($method['set'], $data[1]);
             }
+            $type = $method['type'];
+            return $this->$type(...$param);
         }
+        return false;
     }
-
-    /**
-     * search
-     *
-     * @return mixd
-     */
-    protected function search($method, $param)
+    private function callCreate($data, &$param, $k, $v)
     {
-        if (isset($param['search_type']) && $param['search_type'] == 2) {
-            $join = array();
-            $i = 2;
-            foreach ($param as $k => $v) {
-                if (strpos($k, '-') !== false) {
-                    $k = str_replace(array('option_', 'where_'), '', $k);
-                    if (isset($this->config['struct'][$k]) && isset($this->config['struct'][$k]['sync'])) {
-                        $temp = explode('-', $k);
-                        $join[] = array
-                        (
-                            'table' => $temp[0] . '/' . $temp[1],
-                            'type' => 'left join',
-                            'on' => $this->config['struct'][$k]['sync'],
-                        );
-
-                        $t = 't_' . $i . '.' . $temp[2];
-                        $this->config['request'][$method]['option'][$t] = $this->config['request'][$method]['option'][$k];
-                        $param['option_' . $t] = $param['option_' . $k];
-                        unset($this->config['request'][$method]['option'][$k]);
-                        unset($param['option_' . $k]);
-                        $i++;
-                    }
+        if (is_array($v)) {
+            if (empty($data[0][$k])) {
+                if (empty($v[2])) {
+                    return;
                 }
+                $data[0][$k] = $v[2];
             }
-            if ($join) {
-                $this->config['request'][$method]['join'] = $join;
+            $i = $v[0];
+            $j = array($v[1], $data[0][$k]);
+        } else {
+            if (empty($data[0][$k])) {
+                $data[0][$k] = $v;
             }
+            $i = $k;
+            $j = $data[0][$k];
         }
-
-        return $param;
+        $this->callCreateParam($param, $i, $j);
     }
-
-    protected function searchFulltext($k, $param)
+    private function callCreateParam(&$param, $i, $j)
     {
-
-    }
-
-    /**
-     * initParam
-     *
-     * @return mixd
-     */
-    private function initParam($param, $method)
-    {
-        if ($param && !is_array($param)) {
-            if ($this->config['link']) {
-                $param = array('id' => $param);
-            } else {
-                $param = array('where_id' => $param, 'option_id' => $param);
-            }
+        if (isset($param[$i])) {
+            $i .= '#';
+            return $this->callCreateParam($param, $i, $j);
         }
-        return $param;
+        $param[$i] = $j;
     }
-}
+}

+ 2 - 7
src/Dever/Output.php

@@ -1,12 +1,11 @@
 <?php namespace Dever;
 use Dever;
-use Dever\Pagination\Paginator;
 class Output
 {
     private static $format = 'json';
     public static function success($data, $uuid = false, $code = 0)
     {
-        if (is_object($data)) {# && $data instanceof \Generator
+        if (is_object($data)) {
             $result = array();
             foreach ($data as $v) {
                 $result[] = $v;
@@ -18,7 +17,7 @@ class Output
         $result['msg'] = 'success';
         $result['data'] = $data;
         $result['code'] = $code;
-        if ($page = self::page()) {
+        if ($page = Paginator::get()) {
             $result['page'] = $page;
         }
         if ($uuid) {
@@ -61,10 +60,6 @@ class Output
             self::html($result);
         }
     }
-    public static function page($method = 'current')
-    {
-        return Paginator::getInstance($method)->toArray();
-    }
     private static function json(&$result)
     {
         if (self::$format == 'json' || Route::input('json') == 1) {

+ 0 - 24
src/Dever/Pagination/Paginator.php

@@ -25,7 +25,6 @@ class Paginator
         if (empty(self::$instance[$key])) {
             self::$instance[$key] = new self();
         }
-
         return self::$instance[$key];
     }
     public static function getPage($key = 'current', $total = false, $template = '')
@@ -36,9 +35,7 @@ class Paginator
             self::getInstance($key)->template($key);
             self::getInstance($key)->link();
         }
-
         $result = self::getInstance($key)->handle($template);
-
         if ($total == 'NEXT') {
             if (self::getInstance($key)->next) {
                 $result = self::getInstance($key)->href(self::getInstance($key)->next);
@@ -65,7 +62,6 @@ class Paginator
                 $this->setArray($total, $current);
             }
         }
-
         return $this->array;
     }
     private function setArray($total, $current)
@@ -110,20 +106,17 @@ class Paginator
         if (!$this->total) {
             return 0;
         }
-
         if ($this->first_num > 0) {
             $this->page = ceil(($this->total - $this->first_num) / $this->num) + 1;
         } else {
             $this->page = ceil($this->total / $this->num);
         }
-
         return $this->page;
     }
     public function offset($num, $first_num = 0)
     {
         $this->num = $num;
         $current = $this->current();
-
         if ($first_num > 0) {
             $this->first_num = $first_num;
             if ($current == 1) {
@@ -133,7 +126,6 @@ class Paginator
                 $current = $current - 1;
             }
         }
-
         $offset = $first_num + $this->num * ($current-1);
         
         return $offset;
@@ -143,7 +135,6 @@ class Paginator
         if (!$total) {
             return $this->total ? $this->total : 0;
         }
-
         $this->total = $total;
     }
     public function maxpage($maxpage)
@@ -155,19 +146,14 @@ class Paginator
         if (!$link) {
             $link = Uri::$url;
         }
-
         $this->linkReplaceStr($link, 'pg');
-
         $this->linkReplaceStr($link, 'pt');
-
         $this->linkSearch($link);
-
         return $this->link = $link;
     }
     private function linkSearch(&$link)
     {
         $search = Input::prefix('search_');
-
         if ($search) {
             foreach ($search as $k => $v) {
                 if (is_array($v)) {
@@ -196,27 +182,20 @@ class Paginator
             if ($this->total <= 0) {
                 $this->total = count($content);
             }
-
             $this->current();
-
             $data = isset($content[$this->current - 1]) ? $content[$this->current - 1] : array();
-
             return $data;
         }
-
         return array();
     }
     public function sqlCount($sql, $offset, $total = 0, $db, $data)
     {
         $this->total = $total;
-
         if ($this->total <= 0) {
             $this->sqlCountQuery($sql, $db, $data);
         }
-
         $data_sql = $sql . ' LIMIT ' . $offset . ', ' . $this->num;
         $result = $db->query($data_sql, $data)->fetchAll();
-
         return $result;
     }
     private function sqlCountQuery($sql, $db, $data)
@@ -225,7 +204,6 @@ class Paginator
         if (strstr($sql, ' from ')) {
             $temp = explode(' from ', $sql);
         }
-
         if (isset($temp[1])) {
             if (strstr($temp[1], ' order ')) {
                 $temp = explode(' order ', $temp[1]);
@@ -233,13 +211,11 @@ class Paginator
             } else {
                 $sql = $temp[1];
             }
-
             if (strstr($sql, 'group')) {
                 $sql = 'SELECT count(1) as num FROM (SELECT count(1) FROM '.$sql.' ) a ';
             } else {
                 $sql = 'SELECT count(1) as num FROM ' . $sql;
             }
-            
             $this->total = $db->query($sql, $data)->fetchColumn();
         }
     }

+ 181 - 0
src/Dever/Paginator.php

@@ -0,0 +1,181 @@
+<?php namespace Dever;
+class Paginator
+{
+    protected static $status = false;
+    protected static $num;
+    protected static $rows;
+    protected static $current;
+    protected static $html = '';
+    protected static $link;
+    public static function init($num, $page, $rows)
+    {
+        self::$num = $num;
+        self::$rows = $rows;
+        self::$current = Route::input('pg', '', '', $page);
+        $offset = self::$num * (self::$current-1);
+        if ($offset >= 10000) {
+            return array($offset, self::$num);
+        }
+        return $offset . ',' . self::$num;
+    }
+    public static function status($status)
+    {
+        self::$status = !$status;
+    }
+    public static function get($template = '', $maxpage = 10, $link = '')
+    {
+        if (self::$rows) {
+            $page['num'] = self::$num;
+            $page['status'] = self::$status;
+            $page['current'] = self::$current;
+            if ($template) {
+                $page['rows'] = self::$rows = (self::$rows)();
+                $page['next'] = $page['total'] = ceil(self::$rows / self::$num);
+                if (self::$current < $page['total']) {
+                    $page['next'] = self::$current+1;
+                } else {
+                    self::$current = $page['total'];
+                }
+                $page['prev'] = self::$current-1;
+                if ($page['prev'] < 1) {
+                    $page['prev'] = 1;
+                }
+                self::handle($page, $maxpage);
+                $page['html'] = self::html($page, $template, $maxpage, $link);
+            }
+            return $page;
+        }
+        return false;
+    }
+    protected static function handle(&$page, $maxpage)
+    {
+        if ($page['total'] <= $maxpage) {
+            $page['start'] = 1;
+            $page['end'] = $page['total'];
+        } else {
+            $tpage = intval($maxpage / 2);
+            if ($page['current'] < $tpage) {
+                $page['start'] = 1;
+            } elseif ($page['current'] <= ($page['total'] - $maxpage)) {
+                $page['start'] = $page['current'] - $tpage;
+            } elseif ($page['current'] > $page['total'] - $maxpage && $page['current'] <= $page['total'] - $tpage) {
+                $page['start'] = $page['current'] - $tpage;
+            } elseif ($page['current'] > $page['total'] - $tpage) {
+                $page['start'] = $page['total'] - $maxpage + 1;
+            }
+            $page['end'] = $page['start'] + $maxpage - 1;
+            if ($page['start'] < 1) {
+                $page['end'] = $page['current'] + 1 - $page['start'];
+                $page['start'] = 1;
+                if (($page['end'] - $page['start']) < $maxpage) {
+                    $page['end'] = $maxpage;
+                }
+            } elseif ($page['end'] > $page['total']) {
+                $page['start'] = $page['total'] - $maxpage + 1;
+                $page['end'] = $page['total'];
+            }
+        }
+    }
+    public static function link($page)
+    {
+        if (is_string(self::$link)) {
+            return Route::url(self::$link, array('pg' => $page));
+        }
+        return (self::$link)($page);
+    }
+    protected static function html($page, $template, $maxpage, $link)
+    {
+        $file = DEVER_PROJECT_PATH . 'page/' . $template . '.php';
+        if (is_file($file)) {
+            $html = include $file;
+            self::$link = $link;
+            if (!self::$link) {
+                self::$link = Route::url(false);
+                if (strpos(self::$link, 'pg=') !== false) {
+                    self::$link = preg_replace('/[?|&]pg=(\d+)/i', '', self::$link);
+                }
+            }
+            if (is_array($html)) {
+                self::template($html, $page);
+            } else {
+                self::$html = $html;
+            }
+        }
+        return self::$html;
+    }
+    protected static function template($html, $page)
+    {
+        self::$html = '';
+        if ($page['current'] > 1 && isset($html['start'])) {
+            self::$html .= self::create($html['child'], $html['start'][1], 1, $html['start'][0], $html['page'][2]);
+        }
+        if (isset($html['prev'])) {
+            self::$html .= self::create($html['child'], $html['prev'][1], $page['prev'], $html['prev'][0], $html['page'][2]);
+        }
+        $i = $page['start'];
+        for ($i; $i <= $page['end']; $i++) {
+            self::$html .= self::create($html['child'], self::getClass($i, $html['page'], $page), $i, $i, $html['page'][2]);
+        }
+        if (isset($html['next'])) {
+            self::$html .= self::create($html['child'], $html['next'][1], $page['next'], $html['next'][0], $html['page'][2]);
+        }
+        if (isset($html['end']) && $page['current'] < $page['total']) {
+            self::$html .= self::create($html['child'], $html['end'][1], $page['total'], $html['end'][0], $html['page'][2]);
+        }
+        if (isset($html['jump'])) {
+            $click = 'onclick="var link=\'' . self::link('dever_page') . '\';location.href=link.replace(\'dever_page\', document.getElementById(\'dever_page\').value)"';
+            self::$html .= str_replace('{click}', $click, $html['jump']);
+        }
+        self::$html = self::tag($html['parent'], self::$html);
+    }
+    protected static function create($child, $class, $num, $name, $type = '')
+    {
+        if ($type == 'parent') {
+            $child[1] = 'class="' . $class . '"';
+            $class = '';
+        }
+        return self::tag($child, self::getContent($child, $class, $num, $name));
+    }
+    protected static function tag($tag, $content)
+    {
+        if (!$tag) {
+            return $content;
+        }
+        $attr = '';
+        if (is_array($tag)) {
+            $temp = $tag;unset($tag);
+            $tag = $temp[0];
+            $attr = $temp[1];
+        }
+        return '<' . $tag . ' ' . $attr . '>' . $content . '</' . $tag . '>';
+    }
+    protected static function getContent($child, $class, $num, $name)
+    {
+        if ($child && $child[0] == 'a') {
+            $child[1] = self::attr($class, self::link($num));
+            $content = $name;
+        } else {
+            $content = self::tag(array('a', self::attr($class, self::link($num))), $name);
+        }
+        return $content;
+    }
+    protected static function attr($class, $href)
+    {
+        return ' class="' . $class . '" href="' . $href . '" ';
+    }
+    protected static function getClass($index, $html, $page)
+    {
+        $class = $html[0];
+        if ($index == $page['current']) {
+            if (isset($html[3]) && $html[3] == true) {
+                $class = $html[1];
+            } else {
+                if ($class) {
+                    $class .= ' ';
+                }
+                $class .= $html[1];
+            }
+        }
+        return $class;
+    }
+}

+ 17 - 17
src/Dever/Route.php

@@ -12,13 +12,10 @@ class Route
         if ($condition == 'set') {
             return self::$data[$key] = $lang;
         }
-
         if (is_string($key) && isset(self::$data[$key]) && self::$data[$key]) {
             $value = self::$data[$key];
         }
-
         return $value;
-
         if ($condition) {
             if (!$value) {
                 Output::error($lang . '不能为空');
@@ -30,29 +27,30 @@ class Route
                 Output::error($lang);
             }
         }
-
         return $value;
     }
     public static function url($uri = false, $param = array())
     {
+        if ($uri == false) {
+            if (DEVER_APP_HOST) {
+                $uri = DEVER_PROTO . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
+            } else {
+                $argv = array_merge(\Dever::json_decode($_SERVER['argv'][1]), $param);
+                return $_SERVER['argv'][0] . ' ' . \Dever::json_encode($argv);
+            }
+        }
         if (strpos($uri, 'http') === 0) {
+            if ($param) {
+                $uri .= '&' . http_build_query($param);
+            }
             return $uri;
         }
-        if ($uri) {
-            if (strpos($uri, '/')) {
-                $temp = explode('/', $uri);
-                $project_name = $temp[0];
-            } else {
-                $project_name = DEVER_APP_NAME;
-            }
+        if (strpos($uri, '/')) {
+            $temp = explode('/', $uri);
+            $project_name = $temp[0];
         } else {
-            if ($uri === false) {
-                $uri = self::$data['l'];
-            }
-            
             $project_name = DEVER_APP_NAME;
         }
-
         $project = Project::load($project_name);
         if (!$project) {
             Output::error('project not exists', array($project_name));
@@ -85,6 +83,8 @@ class Route
                     }
                 }
             }
+        } elseif ($param) {
+            $uri .= '&' . http_build_query($param);
         }
         return $project['url'] . self::$type . $uri;
     }
@@ -102,7 +102,7 @@ class Route
         if (isset($_SERVER['argc'])) {
             global $argv;
             if (isset($argv[1]) && $argv[1]) {
-                self::$data = Dever::json_decode($argv[1]);
+                self::$data = \Dever::json_decode($argv[1]);
             }
             define('DEVER_PROTO', 'command');
             define('DEVER_APP_HOST', '');

+ 3 - 3
src/Dever/Session.php

@@ -33,9 +33,9 @@ class Session
             self::$start = true;
         }
     }
-    public static function oper($key, $value = false, $timeout = 3600, $type = 'cookie', $encode = true)
+    public static function oper($key, $value = false, $timeout = 3600, $type = 'session', $encode = true)
     {
-        if (!isset(self::$save[$key])) {
+        if (empty(self::$save[$key])) {
             self::$save[$key] = new self(DEVER_APP_NAME, $type);
         }
         if ($value) {
@@ -150,7 +150,7 @@ class Session
         $this->_initFile();
         $key = $this->prefix . $key;
         if (isset($this->data[$key]) && $this->data[$key]) {
-            return $this->data[$key]
+            return $this->data[$key];
         }
         return false;
     }

+ 19 - 303
src/Dever/Store/Base.php

@@ -1,329 +1,45 @@
 <?php namespace Dever\Store;
-use Dever;
 use Dever\Debug;
 class Base
 {
     protected $read;
     protected $update;
+    protected $type;
     protected static $instance;
-    public static function getInstance($key, $setting)
+    public static function getInstance($key, $setting, $partition)
     {
         if (empty(static::$instance[$key])) {
-            static::$instance[$key] = new static($setting);
+            static::$instance[$key] = new static($setting, $partition);
         }
         return static::$instance[$key];
     }
-    public function __construct($setting)
+    public function __construct($setting, $partition)
     {
         if (isset($setting[0])) {
-            $this->read = $this->connect($setting[0]);
-            $this->update = $this->connect($setting[1]);
+            $this->read = $this->connect($setting[0], $partition);
+            $this->update = $this->connect($setting[1], $partition);
         } else {
-            $this->read = $this->connect($setting);
+            $this->read = $this->connect($setting, $partition);
             $this->update =&$this->read;
         }
     }
-    protected function error($msg, $param = '')
+    protected function error($msg)
     {
-        Debug::wait(array('msg' => $msg, 'param' => $param), 'Dever DB Error!');
+        Debug::error($msg, $this->type);
     }
-    protected function log($value, $param = array(), $data = array())
+    protected function log($msg)
     {
-        if (isset($this->sql)) {
-            $value = $this->replace($value, $param, $this->sql);
-            $this->sql($value);
-            if (!Dever::shell('all') && is_array($data)) {
-                $data = count($data) . ' records';
-            }
-            Debug::add(array('sql' => $value, 'data' => $data), $this->config['type']);
-        } else {
-            Debug::add(array('value' => $value, 'method' => $param), $this->config['type']);
-        }
-    }
-    public function sql($value)
-    {
-        if (!Config::get('database')->sql) {
-            Config::get('database')->sql = array();
-        }
-        $sql = Config::get('database')->sql;
-        array_push($sql, $value);
-        Config::get('database')->sql = $sql;
-    }
-    public function replace($value, $param)
-    {
-        if ($value && is_array($param)) {
-            foreach ($param as $k => $v) {
-                if (is_string($v)) {
-                    if (strstr($value, 'select') && strpos($v, ',')) {
-                        $v = 'in('.$v.')';
-                    } else {
-                        $v = '"' . $v . '"';
-                    }
-                }
-                $value = str_replace($k, $v, $value);
-            }
-        }
-
-        return $value;
-    }
-    protected function call($call, $data)
-    {
-        if ($call) {
-            return $call($data);
-        }
-        return $data;
-    }
-    protected function reset($data, $config, $call = false)
-    {
-        $result = array();
-        $key = $config[0];
-        foreach ($data as $row) {
-            if (isset($row[$key])) {
-                if (isset($config[2]) && isset($row[$config[1]])) {
-                    $result[$row[$key]][$row[$config[1]]] = $this->call($call, $row);
-                } elseif (isset($config[1]) && isset($row[$config[1]])) {
-                    $result[$row[$key]] = $this->call($call, $row[$config[1]]);
-                } elseif (isset($config[1])) {
-                    $result[$row[$key]][] = $this->call($call, $row);
-                } else {
-                    $result[$row[$key]] = $this->call($call, $row);
-                }
-            }
-        }
-        return $result;
-    }
-
-
-
-    /**
-     * insert the default value
-     *
-     * @return mixed
-     */
-    public function insertDefault($value, $name = '')
-    {
-        $file = $this->file($name);
-        if (!is_file($file)) {
-            return false;
-        }
-
-        $data = include $file;
-        if (isset($value['col']) && isset($value['value'])) {
-            $this->truncate();
-
-            $data = $this->insertValues($value, $data);
-
-            file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
-        }
-
-        return true;
-    }
-
-    /**
-     * file
-     *
-     * @return mixed
-     */
-    public function file($name = '')
-    {
-        $path = Config::data();
-
-        if ($name) {
-            $name = $this->table . '_' . $name . '.php';
-        } else {
-            $name = $this->table . '.php';
-        }
-
-        $temp = explode('_', $this->table);
-
-        $file = Path::get($path . 'database/', $temp[0] . '/' . $name);
-        return $file;
+        Debug::add($msg, $this->type);
     }
-
-    /**
-     * create
-     *
-     * @return mixed
-     */
-    
-
-    private function truncate()
+    public function sql(&$sql, $bind)
     {
-        if (isset($this->sql)) {
-            $sql = $this->sql->truncate($this->table);
-
-            $this->exe($sql);
-
-            $this->log($sql, 'truncate');
-        }
-    }
-
-    /**
-     * getPageBySql
-     *
-     * @return array
-     */
-    public function getPageBySql($config = array(), $sql = false, $data = array(), Model $model)
-    {
-        empty($config['template']) && $config['template'] = 'list';
-
-        empty($config['key']) && $config['key'] = 'current';
-
-        empty($config['link']) && $config['link'] = '';
-
-        empty($config['num']) && $config['num'] = 10;
-
-        $page = Page::getInstance($config['key']);
-
-        $page->template($config['template']);
-
-        $page->link($config['link']);
-
-        $total = Input::get('pt', -1);
-
-        if (isset($config['explode']) && isset($config['content'])) {
-            $content = explode($config['explode'], $config['content']);
-            $page->offset(1);
-            $data = $page->data($content, $total);
-        } else {
-            empty($config['first_num']) && $config['first_num'] = 0;
-            $offset = $page->offset($config['num'], $config['first_num']);
-            $data = $page->sqlCount($sql, (isset($config['offset']) ? $config['offset'] : $offset), $total, $model, $data);
-        }
-
-        Dever::$global['page'][$config['key']] = $page->toArray();
-        
-        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]);
-
-        $this->limit($page->offset($num). ',' . $num);
-
-        $total = Input::get('pt', -1);
-
-        if ($total < 0) {
-            $total = $this->count('clear');
-        }
-
-        $page->total($total);
-
-        Dever::$global['page'][$config[1]] = $page->toArray();
-        return $this;
-    }
-
-    public function deleteCache($value, $key, $handle)
-    {
-        return $handle->delete($key);
-    }
-    public function cache($key = false, $method = 'get', $data = false)
-    {
-        $cache = isset($this->config['cache']) ? $this->config['cache'] : Config::get('cache')->cAll;
-
-        //if (isset($cache['route']) && $cache['route'] > 0 && $this->table && !isset(Config::get('base')->clearCache['route'])) {
-        if (isset($cache['route']) && $cache['route'] > 0 && $this->table) {
-            $handle = Handle::getInstance('route', $cache['route']);
-            if ($method == 'put') {
-                $keys = $handle->get($this->table, false);
-                $route = Uri::key();
-                if (!isset($keys[$route])) {
-                    $keys[$route] = 1;
-                    $handle->set($this->table, $keys, 0, false);
-                }
-            } elseif (!$key && $this->table) {
-                if (Config::get('base')->after == 1) {
-                    return;
-                }
-                $keys = $handle->get($this->table, false);
-                if ($keys) {
-                    array_walk($keys, array($this, 'deleteCache'), $handle);
-                }
+        foreach ($bind as $k => $v) {
+            if (strstr($sql, 'select') && strpos($v, ',')) {
+                $v = 'in('.$v.')';
+            } else {
+                $v = '"' . $v . '"';
             }
+            $sql = str_replace($k, $v, $sql);
         }
-
-        if (empty($cache['mysql'])) {
-            return false;
-        }
-
-        $handle = Handle::getInstance('mysql', $cache['mysql']);
-        if (!$key && $this->table) {
-            if (Config::get('base')->after == 1) {
-                return;
-            }
-            $keys = $handle->get($this->table, false);
-            if ($keys) {
-                array_walk($keys, array($this, 'deleteCache'), $handle);
-            }
-        } else {
-            if ($method == 'get') {
-                if (DEVER_APP_NAME == 'manage') {
-                    return false;
-                }
-                return $handle->get($key);
-            }
-
-            if ($method == 'put' && $data !== false) {
-
-                if ($this->table) {
-                    $keys = $handle->get($this->table, false);
-                    if (!isset($keys[$key])) {
-                        $keys[$key] = 1;
-                        $handle->set($this->table, $keys, 0, false);
-                    }
-                }
-
-                return $handle->set($key, $data);
-            }
-        }
-
-        return false;
-    }
-    /**
-     * begin
-     *
-     * @return object
-     */
-    public function begin()
-    {
-        return $this;
-    }
-
-    /**
-     * commit
-     *
-     * @return object
-     */
-    public function commit()
-    {
-        return $this;
-    }
-
-    /**
-     * rollback
-     *
-     * @return object
-     */
-    public function rollback()
-    {
-        return $this;
     }
-}
+}

+ 49 - 415
src/Dever/Store/Pdo.php

@@ -1,11 +1,16 @@
 <?php namespace Dever\Store;
+use Dever;
 use Dever\Debug;
 use Dever\Output;
 use Dever\String\Sql;
 class Pdo extends Base
 {
-    protected function connect($setting)
+    protected function connect($setting, $partition)
     {
+        if (isset($partition['database'])) {
+            $setting['name'] .= '_' . $partition['database'];
+        }
+        $this->type = $setting['type'];
         $handle = false;
         if (strpos($setting['host'], ':') !== false) {
             list($setting['host'], $setting['port']) = explode(':', $setting['host']);
@@ -34,14 +39,14 @@ class Pdo extends Base
             return $handle;
         } catch (\PDOException $e) {
             if (strstr($e->getMessage(), 'Unknown database')) {
-                $this->createDb($setting);
-                $this->connect($setting);
+                $this->create($setting);
+                $this->connect($setting, $partition);
             } else {
                 Output::error($e->getMessage());
             }
         }
     }
-    public function createDb($setting)
+    private function create($setting)
     {
         $method = 'mysql';
         if (function_exists('mysqli_connect')) {
@@ -64,453 +69,82 @@ class Pdo extends Base
     public function struct($config, $state = 0)
     {
         if ($state) {
-            $this->exe(Sql::alter($config['name'], $config['struct'], $this->fetchAll(Sql::desc($config['name']))));
-            if (isset($config['default']) && $config['default']) {
-                echo 11;die;
-            }
+            $this->query(Sql::alter($config['table'], $config['struct'], $this->query(Sql::desc($config['table']))));
         } else {
-            $this->exe(Sql::create($config));
+            $this->query(Sql::create($config));
+        }
+        if (isset($config['default']) && $config['default']) {
+            $count = $this->count($config['table'], array());
+            if (!$count) {
+                $this->query(Sql::inserts($config['table'], $config['default']));
+            }
         }
     }
     public function index($config, $state = 0)
     {
-        $this->exe(Sql::index($config['name'], $config['index'], $this->fetchAll(Sql::showIndex($config['name']))));
+        $this->query(Sql::index($config['table'], $config['index'], $this->query(Sql::showIndex($config['table']))));
     }
-    public function fetchAll($sql)
+    public function partition($config, $partition)
     {
-        return $this->exe($sql, 'fetchAll');
+        $this->query(Sql::partition($config['table'], $partition, $this->query(Sql::showIndex($config['table']))));
     }
-    public function exe($sql, $method = '')
+    public function query($sql, $bind = array(), $method = 'read')
     {
-        $bind = false;
-        if (is_array($sql)) {
-            $bind = $sql[1];
-            $sql = $sql[0];
-        }
-        if (stristr($sql, 'select')) {
-            $db = $this->read;
-        } else {
-            $db = $this->update;
-        }
         try {
             if ($bind) {
-                if (!is_array($bind)) {
-                    $bind = array(':0' => $bind);
-                }
-                $handle = $db->prepare($sql);
+                $handle = $this->$method->prepare($sql);
                 $handle->execute($bind);
             } else {
-                $handle = $db->query($sql);
+                $handle = $this->$method->query($sql);
             }
         } catch (\PDOException $exception) {
             $this->error($exception->getMessage(), $sql);
         }
-        if ($method) {
-            $data = $handle->$method();
-            //$this->log($sql, $bind, $data);
-            return $data;
-        } else {
-            return $handle;
+        if (Debug::$shell) {
+            $this->sql($sql, $bind);
+            $this->log(array('sql' => $sql, 'count' => $handle->rowCount()));
         }
+        return $handle;
     }
-    public function select($table, $param, $set = array())
+    public function select($table, $param, $set, $field)
     {
         $bind = array();
-        $sql = Sql::select($table, $param, $bind, $set);
-        try {
-            if ($bind) {
-                $handle = $this->read->prepare($sql);
-                $handle->execute($bind);
-            } else {
-                $handle = $this->read->query($sql);
-            }
-        } catch (\PDOException $exception) {
-            $this->error($exception->getMessage(), $sql);
-        }
-        while ($row = $handle->fetch()) {
-            yield $row;
-        }
-    }
-
-    public function query($sql, $state = false)
-    {
-        if (empty($this->config['shell'])) {
-            if (is_string($this->config['host']) && strpos($this->config['host'], ':') !== false) {
-                $temp = explode(':', $this->config['host']);
-                $this->config['host'] = $temp[0];
-                $this->config['port'] = $temp[1];
-            } elseif (isset($this->config['host']['read']) && strpos($this->config['host']['read'], ':') !== false) {
-                $temp = explode(':', $this->config['host']['read']);
-                $this->config['host'] = $temp[0];
-                $this->config['port'] = $temp[1];
-            }
-
-            $this->config['shell'] = 'mysql -u' . $this->config['user'] . ' -p' . $this->config['pwd'] . ' ' . $this->config['name'] . ' -h' . $this->config['host'] . ' -P' . $this->config['port'] . ' -e ';
-        }
-
-        if ($state == true) {
-            # 异步执行
-            \Dever::run($this->config['shell'] . '"' . $sql . '"');
-        } else {
-            try {
-                $this->register();
-                # 同步执行
-                if (strpos($sql, ';')) {
-                    $temp = explode(';', $sql);
-                    foreach ($temp as $k => $v) {
-                        $this->update->query($v);
-                    }
-                } else {
-                    $this->update->query($sql);
-                }
-            } catch (\PDOException $exception) {
-                $this->error($exception->getMessage(), $sql);
-            }
-        }
-    }
-
-    /**
-     * insert the default value
-     *
-     * @return mixed
-     */
-    public function insertValues($value, $data = array())
-    {
-        $this->register();
-        $sql = $this->sql->insertValues($this->table, $value['col'], $value['value']);
-
-        try {
-            $this->update->query($sql);
-        } catch (\PDOException $exception) {
-            $this->error($exception->getMessage(), $sql);
-        }
-
-        $this->log($sql, 'insertValues');
-
-        $data['insert'] = $sql;
-
-        return $data;
-    }
-
-    /**
-     * all
-     *
-     * @return array
-     */
-    public function all($col, $call = false)
-    {
-        $config = false;
-        if (strpos($col, '|') !== false) {
-            $config = explode('|', $col);
-            $col = $config[0];
-        }
-        return $this->select($col, 'fetchAll', 'select', $config, $call);
-    }
-
-    /**
-     * one
-     *
-     * @return array
-     */
-    public function one($col)
-    {
-        return $this->select($col);
+        $sql = Sql::select($table, $param, $bind, $set, $field);
+        return $this->query($sql, $bind);
     }
-
-    /**
-     * count
-     *
-     * @return array
-     */
-    public function count($col = '')
+    public function count($table, $param, $field)
     {
-        return $this->select($col, 'fetchColumn', 'count');
+        return $this->select($table, $param, array('col'=>'count(*)'), $field)->fetch(\PDO::FETCH_NUM)[0];
     }
-
-    /**
-     * insert
-     *
-     * @return int
-     */
-    public function insert($num = 1)
+    public function insert($table, $data, $field)
     {
-        $sql = $this->sql->insert($this->table, $num);
-
-        if ($sql) {
-            try {
-                $this->register();
-
-                $handle = $this->update->prepare($sql);
-
-                $handle->execute($this->value);
-            } catch (\PDOException $exception) {
-                $this->error($exception->getMessage(), $sql);
-            }
-
-            $id = $this->update->id();
-
-            $this->log($sql, $this->value);
-
-            $this->cache();
-        }
-
-        $this->value = array();
-
-        return $id;
-    }
-
-    /**
-     * update
-     *
-     * @return int
-     */
-    public function update()
-    {
-        $sql = $this->sql->update($this->table);
-
-        $result = false;
-
-        if ($sql) {
-            try {
-                $this->register();
-
-                $handle = $this->update->prepare($sql);
-
-                $handle->execute($this->value);
-            } catch (\PDOException $exception) {
-                $this->error($exception->getMessage(), $sql);
-            }
-
-            $result = $handle->rowCount();
-            //$result = $this->update->id();
-
-            $this->log($sql, $this->value);
-
-            $this->cache();
-        }
-
-        $this->value = array();
-
-        return $result;
-    }
-
-    /**
-     * delete
-     *
-     * @return int
-     */
-    public function delete()
-    {
-        $sql = $this->sql->delete($this->table);
-
-        $result = false;
-
-        if ($sql) {
-            try {
-                $this->register();
-
-                $handle = $this->update->prepare($sql);
-
-                $handle->execute($this->value);
-            } catch (\PDOException $exception) {
-                $this->error($exception->getMessage(), $sql);
-            }
-
-            $result = $handle->rowCount();
-
-            $this->log($sql, $this->value);
-
-            $this->cache();
-        }
-
-        $this->value = array();
-
-        return $result;
+        $bind = array();
+        $sql = Sql::insert($table, $data, $bind, $field);
+        $this->query($sql, $bind, 'update');
+        return $this->update->lastInsertId();
     }
-
-    /**
-     * select
-     *
-     * @return array
-     */
-    private function selec1t($col = '', $method = 'fetch', $type = 'select', $config = false, $call = false)
+    public function update($table, $param, $data, $field)
     {
-        $sql = $this->sql->{$type}($this->table, $col);
-
-        $key = $this->table . '_' . $method . '_' . md5(serialize($this->value) . $sql);
-
-        $data = $this->cache($key);
-
-        if ($data !== false) {
-            if ($col != 'clear') {
-                $this->value = array();
-            }
-
-            return $data;
-        }
-
-        if ($type == 'count' && strpos($sql, 'group by `')) {
-            $method = 'fetchAll';
-        }
-
-        try {
-            $this->register();
-
-            if ($this->value) {
-                $handle = $this->read->prepare($sql);
-                /*
-                if ($method == 'fetchAll') {
-                    # 查询大量数据时,不缓存数据到内存,防止内存溢出
-                    $this->read->set();
-                }*/
-                $handle->execute($this->value);
-            } else {
-                $handle = $this->read->query($sql);
-            }
-        } catch (\PDOException $exception) {
-            $this->error($exception->getMessage(), $sql);
-        }
-
-        if ($method == 'fetchAll') {
-            if ($call) {
-                $this->value = array();
-            }
-            $data = $this->fetchAll($handle, $config, $call);
-            
-        } else {
-            $data = $handle->$method();
-        }
-
-        //print_r($data);die;
-        $state = $this->cache($key, 'put', $data);
-        $this->log($sql, $this->value, $data);
-        if ($col != 'clear') {
-            $this->value = array();
-        }
-        return $data;
+        $bind = array();
+        $sql = Sql::update($table, $param, $data, $bind, $field);
+        return $this->query($sql, $bind, 'update')->rowCount();
     }
-
-    /**
-     * join
-     *
-     * @return object
-     */
-    public function join($param)
+    public function delete($table, $param, $field)
     {
-        $this->sql->join($param);
-
-        return $this;
+        $bind = array();
+        $sql = Sql::delete($table, $param, $bind, $field);
+        return $this->query($sql, $bind, 'update')->rowCount();
     }
-
-    /**
-     * begin
-     *
-     * @return object
-     */
     public function begin()
     {
-        $this->register();
-        $this->update->method('beginTransaction');
-
-        return $this;
+        $this->update->beginTransaction();
     }
-
-    /**
-     * commit
-     *
-     * @return object
-     */
     public function commit()
     {
-        $this->register();
-        $this->update->method('commit');
-
-        return $this;
+        $this->update->commit();
     }
-
-    /**
-     * rollback
-     *
-     * @return object
-     */
     public function rollback()
     {
-        $this->register();
-        $this->update->method('rollBack');
-
-        return $this;
-    }
-
-    /**
-     * __call
-     *
-     * @return object
-     */
-    public function __call($method, $param)
-    {
-        if (isset($param[0]) && 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 call11($method, $param)
-    {
-        if ($method == 'where' || $method == 'set' || $method == 'add') {
-            # 特殊处理in
-            if (!strstr($param[1], '/') && strstr($param[1], 'select') && $param[1] != 'selected') {
-
-            } elseif (isset($param[2]) && ($param[2] == 'in' || $param[2] == 'not in')) {
-                if (!is_array($param[1])) {
-                    $param[1] = explode(',', $param[1]);
-                }
-
-                $prefix = 'in_';
-                $temp = $param[0];
-                foreach ($param[1] as $k => $v) {
-                    if (strpos($temp, '.')) {
-                        $temp = str_replace('.', '_', $temp);
-                    }
-                    $k = ':' . $temp . '_' . $prefix . $k;
-                    $key[] = $k;
-                    $this->value[$k] = $v;
-                }
-
-                $param[1] = '(' . implode(',', $key) . ')';
-            } else {
-                $prefix = $param[0];
-                if (strstr($prefix, ',')) {
-                    $prefix = str_replace(',', '_', $prefix);
-                }
-                if (strstr($prefix, '.')) {
-                    $prefix = str_replace('.', '_', $prefix);
-                }
-                if (strstr($prefix, '-')) {
-                    $prefix = str_replace('-', '_', $prefix);
-                }
-                $key = ':' . $prefix . count($this->value);
-
-                $this->value[$key] = $param[1];
-
-                $param[1] = $key;
-
-                if (isset($param[2]) && $param[2] == 'like') {
-                    $param[2] = 'like^' . $this->value[$key];
-                    //$this->value[$key] = trim($this->value[$key], ',');
-                }
-            }
-        }
-
-        $this->sql->$method($param);
+        $this->update->rollback();
     }
-}
+}

+ 126 - 269
src/Dever/String/Sql.php

@@ -27,8 +27,8 @@ class Sql
     }
     public static function create($config)
     {
-        $sql = 'DROP TABLE IF EXISTS `' . $config['name'] . '`;CREATE TABLE IF NOT EXISTS `' . $config['name'] . '`(';
-        $struct = array('id' => array('name' => 'ID', 'type' => 'int(11)'));
+        $sql = 'DROP TABLE IF EXISTS `' . $config['table'] . '`;CREATE TABLE IF NOT EXISTS `' . $config['table'] . '`(';
+        $struct = array('id' => array('name' => 'ID', 'type' => 'int(11)'),'cdate' => array('name' => 'cdate', 'type' => 'int(11)'));
         $struct = array_merge($struct, $config['struct']);
         foreach ($struct as $k => $v) {
             $sql .= self::createField($k, $v) . ',';
@@ -38,22 +38,9 @@ class Sql
             $sql .= ' AUTO_INCREMENT = ' . $config['auto'];
         }
         if (isset($config['type'])) {
-            $sql .= ' ENGINE = ' . $type . ';';
+            $sql .= ' ENGINE = ' . $config['type'] . ';';
         }
-        if (isset($config['partition'])) {
-            foreach ($partition['value'] as $k => $v) {
-                $partition['value'][$k] = 'PARTITION ' . $config['name'] .'_' . $k . ' VALUES ' . strtoupper($partition['exp']) . ' (' . $v . ')';
-            }
-            if (stristr($partition['exp'],'THEN')) {
-                $k = $k + 1;
-                $v = 'MAXVALUE';
-                $partition['value'][$k] = 'PARTITION ' . $config['name'] .'_' . $k . ' VALUES ' . strtoupper($partition['exp']) . ' ' . $v . '';
-            }
-            
-            $sql .= ' PARTITION BY ' . strtoupper($partition['type']).'('.$partition['col'].') ) (
-                '.implode(',', $partition['value']).');';
-        }
-        return $sql;
+        return $sql . ' COMMENT="'.$config['name'].'"';
     }
     public static function createField($name, $set)
     {
@@ -62,6 +49,8 @@ class Sql
             $field .= 'UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL';
         } elseif (isset($set['default']) && $set['default']) {
             $field .= ' NOT NULL DEFAULT "' . $set['default'] . '"';
+        } elseif (strpos($set['type'], 'int') !== false || strpos($set['type'], 'float') !== false || strpos($set['type'], 'decimal') !== false || strpos($set['type'], 'double') !== false) {
+            $field .= ' NOT NULL DEFAULT 0';
         } else {
             $field .= ' NULL';
         }
@@ -94,18 +83,15 @@ class Sql
                 $sql[] = $alter . ' ADD ' . self::createField($k, $v);
             }
         }
-        $sql = implode(';', $sql);
-        return $sql;
+        return implode(';', $sql);
     }
     public static function index($table, $index, $del = array())
     {
         $sql = array();
         $alter = 'ALTER TABLE `' . $table . '` ';
-        if ($del) {
-            foreach ($del as $v) {
-                if ($v['Key_name'] != 'PRIMARY') {
-                    $sql[] = $alter . ' DROP INDEX ' . $v['Key_name'];
-                }
+        foreach ($del as $v) {
+            if ($v['Key_name'] != 'PRIMARY') {
+                $sql[] = $alter . ' DROP INDEX ' . $v['Key_name'];
             }
         }
         if ($index) {
@@ -117,25 +103,30 @@ class Sql
                 $sql[] = $alter . ' ADD ' . strtoupper($t) . ' ' . $k . ' (' . $v . ')';
             }
         }
-        $sql = implode(';', $sql);
-        return $sql;
+        return implode(';', $sql);
     }
-    public static function select($table, $param, &$bind, $set = array())
+    public static function partition($table, $partition, $index)
+    {
+        $state = true;
+        foreach ($index as $k => $v) {
+            if ($v['Key_name'] == 'PRIMARY' && $v['Column_name'] == 'cdate') {
+                $state = false;
+            }
+        }
+        $sql = '';
+        if ($state) {
+            return 'ALTER TABLE `' . $table . '` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `cdate`) USING BTREE;ALTER TABLE `' . $table . '` PARTITION BY RANGE (cdate) (PARTITION p'.$partition.' VALUES LESS THAN ('.$partition.'));';
+        } else {
+            return 'ALTER TABLE `' . $table . '` ADD PARTITION (PARTITION p'.$partition.' VALUES LESS THAN ('.$partition.'));';
+        }
+    }
+    public static function select($table, $param, &$bind, $set = array(), $field = array())
     {
         $col = '*';
         $rule = '';
         if (isset($set['col'])) {
             $col = $set['col'];
         }
-        if (isset($set['group'])) {
-            $rule .= ' GROUP BY ' . $set['group'];
-        }
-        if (isset($set['order'])) {
-            $rule .= ' ORDER BY ' . $set['order'];
-        }
-        if (isset($set['limit'])) {
-            $rule .= ' LIMIT ' . $set['limit'];
-        }
         if (isset($set['join'])) {
             $temp = explode('_', $table);
             $prefix = $temp[0] . '_'  . $temp[1] . '_';
@@ -144,266 +135,132 @@ class Sql
                 $table .= ' ' . $v['type'] . ' ' . $prefix . $v['table'] . ' AS ' . $v['table'] . ' ON ' . $v['on'];
             }
         }
-        if ($param) {
-            $where = ' WHERE';
-            $i = 0;
-            foreach ($param as $k => $v) {
-                if (strpos($k, '#')) {
-                    $k = trim($k, '#');
-                }
-                if ($k == 'or' || $k == 'and') {
-                    $link = '';
-                    foreach ($v as $k1 => $v1) {
-                        self::where($link, $bind, $i, $k1, $v1);
-                    }
-                    $link = rtrim($link, 'and');
-                    $where = rtrim($where, 'and');
-                    $where .= $k.' (' . $link . ') and';
-                } else {
-                    self::where($where, $bind, $i, $k, $v);
-                }
-            }
-            $where = rtrim($where, 'and');
+        if (isset($set['group'])) {
+            $rule .= ' GROUP BY ' . $set['group'];
         }
-        return 'SELECT ' . $col . ' FROM ' . $table . $where . $rule;
-    }
-
-    # $this->orderBy = 'order by field(' . $param[0] . ', ' . $param[1] . ')';
-    private static function where(&$where, &$bind, &$i, $k, $v)
-    {
-        $j = ':' . $k . $i;
-        $k = ' `' . $k . '` ';
-        if (is_array($v)) {
-            if (empty($v[0])) {
-                foreach ($v as $k1 => $v1) {
-                    self::where($where, $bind, $i, $k1, $v1);
-                }
-                return;
-            }
-            if (empty($v[1])) {
-                $v[1] = '=';
-            }
-            if (empty($v[2])) {
-                $v[2] = 'and';
-            }
-            if ($v[1] == 'between') {
-                $jd = $j.'d';
-                if (is_string($v[0])) {
-                    $v[0] = explode(',', $v[0]);
-                }
-                $bind[$j] = $v[0][0];
-                $bind[$jd] = $v[0][1];
-                $where .= $k . 'between ' . $j . ' and ' . $jd;
-            } elseif ($v[1] == 'in') {
-                $where .= $k . 'in('.$j.')';
-            } elseif ($v[1] == 'like') {
-                if (strstr($v[0], ',')) {
-                    $concat = 'concat(",", ' . $j . ',",")';
-                } else {
-                    $concat = $j;
-                }
-                $instr = 'instr('.$concat.', ' . $k . ')';
-                $where .= $k . ' ' . $instr . ' > 0';
+        if (isset($set['order'])) {
+            $rule .= ' ORDER BY ' . $set['order'];
+        }
+        if (isset($set['limit'])) {
+            if (is_array($set['limit'])) {
+                $table .= ' inner join (select id from ' . $table . self::where($param, $bind, $field) . $rule . ' limit ' . $set['limit'][0].','.$set['limit'][1].') as t on '.$table.'.id=t.id';
+                $rule = '';
+                $param = false;
             } else {
-                $where .= $k . $v[1] . $j;
+                $rule .= ' LIMIT ' . $set['limit'];
             }
-            $where .= ' ' . $v[2];
-            $v = $v[0];
-        } else {
-            $where .= $k . '=' . $j . ' and';
-        }
-        if (empty($bind[$j])) {
-            $bind[$j] = $v;
         }
-        $i++;
+        return 'SELECT ' . $col . ' FROM ' . $table . self::where($param, $bind, $field) . $rule;
     }
-
-
-    private function col($col)
+    public static function where($param, &$bind, $field = array())
     {
-        $result = '';
-
-        if (is_array($col)) {
-            $array = array();
-            foreach ($col as $k => $v) {
-                if (!is_numeric($k)) {
-                    $array[] = $this->prefix . $k . ' AS ' . $v;
-                } else {
-                    $array[] = $this->prefix . $v;
-                }
-            }
-            $result = implode(' ', $array);
-        } else {
-            if ($col == '*' && isset($this->colOrder) && $this->colOrder) {
-                $col = array();
-                foreach ($this->colOrder as $k => $v) {
-                    if (!preg_match('/_no_col$/i', $k) && !preg_match('/^hr/i', $k))  {
-                        $col[] = '`'.$k.'`';
+        if ($param) {
+            $first = $second = '';
+            if (is_array($param)) {
+                $i = 0;
+                foreach ($param as $k => $v) {
+                    if (strpos($k, '#')) {
+                        $k = trim($k, '#');
+                    }
+                    if ($k == 'or' || $k == 'and') {
+                        $first_link = $second_link = '';
+                        foreach ($v as $k1 => $v1) {
+                            if (is_array($v1)) {
+                                self::field($second_link, $bind, $i, $k1, $v1[0], $v1[1], $field);
+                            } else {
+                                self::field($first_link, $bind, $i, $k1, '=', $v1, $field);
+                            }
+                        }
+                        $first_link = ltrim($first_link, ' and ');
+                        $second .= ' ' . $k . ' (' . $first_link . $second_link . ')';
+                    } else {
+                        if (is_array($v)) {
+                            self::field($second, $bind, $i, $k, $v[0], $v[1], $field);
+                        } else {
+                            self::field($first, $bind, $i, $k, '=', $v, $field);
+                        }
                     }
                 }
-                $col = implode(',', $col);
-                $this->colOrder = array();
-                //$col = '`' . implode('`,`', array_keys($this->colOrder)) . '`';
+                return ' WHERE ' . ltrim($first . $second, ' and ');
+            } elseif (is_numeric($param)) {
+                return ' WHERE id = ' . $param;
+            } else {
+                return ' WHERE ' . $param;
             }
-            $result = $col ? $this->prefix . $col : $this->prefix . '*';
-        }
-
-        if (isset($this->col) && $this->col) {
-            $result .= ',' . $this->col;
-        }
-
-        if (isset($this->score) && $this->score) {
-            $result .= ',(' . implode('+', $this->score) . ') as col_score';
-        }
-
-        if (isset($this->as) && $this->as) {
-            $result .= ','.implode(',', $this->as);
+        } else {
+            return '';
         }
-        return $result;
     }
-
-    /**
-     * count
-     *
-     * @return string
-     */
-    public static function count($table, $col = '')
+    private static function field(&$s, &$b, &$i, $k, $e, $v, $f)
     {
-        $where = $this->createWhere();
-
-        $state = 1;
-        if ($col == 'clear') {
-            $col = '';
-            $state = 2;
-        }
-
-        if (!$col || $col == '*') {
-            $col = 'count(*) as total';
+        if ($f && empty($f[$k]) && strpos('id,cdate', $k) === false) {
+            return;
         }
-
-        $join = isset($this->join) ? implode(' ', $this->join) : '';
-
-        if (strstr($this->group, 'group')) {
-            $sql = '`' . $table . '` ' . $join . $where . ' ' . $this->group . ' ';
-
-            $sql = 'SELECT ' . $col . ' FROM (SELECT '.$col.' FROM '.$sql.' ) a ';
-
+        $s .= ' and ';
+        $p = ':'.$k.$i;
+        $k = '`'.$k.'`';
+        if (strpos($e,'in') !== false) {
+            $s .= $k.$e.'('.$p.')';
+        } elseif ($e == 'like') {
+            $s .= 'instr('.$k.','.$p.')'.' > 0';
+        } elseif ($e == 'group') {
+            $v = ','.$v.',';
+            $s .= 'instr(concat(",",'.$k.',","),'.$p.')' . ' > 0';
+        } elseif ($e == 'between') {
+            $b[$p.'_e'] = $v[1];
+            $v = $v[0];
+            $s .= $k.' between '.$p.' and '.$p.'_e';
         } else {
-            $sql = 'SELECT ' . $col . ' FROM `' . $table . '` ' . $join . $where . ' ' . $this->group . ' ';
-        }
-
-        
-
-        if ($state == 1) {
-            $this->init();
+            $s .= $k.$e.$p;
         }
-
-        return $sql;
+        $b[$p] = $v;
+        $i++;
     }
-
-    /**
-     * insert
-     *
-     * @return string
-     */
-    public static function insert($table, $num = 1)
+    public static function insert($table, $data, &$bind, $field)
     {
-        $sql = 'INSERT INTO `' . $table . '` (' . implode(',', $this->col) . ') VALUES (' . implode(',', $this->value) . ')';
-
-        if ($num > 1) {
-            $insert = array();
-            for ($i = 1; $i < $num; $i++) {
-                $insert[] = '(' . implode(',', $this->value) . ')';
+        $sql = 'INSERT INTO `' . $table . '` SET ';#IGNORE
+        foreach ($data as $k => $v) {
+            if ($field && empty($field[$k]) && strpos('id,cdate', $k) === false) {
+                continue;
             }
-            $sql .= ',' . implode(',', $insert);
+            $sql .= '`' . $k . '`=:' . $k . ',';
+            $bind[':'.$k] = $v;
         }
-
-        $this->init();
-
-        return $sql;
-    }
-
-    /**
-     * replace
-     *
-     * @return string
-     */
-    public static function replace($table)
-    {
-        $sql = 'REPLACE INTO `' . $table . '` (' . implode(',', $this->col) . ') VALUES (' . implode(',', $this->value) . ')';
-
-        $this->init();
-
+        $sql = rtrim($sql, ',');
         return $sql;
     }
-
-    /**
-     * insertValues
-     *
-     * @return string
-     */
-    public static function insertValues($table, $col, $value)
+    public static function update($table, $param, $data, &$bind, $field)
     {
-        $sql = 'INSERT INTO `' . $table . '` (' . $col . ') VALUES ';
-
-        $max = count($value) - 1;
-
-        foreach ($value as $k => $v) {
-            $sql .= '(' . $v . ')';
-
-            if ($k >= $max) {
-                $sql .= '';
-            } else {
-                $sql .= ',';
+        $i = 0;
+        $sql = 'UPDATE `' . $table . '` SET ';
+        foreach ($data as $k => $v) {
+            if ($field && empty($field[$k]) && strpos('id,cdate', $k) === false) {
+                continue;
+            }
+            $a = '';
+            if (is_array($v)) {
+                $a = $v[1];
+                $v = $v[0];
             }
+            $sql .= '`' . $k . '`=`' . $k . '`' . $a . ':' . $k . ',';
+            $bind[':'.$k] = $v;
         }
-
-        return $sql;
+        return rtrim($sql, ',') . self::where($param, $bind, $field);
     }
-
-    /**
-     * update
-     *
-     * @return string
-     */
-    public static function update($table)
+    public static function delete($table, $param, &$bind, $field)
     {
-        if (!$this->where) {
-            return false;
-        }
-
-        $where = $this->createWhere();
-
-        $sql = 'UPDATE `' . $table . '` SET ' . implode(',', $this->value) . ' ' . $where;
-
-        //echo $sql;die;
-
-        $this->init();
-
-        return $sql;
+        return 'DELETE FROM `' . $table . '`' . self::where($param, $bind, $field);
     }
-
-    /**
-     * delete
-     *
-     * @return string
-     */
-    public static function delete($table)
+    public static function inserts($table, $param)
     {
-        if (!$this->where) {
-            return false;
-        }
-
-        $where = $this->createWhere();
-
-        if (!$where) {
-            return false;
+        $num = $param['num'] ?? 1;
+        $sql = 'INSERT INTO `' . $table . '` (' . $param['field'] . ') VALUES ';
+        foreach ($param['value'] as $k => $v) {
+            for ($i = 1; $i <= $num; $i++) {
+                $insert[] = '(' . $v . ')';
+            }
         }
-
-        $sql = 'DELETE FROM `' . $table . '` ' . $where;
-
-        $this->init();
-
-        return $sql;
+        $sql .= implode(',', $insert) . ',';
+        return rtrim($sql, ',');
     }
-}
+}

+ 2 - 2
src/Dever/Support/Date.php

@@ -1,5 +1,5 @@
-<?php namespace Dever;
-class Math
+<?php namespace Dever\Support;
+class Date
 {
 	public static function mdate($num, $type = 2)
     {

+ 1 - 2
src/Dever/Support/Math.php

@@ -1,4 +1,4 @@
-<?php namespace Dever;
+<?php namespace Dever\Support;
 class Math
 {
 	# 笛卡尔积
@@ -64,7 +64,6 @@ class Math
     {
         return 'round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2) as distance';
     }
-
     # 将数值金额转换为中文大写金额
     public static function convertNum($amount, $type = 1) {
         // 判断输出的金额是否为数字或数字字符串