rabin 1 năm trước cách đây
mục cha
commit
1eba95571e

+ 1 - 3
dever.php

@@ -5,10 +5,8 @@ class Dever
     private static $define = array();
     public static function run()
     {
-        if (Dever\Config::get('setting')['debug']) {
-            Dever\Debug::report();
-        }
         $route = Dever\Route::get();
+        Dever\Debug::init();
         Dever\Project::register();
         Dever\Output::success(self::load($route['l'], DEVER_APP_NAME, 'api'));
     }

+ 76 - 193
src/Dever/Debug.php

@@ -2,111 +2,113 @@
 use Dever;
 class Debug
 {
-    private static $data;
+    private static $data = array();
     private static $trace;
     private static $tool;
     private static $time;
     private static $memory;
+    public static $shell;
+    private static $start;
     public static function init()
     {
-        if (Config::get('setting')['debug'] && Dever::shell(Config::get('setting')['shell'])) {
-            return true;
-        }
-        return false;
-    }
-    public static function report()
-    {
-        ini_set('display_errors', true);
-        self::tool();
-    }
-    private static function tool()
-    {
-        if (self::$tool) {
-            return true;
-        }
-        if (class_exists('\\Whoops\\Run')) {
+        self::$shell = Dever::shell(Config::get('setting')['shell']);
+        if (self::$shell) {
+            ini_set('display_errors', true);
+            self::$start = microtime();
             self::$tool = new \Whoops\Run;
             self::$tool->pushHandler(new \Whoops\Handler\PrettyPageHandler);
-            if (\Whoops\Util\Misc::isAjaxRequest() || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST')) {
+            if (\Whoops\Util\Misc::isAjaxRequest()) {
                 self::$tool->pushHandler(new \Whoops\Handler\JsonResponseHandler);
             }
-
             self::$tool->register();
-            return true;
-        }
-        return false;
-    }
-    public static function overtime()
-    {
-        $time = Config::get('setting')['overtime'];
-        if ($time && self::time() > $time) {
-            self::error('Execution time timeout');
         }
     }
-    public static function log($msg, $type = 'log')
+    public static function add($msg, $type = 'log')
     {
-        if (self::init()) {
+        if (self::$shell) {
             $format = array('msg' => $msg);
             $format = array_merge($format, self::env());
-            $msg = Output::format($format);
-
-            self::add($type, $msg);
+            self::data($type, Output::format($format));
         }
     }
-    private static function env()
+    public static function out($data)
     {
-        $trace = self::trace();
-        return array
-        (
-            'time' => 'current:' . self::time(2) . ' total:' . self::time(),
-            'memory' => 'current:' . self::memory(2) . ' total:' . self::memory(),
-            'trace' => $trace
-        );
-    }
-    public static function error($msg)
-    {
-        if (Config::get('setting')['log']) {
-            $data = self::env();
-            $data['msg'] = $msg;
-            Log::add($msg);
-        }
-    }
-    public static function info($msg)
-    {
-        if (Config::get('setting')['log']) {
-            Log::add($msg, 3);
+        if (self::$shell) {
+            self::add($data, 'data');
+            self::runtime();
+            $handler = self::$tool->getHandlers();
+            foreach (self::$data as $k => $v) {
+                $handler[0]->addDataTable(ucwords($k), $v);
+            }
+            $title = 'Dever Debug!';
+            $handler[0]->setPageTitle($title);
+            throw new Exceptions($title);
         }
     }
-    public static function runtime()
+    public static function lib($class, $method)
     {
-        if (self::init()) {
-            self::add('runtime', Output::format(self::loadfile()), 'Load Files');
-            $msg = Output::format(array('time' => self::time(), 'memory' => self::memory()));
-            self::add('runtime', $msg, 'Total');
+        if (self::$shell) {
+            $class = new \ReflectionClass($class);
+            $trace['file'] = $class->getFileName();
+            $trace['line'] = $class->getStartLine();
+            $trace['class'] = $class->getName();
+            $trace['function'] = $method;
+            $content = explode("\n", file_get_contents($trace['file']));
+            foreach ($content as $k => $v) {
+                if (strpos($v, 'function ' . $method . '(')) {
+                    $trace['line'] = $k+1;
+                    break;
+                }
+            }
+            $key = $trace['file'] . ':' . $trace['line'];
+            self::$trace[$key] = $trace;
         }
     }
-    public static function trace($debug = false)
+    public static function trace()
     {
-        if (!$debug) {
-            $debug = debug_backtrace();
-        }
-        $trace = array();
+        $debug = debug_backtrace();
+        $trace = '';
         if ($debug) {
             foreach($debug as $k => $v) {
-                if (self::checkFile($v)) {
+                if (self::check($v)) {
                     $trace = $v['file'] . ':' . $v['line'];
                     self::$trace[$trace] = $v;
                     break;
                 }
             }
         }
-
         return $trace;
     }
-    private static function checkFile($value)
+    private static function data($method, $msg, $key = false)
+    {
+        if (self::$shell) {
+            if ($key) {
+                self::$data[$method][$key] = $msg;
+            } else {
+                self::$data[$method][] = $msg;
+            }
+        }
+    }
+    private static function runtime()
     {
-        if (isset($value['file']) && strpos($value['file'], DEVER_APP_PATH) !== false && isset($value['function']) && $value['function'] != '__callStatic') {
-            $config = array('data', 'template', 'assets', 'config', 'daemon', 'route', 'database', 'api', DEVER_ENTRY);
+        self::data('runtime', Output::format(self::loadfile()), 'Load Files');
+        $msg = Output::format(array('time' => self::time(), 'memory' => self::memory()));
+        self::data('runtime', $msg, 'Total');
+    }
+    private static function env()
+    {
+        $trace = self::trace();
+        return array
+        (
+            'time' => 'current:' . self::time(2) . ' total:' . self::time(),
+            'memory' => 'current:' . self::memory(2) . ' total:' . self::memory(),
+            'trace' => $trace
+        );
+    }
+    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);
             foreach ($config as $k => $v) {
                 if (strpos($value['file'], DEVER_APP_PATH . $v) !== false) {
                     return false;
@@ -116,20 +118,20 @@ class Debug
         }
         return false;
     }
-    public static function time($state = 1)
+    private static function time($state = 1)
     {
         $start = self::startTime($state);
         $end = self::endTime();
         return '[' . ($end - $start) . 'S]';
     }
-    public static function endTime()
+    private static function endTime()
     {
         self::$time = microtime();
         return self::createTime(self::$time);
     }
-    public static function startTime($state = 1)
+    private static function startTime($state = 1)
     {
-        $start = DEVER_START;
+        $start = self::$start;
         if ($state == 2 && self::$time) {
             $start = self::$time;
         }
@@ -147,7 +149,6 @@ class Debug
             $memory = $memory - self::$memory;
         }
         self::$memory = $memory;
-
         return '[' . ($memory / 1024) . 'KB]';
     }
     private static function loadfile()
@@ -162,14 +163,7 @@ class Debug
         }
         return $result;
     }
-    private static function add($method, $msg, $key = false)
-    {
-        if ($key) {
-            self::$data[$method][$key] = $msg;
-        } else {
-            self::$data[$method][] = $msg;
-        }
-    }
+    
     public static function sql($key = '')
     {
         if (Config::get('database')['sql']) {
@@ -192,115 +186,4 @@ class Debug
         }
         return array();
     }
-    public static function wait($msg = '', $notice = 'Dever Data Debug!')
-    {
-        if ($msg && self::tool()) {
-            self::runtime();
-            self::error($msg);
-
-            $function = Route::input('function');
-            if ($function) {
-                Output::error(json_encode($msg, JSON_UNESCAPED_UNICODE));
-            }
-            $handler = self::$tool->getHandlers();
-            if (self::$data && is_array(self::$data)) {
-                foreach (self::$data as $k => $v) {
-                    $handler[0]->addDataTable(ucwords($k), $v);
-                }
-            }
-            if (is_array($msg) && isset($msg['sql']) && isset($msg['error'])) {
-                $notice = $msg['error'];
-            }
-            if (is_array($msg)) {
-                $msg = array('array' => Output::format($msg));
-            } else {
-                $msg = array(gettype($msg) => $msg);
-            }
-            $handler[0]->addDataTable('Data', $msg);
-            $handler[0]->addDataTable('Env', array('time' => self::time(), 'memory' => self::memory(), 'trace' => self::trace()));
-
-            $handler[0]->setPageTitle($notice);
-            throw new Exceptions($notice);
-        } else {
-            print_r($msg);
-        }
-        die;
-    }
-    public static function data()
-    {
-        self::overtime();
-        if (self::init()) {
-            self::out();
-        }
-    }
-    public static function reflection($class, $method)
-    {
-        if (self::init()) {
-            $class = new \ReflectionClass($class);
-            $trace['file'] = $class->getFileName();
-            $trace['line'] = $class->getStartLine();
-            $trace['class'] = $class->getName();
-            $trace['function'] = $method;
-            $content = explode("\n", file_get_contents($trace['file']));
-            foreach ($content as $k => $v) {
-                if (strpos($v, 'function ' . $method . '(')) {
-                    $trace['line'] = $k+1;
-                    break;
-                }
-            }
-            $key = $trace['file'] . ':' . $trace['line'];
-            self::$trace[$key] = $trace;
-        }
-    }
-    public static function out()
-    {
-        self::runtime();
-
-        if (self::$data && is_array(self::$data)) {
-            if (self::tool()) {
-                $handler = self::$tool->getHandlers();
-                $title = 'Dever Runtime Debug!';
-                foreach (self::$data as $k => $v) {
-                    $handler[0]->addDataTable(ucwords($k), $v);
-                    $handler[0]->setPageTitle($title);
-                }
-                throw new Exceptions($title);
-            } else {
-                echo self::html();
-            }
-        }
-    }
-    private static function html($show = 'display:none;')
-    {
-        self::runtime();
-        $html = self::createHtml($show);
-        if (self::$data) {
-            foreach (self::$data as $k => $v) {
-                self::createA($html, $k);
-                $html .= '<div style="' . $show . '">';
-                $html .= '<table border="1" style="width:100%;">';
-                foreach ($v as $i => $j) {
-                    $html .= '<tr>';
-                    $html .= '<td>' . $j . '</td>';
-                    $html .= '</tr>';
-                }
-                $html .= '</table>';
-                $html .= '</div>';
-            }
-        }
-        $html .= '</div>';
-        return $html;
-    }
-    private static function createHtml($show)
-    {
-        $fix = 'fixed';
-        if (!$show) {
-            $fix = '';
-        }
-        return '<div style="position:' . $fix . ';z-index:10000;bottom:0;background:white;overflow:auto;width:100%;height:auto;">';
-    }
-    private static function createA(&$html, $k)
-    {
-        $html .= '<a style="font-size:14px;font-weight:bold;margin-left:5px;" href="javascript:;" onclick="var a = $(this).next();if(a.get(0).style.display == \'none\'){a.show();$(this).parent().height(500)}else if(a.get(0).style.display != \'none\'){a.hide();$(this).parent().height(\'auto\')}">' . $k . '</a>';
-    }
 }

+ 1 - 1
src/Dever/Import.php

@@ -75,7 +75,7 @@ class Import
     {
         $param = $this->loadDevelopParam();
         $data = $this->class->{$this->method}(...$param);
-        Debug::reflection($this->class, $this->method);
+        Debug::lib($this->class, $this->method);
         return $data;
     }
     private function loadDevelopParam()

+ 41 - 28
src/Dever/Model.php

@@ -27,47 +27,60 @@ class Model
         $setting = Config::get('setting')['database'][$store];
         $class = 'Dever\\Store\\' . $setting['type'];
         $this->store = $class::getInstance($store, $setting);
-        $this->create($file);
+        $this->load();
+        $this->init($file);
     }
-
-    private function create(&$file)
+    private function load()
     {
-        if (isset($this->config['struct'])) {
-            $file = Path::get($file);
-            if (is_file($file)) {
-                $data = include $file;
-                if ($data['struct'] < count($this->config['struct'])) {
-                    $this->store->alter($this->config);
-                    $data['struct'] = count($this->config['struct']);
-                    file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
+        if (isset($this->config['lang']) && isset($this->config['struct']) && $pack = Config::get('setting')['lang_pack']) {
+            foreach ($this->config['lang'] as $lang) {
+                if (isset($this->config['struct'][$lang])) {
+                    foreach ($pack as $key => $value) {
+                        if (Config::get('setting')['lang'] != $key) {
+                            $this->config['struct'][$key . '_' . $lang] = $this->config['struct'][$lang];
+                        }
+                    }
                 }
-            } else {
-                $this->store->create($this->config);
-                $data['struct'] = count($this->config['struct']);
-                file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
             }
         }
     }
-
-    private function loadLang(&$config)
+    private function init($file)
     {
-        if (isset($config['lang']) && isset($config['struct']) && $pack = Config::get('setting')['lang_pack']) {
-            foreach ($config['lang'] as $lang) {
-                if (isset($config['struct'][$lang])) {
-                    foreach ($pack as $key => $value) {
-                        if (Config::get('setting')['lang'] != $key) {
-                            $config['struct'][$key . '_' . $lang] = $config['struct'][$lang];
-                        }
-                    }
+        $data['struct'] = $data['index'] = 0;
+        $file = Path::get($file);
+        if (is_file($file)) {
+            $data = include $file;
+        }
+        foreach ($data as $k => $v) {
+            if (isset($this->config[$k])) {
+                $num = count($this->config[$k]);
+                if ($v != $num) {
+                    $this->store->$k($this->config, $v);
+                    $data[$k] = $num;
+                    file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
                 }
             }
         }
     }
 
-    public function select($where = array(), $call = false)
+    public function select($param = array(), $set, $call = false)
     {
-        print_r($where);die;
-        return $this->store->select($where, $call);
+        return $this->store->select($this->config['name'], $param, $set);
+        if ($call) {
+
+        }
+        if ($data) {
+            if (isset($set['reset'])) {
+                $result = $this->reset($data, $set['reset'], $call);
+            } else {
+                if ($call) {
+                    foreach ($data as $row) {
+                        $result[] = $call($row);
+                    }
+                }
+            }
+        }
+        return $result;
     }
 
     public function find($where, $call = false)

+ 9 - 6
src/Dever/Output.php

@@ -6,7 +6,13 @@ class Output
     private static $format = 'json';
     public static function success($data, $uuid = false, $code = 0)
     {
-        Debug::data();
+        if (is_object($data)) {# && $data instanceof \Generator
+            $result = array();
+            foreach ($data as $v) {
+                $result[] = $v;
+            }
+            $data = $result;
+        }
         $result = array();
         $result['status'] = 1;
         $result['msg'] = 'success';
@@ -18,7 +24,6 @@ class Output
         if ($uuid) {
             $result['uuid'] = Dever::string('uuid');
         }
-        
         return self::handle($result);
     }
     public static function error($msg, $param = array(), $code = 1)
@@ -49,10 +54,8 @@ class Output
         self::json($result);
         self::callback($result);
         self::func($result);
-        Log::add($result);
-        if (Debug::init()) {
-            Debug::wait($result);
-        } elseif (self::$format == 'json') {
+        Debug::out($result);
+        if (self::$format == 'json') {
             print_r($result);
         } else {
             self::html($result);

+ 1 - 1
src/Dever/Secure.php

@@ -255,7 +255,7 @@ class Secure
         $data['api'] = $api;
         $data['request'] = $request;
         //$data['response'] = $response;
-        Debug::info($data);
+        Log::add($msg, 3);
     }
 
     /**

+ 32 - 82
src/Dever/Store/Base.php

@@ -23,23 +23,9 @@ class Base
             $this->update =&$this->read;
         }
     }
-    protected function error($msg, $sql = '')
+    protected function error($msg, $param = '')
     {
-        if (isset($this->sql)) {
-            if ($sql) {
-                $msg = array('sql' => $sql, 'error' => $msg);
-            } elseif (is_object($msg)) {
-                $msg = (array) $msg;
-            } elseif (is_string($msg)) {
-                $msg = array('sql' => $msg);
-            }
-            Debug::wait($msg, 'Dever SQL DB Error!');
-        } else {
-            if (is_string($msg)) {
-                $msg = array('value' => $msg);
-            }
-            Debug::wait($msg, 'Dever NOSQL DB Error!');
-        }
+        Debug::wait(array('msg' => $msg, 'param' => $param), 'Dever DB Error!');
     }
     protected function log($value, $param = array(), $data = array())
     {
@@ -49,9 +35,9 @@ class Base
             if (!Dever::shell('all') && is_array($data)) {
                 $data = count($data) . ' records';
             }
-            Debug::log(array('sql' => $value, 'data' => $data), $this->config['type']);
+            Debug::add(array('sql' => $value, 'data' => $data), $this->config['type']);
         } else {
-            Debug::log(array('value' => $value, 'method' => $param), $this->config['type']);
+            Debug::add(array('value' => $value, 'method' => $param), $this->config['type']);
         }
     }
     public function sql($value)
@@ -80,6 +66,34 @@ class Base
 
         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
@@ -312,68 +326,4 @@ class Base
     {
         return $this;
     }
-
-    /**
-     * fetchAll
-     *
-     * @return array
-     */
-    protected function fetchAll($handle, $config = false, $call = false)
-    {
-        $rows = function() use ($handle) {
-            while ($row = $handle->fetch()) {
-                yield $row;
-            }
-        };
-        $result = array();
-        $data = $rows();
-        if ($data) {
-            if ($config) {
-                $result = $this->fetchSet($data, $config, $call);
-            } else {
-                foreach ($data as $row) {
-                    $result[] = $this->cb($call, $row);
-                }
-            }
-        }
-        return $result;
-    }
-
-    /**
-     * call
-     *
-     * @return array
-     */
-    protected function cb($call, $data)
-    {
-        if ($call) {
-            return $call($data);
-        }
-        return $data;
-    }
-
-    /**
-     * fetchSet
-     *
-     * @return array
-     */
-    protected function fetchSet($data, $config, $call = false)
-    {
-        $result = array();
-        $key = $config[1];
-        foreach ($data as $row) {
-            if (isset($row[$key])) {
-                if (isset($config[3]) && isset($row[$config[2]])) {
-                    $result[$row[$key]][$row[$config[2]]] = $this->cb($call, $row);
-                } elseif (isset($config[2]) && isset($row[$config[2]])) {
-                    $result[$row[$key]] = $this->cb($call, $row[$config[2]]);
-                } elseif (isset($config[2])) {
-                    $result[$row[$key]][] = $this->cb($call, $row);
-                } else {
-                    $result[$row[$key]] = $this->cb($call, $row);
-                }
-            }
-        }
-        return $result;
-    }
 }

+ 44 - 48
src/Dever/Store/Pdo.php

@@ -30,7 +30,7 @@ class Pdo extends Base
             $handle->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
             //$handle->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
             //handle->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
-            Debug::log('db ' . $setting['host'] . ' connected', $setting['type']);
+            Debug::add('db ' . $setting['host'] . ' connected', $setting['type']);
             return $handle;
         } catch (\PDOException $e) {
             if (strstr($e->getMessage(), 'Unknown database')) {
@@ -61,66 +61,44 @@ class Pdo extends Base
             $close($link);
         }
     }
-    public function create($config)
+    public function struct($config, $state = 0)
     {
-        $this->exe(Sql::create($config));
+        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;
+            }
+        } else {
+            $this->exe(Sql::create($config));
+        }
     }
-    public function alter($config)
+    public function index($config, $state = 0)
     {
-        $data = $this->exe(Sql::desc($config['name']), array(), 'fetchAll');
-        $this->exe(Sql::alter($config['name'], $config['struct'], $data));
+        $this->exe(Sql::index($config['name'], $config['index'], $this->fetchAll(Sql::showIndex($config['name']))));
     }
-    public function getIndex($version, $index)
+    public function fetchAll($sql)
     {
-        $sql = Sql::showIndex($this->table);
-
-        $handle = $this->update->query($sql);
-
-        $info = $handle->fetchAll();
-
-        $this->dropIndex($info);
-
-        $sql = $this->sql->index($this->table, $index[$version]);
-
-        $this->query($sql);
-
-        $this->log($sql, 'index');
-
-        $data['index'] = $version;
-
-        $data['order'] = array_flip(explode(',', array_shift($index[$version])));
-
-        return $data;
+        return $this->exe($sql, 'fetchAll');
     }
-
-    private function dropIndex($info)
+    public function exe($sql, $method = '')
     {
-        if ($info) {
-            $this->register();
-            $index = array();
-            foreach ($info as $k => $v) {
-                if ($v['Key_name'] != 'PRIMARY' && !isset($index[$v['Key_name']])) {
-                    $sql = $this->sql->dropIndex($this->table, $v['Key_name']);
-                    $this->update->query($sql);
-                    $index[$v['Key_name']] = true;
-                }
-            }
+        $bind = false;
+        if (is_array($sql)) {
+            $bind = $sql[1];
+            $sql = $sql[0];
         }
-    }
-    public function exe($sql, $value = array(), $method = '')
-    {
         if (stristr($sql, 'select')) {
             $db = $this->read;
         } else {
             $db = $this->update;
         }
         try {
-            if ($value) {
-                if (!is_array($value)) {
-                    $value = array(':0' => $value);
+            if ($bind) {
+                if (!is_array($bind)) {
+                    $bind = array(':0' => $bind);
                 }
                 $handle = $db->prepare($sql);
-                $handle->execute($value);
+                $handle->execute($bind);
             } else {
                 $handle = $db->query($sql);
             }
@@ -129,12 +107,30 @@ class Pdo extends Base
         }
         if ($method) {
             $data = $handle->$method();
-            //$this->log($sql, $value, $data);
+            //$this->log($sql, $bind, $data);
             return $data;
         } else {
             return $handle;
         }
     }
+    public function select($table, $param, $set = array())
+    {
+        $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)
     {
@@ -337,7 +333,7 @@ class Pdo extends Base
      *
      * @return array
      */
-    private function select($col = '', $method = 'fetch', $type = 'select', $config = false, $call = false)
+    private function selec1t($col = '', $method = 'fetch', $type = 'select', $config = false, $call = false)
     {
         $sql = $this->sql->{$type}($this->table, $col);
 
@@ -468,7 +464,7 @@ class Pdo extends Base
      *
      * @return mixd
      */
-    private function call($method, $param)
+    private function call11($method, $param)
     {
         if ($method == 'where' || $method == 'set' || $method == 'add') {
             # 特殊处理in

+ 159 - 493
src/Dever/String/Sql.php

@@ -5,24 +5,33 @@ class Sql
     {
         $this->colOrder = $order;
     }
+    public static function desc($table)
+    {
+        return 'DESC ' . $table;
+    }
+    public static function truncate($table)
+    {
+        return 'TRUNCATE TABLE `' . $table . '`';
+    }
+    public static function opt($table)
+    {
+        return 'OPTIMIZE TABLE `' . $table . '`';
+    }
+    public static function explain($sql)
+    {
+        return 'EXPLAIN ' . $sql;
+    }
+    public static function showIndex($table)
+    {
+        return 'SHOW INDEX FROM ' . $table;
+    }
     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)'));
         $struct = array_merge($struct, $config['struct']);
         foreach ($struct as $k => $v) {
-            $sql .= '`' . $k . '` ' . strtoupper($v['type']);
-            if ($k == 'id') {
-                $sql .= 'UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL';
-            } elseif (isset($v['default']) && $v['default']) {
-                $sql .= ' NOT NULL DEFAULT "' . $v['default'] . '"';
-            } else {
-                $sql .= ' NULL';
-            }
-            if (isset($v['name'])) {
-                $sql .= ' COMMENT \'' . $v['name'] . '\'';
-            }
-            $sql .= ',';
+            $sql .= self::createField($k, $v) . ',';
         }
         $sql = rtrim($sql, ',') . ')';
         if (isset($config['auto'])) {
@@ -46,93 +55,170 @@ class Sql
         }
         return $sql;
     }
-    public static function desc($table)
+    public static function createField($name, $set)
     {
-        return 'DESC ' . $table;
+        $field = '`' . $name . '` ' . strtoupper($set['type']);
+        if ($name == 'id') {
+            $field .= 'UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL';
+        } elseif (isset($set['default']) && $set['default']) {
+            $field .= ' NOT NULL DEFAULT "' . $set['default'] . '"';
+        } else {
+            $field .= ' NULL';
+        }
+        if (isset($set['name'])) {
+            $field .= ' COMMENT \'' . $set['name'] . '\'';
+        }
+        return $field;
     }
     public static function alter($table, $struct, $data)
     {
-        print_r($struct);
-        print_r($data);
-
-        $drop = $change = array();
+        $sql = array();
+        $alter = 'ALTER TABLE `' . $table . '` ';
         foreach ($data as $v) {
-            if ($v['Field'] != 'id') {
-                if (!isset($struct[$v['Field']])) {
-                    $drop[] = $v['Field'];
-                } elseif (isset($struct[$v['Field']])) {
-                    if ($struct[$v['Field']]['type'] != $v['Type'] || (isset($struct[$v['Field']]['default']) && $struct[$v['Field']]['default'] != $v['Default'])) {
-                        $change[] = $struct[$v['Field']];
+            $field = $v['Field'];
+            if ($field != 'id') {
+                $set = $struct[$field] ?? false;
+                if ($set) {
+                    if ($set['type'] != $v['Type'] || (isset($set['default']) && $set['default'] != $v['Default'])) {
+                        $sql[] = $alter . ' CHANGE `' . $field . '` `' . $field . '` ' . self::createField($field, $set);
                     } else {
-                        unset($struct[$v['Field']]);
+                        unset($struct[$field]);
                     }
+                } else {
+                    $sql[] = $alter . ' DROP `' . $field . '`';
                 }
             }
         }
-
-        print_r($drop);
-        print_r($change);
-        print_r($struct);
-        die;
-
-        $alter = 'ALTER TABLE `' . $table;
-
-        if ($drop) {
-            
+        if ($struct) {
+            foreach ($struct as $k => $v) {
+                $sql[] = $alter . ' ADD ' . self::createField($k, $v);
+            }
         }
-
+        $sql = implode(';', $sql);
+        return $sql;
+    }
+    public static function index($table, $index, $del = array())
+    {
         $sql = array();
-        foreach ($config as $k => $v) {
-            if (!isset($v['default'])) {
-                $v['default'] = '';
+        $alter = 'ALTER TABLE `' . $table . '` ';
+        if ($del) {
+            foreach ($del as $v) {
+                if ($v['Key_name'] != 'PRIMARY') {
+                    $sql[] = $alter . ' DROP INDEX ' . $v['Key_name'];
+                }
             }
-            if (isset($v['type'])) {
-                $v = array
-                    (
-                    'add', $k, $k, $v['type'] . ' ' . $v['default'] . ' ' . $v['name'],
-                );
+        }
+        if ($index) {
+            foreach ($index as $k => $v) {
+                $t = 'INDEX';
+                if (strpos($v, '.')) {
+                    list($v, $t) = explode('.', $v);
+                }
+                $sql[] = $alter . ' ADD ' . strtoupper($t) . ' ' . $k . ' (' . $v . ')';
             }
-
-            if (empty($v[3])) {
-                continue;
+        }
+        $sql = implode(';', $sql);
+        return $sql;
+    }
+    public static function select($table, $param, &$bind, $set = 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] . '_';
+            $table .= ' AS ' . $temp[2];
+            foreach ($set['join'] as $k => $v) {
+                $table .= ' ' . $v['type'] . ' ' . $prefix . $v['table'] . ' AS ' . $v['table'] . ' ON ' . $v['on'];
             }
-            $sql[$k] = '';
-            if (isset($v[3]) && strpos($v[3], ' ') !== false) {
-                $com = explode(' ', $v[3]);
-                if (strstr($com[0], '-')) {
-                    $sql[$k] = str_replace('-', '(', $com[0]) . ') ';
-                } else {
-                    $sql[$k] = $com[0] . ' ';
+        }
+        if ($param) {
+            $where = ' WHERE';
+            $i = 0;
+            foreach ($param as $k => $v) {
+                if (strpos($k, '#')) {
+                    $k = trim($k, '#');
                 }
-                
-                if (isset($com[1]) && $com[1] != '') {
-                    $sql[$k] .= 'not null default \'' . $com[1] . '\'';
+                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 {
-                    $sql[$k] .= 'not null';
+                    self::where($where, $bind, $i, $k, $v);
                 }
+            }
+            $where = rtrim($where, 'and');
+        }
+        return 'SELECT ' . $col . ' FROM ' . $table . $where . $rule;
+    }
 
-                if (!empty($com[2])) {
-                    $sql[$k] .= ' comment \'' . $com[2] . '\'';
+    # $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);
                 }
-                $sql[$k] = strtoupper($sql[$k]);
+                return;
             }
-
-            if ($v[0] == 'add') {
-                # 新增字段
-                $sql[$k] = $alter . '` ADD `' . $v[1] . '` ' . $sql[$k];
-            } elseif ($v[0] == 'delete') {
-                # 删除字段
-                $sql[$k] = $alter . '` DROP `' . $v[1] . '`';
+            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';
             } else {
-                # 修改字段
-                $sql[$k] = $alter . '` CHANGE `' . $v[1] . '` `' . $v[2] . '` ' . $sql[$k];
+                $where .= $k . $v[1] . $j;
             }
+            $where .= ' ' . $v[2];
+            $v = $v[0];
+        } else {
+            $where .= $k . '=' . $j . ' and';
+        }
+        if (empty($bind[$j])) {
+            $bind[$j] = $v;
         }
+        $i++;
+    }
 
-        $sql = implode(';', $sql);
 
-        return $sql;
-    }
     private function col($col)
     {
         $result = '';
@@ -175,42 +261,6 @@ class Sql
         }
         return $result;
     }
-    public static function select($table, $col = '')
-    {
-        $this->table = $table;
-        $where = $this->createWhere();
-
-        $join = isset($this->join) ? implode(' ', $this->join) : '';
-
-        if (isset($this->orderBy) && $this->orderBy) {
-            $order = $this->orderBy;
-        } else {
-            $order = $this->order;
-        }
-        $sql = 'SELECT ' . $this->col($col) . ' FROM `' . $table . '` ' . $join . $where . ' ' . $this->group . ' ' . $order . ' ' . $this->limit;
-
-        $this->init();
-
-        return $sql;
-    }
-    public static function createWhere()
-    {
-        $where = '';
-        if ($this->where) {
-            if (isset($this->colOrder)) {
-                ksort($this->where);
-            }
-
-            if (isset($this->between)) {
-                $where = 'WHERE ' . $this->between . ' ' . implode(' ', $this->where);
-                $this->limit = '';
-            } else {
-                $where = 'WHERE ' . ltrim(implode(' ', $this->where), ' and');
-            }
-        }
-
-        return $where;
-    }
 
     /**
      * count
@@ -250,48 +300,6 @@ class Sql
 
         return $sql;
     }
-    public static function showIndex($table)
-    {
-        return 'SHOW INDEX FROM ' . $table . ' ';
-    }
-    public static function dropIndex($table, $name)
-    {
-        return 'ALTER TABLE ' . $table . ' DROP INDEX ' . $name;
-    }
-
-    /**
-     * index
-     *
-     * @return string
-     */
-    public static function index($table, $value)
-    {
-        $sql = 'ALTER TABLE ' . $table . ' ';
-
-        $max = count($value) - 1;
-
-        $i = 0;
-
-        foreach ($value as $k => $v) {
-            $type = 'INDEX';
-            if (strpos($v, '.')) {
-                $t = explode('.', $v);
-                $v = $t[0];
-                $type = ucwords($t[1]);
-            }
-            $sql .= 'ADD ' . $type . ' ' . $k . ' (' . $v . ')';
-
-            if ($i >= $max) {
-                $sql .= '';
-            } else {
-                $sql .= ',';
-            }
-
-            $i++;
-        }
-
-        return $sql;
-    }
 
     /**
      * insert
@@ -353,18 +361,6 @@ class Sql
         return $sql;
     }
 
-    /**
-     * explain
-     *
-     * @return string
-     */
-    public static function explain($sql)
-    {
-        $sql = 'EXPLAIN ' . $sql . ' ';
-
-        return $sql;
-    }
-
     /**
      * update
      *
@@ -410,334 +406,4 @@ class Sql
 
         return $sql;
     }
-
-    /**
-     * truncate
-     *
-     * @return string
-     */
-    public static function truncate($table)
-    {
-        $sql = 'TRUNCATE TABLE `' . $table . '`';
-
-        return $sql;
-    }
-
-    /**
-     * opt
-     *
-     * @return string
-     */
-    public static function opt($table)
-    {
-        $sql = 'OPTIMIZE TABLE `' . $table . '`';
-
-        return $sql;
-    }
-
-    /**
-     * sql
-     *
-     * @return string
-     */
-    public static function sql($sql)
-    {
-        return $sql;
-    }
-
-    /**
-     * init
-     *
-     * @return object
-     */
-    public static function init()
-    {
-        $this->where = $this->value = $this->col = $this->join = $this->score = $this->as = array();
-        $this->order = '';
-        $this->orderBy = '';
-        $this->group = '';
-        $this->limit = '';
-        $this->prefix = '';
-
-        return $this;
-    }
-
-    /**
-     * where
-     *
-     * @return string
-     */
-    public static function where($param)
-    {
-        $where = '';
-        if (empty($param[2])) {
-            $param[2] = '=';
-        }
-
-        $value = 1;
-        if (strpos($param[2], '|') !== false) {
-            $temp = explode('|', $param[2]);
-            $param[2] = $temp[0];
-            $value = $temp[1];
-        }
-
-        if (empty($param[3])) {
-            $param[3] = 'and';
-        }
-
-        if (strpos($param[3], '))') !== false) {
-            $temp = explode('))', $param[3]);
-            $param[3] = $temp[0];
-            $where = ' ))';
-        } elseif (strpos($param[3], ')') !== false) {
-            $temp = explode(')', $param[3]);
-            $param[3] = $temp[0];
-            $where = ' )';
-        }
-
-        $col = '`s_' . $param[0] . '`';
-
-        /*
-        if (!strpos($param[0], '.')) {
-            $param[0] = $this->prefix . '`' . $param[0] . '`';
-        }*/
-
-        if (!$this->prefix && !strstr($param[0], ',')) {
-            $param[0] = '`' . $param[0] . '`';
-        }
-        if (strstr($param[0], ',')) {
-            $temp = explode(',', $param[0]);
-            $w = array();
-            if (strstr($param[2], 'like')) {
-                $content = explode('^', $param[2]);
-                $param[2] = $content[0];
-
-                foreach ($temp as $v) {
-                    if (isset($content[1]) && strstr($content[1], ',')) {
-                        $concat = 'concat(",", ' . $v . ',",")';
-                    } else {
-                        $concat = $v;
-                    }
-                    $instr = 'instr('.$concat.', ' . $param[1] . ')';
-                    $w[] = ' ' . $instr . ' > 0 ';
-                }
-            } else {
-                foreach ($temp as $v) {
-                    $w[] = '`' . $v . '` '. $param[2] . ' ' . $param[1];
-                }
-            }
-            
-            $where = ' and (' . implode(' or ', $w) . ') ' . $where;
-        } elseif (strstr($param[2], 'like')) {
-            $content = explode('^', $param[2]);
-            $param[2] = $content[0];
-
-            if (isset($content[1]) && strstr($content[1], ',')) {
-                //$content[1] = trim($content[1], ',');
-                $len = strlen($content[1]);
-                if (isset($content[1]) && strstr($content[1], ',')) {
-                    $concat = 'concat(' . $param[0] . ',",")';
-                } else {
-                    $concat = $param[0];
-                }
-                $instr = 'left('.$concat.', '.$len.')=' . $param[1];
-                $where = $param[3] . ' ' . $instr . $where;
-            } else {
-                if ((isset($content[1]) && strstr($content[1], ',')) || $param[2] == 'like_num') {
-                    $concat = 'concat(",", ' . $param[0] . ',",")';
-                } else {
-                    $concat = $param[0];
-                }
-                $instr = 'instr('.$concat.', ' . $param[1] . ')';
-                $where = $param[3] . ' ' . $instr . ' > 0' . $where;
-                if ($param[2] == 'like_score') {
-                    $this->orderBy = 'order by col_score desc';
-                    $this->score[] = 'IF('.$instr.', IF(' . $param[0] . '=' . $param[1] . ', 1000*'.$value.', (100-'.$instr.')*'.$value.'), 0)';
-                }
-            }
-            
-            //$this->as[] = ' CONCAT("<em class=\"dever_highlight\">",' . $param[0] . ',"</em>") as ' . $col;
-        } else {
-            $where = $param[3] . ' ' . $param[0] . ' ' . $param[2] . ' ' . $param[1] . $where;
-        }
-
-        if ($param[2] == 'in' && isset($param[4]) && $param[4] == 'order') {
-            $param[1] = str_replace(array('(',')'), '', $param[1]);
-            $this->orderBy = 'order by field(' . $param[0] . ', ' . $param[1] . ')';
-        }
-
-        if (isset($this->colOrder)) {
-            $param[0] = trim($param[0], '`');
-            if (isset($this->colOrder[$param[0]])) {
-                $this->where[$this->colOrder[$param[0]]] = $where;
-            } else {
-                $num = count($this->where) + 100;
-                $this->where[$num] = $where;
-            }
-        } else {
-            $this->where[] = $where;
-        }
-    }
-
-    /**
-     * order
-     *
-     * @return string
-     */
-    public static function order($param)
-    {
-        if (is_array($param[0])) {
-            $this->order = 'order by ';
-            foreach ($param[0] as $k => $v) {
-                $k1 = '';
-                if (strpos($k, '.')) {
-                    $t = explode('.', $k);
-                    $k = $t[1];
-                    $k1 = $t[0] . '.';
-                }
-                $order[] = $k1 . '`' . $k . '` ' . $v;
-            }
-
-            $this->order .= implode(',', $order);
-
-            //echo $this->order;die;
-        } else {
-            if (empty($param[1])) {
-                $param[1] = 'desc';
-            }
-
-            if (!strstr($param[0], '()') && !strstr($param[0], 't_')) {
-                $param[0] = '`'.$param[0].'`';
-            }
-
-            $this->order = 'order by ' . $param[0] . ' ' . $param[1];
-        }
-    }
-
-    /**
-     * group
-     *
-     * @return string
-     */
-    public static function group($param)
-    {
-        if (is_array($param) && isset($param[0])) {
-            //$param = trim(implode(',', $param), ',');
-            $param = $param[0];
-
-            $this->group = ' group by ' . $param;
-        }
-
-        # 去掉id的分组,没用
-        if (is_string($param) && $param != 'id') {
-            if ($param == 'day') {
-                $this->col = 'FROM_UNIXTIME(day, "%Y-%m-%d") as day';
-            } elseif ($param == 'month') {
-                $this->col = 'FROM_UNIXTIME(day, "%Y-%m") as month';
-            } elseif ($param == 'year') {
-                $this->col = 'FROM_UNIXTIME(day, "%Y") as year';
-            } elseif (strpos($param, ',') === false) {
-                $this->col = $param;
-            }
-
-            $this->group = ' group by ' . $param;
-        }
-    }
-
-    /**
-     * join 为临时解决方案,不建议用join
-     *
-     * @return string
-     */
-    public static function join($param)
-    {
-        $this->prefix = 't_1.';
-        $this->join[] = 'as t_1';
-        $num = 2;
-        foreach ($param as $k => $v) {
-            if ($v) {
-                $table = 't_' . $num;
-                if (strpos($v['on'][0], '.') === false) {
-                    $v['on'][0] = 't_1.`' . $v['on'][0] . '`';
-                } else {
-                    $v['on'][0] = 't_' . $v['on'][0];
-                }
-
-                $v['on'][1] = $table . '.`' . $v['on'][1] . '`';
-
-                $v['table'] = str_replace('/', '_', $v['table']);
-                if (DEVER_PROJECT != 'default') {
-                    $v['table'] = DEVER_PROJECT . '_' . $v['table'];
-                }
-
-                $this->join[] = ucwords($v['type']) . ' `' . $v['table'] . '` AS ' . $table . ' ON ' . $v['on'][0] . '=' . $v['on'][1] . ' ';
-
-                if (isset($v['col'])) {
-                    $this->col = $v['col'];
-                }
-
-                $num++;
-            }
-        }
-    }
-
-    /**
-     * limit
-     *
-     * @return string
-     */
-    public static function limit($param)
-    {
-        //if(empty($param[1])) $param[1] = 0;
-
-        //$this->limit = 'limit ' . $param[1] . ',' . $param[0];
-        $this->limit = 'limit ' . $param[0];
-
-        //$this->between = ' `id` BETWEEN ' . $param[1] . ' AND ' . ($param[1] + $param[0]);
-    }
-
-    /**
-     * reset limit
-     *
-     * @return string
-     */
-    public static function reset($param)
-    {
-        $this->{$param[0]} = '';
-    }
-
-    /**
-     * add
-     *
-     * @return string
-     */
-    public static function add($param)
-    {
-        $this->col[] = '`' . $param[0] . '`';
-        $this->value[] = $param[1];
-    }
-
-    /**
-     * set
-     *
-     * @return string
-     */
-    public static function set($param)
-    {
-        if (empty($param[2])) {
-            $param[2] = '=';
-        }
-
-        $param[0] = '`' . $param[0] . '`';
-
-        if (strpos($param[2], '+=') !== false) {
-            $param[2] = '=' . $param[0] . '+';
-        }
-
-        if (strpos($param[2], '-=') !== false) {
-            $param[2] = '=' . $param[0] . '-';
-        }
-
-        $this->value[] = $param[0] . $param[2] . $param[1];
-    }
 }

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

@@ -51,7 +51,6 @@ class Math
             return $result;
         }
     }
-
     public static function ddate($num)
     {
         $date = date('Ymd');
@@ -75,7 +74,6 @@ class Math
         }
         return date('Y-m-d H:i', $num);
     }
-
     public static function maketime($v)
     {
         if (!$v) {