From 79f015b741b6c585ed1cd58937775fb08d5af77d Mon Sep 17 00:00:00 2001 From: yang chen Date: Fri, 21 Nov 2025 17:23:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(test):=20=E6=B7=BB=E5=8A=A0=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=9C=80=E5=90=8E=E6=89=A7=E8=A1=8CSQL=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增GenerateLastExecuteSQLTest单元测试类 - 实现自动生成z_exec_last.sql文件的功能 - 根据指定日期读取对应SQL目录下的所有文件 - 过滤并提取SQL文件名用于构建更新语句 - 自动生成针对LOWCODE_DBFORM表的更新脚本 - 支持按月分类存储SQL文件的目录结构处理 --- SQL/202511/20251121/z_exec_last.sql | 7 +++ .../test/sql/GenerateLastExecuteSQLTest.java | 60 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 SQL/202511/20251121/z_exec_last.sql create mode 100644 jeelowcode-admin/src/test/java/com/jeelowcode/test/sql/GenerateLastExecuteSQLTest.java diff --git a/SQL/202511/20251121/z_exec_last.sql b/SQL/202511/20251121/z_exec_last.sql new file mode 100644 index 0000000..9635398 --- /dev/null +++ b/SQL/202511/20251121/z_exec_last.sql @@ -0,0 +1,7 @@ +update + "LOWCODE_FRAME"."LOWCODE_DBFORM" +set IS_DB_SYNC='N' +where TABLE_NAME in ( + 'lc_confined_space_operation', 'lc_fire_operation', 'lc_high_operation', 'lc_item_result', + 'lc_land_operation', 'lc_lifting_operation', 'lc_outside_person', 'lc_temporary_power_operation' + ); \ No newline at end of file diff --git a/jeelowcode-admin/src/test/java/com/jeelowcode/test/sql/GenerateLastExecuteSQLTest.java b/jeelowcode-admin/src/test/java/com/jeelowcode/test/sql/GenerateLastExecuteSQLTest.java new file mode 100644 index 0000000..07f1e62 --- /dev/null +++ b/jeelowcode-admin/src/test/java/com/jeelowcode/test/sql/GenerateLastExecuteSQLTest.java @@ -0,0 +1,60 @@ +package com.jeelowcode.test.sql; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.db.sql.SqlUtil; +import com.jeelowcode.tool.framework.test.core.ut.BaseMockitoUnitTest; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 描述:生成最后执行SQL + * + * @author shelly + */ +public class GenerateLastExecuteSQLTest extends BaseMockitoUnitTest { + + @Test + public void testGenerateLastExecuteSQL() throws Exception { + String executeDateStr = "20251121"; + Date executeDate = DateUtil.parse(executeDateStr, "yyyyMMdd"); + String monthStr = DateUtil.format(executeDate, "yyyyMM"); + // 获取项目目录下的SQL目录路径 + String projectPath = System.getProperty("user.dir"); + File adminPath = new File(projectPath); + File baseProjectPath = adminPath.getParentFile(); + File sqlPath = new File(baseProjectPath, "SQL"); + File monthSqlPath = new File(sqlPath, monthStr); + if (monthSqlPath.exists()) { + File dateSqlPath = new File(monthSqlPath, executeDateStr); + if (dateSqlPath.exists()) { + // 查询目录下所有的文件 + List sqlFileList = FileUtil.loopFiles(dateSqlPath); + if (CollUtil.isNotEmpty(sqlFileList)) { + // 将sqlFileList转换成字符串列表,字符串使用文件名,不带后缀 + List sqlFileNameList = sqlFileList.stream() + .map(file -> file.getName().substring(0, file.getName().lastIndexOf("."))) + .filter(fileName -> !StrUtil.equals(fileName, "z_exec_last")) + .map(fileName -> StrUtil.concat(true, "'", fileName, "'")) + .collect(Collectors.toList()); + //将列表sqlFileNameList转换成字符串,使用逗号分隔 + String template = "update \"LOWCODE_FRAME\".\"LOWCODE_DBFORM\" set IS_DB_SYNC='N' where TABLE_NAME in ({});"; + String formatSql = SqlUtil.formatSql(StrUtil.format(template, CollUtil.join(sqlFileNameList, ","))); + File lastExec = new File(dateSqlPath, "z_exec_last.sql"); + if (lastExec.exists() || (!lastExec.exists() && lastExec.createNewFile())) { + // 将formatSql写到lastExec中 + FileUtil.writeString(formatSql, lastExec, "utf-8"); + } + } + } + } + + } + +}