From 2b476fe7ff247261223f48915d509fbc7aedfa7a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 18 Sep 2016 17:01:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20optimize:schema=20?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=20=E7=94=A8=E4=BA=8E=E7=94=9F=E6=88=90=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A1=A8=E5=AD=97=E6=AE=B5=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/Console.php | 11 ++-- .../think/console/command/optimize/Schema.php | 53 +++++++++++++++++++ library/think/db/Query.php | 7 ++- library/think/db/connector/Mysql.php | 1 + 5 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 library/think/console/command/optimize/Schema.php diff --git a/helper.php b/helper.php index 11f0b76c..6ab2cb34 100644 --- a/helper.php +++ b/helper.php @@ -147,7 +147,7 @@ if (!function_exists('widget')) { /** * 渲染输出Widget * @param string $name Widget名称 - * @param array $data 传人的参数 + * @param array $data 传入的参数 * @return mixed */ function widget($name, $data = []) diff --git a/library/think/Console.php b/library/think/Console.php index 70defadf..ab8e0211 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -44,6 +44,7 @@ class Console "think\\console\\command\\optimize\\Autoload", "think\\console\\command\\optimize\\Config", "think\\console\\command\\optimize\\Route", + "think\\console\\command\\optimize\\Schema", ]; public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') @@ -130,7 +131,7 @@ class Console $exitCode = $e->getCode(); if (is_numeric($exitCode)) { - $exitCode = (int)$exitCode; + $exitCode = (int) $exitCode; if (0 === $exitCode) { $exitCode = 1; } @@ -221,7 +222,7 @@ class Console */ public function setCatchExceptions($boolean) { - $this->catchExceptions = (bool)$boolean; + $this->catchExceptions = (bool) $boolean; } /** @@ -231,7 +232,7 @@ class Console */ public function setAutoExit($boolean) { - $this->autoExit = (bool)$boolean; + $this->autoExit = (bool) $boolean; } /** @@ -399,7 +400,7 @@ class Console $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]) . '[^:]*'; }, $namespace); - $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); + $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); if (empty($namespaces)) { $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); @@ -437,7 +438,7 @@ class Console $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]) . '[^:]*'; }, $name); - $commands = preg_grep('{^' . $expr . '}', $allCommands); + $commands = preg_grep('{^' . $expr . '}', $allCommands); if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) { if (false !== $pos = strrpos($name, ':')) { diff --git a/library/think/console/command/optimize/Schema.php b/library/think/console/command/optimize/Schema.php new file mode 100644 index 00000000..0cc3d3df --- /dev/null +++ b/library/think/console/command/optimize/Schema.php @@ -0,0 +1,53 @@ + +// +---------------------------------------------------------------------- +namespace think\console\command\optimize; + +use think\console\Command; +use think\console\Input; +use think\console\input\Option; +use think\console\Output; +use think\Db; + +class Schema extends Command +{ + /** @var Output */ + protected $output; + + protected function configure() + { + $this->setName('optimize:schema') + ->addOption('table', null, Option::VALUE_REQUIRED, 'Build table schema cache .') + ->setDescription('Build database schema cache.'); + } + + protected function execute(Input $input, Output $output) + { + if ($input->hasOption('table')) { + $tables[] = $input->getOption('table'); + } else { + $tables = Db::getTables(); + } + + if (!is_dir(RUNTIME_PATH . 'schema')) { + @mkdir(RUNTIME_PATH . 'schema', 0755, true); + } + + foreach ($tables as $table) { + $content = 'writeln('Succeed!'); + } + +} diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 786ed0c9..238523d6 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1375,7 +1375,12 @@ class Query list($guid) = explode(' ', $tableName); if (!isset(self::$info[$guid])) { - $info = $this->connection->getFields($tableName); + // 读取缓存 + if (is_file(RUNTIME_PATH . 'schema/' . $guid . '.php')) { + $info = include RUNTIME_PATH . 'schema/' . $guid . '.php'; + } else { + $info = $this->connection->getFields($guid); + } $fields = array_keys($info); $bind = $type = []; foreach ($info as $key => $val) { diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 42a6376b..47b069fb 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -86,6 +86,7 @@ class Mysql extends Connection */ public function getTables($dbName = '') { + $this->initConnect(true); $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; // 调试开始 $this->debug(true);