Эх сурвалжийг харах

fix:切换数据源连带新增表时选择数据源,脏数据条不回传前端异常信息

韩帛霖 1 жил өмнө
parent
commit
83fc89446b

+ 0 - 18
ruoyi-common/src/main/java/com/ruoyi/common/config/datamodeling/RedisDataClear.java

@@ -1,18 +0,0 @@
-package com.ruoyi.common.config.datamodeling;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Component;
-
-@Component
-
-public class RedisDataClear implements CommandLineRunner {
-    @Autowired
-    private RedisTemplate redisTemplate;
-
-    @Override
-    public void run(String... args) throws Exception {
-        redisTemplate.getConnectionFactory().getConnection().flushDb();
-    }
-}

+ 32 - 6
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/SqlInterceptor.java

@@ -25,16 +25,25 @@ public class SqlInterceptor implements Interceptor {
     public Object intercept(Invocation invocation) throws Throwable, TenantDataSource {
         StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
         String sql = statementHandler.getBoundSql().getSql();
-        // 修改SQL语句
-        String modifiedSql = BeforeSQL(sql);
+        // SQL执行前
+        // String modifiedSql = BeforeSQL(sql);
+        // 拼接sql
+        String modifiedSql = modifySql(sql);
+
+
         if (modifiedSql.equals("error")) {
             // 终止程序
             return AjaxResult.error("当前用户没有数据源信息!");
         } else {
+            // 将修改后的SQL语句设置回StatementHandler
+//            ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", modifiedSql);
+//            invocation.proceed();
+//            ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", sql);
+//            return invocation.proceed();
+
+
             // 将修改后的SQL语句设置回StatementHandler
             ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", modifiedSql);
-            invocation.proceed();
-            ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", sql);
             return invocation.proceed();
         }
     }
@@ -50,8 +59,25 @@ public class SqlInterceptor implements Interceptor {
             return "USE `" + SecurityUtils.getDatabaseName() + "`; " + sql;
         if (SecurityUtils.getDatabaseType().equals("dm"))
             return "set schema " + SecurityUtils.getDatabaseName() + "; " + sql;
-        if (SecurityUtils.getDatabaseType().equals("mysql"))
-            return "USE `" + SecurityUtils.getDatabaseName() + "`; " + sql;
+        if (SecurityUtils.getDatabaseType().equals("mysql")) {
+            if (sql.contains("{DBNAME}.")) {
+                return sql.replace("{DBNAME}.", "`" + SecurityUtils.getDatabaseName() + "`.");
+            } else {
+                if (sql.contains("information_schema")) return sql;  // 执行当前sql不需要选择数据源
+                StringBuilder sb = new StringBuilder(sql);
+
+                if (sb.indexOf(" from ") != -1) {
+                    sb.insert(sb.indexOf(" from ") + 6, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                } else if (sb.indexOf(" FROM ") != -1) {
+                    sb.insert(sb.indexOf(" FROM ") + 6, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                } else if (sb.indexOf(" into ") != -1) {
+                    sb.insert(sb.indexOf(" into ") + 6, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                } else if (sb.indexOf("update ") != -1) {
+                    sb.insert(sb.indexOf("update ") + 7, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                }
+                return sb.toString();
+            }
+        }
         return "error";
     }
 

+ 36 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/SqlLogAspect.java

@@ -0,0 +1,36 @@
+package com.ruoyi.framework.aspectj;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import org.aspectj.lang.reflect.MethodSignature;
+
+@Aspect
+@Component
+public class SqlLogAspect {
+
+    @Around("execution(* com.ruoyi.system.mapper.*.*(..))") // 匹配所有Mapper接口的方法
+    public Object logSql(ProceedingJoinPoint joinPoint) throws Throwable {
+        // 获取目标对象、方法和参数
+        Object target = joinPoint.getTarget();
+        String methodName = joinPoint.getSignature().getName();
+        Object[] args = joinPoint.getArgs();
+        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+        Class<?> returnType = signature.getReturnType();
+
+        // 执行目标方法
+        Object result = joinPoint.proceed();
+
+        // 判断结果集类型
+        if (!(result instanceof List)) {
+            return Class.forName(returnType.toString().split("class ")[1]).newInstance();
+        }
+
+        return result;
+    }
+}

+ 3 - 3
zkqy-datamodeling/src/main/java/com/zkqy/datamodeling/mapper/TableInfoMapper.java

@@ -29,7 +29,7 @@ public interface TableInfoMapper {
      * @param tableComment 表注释
      * @param filedList    表字段信息
      */
-    void createMysqlTable(@Param("tableName") String tableName, @Param("tableComment") String tableComment, @Param("filedList") List<String> filedList);
+    void createMysqlTable(@Param("tableName") String tableName, @Param("tableComment") String tableComment, @Param("filedList") List<String> filedList, @Param("databases") String databases);
 
     /**
      * 创建mysql数据库
@@ -77,7 +77,7 @@ public interface TableInfoMapper {
     /**
      * 备份表sql
      */
-    Map<String,String> backupTableSql(@Param("dataBaseName") String dataBaseName, @Param("tableName") String tableName);
+    Map<String, String> backupTableSql(@Param("dataBaseName") String dataBaseName, @Param("tableName") String tableName);
 
     //---------------------------------sqlServer------------------------------------------
 
@@ -229,7 +229,7 @@ public interface TableInfoMapper {
     /**
      * 创建触发器
      */
-    void createTrigger(@Param("triggerName") String triggerName,@Param("autoField") String autoField);
+    void createTrigger(@Param("triggerName") String triggerName, @Param("autoField") String autoField);
 
     /**
      * 添加注释

+ 2 - 1
zkqy-datamodeling/src/main/java/com/zkqy/datamodeling/service/impl/TableInfoServiceImpl.java

@@ -13,6 +13,7 @@ import java.nio.charset.StandardCharsets;
 import java.sql.*;
 import javax.sql.rowset.JdbcRowSet;
 
+import com.ruoyi.common.utils.SecurityUtils;
 import com.sun.rowset.JdbcRowSetImpl;
 
 import com.zkqy.datamodeling.domain.TableInfo;
@@ -77,7 +78,7 @@ public class TableInfoServiceImpl implements ITableInfoService {
         list.add("update_by varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT '更新者'");
         list.add("update_time datetime NULL DEFAULT NULL COMMENT '更新时间'");
         list.add("del_flag char(1) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT '删除标志(0代表存在 2代表删除)'");
-        tableInfoMapper.createMysqlTable(tableName, tableComment, list);
+        tableInfoMapper.createMysqlTable(tableName, tableComment, list, SecurityUtils.getDatabaseName());
     }
 
     @Override

+ 1 - 1
zkqy-datamodeling/src/main/resources/mapper/datamodeling/TableInfoMapper.xml

@@ -21,7 +21,7 @@
     </update>
 
     <update id="createMysqlTable">
-        CREATE TABLE IF NOT EXISTS `${tableName}`
+        CREATE TABLE IF NOT EXISTS `${databases}`.`${tableName}`
         (
         <foreach collection="filedList" item="filed" separator=",">
             ${filed}