136 lines
4.3 KiB
PHP
136 lines
4.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace app\command;
|
||
|
|
|
||
|
|
use Exception;
|
||
|
|
use Symfony\Component\Console\Command\Command;
|
||
|
|
use Symfony\Component\Console\Input\InputInterface;
|
||
|
|
use Symfony\Component\Console\Input\InputOption;
|
||
|
|
use Symfony\Component\Console\Input\InputArgument;
|
||
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
||
|
|
use support\think\Db;
|
||
|
|
use app\model\User as UserModel;
|
||
|
|
|
||
|
|
|
||
|
|
class Database extends Command
|
||
|
|
{
|
||
|
|
protected static $defaultName = 'Db';
|
||
|
|
protected static $defaultDescription = 'Database 优化';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected function configure()
|
||
|
|
{
|
||
|
|
$this->addOption('action','a', InputArgument::OPTIONAL, '要做什么操作');
|
||
|
|
$this->addOption('table','t', InputArgument::OPTIONAL, '表名');
|
||
|
|
$this->addOption('domain','ym', InputArgument::OPTIONAL, 'domain');
|
||
|
|
$this->addOption('robot_id','rid', InputArgument::OPTIONAL, 'robot_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param InputInterface $input
|
||
|
|
* @param OutputInterface $output
|
||
|
|
* @return int
|
||
|
|
*/
|
||
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||
|
|
{
|
||
|
|
$action = $input->getOption('action');
|
||
|
|
if($action == 'prototype'){
|
||
|
|
return $this->prototype($input, $output);
|
||
|
|
}
|
||
|
|
cp('操作不存在:'.$action);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
function prototype(InputInterface $input, OutputInterface $output){
|
||
|
|
$table = $input->getOption('table');
|
||
|
|
// 获取表前缀并构建完整表名
|
||
|
|
$prefix = config('thinkorm.connections.mysql.prefix', '');
|
||
|
|
$fullTableName = '`' . $prefix . $table . '`';
|
||
|
|
|
||
|
|
// 查询表结构
|
||
|
|
$res = Db::query('SHOW FULL COLUMNS FROM ' . $fullTableName);
|
||
|
|
|
||
|
|
if (empty($res)) {
|
||
|
|
return "// 表 {$table} 不存在或没有字段";
|
||
|
|
}
|
||
|
|
|
||
|
|
$annotations = [];
|
||
|
|
$annotations[] = '/**';
|
||
|
|
|
||
|
|
foreach ($res as $row) {
|
||
|
|
$field = $row['Field'];
|
||
|
|
$type = $row['Type'];
|
||
|
|
$comment = $row['Comment'] ?: '无注释';
|
||
|
|
|
||
|
|
// 处理字段类型映射
|
||
|
|
$phpType = $this->mapMysqlTypeToPhp($type);
|
||
|
|
|
||
|
|
// 处理特殊字段
|
||
|
|
if ($field === 'id') {
|
||
|
|
$annotations[] = " * @property integer \${$field} 主键(ID) - {$comment}";
|
||
|
|
} else {
|
||
|
|
$annotations[] = " * @property {$phpType} \${$field} {$comment}";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$annotations[] = ' */';
|
||
|
|
|
||
|
|
cp( implode("\n", $annotations));
|
||
|
|
return self::SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将MySQL字段类型映射到PHP类型
|
||
|
|
*
|
||
|
|
* @param string $mysqlType MySQL字段类型
|
||
|
|
* @return string PHP类型
|
||
|
|
*/
|
||
|
|
protected function mapMysqlTypeToPhp($mysqlType)
|
||
|
|
{
|
||
|
|
$mysqlType = strtolower($mysqlType);
|
||
|
|
|
||
|
|
// 整数类型
|
||
|
|
if (preg_match('/^(tinyint|smallint|mediumint|int|bigint)/', $mysqlType)) {
|
||
|
|
// 检查是否为无符号
|
||
|
|
if (strpos($mysqlType, 'unsigned') !== false) {
|
||
|
|
return 'integer'; // 无符号整数也返回integer
|
||
|
|
}
|
||
|
|
return 'integer';
|
||
|
|
}
|
||
|
|
|
||
|
|
// 浮点类型
|
||
|
|
if (preg_match('/^(float|double|decimal)/', $mysqlType)) {
|
||
|
|
return 'float';
|
||
|
|
}
|
||
|
|
|
||
|
|
// 字符串类型
|
||
|
|
if (preg_match('/^(varchar|char|text|tinytext|mediumtext|longtext|enum|set)/', $mysqlType)) {
|
||
|
|
return 'string';
|
||
|
|
}
|
||
|
|
|
||
|
|
// 日期时间类型
|
||
|
|
if (preg_match('/^(date|time|datetime|timestamp|year)/', $mysqlType)) {
|
||
|
|
return 'string'; // 或者可以返回 '\\DateTime' 如果需要更精确的类型
|
||
|
|
}
|
||
|
|
|
||
|
|
// 二进制类型
|
||
|
|
if (preg_match('/^(blob|tinyblob|mediumblob|longblob|binary|varbinary)/', $mysqlType)) {
|
||
|
|
return 'string'; // 或者根据需求返回其他类型
|
||
|
|
}
|
||
|
|
|
||
|
|
// JSON类型
|
||
|
|
if (strpos($mysqlType, 'json') !== false) {
|
||
|
|
return 'array'; // 或者 'mixed'
|
||
|
|
}
|
||
|
|
|
||
|
|
// 布尔类型(tinyint(1)通常用作布尔值)
|
||
|
|
if ($mysqlType === 'tinyint(1)' || $mysqlType === 'boolean' || $mysqlType === 'bool') {
|
||
|
|
return 'boolean';
|
||
|
|
}
|
||
|
|
|
||
|
|
// 默认返回混合类型
|
||
|
|
return 'mixed';
|
||
|
|
}
|
||
|
|
}
|