SelectItemVisitorImpl selectItemVisitor = new SelectItemVisitorImpl();
//替换之后的列字段
List<String> columnList = new ArrayList<>();
// 解释在下方
List<AllTableColumns> allTableColumns = new ArrayList<>();
List<SelectExpressionItem> selectExpressionItems = new ArrayList<>();
List<String> tableList = getTableList(plainSelect.toString());
if (tableList != null) {
for (SelectItem item : plainSelect.getSelectItems()) {
item.accept(selectItemVisitor);
//设置可查询的列
if (selectItemVisitor.getEnhancedCondition() != null) {
String aliasColumn = selectItemVisitor.getEnhancedCondition().toString();
//正则校验以'开头,以'结尾,存在替换
String regex = "'([\\s\\S]*?)'";
Matcher matcher = Pattern.compile(regex).matcher(aliasColumn);
// 满足正则校验,剔除字符串的第一位和最后一位
if (matcher.find()){
aliasColumn = aliasColumn.substring(1,aliasColumn.length()-1);
}
//将字符串按照.进行分隔,前面为别名后面的字段名
String[] tableArr = aliasColumn.split("\\.");
String table = tableList.size() ==1 ? tableList.get(0):tableArr[0];
if (aliasColumn.contains("*")) {
TableInfo tableInfo1 = tableMap.get(table);
if (tableInfo1 != null) {
List<ColumnInfo> columnInfos = tableInfo1.getColumnInfos();
//设置权限且有权限的
List<ColumnInfo> collect = columnInfos.stream()
.filter(e -> e.getIsSetPermission() && e
.getIsHavePermission()).collect(
Collectors.toList());
//不做权限设置的,通用的
List<ColumnInfo> collect1 = columnInfos.stream()
.filter(e -> !e.getIsSetPermission()).collect(
Collectors.toList());
for (ColumnInfo info : collect) {
selectExpressionItems.add(new SelectExpressionItem(new Column(new Table(table),info.getColumnName())));
columnList.add(table+"."+info.getColumnName());
}
for (ColumnInfo info : collect1) {
selectExpressionItems.add(new SelectExpressionItem(new Column(new Table(table),info.getColumnName())));
columnList.add(table+"."+info.getColumnName());
}
} else {
allTableColumns.add(new AllTableColumns(new Table(table)));
}
} else {
TableInfo tableInfo1 = tableMap.get(table);
boolean flag = tableArr.length == 1;
if (tableInfo1 != null) {
List<ColumnInfo> columnInfos = tableInfo1.getColumnInfos();
//对比字段
Map<String, ColumnInfo> collect = columnInfos.stream()
.collect(Collectors.toMap(ColumnInfo::getColumnName,
Function.identity()));
if (flag){
ColumnInfo columnInfo = collect.get(tableArr[0]);
if ((columnInfo.getIsSetPermission() && columnInfo.getIsHavePermission()) || !columnInfo.getIsSetPermission()) {
selectExpressionItems.add(new SelectExpressionItem(new Column(new Table(table),tableArr[0])));
}
} else {
ColumnInfo columnInfo = collect.get(tableArr[1]);
if ((columnInfo.getIsSetPermission() && columnInfo.getIsHavePermission()) || !columnInfo.getIsSetPermission()) {
selectExpressionItems.add(new SelectExpressionItem(new Column(new Table(table),tableArr[1])));
}
}
}
}
}
}
}
// 替换字段
SelectItem[] list = new SelectItem[allTableColumns.size()+selectExpressionItems.size()];
plainSelect.setSelectItems(null);
for (int i = 0 ; i < allTableColumns.size();i++) {
list[i] = allTableColumns.get(i);
plainSelect.addSelectItems(list[i]);
}
for(int j = 0 ; j < selectExpressionItems.size();j++){
list[j+allTableColumns.size()] = selectExpressionItems.get(j);
plainSelect.addSelectItems(list[j+allTableColumns.size()]);
}
}
// 访问from
FromItem fromItem = plainSelect.getFromItem();
FromItemVisitorImpl fromItemVisitorImpl = new FromItemVisitorImpl();
fromItem.accept(fromItemVisitorImpl);
// 访问join
if (plainSelect.getJoins() != null) {
for (Join join : plainSelect.getJoins()) {
join.getRightItem().accept(fromItemVisitorImpl);
}
}
// 访问where
if (plainSelect.getWhere() != null) {
plainSelect.getWhere().accept(new ExpressionVisitorImpl());
}
//过滤增强的条件
if (fromItemVisitorImpl.getEnhancedCondition() != null) {
if (plainSelect.getWhere() != null) {
Expression expr = new Parenthesis(plainSelect.getWhere());
Expression enhancedCondition = new Parenthesis(fromItemVisitorImpl.getEnhancedCondition());
AndExpression and = new AndExpression(enhancedCondition, expr);
plainSelect.setWhere(and);
} else {
plainSelect.setWhere(fromItemVisitorImpl.getEnhancedCondition());
}
}
// 访问 order by
if (plainSelect.getOrderByElements() != null) {
for (OrderByElement orderByElement : plainSelect
.getOrderByElements()) {
orderByElement.getExpression().accept(
new ExpressionVisitorImpl());
}
}
// 访问group by having
if (plainSelect.getHaving() != null) {
plainSelect.getHaving().accept(new ExpressionVisitorImpl());
}
}
/**
* 获取sql中的表名
* @param sql
* @return
*/
private List<String> getTableList(String sql){
//获取table表名,替换字段
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
try {
return tablesNamesFinder
.getTableList(CCJSqlParserUtil.parse(sql));
} catch (JSQLParserException e) {
e.printStackTrace();
return null;
}
}
@Override
public void visit(SetOperationList setOpList) {
for (SelectBody plainSelect : setOpList.getSelects()) {
plainSelect.accept(new SelectVisitorImpl());
}
}
@Override
public void visit(WithItem withItem) {
// withItem.getSelectBody().accept(new SelectVisitorImpl());
}
}