|
@@ -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";
|
|
|
}
|
|
|
|