当前位置: 首页 > news >正文

将mysql数据库表结构导出成DBML格式

前言

DBML(数据库标记语言)是一种简单易读的 DSL 语言,用于定义数据库结构。

因为需要分析商品模块的表设计是否合理,所以需要图形化表,并显示表之前的关系。
想来想去,找到了DBML。所以就需要将数据库结构,导出成DBML格式。

方法

用的laravel框架。

有两种方法

  • 使用 desc tableName
输入如下:
array:13 [0 => array:6 ["Field" => "id""Type" => "bigint(20)""Null" => "NO""Key" => "PRI""Default" => null"Extra" => "auto_increment"]1 => array:6 ["Field" => "parent_id""Type" => "bigint(20)""Null" => "YES""Key" => "MUL""Default" => "0""Extra" => ""]
  • 使用 show create table tableName
输出如下:
CREATE TABLE `category` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分类ID',`parent_id` bigint(20) DEFAULT '0' COMMENT '父分类ID(0表示顶级分类)',`category_name` varchar(255) NOT NULL COMMENT '分类名称',`category_alias` varchar(255) NOT NULL DEFAULT '' COMMENT '分类别名',`category_code` varchar(50) DEFAULT NULL COMMENT '分类编码',`category_sort` int(11) NOT NULL DEFAULT '1' COMMENT '分类排序值',`level` int(11) NOT NULL DEFAULT '1' COMMENT '分类层级(1=一级分类)',`status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态(1=启用,0=停用)',`user_created` varchar(255) NOT NULL DEFAULT '' COMMENT '创建人',`user_cid` int(11) NOT NULL DEFAULT '0' COMMENT '创建人id',`user_updated` varchar(255) NOT NULL DEFAULT '' COMMENT '更新人',`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE,KEY `idx_parent_id` (`parent_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=777 DEFAULT CHARSET=utf8mb4 COMMENT='分类表'

因此就需要结合这两种命令得到DBML格式所需要的数据。,脚本如下。

<?phpnamespace App\Http\Controllers;use Illuminate\Support\Facades\DB;class TestController extends Controller
{public function dbtableToDbml(){$tables = ['category','category_attribute','attribute','attribute_option','product','product_attribute','product_attribute_option','process','process_resource','combo','combo_item','combo_item_attribute','customer_subtotal','customer_product','customer_combo','customer_combo_item','customer_combo_attribute','customer_combo_material','customer_combo_material_attribute','customer_product_process','customer_product_process_resource',];$res = $this->dbtableStruToDbml($tables);return $res;}//数据库表结构转为DBML格式public function dbtableStruToDbml($tables){$tableArr = is_string($tables) ? explode(',', $tables) : $tables;$res = '';//获取字段名及备注$extractFieldAndComment = function ($str) {$result = ['field' => null,'comment' => null];// 1. 提取字段名(匹配反引号包裹的内容)if (preg_match('/`([^`]+)`/', $str, $fieldMatches)) {$result['field'] = trim($fieldMatches[1]);}// 2. 提取COMMENT后的内容(如果存在)if (preg_match('/COMMENT\s+\'([^\']*)\'/i', $str, $commentMatches)) {$result['comment'] = trim($commentMatches[1]);}return $result;};$getTableComment = function ($str) {// 正则表达式解析:// COMMENT\s*=\s* 匹配"COMMENT"及可能的空格、等号、空格(不区分大小写)// '([^']*)' 匹配单引号内的内容(捕获组1),[^']*表示非单引号的任意字符(包括空)$pattern = '/COMMENT\s*=\s*\'([^\']*)\'/i';// 执行匹配if (preg_match($pattern, $str, $matches)) {// 匹配成功,返回捕获到的内容(trim处理避免意外空格,保留空内容)return trim($matches[1]);}// 无COMMENT时返回空字符串return '';};foreach ($tableArr as $table) {$tableComment = '';$tableDesc = DB::select("desc {$table}");$filedArr = [];foreach ($tableDesc as $item) {$filedArr[$item->Field] = ['field' => $item->Field,'type' => $item->Type,'comment' => ''];}$createTable = DB::select("show create table {$table}");$createTable = json_decode(json_encode($createTable), true);$createTable = $createTable[0]['Create Table'];$fieldInfos = explode("\n", $createTable);foreach ($fieldInfos as $fieldInfo) {$fieldInfo = trim($fieldInfo, ' '); //去掉前面的空格$pattern = '/^`/'; //是否以 ` 号开头,是的话,才是字段,否则就是其他的,其他的不考虑if (preg_match($pattern, $fieldInfo)) {//是字段才处理$fieldAndComment = $extractFieldAndComment($fieldInfo);$filedArr[$fieldAndComment['field']]['comment'] = $fieldAndComment['comment'];} elseif (preg_match('/^\)/', $fieldInfo)) {$tableComment = $getTableComment($fieldInfo);}}$str = '';$str = "Table {$table} {\n";foreach ($filedArr as $fieldItem) {$str .= "   {$fieldItem['field']} {$fieldItem['type']} [note: '{$fieldItem['comment']}'] \n";}$str .= "   Note: '{$tableComment}'\n";$str .= "}\n";strlen($res) ? $res =  $res . "\n\n\n" : '';$res .= $str;}return $res;}}

导出的内容如下:

Table category {id bigint(20) [note: '分类ID'] parent_id bigint(20) [note: '父分类ID(0表示顶级分类)'] category_name varchar(255) [note: '分类名称'] category_alias varchar(255) [note: '分类别名'] category_code varchar(50) [note: '分类编码'] category_sort int(11) [note: '分类排序值'] level int(11) [note: '分类层级(1=一级分类)'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_cid int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '分类表'
}Table category_attribute {id bigint(20) [note: '记录ID'] category_id bigint(20) [note: '分类ID'] attribute_id bigint(20) [note: '属性ID'] limit_ids text [note: '限定属性值id'] Note: '分类属性关联表'
}Table attribute {id bigint(20) [note: '属性ID'] attribute_name varchar(255) [note: '属性名称(如颜色,尺寸)'] attribute_code varchar(50) [note: '属性编码(程序识别唯一值)'] sort mediumint(9) [note: '排序'] remark varchar(255) [note: '属性说明'] input_type varchar(50) [note: '输入类型(single=单选,double=多选)'] unit varchar(50) [note: '属性单位'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '属性定义表'
}Table attribute_option {id bigint(20) [note: '选项ID'] attribute_id bigint(20) [note: '所属属性ID'] option_value varchar(255) [note: '属性选项值'] option_code varchar(50) [note: '属性选项编码'] status tinyint(4) [note: '状态(1=启用,0=停用)'] sort_order int(11) [note: '排序值(用于前端展示顺序)'] remark varchar(255) [note: '备注'] Note: '属性选项表(全局)'
}Table product {id bigint(20) [note: '商品ID(主键,自增)'] category_id bigint(20) [note: '分类ID(外键)'] category_top_ids varchar(255) [note: '上级分类id'] category_top_name varchar(255) [note: '上级分类名称'] product_code varchar(50) [note: '商品编码(内部唯一)'] product_name varchar(255) [note: ''] product_image varchar(255) [note: '商品图片'] product_type int(11) [note: '物料类型:100-原料 200-辅料 201-纸箱 202-套袋'] short_name varchar(255) [note: '商品简称'] scientific_name varchar(255) [note: '商品学名'] label_alias varchar(255) [note: '标签别名'] description text [note: '商品描述'] unit_name varchar(50) [note: '基础单位名称'] inspection_standard text [note: '质检标准'] shelf_life int(11) [note: '保质期,单位:天'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] safe_stock int(11) [note: '安全库存值'] Note: '商品主数据表'
}Table product_attribute {id bigint(20) [note: '记录ID'] product_id bigint(20) [note: '商品ID'] attribute_id bigint(20) [note: '属性ID'] is_must tinyint(4) [note: '是否必填:0-否 1-是'] Note: '商品属性值表'
}Table product_attribute_option {id bigint(20) [note: '记录ID'] product_id bigint(20) [note: '商品ID'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '商品属性可选值关联表'
}Table process {id bigint(20) [note: '工艺ID'] process_code varchar(50) [note: '工艺编码'] process_name varchar(255) [note: '工艺名称'] process_type varchar(255) [note: '工艺类型'] process_duration int(11) [note: '工艺工期'] duration varchar(255) [note: '时间单位'] description text [note: '工艺描述'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '组合商品工艺表'
}Table process_resource {process_id bigint(20) [note: '工艺ID'] product_id bigint(20) [note: '物料ID'] Note: '工艺物料关联表'
}Table combo {id bigint(20) [note: '组合ID'] category_id bigint(20) [note: '分类ID(外键)'] category_top_ids varchar(255) [note: '上级分类id'] category_top_name varchar(255) [note: '上级分类名称'] combo_name varchar(255) [note: '组合名称'] combo_code varchar(255) [note: '组合编码'] combo_image text [note: '组合图片'] combo_price decimal(10,2) [note: '组合价格'] combo_label varchar(255) [note: '组合成品标签'] subtotal_name varchar(255) [note: '小计名称'] unit_name varchar(50) [note: '基础单位名称'] unit_spec decimal(10,2) [note: '基础单位规格'] unit_pack_name varchar(50) [note: '包装单位名称'] unit_pack_spec decimal(10,2) [note: '包装单位规格'] description text [note: '组合描述'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '组合产成品主表'
}Table combo_item {id bigint(20) [note: '组合项ID'] combo_id bigint(20) [note: '组合ID'] product_id bigint(20) [note: '所属组合的商品ID'] category_id bigint(20) [note: '所属组合的分类ID'] category_name varchar(255) [note: '分类名称'] item_name varchar(255) [note: '组合名称'] item_type varchar(50) [note: '组合项类型(LABEL/PRODUCT)'] quantity int(11) [note: '数量'] label_name varchar(50) [note: '标签名称'] remark text [note: '备注'] Note: '组合项表'
}Table combo_item_attribute {id bigint(20) [note: '记录ID'] item_id bigint(20) [note: '组合项ID'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '组合项属性值表'
}Table customer_subtotal {id bigint(20) [note: '小计ID'] name varchar(128) [note: '小计名称'] remark varchar(255) [note: '备注'] plan_name varchar(128) [note: '小计名称-采购'] production_name varchar(128) [note: '小计名称-生产'] sort_num int(11) [note: '排序值'] Note: '客户商品小计关联表'
}Table customer_product {id bigint(20) [note: '记录ID'] client_id bigint(20) [note: '客户ID'] customer_product_name varchar(255) [note: '客户成品名称'] customer_product_code varchar(255) [note: '客户成品编码'] customer_product_alias varchar(255) [note: '客户成品代号'] customer_product_process text [note: '加工工艺描述'] customer_product_label text [note: '关联组合成品名称(combo名称)'] customer_product_tag text [note: '关联组合成品代码(combo编码)'] customer_product_image text [note: '客户成品图片'] subtotal_id bigint(20) [note: '小计关联'] subtotal_name varchar(255) [note: '小计分类名称'] unit_name varchar(50) [note: '单位名称'] unit_spec int(11) [note: '单位规格'] custom_price decimal(10,2) [note: '客户成品定价'] piece_count decimal(10,2) [note: '计件价格'] gross_weight int(11) [note: '毛重'] pure_weight int(11) [note: '净重'] currency varchar(10) [note: '币种'] custom_spec1 varchar(50) [note: '规格1'] custom_spec2 varchar(50) [note: '规格2'] custom_length varchar(50) [note: '客户商品长度'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] remark varchar(255) [note: '备注'] Note: '客户商品表'
}Table customer_combo {id bigint(20) [note: '记录ID'] customer_product_id bigint(20) [note: '客户商品ID'] customer_combo_name varchar(255) [note: '组合名称'] customer_combo_code varchar(255) [note: '组合代码'] quantity int(11) [note: '组合数量'] Note: '客户组合商品表'
}Table customer_combo_item {id bigint(20) [note: '组合项ID'] customer_combo_id bigint(20) [note: '组合ID'] product_id bigint(20) [note: '所属组合的商品ID'] category_id bigint(20) [note: '所属组合的分类ID'] category_name varchar(255) [note: '分类名称'] item_name varchar(255) [note: '组合名称'] item_type varchar(50) [note: '组合项类型(LABEL/PRODUCT)'] quantity int(11) [note: '数量'] label_name varchar(50) [note: '标签名称'] replace_product_id varchar(255) [note: '可替换商品ID'] replace_label varchar(255) [note: '可替换标签'] remark text [note: '备注'] Note: '客户组合商品明细表'
}Table customer_combo_attribute {id bigint(20) [note: '记录ID'] item_id bigint(20) [note: '组合项ID'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '客户组合商品属性表'
}Table customer_combo_material {id bigint(20) [note: '辅料ID'] customer_product_id bigint(20) [note: '客户商品ID'] customer_combo_id bigint(20) [note: '客户商品组合ID'] product_id bigint(20) [note: '产品ID'] process_type varchar(255) [note: '标记所属工艺'] product_type varchar(255) [note: '标记物料类型'] quantity int(11) [note: '数量'] remark text [note: '备注'] is_new tinyint(4) [note: '是否新数据:1-新'] Note: '客户组合辅料表'
}Table customer_combo_material_attribute {id bigint(20) [note: '记录ID'] customer_material_id bigint(20) [note: '辅料ID,关联customer_combo_material表id'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '客户组合辅料属性值表'
}Table customer_product_process {id bigint(20) [note: '记录ID'] customer_product_id bigint(20) [note: '客户商品ID'] process_id bigint(20) [note: '工艺id'] description text [note: '描述'] source varchar(255) [note: '来源'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '客户组合商品工艺表'
}Table customer_product_process_resource {id bigint(20) [note: 'ID'] customer_process_id bigint(20) [note: '客户商品工艺ID,关联customer_product_process表id'] product_id bigint(20) [note: '工艺产品id'] Note: '客户组合商品工艺物料关联表'
}
总结

表与表之间的关系,需要在导出的DBML中用ref定义。

http://www.dtcms.com/a/332081.html

相关文章:

  • Qt---Qt函数库
  • ActionChains 鼠标操作笔记
  • # Vue 列表渲染详解
  • AI智能体|扣子(Coze)搭建【批量识别发票并录入飞书】Agent
  • FTP 服务详解:原理、配置与实践
  • 8月14日星期四今日早报简报微语报早读
  • [激光原理与应用-273]:理论 - 波动光学 - 光是电磁波,本身并没有颜色,可见光的颜色不过是人的主观感受
  • 时钟 中断 day54
  • close函数概念和使用案例
  • rustdesk 开源遥控软件
  • 云服务器运行持续强化学习COOM框架的问题
  • 低配硬件运行智谱GLM-4.5V视觉语言模型推理服务的方法
  • C#WPF实战出真汁01--项目介绍
  • linux设备驱动之USB驱动-USB主机、设备与Gadget驱动
  • 【Java|第十九篇】面向对象九——String类和枚举类
  • AI更换商品背景,智能融合,无痕修图
  • Java中加载语义模型
  • Windows bypassUAC 提权技法详解(一)
  • 洗浴中心泡池水过滤系统原理深度解析与工程实践
  • RocketMQ 4.9.3源码解读-客户端Consumer消费者组件启动流程分析
  • 具身智能Scaling Law缺失:机器人界的“摩尔定律“何时诞生?
  • Ansible企业级实战
  • centos部署chrome和chromedriver
  • C#WPF实战出真汁03--登录界面设计
  • C#WPF实战出真汁04--登录功能实现
  • 单目操作符与逗号表达式
  • CoreShop商城框架开启多租户(2)
  • 莫队 + 离散化 Ann and Books
  • 【19-模型训练细节 】
  • 业务敏捷性对SAP驱动型企业意味着什么?如何保持企业敏捷性?