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('连接名称:' . $connection); $output->writeln('数据库类型:' . ($config['type'] ?? 'unknown')); $output->writeln('主机地址:' . ($config['hostname'] ?? 'unknown')); $output->writeln('数据库名:' . ($config['database'] ?? 'unknown')); $output->writeln('端口:' . ($config['hostport'] ?? 'unknown')); $output->writeln('字符集:' . ($config['charset'] ?? 'unknown')); $output->writeln('表前缀:' . ($config['prefix'] ?? '')); $output->writeln(''); $output->writeln('' . str_repeat('-', 60) . ''); $output->writeln('表列表'); $output->writeln('' . str_repeat('-', 60) . ''); $output->writeln(''); if (empty($tables)) { $output->writeln('无表'); $output->writeln(''); return; } foreach ($tableList as $tableItem) { if ($withCount) { $output->writeln('' . str_pad($tableItem['name'], 40) . '' . ($tableItem['count'] ?? 0) . ' 条记录'); } else { $output->writeln('' . $tableItem['name'] . ''); } } $output->writeln(''); $output->writeln('' . str_repeat('=', 60) . ''); $output->writeln(''); } catch (\Exception $e) { $output->error('获取信息失败:' . $e->getMessage()); return; } } }