|
@@ -12,9 +12,11 @@ import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
import net.sf.jsqlparser.schema.Column;
|
|
import net.sf.jsqlparser.schema.Column;
|
|
import net.sf.jsqlparser.statement.Statement;
|
|
import net.sf.jsqlparser.statement.Statement;
|
|
|
|
+import net.sf.jsqlparser.statement.delete.Delete;
|
|
import net.sf.jsqlparser.statement.select.PlainSelect;
|
|
import net.sf.jsqlparser.statement.select.PlainSelect;
|
|
import net.sf.jsqlparser.statement.select.Select;
|
|
import net.sf.jsqlparser.statement.select.Select;
|
|
import net.sf.jsqlparser.statement.select.SelectBody;
|
|
import net.sf.jsqlparser.statement.select.SelectBody;
|
|
|
|
+import net.sf.jsqlparser.statement.update.Update;
|
|
import org.apache.ibatis.executor.Executor;
|
|
import org.apache.ibatis.executor.Executor;
|
|
import org.apache.ibatis.mapping.BoundSql;
|
|
import org.apache.ibatis.mapping.BoundSql;
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
@@ -29,6 +31,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
import java.sql.SQLException;
|
|
import java.sql.SQLException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @anthor 畅
|
|
* @anthor 畅
|
|
@@ -48,27 +51,61 @@ public class ProjectInterceptor implements InnerInterceptor {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
|
|
|
|
- BoundSql boundSql = ms.getBoundSql(parameter);
|
|
|
|
- if (ms.getSqlCommandType().equals(SqlCommandType.DELETE)) {
|
|
|
|
- // 转换DELETE为UPDATE,此处需复杂处理,示例简化
|
|
|
|
- // 实际需解析原始SQL并替换,此处仅为思路
|
|
|
|
- String newSql = convertDeleteToUpdate(boundSql.getSql());
|
|
|
|
- // 反射修改BoundSql中的SQL
|
|
|
|
- Class<? extends BoundSql> aClass = boundSql.getClass();
|
|
|
|
- Field declaredField = null;
|
|
|
|
- try {
|
|
|
|
- declaredField = aClass.getDeclaredField("sql");
|
|
|
|
- declaredField.setAccessible(true);
|
|
|
|
- declaredField.set(boundSql,newSql);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
|
|
+// @Override
|
|
|
|
+// public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
|
|
|
|
+// BoundSql boundSql = ms.getBoundSql(parameter);
|
|
|
|
+// if (ms.getSqlCommandType().equals(SqlCommandType.DELETE)) {
|
|
|
|
+//
|
|
|
|
+//// // 转换DELETE为UPDATE,此处需复杂处理,示例简化
|
|
|
|
+//// // 实际需解析原始SQL并替换,此处仅为思路
|
|
|
|
+//// String newSql = convertDeleteToUpdate(boundSql.getSql());
|
|
|
|
+//// // 反射修改BoundSql中的SQL
|
|
|
|
+//// Class<? extends BoundSql> aClass = boundSql.getClass();
|
|
|
|
+//// Field declaredField = null;
|
|
|
|
+//// try {
|
|
|
|
+//// declaredField = aClass.getDeclaredField("sql");
|
|
|
|
+//// declaredField.setAccessible(true);
|
|
|
|
+//// declaredField.set(boundSql,newSql);
|
|
|
|
+//// } catch (Exception e) {
|
|
|
|
+//// throw new RuntimeException(e);
|
|
|
|
+//// }
|
|
|
|
+//
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+@Override
|
|
|
|
+public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
|
|
|
|
+ if (ms.getSqlCommandType() == SqlCommandType.DELETE) {
|
|
|
|
+ try {
|
|
|
|
+ // 获取原始 SQL
|
|
|
|
+ BoundSql boundSql = ms.getBoundSql(parameter);
|
|
|
|
+ String originalSql = boundSql.getSql();
|
|
|
|
+
|
|
|
|
+ // 解析 DELETE 语句
|
|
|
|
+ Statement statement = CCJSqlParserUtil.parse(originalSql);
|
|
|
|
+ if (statement instanceof Delete) {
|
|
|
|
+ Delete delete = (Delete) statement;
|
|
|
|
|
|
|
|
+ // 构建 UPDATE 语句
|
|
|
|
+ Update update = new Update();
|
|
|
|
+ update.setTable(delete.getTable());
|
|
|
|
+ update.setWhere(delete.getWhere());
|
|
|
|
+ ArrayList<Expression> columns = new ArrayList<>();
|
|
|
|
+ // 添加 SET deleted=1
|
|
|
|
+ EqualsTo setDeletion = new EqualsTo();
|
|
|
|
+ setDeletion.setLeftExpression(new Column("delete_flag"));
|
|
|
|
+ setDeletion.setRightExpression(new LongValue(1));
|
|
|
|
+ columns.add(setDeletion);
|
|
|
|
+ update.setExpressions(columns);
|
|
|
|
+
|
|
|
|
+ PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
|
|
|
|
+ System.out.println(update.toString());
|
|
|
|
+ mpBoundSql.sql(update.toString());
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new SQLException("SQL转换失败", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+}
|
|
@Override
|
|
@Override
|
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|