提交 | 用户 | 时间
|
a6de49
|
1 |
///** |
H |
2 |
// * Copyright (c) 2018 人人开源 All rights reserved. |
|
3 |
// * |
|
4 |
// * https://www.renren.io |
|
5 |
// * |
|
6 |
// * 版权所有,侵权必究! |
|
7 |
// */ |
|
8 |
// |
|
9 |
//package com.iailab.common.interceptor; |
|
10 |
// |
|
11 |
//import cn.hutool.core.util.StrUtil; |
|
12 |
//import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
|
13 |
//import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
|
14 |
//import net.sf.jsqlparser.JSQLParserException; |
|
15 |
//import net.sf.jsqlparser.expression.Expression; |
|
16 |
//import net.sf.jsqlparser.expression.StringValue; |
|
17 |
//import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
|
18 |
//import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
|
19 |
//import net.sf.jsqlparser.statement.select.PlainSelect; |
|
20 |
//import net.sf.jsqlparser.statement.select.Select; |
|
21 |
//import org.apache.ibatis.executor.Executor; |
|
22 |
//import org.apache.ibatis.mapping.BoundSql; |
|
23 |
//import org.apache.ibatis.mapping.MappedStatement; |
|
24 |
//import org.apache.ibatis.session.ResultHandler; |
|
25 |
//import org.apache.ibatis.session.RowBounds; |
|
26 |
// |
|
27 |
//import java.util.Map; |
|
28 |
// |
|
29 |
///** |
|
30 |
// * 数据过滤 |
|
31 |
// * |
|
32 |
// * @author Mark sunlightcs@gmail.com |
|
33 |
// */ |
|
34 |
//public class DataFilterInterceptor implements InnerInterceptor { |
|
35 |
// |
|
36 |
// @Override |
|
37 |
// public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { |
|
38 |
// DataScope scope = getDataScope(parameter); |
|
39 |
// // 不进行数据过滤 |
|
40 |
// if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ |
|
41 |
// return; |
|
42 |
// } |
|
43 |
// |
|
44 |
// // 拼接新SQL |
|
45 |
// String buildSql = getSelect(boundSql.getSql(), scope); |
|
46 |
// |
|
47 |
// // 重写SQL |
|
48 |
// PluginUtils.mpBoundSql(boundSql).sql(buildSql); |
|
49 |
// } |
|
50 |
// |
|
51 |
// private DataScope getDataScope(Object parameter){ |
|
52 |
// if (parameter == null){ |
|
53 |
// return null; |
|
54 |
// } |
|
55 |
// |
|
56 |
// // 判断参数里是否有DataScope对象 |
|
57 |
// if (parameter instanceof Map) { |
|
58 |
// Map<?, ?> parameterMap = (Map<?, ?>) parameter; |
|
59 |
// for (Map.Entry entry : parameterMap.entrySet()) { |
|
60 |
// if (entry.getValue() != null && entry.getValue() instanceof DataScope) { |
|
61 |
// return (DataScope) entry.getValue(); |
|
62 |
// } |
|
63 |
// } |
|
64 |
// } else if (parameter instanceof DataScope) { |
|
65 |
// return (DataScope) parameter; |
|
66 |
// } |
|
67 |
// |
|
68 |
// return null; |
|
69 |
// } |
|
70 |
// |
|
71 |
// private String getSelect(String buildSql, DataScope scope){ |
|
72 |
// try { |
|
73 |
// Select select = (Select) CCJSqlParserUtil.parse(buildSql); |
|
74 |
// PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); |
|
75 |
// |
|
76 |
// Expression expression = plainSelect.getWhere(); |
|
77 |
// if(expression == null){ |
|
78 |
// plainSelect.setWhere(new StringValue(scope.getSqlFilter())); |
|
79 |
// }else{ |
|
80 |
// AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); |
|
81 |
// plainSelect.setWhere(andExpression); |
|
82 |
// } |
|
83 |
// |
|
84 |
// return select.toString().replaceAll("'", ""); |
|
85 |
// }catch (JSQLParserException e){ |
|
86 |
// return buildSql; |
|
87 |
// } |
|
88 |
// } |
|
89 |
//} |