mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 23:42:48 +08:00
170 lines
6.0 KiB
PHP
170 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace base\common\command\tools\db;
|
|
|
|
use app\common\service\tools\DbService;
|
|
use app\common\console\Command;
|
|
use think\console\Input;
|
|
use think\console\input\Option;
|
|
use think\console\Output;
|
|
use think\facade\Db;
|
|
|
|
class ToolsDbDescBase extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
parent::configure();
|
|
|
|
$this->setName('tools:db:desc')
|
|
->setDescription('显示表结构信息')
|
|
->addArgument('table', null, '表名')
|
|
->addOption('show-index', null, Option::VALUE_NONE, '显示索引信息')
|
|
->addOption('connection', null, Option::VALUE_OPTIONAL, '指定数据库连接配置')
|
|
->addOption('help', 'h', Option::VALUE_NONE, '显示帮助信息');
|
|
}
|
|
|
|
protected function execute($input, $output)
|
|
{
|
|
if ($input->getOption('help')) {
|
|
$service = new DbService();
|
|
$service->showHelp('tools:db:desc', $output);
|
|
return;
|
|
}
|
|
|
|
$service = new DbService();
|
|
|
|
if (!$service->checkDebugMode($output)) {
|
|
return;
|
|
}
|
|
|
|
$connection = $service->getDbConnection($input);
|
|
$service->setConnection($connection);
|
|
|
|
$tableName = $input->getArgument('table');
|
|
if (empty($tableName)) {
|
|
$output->error('请提供表名');
|
|
return;
|
|
}
|
|
|
|
$fullTableName = $service->getFullTableName($tableName);
|
|
$showIndex = $input->getOption('show-index');
|
|
|
|
try {
|
|
$tableComment = '';
|
|
try {
|
|
$escaped = addcslashes($fullTableName, "\\_%");
|
|
$rows = Db::connect($connection)->query("SHOW TABLE STATUS LIKE '$escaped'");
|
|
if (!empty($rows)) {
|
|
$tableComment = (string)($rows[0]['Comment'] ?? '');
|
|
}
|
|
} catch (\Throwable $e) {
|
|
}
|
|
|
|
if ($tableComment === '') {
|
|
try {
|
|
$rows = Db::connect($connection)->query("SHOW CREATE TABLE `$fullTableName`");
|
|
if (!empty($rows)) {
|
|
$create = $rows[0]['Create Table'] ?? '';
|
|
if ($create !== '' && preg_match("/COMMENT='(.*?)'/s", $create, $matches)) {
|
|
$tableComment = stripcslashes($matches[1]);
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
}
|
|
}
|
|
|
|
$columns = Db::connect($connection)->query("SHOW FULL COLUMNS FROM `$fullTableName`");
|
|
|
|
if (empty($columns)) {
|
|
$output->writeln('表不存在或无字段');
|
|
$output->writeln('');
|
|
return;
|
|
}
|
|
|
|
$columnData = [];
|
|
foreach ($columns as $column) {
|
|
$columnData[] = [
|
|
'name' => $column['Field'],
|
|
'type' => $column['Type'],
|
|
'null' => $column['Null'] === 'YES',
|
|
'key' => $column['Key'],
|
|
'default' => $column['Default'] ?? null,
|
|
'extra' => $column['Extra'] ?? '',
|
|
'comment' => $column['Comment'] ?? '',
|
|
];
|
|
}
|
|
|
|
$indexData = [];
|
|
if ($showIndex) {
|
|
$indexes = Db::connect($connection)->query("SHOW INDEX FROM `$fullTableName`");
|
|
|
|
if (!empty($indexes)) {
|
|
foreach ($indexes as $index) {
|
|
$indexData[] = [
|
|
'name' => $index['Key_name'],
|
|
'column' => $index['Column_name'],
|
|
'unique' => $index['Non_unique'] == 0,
|
|
'type' => $index['Index_type'],
|
|
'sequence' => $index['Seq_in_index'],
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
$output->newLine();
|
|
$output->comment(str_repeat('=', 60));
|
|
$output->info('表结构:' . $fullTableName);
|
|
$output->comment(str_repeat('=', 60));
|
|
$output->newLine();
|
|
|
|
$output->info('表注释:' . $tableComment);
|
|
$output->newLine();
|
|
|
|
$tableData = [];
|
|
foreach ($columns as $column) {
|
|
$tableData[] = [
|
|
'字段名' => $column['Field'],
|
|
'类型' => $column['Type'],
|
|
'是否为空' => $column['Null'] === 'YES' ? '是' : '否',
|
|
'键' => $column['Key'],
|
|
'默认值' => $column['Default'] ?? 'NULL',
|
|
'额外' => $column['Extra'] ?? '',
|
|
'注释' => $column['Comment'] ?? '',
|
|
];
|
|
}
|
|
|
|
$service->formatTableOutput($tableData, $output);
|
|
|
|
if ($showIndex) {
|
|
$output->newLine();
|
|
$output->comment(str_repeat('-', 60));
|
|
$output->info('索引信息');
|
|
$output->comment(str_repeat('-', 60));
|
|
$output->newLine();
|
|
|
|
if (!empty($indexes)) {
|
|
$indexDataDisplay = [];
|
|
foreach ($indexes as $index) {
|
|
$indexDataDisplay[] = [
|
|
'索引名' => $index['Key_name'],
|
|
'列名' => $index['Column_name'],
|
|
'唯一' => $index['Non_unique'] == 0 ? '是' : '否',
|
|
'索引类型' => $index['Index_type'],
|
|
'顺序' => $index['Seq_in_index'],
|
|
];
|
|
}
|
|
$service->formatTableOutput($indexDataDisplay, $output);
|
|
} else {
|
|
$output->writeln('无索引');
|
|
}
|
|
}
|
|
|
|
$output->newLine();
|
|
$output->comment(str_repeat('=', 60));
|
|
$output->newLine();
|
|
} catch (\Exception $e) {
|
|
$output->error('获取表结构失败:' . $e->getMessage());
|
|
return;
|
|
}
|
|
}
|
|
} |