From 15686b44ad6a1b8671046c1b9ddc0c4a00b6c6d8 Mon Sep 17 00:00:00 2001 From: insistence <3055204202@qq.com> Date: Mon, 17 Feb 2025 23:06:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E9=80=82=E9=85=8Dpg=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module_generator/dao/gen_dao.py | 156 ++++++++++++------ .../module_generator/templates/sql/sql.jinja2 | 27 ++- .../sql/ruoyi-fastapi-pg.sql | 14 +- ruoyi-fastapi-backend/utils/template_util.py | 2 + 4 files changed, 136 insertions(+), 63 deletions(-) diff --git a/ruoyi-fastapi-backend/module_generator/dao/gen_dao.py b/ruoyi-fastapi-backend/module_generator/dao/gen_dao.py index c125741..4e080db 100644 --- a/ruoyi-fastapi-backend/module_generator/dao/gen_dao.py +++ b/ruoyi-fastapi-backend/module_generator/dao/gen_dao.py @@ -3,6 +3,7 @@ from sqlalchemy import delete, func, select, text, update from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import selectinload from typing import List +from config.env import DataBaseConfig from module_generator.entity.do.gen_do import GenTable, GenTableColumn from module_generator.entity.vo.gen_vo import ( GenTableBaseModel, @@ -127,27 +128,47 @@ class GenTableDao: :param is_page: 是否开启分页 :return: 数据库列表信息对象 """ - query_sql = """ - table_name as table_name, - table_comment as table_comment, - create_time as create_time, - update_time as update_time - from - information_schema.tables - where - table_schema = (select database()) - and table_name not like 'apscheduler\_%' - and table_name not like 'gen\_%' - and table_name not in (select table_name from gen_table) - """ + if DataBaseConfig.db_type == 'postgresql': + query_sql = """ + table_name as table_name, + table_comment as table_comment, + create_time as create_time, + update_time as update_time + from + list_table + where + table_name not like 'apscheduler_%' + and table_name not like 'gen_%' + and table_name not in (select table_name from gen_table) + """ + else: + query_sql = """ + table_name as table_name, + table_comment as table_comment, + create_time as create_time, + update_time as update_time + from + information_schema.tables + where + table_schema = (select database()) + and table_name not like 'apscheduler\_%' + and table_name not like 'gen\_%' + and table_name not in (select table_name from gen_table) + """ if query_object.table_name: query_sql += """and lower(table_name) like lower(concat('%', :table_name, '%'))""" if query_object.table_comment: query_sql += """and lower(table_comment) like lower(concat('%', :table_comment, '%'))""" if query_object.begin_time: - query_sql += """and date_format(create_time, '%Y%m%d') >= date_format(:begin_time, '%Y%m%d')""" + if DataBaseConfig.db_type == 'postgresql': + query_sql += """and create_time::date >= to_date(:begin_time, 'yyyy-MM-dd')""" + else: + query_sql += """and date_format(create_time, '%Y%m%d') >= date_format(:begin_time, '%Y%m%d')""" if query_object.end_time: - query_sql += """and date_format(create_time, '%Y%m%d') >= date_format(:end_time, '%Y%m%d')""" + if DataBaseConfig.db_type == 'postgresql': + query_sql += """and create_time::date <= to_date(:end_time, 'yyyy-MM-dd')""" + else: + query_sql += """and date_format(create_time, '%Y%m%d') >= date_format(:end_time, '%Y%m%d')""" query_sql += """order by create_time desc""" query = select( text(query_sql).bindparams( @@ -170,20 +191,35 @@ class GenTableDao: :param table_names: 业务表名称组 :return: 数据库列表信息对象 """ - query_sql = """ - select - table_name as table_name, - table_comment as table_comment, - create_time as create_time, - update_time as update_time - from - information_schema.tables - where - table_name not like 'qrtz\_%' - and table_name not like 'gen\_%' - and table_schema = (select database()) - and table_name in :table_names - """ + if DataBaseConfig.db_type == 'postgresql': + query_sql = """ + select + table_name as table_name, + table_comment as table_comment, + create_time as create_time, + update_time as update_time + from + list_table + where + table_name not like 'qrtz_%' + and table_name not like 'gen_%' + and table_name = any(:table_names) + """ + else: + query_sql = """ + select + table_name as table_name, + table_comment as table_comment, + create_time as create_time, + update_time as update_time + from + information_schema.tables + where + table_name not like 'qrtz\_%' + and table_name not like 'gen\_%' + and table_schema = (select database()) + and table_name in :table_names + """ query = text(query_sql).bindparams(table_names=tuple(table_names)) gen_db_table_list = (await db.execute(query)).fetchall() @@ -256,32 +292,42 @@ class GenTableColumnDao: :param table_name: 业务表名称 :return: 业务表字段列表信息对象 """ - query_sql = """ - select - column_name as column_name, - case - when is_nullable = 'no' and column_key != 'PRI' then '1' - else '0' - end as is_required, - case - when column_key = 'PRI' then '1' - else '0' - end as is_pk, - ordinal_position as sort, - column_comment as column_comment, - case - when extra = 'auto_increment' then '1' - else '0' - end as is_increment, - column_type as column_type - from - information_schema.columns - where - table_schema = (select database()) - and table_name = :table_name - order by - ordinal_position - """ + if DataBaseConfig.db_type == 'postgresql': + query_sql = """ + select + column_name, is_required, is_pk, sort, column_comment, is_increment, column_type + from + list_column + where + table_name = :table_name + """ + else: + query_sql = """ + select + column_name as column_name, + case + when is_nullable = 'no' and column_key != 'PRI' then '1' + else '0' + end as is_required, + case + when column_key = 'PRI' then '1' + else '0' + end as is_pk, + ordinal_position as sort, + column_comment as column_comment, + case + when extra = 'auto_increment' then '1' + else '0' + end as is_increment, + column_type as column_type + from + information_schema.columns + where + table_schema = (select database()) + and table_name = :table_name + order by + ordinal_position + """ query = text(query_sql).bindparams(table_name=table_name) gen_db_table_columns = (await db.execute(query)).fetchall() diff --git a/ruoyi-fastapi-backend/module_generator/templates/sql/sql.jinja2 b/ruoyi-fastapi-backend/module_generator/templates/sql/sql.jinja2 index 809851c..346d5d4 100644 --- a/ruoyi-fastapi-backend/module_generator/templates/sql/sql.jinja2 +++ b/ruoyi-fastapi-backend/module_generator/templates/sql/sql.jinja2 @@ -1,3 +1,27 @@ +{% if dbType == 'postgresql' %} +-- 菜单 SQL +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('${functionName}', '${parentMenuId}', '1', '${businessName}', '${moduleName}/${businessName}/index', 1, 0, 'C', '0', '0', '${permissionPrefix}:list', '#', 'admin', current_timestamp, '', null, '${functionName}菜单'); + +-- 按钮父菜单ID +select max(menu_id) from sys_menu; + +-- 按钮 SQL +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('${functionName}查询', max(menu_id), '1', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:query', '#', 'admin', current_timestamp, '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('${functionName}新增', max(menu_id), '2', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:add', '#', 'admin', current_timestamp, '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('${functionName}修改', max(menu_id), '3', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:edit', '#', 'admin', current_timestamp, '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('${functionName}删除', max(menu_id), '4', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:remove', '#', 'admin', current_timestamp, '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('${functionName}导出', max(menu_id), '5', '#', '', 1, 0, 'F', '0', '0', '${permissionPrefix}:export', '#', 'admin', current_timestamp, '', null, ''); +{% else %} -- 菜单 SQL insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) values('{{ functionName }}', '{{ parentMenuId }}', '1', '{{ businessName }}', '{{ moduleName }}/{{ businessName }}/index', 1, 0, 'C', '0', '0', '{{ permissionPrefix }}:list', '#', 'admin', sysdate(), '', null, '{{ functionName }}菜单'); @@ -19,4 +43,5 @@ insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame values('{{ functionName }}删除', @parentId, '4', '#', '', 1, 0, 'F', '0', '0', '{{ permissionPrefix }}:remove', '#', 'admin', sysdate(), '', null, ''); insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) -values('{{ functionName }}导出', @parentId, '5', '#', '', 1, 0, 'F', '0', '0', '{{ permissionPrefix }}:export', '#', 'admin', sysdate(), '', null, ''); \ No newline at end of file +values('{{ functionName }}导出', @parentId, '5', '#', '', 1, 0, 'F', '0', '0', '{{ permissionPrefix }}:export', '#', 'admin', sysdate(), '', null, ''); +{% endif %} \ No newline at end of file diff --git a/ruoyi-fastapi-backend/sql/ruoyi-fastapi-pg.sql b/ruoyi-fastapi-backend/sql/ruoyi-fastapi-pg.sql index 4ef7154..980c31f 100644 --- a/ruoyi-fastapi-backend/sql/ruoyi-fastapi-pg.sql +++ b/ruoyi-fastapi-backend/sql/ruoyi-fastapi-pg.sql @@ -909,7 +909,7 @@ comment on table gen_table is '代码生成业务表'; drop table if exists gen_table_column; create table gen_table_column ( column_id bigserial not null, - table_id varchar(64), + table_id bigint, column_name varchar(200), column_comment varchar(500), column_type varchar(100), @@ -981,18 +981,18 @@ SELECT c.relname a.attname AS column_name, d.description AS column_comment, CASE - WHEN a.attnotnull AND con.conname IS NULL THEN 1 - ELSE 0 + WHEN a.attnotnull AND con.conname IS NULL THEN '1' + ELSE '0' END AS is_required, CASE - WHEN con.conname IS NOT NULL THEN 1 - ELSE 0 + WHEN con.conname IS NOT NULL THEN '1' + ELSE '0' END AS is_pk, a.attnum AS sort, CASE WHEN "position"(pg_get_expr(ad.adbin, ad.adrelid), ((c.relname::text || '_'::text) || a.attname - ::text) || '_seq'::text) > 0 THEN 1 - ELSE 0 + ::text) || '_seq'::text) > 0 THEN '1' + ELSE '0' END AS is_increment, btrim( CASE diff --git a/ruoyi-fastapi-backend/utils/template_util.py b/ruoyi-fastapi-backend/utils/template_util.py index 198dd0b..2ef4e9a 100644 --- a/ruoyi-fastapi-backend/utils/template_util.py +++ b/ruoyi-fastapi-backend/utils/template_util.py @@ -4,6 +4,7 @@ from datetime import datetime from jinja2 import Environment, FileSystemLoader from typing import Dict, List, Set from config.constant import GenConstant +from config.env import DataBaseConfig from module_generator.entity.vo.gen_vo import GenTableModel, GenTableColumnModel from utils.common_util import CamelCaseUtil, SnakeCaseUtil from utils.string_util import StringUtil @@ -86,6 +87,7 @@ class TemplateUtils: 'columns': gen_table.columns, 'table': gen_table, 'dicts': cls.get_dicts(gen_table), + 'dbType': DataBaseConfig.db_type, } # 设置菜单、树形结构、子表的上下文