数据字典
大约 1 分钟
数据字典
需求分析
- 快速查阅各数据库表结构和字段说明
- 快速了解数据库设计的基本数据结构
- 有利于对E-R模型理解,分析各表之间的关系及主外键依赖等信息
- 生成模型字段信息,在新建model时快速设置相关字段信息并加上注释
- 生成postman接口示例,解析接口测试时各接口基础数据录入的问题
- 因为各数据库驱动代码不一样,所以只取mysql做处理
软件设计
// 数据初始化
$this->init();
// 读取数据库表信息
$this->getDBInfo();
foreach ($this->tableInfos as $tableInfo) {
// 读取数据库表字段信息
$this->getColumnInfo($tableInfo);
// 生成数据字典
$this->makeDBDict($tableInfo);
// 生成模型字段信息
$this->makeModelInfo($tableInfo);
// 设置字段默认值
$this->setColumnValue();
// 生成postman接口示例
$this->makePostman($tableInfo);
}
编码
$this->tableInfos =
$this->db->table('information_schema.TABLES')
->addSelect(['TABLE_SCHEMA', 'TABLE_NAME', 'TABLE_COMMENT'])
->whereIn('table_schema', (array) $this->tableSchema)
->whereNotIn('table_name', ['migrations'])
->get()->toArray();
$this->columnInfos =
$this->db->table('information_schema.COLUMNS')
->addSelect(['COLUMN_NAME', 'COLUMN_TYPE', 'COLUMN_DEFAULT', 'IS_NULLABLE', 'EXTRA', 'COLUMN_COMMENT', 'ORDINAL_POSITION'])
->where('table_schema', $tableInfo->TABLE_SCHEMA)
->where('table_name', $tableInfo->TABLE_NAME)
->orderBy('ORDINAL_POSITION')
->get()->toArray();
$sections = view('db_dict.postman.types')
->with('apiName', $apiName)
->with('tableInfo', $tableInfo)
->with('columnInfo', $this->columnInfos)
->renderSections();
foreach ($this->apiTypes as $index => $type) {
if (isset($sections[$type])) {
$api = json_decode($sections[$type]);
$item->appendArray('item', $api);
}
}