Files
ulthon_admin/extend/base/common/command/tools/db/ToolsDbInfoBase.php
2026-03-26 20:22:34 +08:00

151 lines
5.5 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;
use think\facade\Config;
class ToolsDbInfoBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:info')
->setDescription('显示数据库连接信息和表列表')
->addOption('with-count', 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:info', $output);
return;
}
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
}
$connection = $service->getDbConnection($input);
$service->setConnection($connection);
$withCount = $input->getOption('with-count');
try {
$config = Config::get('database.connections.' . $connection);
if (!$config) {
$output->error('数据库连接配置不存在:' . $connection);
return;
}
$tables = Db::connect($connection)->getTables();
$tableList = [];
if (!empty($tables)) {
$prefix = $config['prefix'] ?? '';
foreach ($tables as $table) {
$shortName = $table;
if ($prefix && str_starts_with($table, $prefix)) {
$shortName = substr($table, strlen($prefix));
}
$tableItem = [
'name' => $shortName,
'full_name' => $table
];
if ($withCount) {
$count = Db::connect($connection)->table($table)->count();
$tableItem['count'] = $count;
}
$tableList[] = $tableItem;
}
}
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->info('数据库连接信息');
$output->comment(str_repeat('=', 60));
$output->newLine();
$output->info('连接名称:' . $connection);
$output->info('数据库类型:' . ($config['type'] ?? 'unknown'));
$output->info('主机地址:' . ($config['hostname'] ?? 'unknown'));
$output->info('数据库名:' . ($config['database'] ?? 'unknown'));
$output->info('端口:' . ($config['hostport'] ?? 'unknown'));
$output->info('字符集:' . ($config['charset'] ?? 'unknown'));
$output->info('表前缀:' . ($config['prefix'] ?? ''));
$output->newLine();
$output->comment(str_repeat('-', 60));
$output->info('表列表');
$output->comment(str_repeat('-', 60));
$output->newLine();
if (empty($tables)) {
$output->writeln('无表');
$output->newLine();
return;
}
foreach ($tableList as $tableItem) {
if ($withCount) {
$output->info(str_pad($tableItem['name'], 40) . ($tableItem['count'] ?? 0) . ' 条记录');
} else {
$output->info($tableItem['name']);
}
}
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->newLine();
$output->writeln('<info>连接名称:</info>' . $connection);
$output->writeln('<info>数据库类型:</info>' . ($config['type'] ?? 'unknown'));
$output->writeln('<info>主机地址:</info>' . ($config['hostname'] ?? 'unknown'));
$output->writeln('<info>数据库名:</info>' . ($config['database'] ?? 'unknown'));
$output->writeln('<info>端口:</info>' . ($config['hostport'] ?? 'unknown'));
$output->writeln('<info>字符集:</info>' . ($config['charset'] ?? 'unknown'));
$output->writeln('<info>表前缀:</info>' . ($config['prefix'] ?? ''));
$output->writeln('');
$output->writeln('<comment>' . str_repeat('-', 60) . '</comment>');
$output->writeln('<info>表列表</info>');
$output->writeln('<comment>' . str_repeat('-', 60) . '</comment>');
$output->writeln('');
if (empty($tables)) {
$output->writeln('无表');
$output->writeln('');
return;
}
foreach ($tableList as $tableItem) {
if ($withCount) {
$output->writeln('<info>' . str_pad($tableItem['name'], 40) . '</info>' . ($tableItem['count'] ?? 0) . ' 条记录');
} else {
$output->writeln('<info>' . $tableItem['name'] . '</info>');
}
}
$output->writeln('');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('');
} catch (\Exception $e) {
$output->error('获取信息失败:' . $e->getMessage());
return;
}
}
}