增加 optimize:schema 命令 用于生成 数据表字段信息缓存

This commit is contained in:
thinkphp
2016-09-18 17:01:55 +08:00
parent 09598e67b3
commit 2b476fe7ff
5 changed files with 67 additions and 7 deletions

View File

@@ -147,7 +147,7 @@ if (!function_exists('widget')) {
/** /**
* 渲染输出Widget * 渲染输出Widget
* @param string $name Widget名称 * @param string $name Widget名称
* @param array $data 传的参数 * @param array $data 传的参数
* @return mixed * @return mixed
*/ */
function widget($name, $data = []) function widget($name, $data = [])

View File

@@ -44,6 +44,7 @@ class Console
"think\\console\\command\\optimize\\Autoload", "think\\console\\command\\optimize\\Autoload",
"think\\console\\command\\optimize\\Config", "think\\console\\command\\optimize\\Config",
"think\\console\\command\\optimize\\Route", "think\\console\\command\\optimize\\Route",
"think\\console\\command\\optimize\\Schema",
]; ];
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
@@ -130,7 +131,7 @@ class Console
$exitCode = $e->getCode(); $exitCode = $e->getCode();
if (is_numeric($exitCode)) { if (is_numeric($exitCode)) {
$exitCode = (int)$exitCode; $exitCode = (int) $exitCode;
if (0 === $exitCode) { if (0 === $exitCode) {
$exitCode = 1; $exitCode = 1;
} }
@@ -221,7 +222,7 @@ class Console
*/ */
public function setCatchExceptions($boolean) public function setCatchExceptions($boolean)
{ {
$this->catchExceptions = (bool)$boolean; $this->catchExceptions = (bool) $boolean;
} }
/** /**
@@ -231,7 +232,7 @@ class Console
*/ */
public function setAutoExit($boolean) public function setAutoExit($boolean)
{ {
$this->autoExit = (bool)$boolean; $this->autoExit = (bool) $boolean;
} }
/** /**
@@ -399,7 +400,7 @@ class Console
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*'; return preg_quote($matches[1]) . '[^:]*';
}, $namespace); }, $namespace);
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
if (empty($namespaces)) { if (empty($namespaces)) {
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
@@ -437,7 +438,7 @@ class Console
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*'; return preg_quote($matches[1]) . '[^:]*';
}, $name); }, $name);
$commands = preg_grep('{^' . $expr . '}', $allCommands); $commands = preg_grep('{^' . $expr . '}', $allCommands);
if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) { if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
if (false !== $pos = strrpos($name, ':')) { if (false !== $pos = strrpos($name, ':')) {

View File

@@ -0,0 +1,53 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
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 = '<?php ' . PHP_EOL . 'return ';
$info = Db::getFields($table);
$content .= var_export($info, true) . ';';
file_put_contents(RUNTIME_PATH . 'schema' . DS . $table . EXT, $content);
}
$output->writeln('<info>Succeed!</info>');
}
}

View File

@@ -1375,7 +1375,12 @@ class Query
list($guid) = explode(' ', $tableName); list($guid) = explode(' ', $tableName);
if (!isset(self::$info[$guid])) { 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); $fields = array_keys($info);
$bind = $type = []; $bind = $type = [];
foreach ($info as $key => $val) { foreach ($info as $key => $val) {

View File

@@ -86,6 +86,7 @@ class Mysql extends Connection
*/ */
public function getTables($dbName = '') public function getTables($dbName = '')
{ {
$this->initConnect(true);
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
// 调试开始 // 调试开始
$this->debug(true); $this->debug(true);