mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-13 21:17:08 +00:00
(improvement)(chat) logic sql show in chinese and convert to bizName in execute (#156)
This commit is contained in:
@@ -16,6 +16,7 @@ public class DateUtils {
|
||||
|
||||
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
public static final String DATE_FIELD = "数据日期";
|
||||
public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
public static Integer currentYear() {
|
||||
|
||||
@@ -9,16 +9,16 @@ import net.sf.jsqlparser.schema.Column;
|
||||
public class FieldReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
ParseVisitorHelper parseVisitorHelper = new ParseVisitorHelper();
|
||||
private Map<String, String> fieldToBizName;
|
||||
private Map<String, String> fieldNameMap;
|
||||
private boolean exactReplace;
|
||||
|
||||
public FieldReplaceVisitor(Map<String, String> fieldToBizName, boolean exactReplace) {
|
||||
this.fieldToBizName = fieldToBizName;
|
||||
public FieldReplaceVisitor(Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
this.fieldNameMap = fieldNameMap;
|
||||
this.exactReplace = exactReplace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Column column) {
|
||||
parseVisitorHelper.replaceColumn(column, fieldToBizName, exactReplace);
|
||||
parseVisitorHelper.replaceColumn(column, fieldNameMap, exactReplace);
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,11 @@ import org.apache.commons.lang3.StringUtils;
|
||||
public class GroupByReplaceVisitor implements GroupByVisitor {
|
||||
|
||||
ParseVisitorHelper parseVisitorHelper = new ParseVisitorHelper();
|
||||
private Map<String, String> fieldToBizName;
|
||||
private Map<String, String> fieldNameMap;
|
||||
private boolean exactReplace;
|
||||
|
||||
public GroupByReplaceVisitor(Map<String, String> fieldToBizName, boolean exactReplace) {
|
||||
this.fieldToBizName = fieldToBizName;
|
||||
public GroupByReplaceVisitor(Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
this.fieldNameMap = fieldNameMap;
|
||||
this.exactReplace = exactReplace;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class GroupByReplaceVisitor implements GroupByVisitor {
|
||||
for (int i = 0; i < groupByExpressions.size(); i++) {
|
||||
Expression expression = groupByExpressions.get(i);
|
||||
|
||||
String replaceColumn = parseVisitorHelper.getReplaceColumn(expression.toString(), fieldToBizName,
|
||||
String replaceColumn = parseVisitorHelper.getReplaceColumn(expression.toString(), fieldNameMap,
|
||||
exactReplace);
|
||||
if (StringUtils.isNotEmpty(replaceColumn)) {
|
||||
if (expression instanceof Column) {
|
||||
|
||||
@@ -11,11 +11,11 @@ import net.sf.jsqlparser.statement.select.OrderByVisitorAdapter;
|
||||
public class OrderByReplaceVisitor extends OrderByVisitorAdapter {
|
||||
|
||||
ParseVisitorHelper parseVisitorHelper = new ParseVisitorHelper();
|
||||
private Map<String, String> fieldToBizName;
|
||||
private Map<String, String> fieldNameMap;
|
||||
private boolean exactReplace;
|
||||
|
||||
public OrderByReplaceVisitor(Map<String, String> fieldToBizName, boolean exactReplace) {
|
||||
this.fieldToBizName = fieldToBizName;
|
||||
public OrderByReplaceVisitor(Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
this.fieldNameMap = fieldNameMap;
|
||||
this.exactReplace = exactReplace;
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ public class OrderByReplaceVisitor extends OrderByVisitorAdapter {
|
||||
public void visit(OrderByElement orderBy) {
|
||||
Expression expression = orderBy.getExpression();
|
||||
if (expression instanceof Column) {
|
||||
parseVisitorHelper.replaceColumn((Column) expression, fieldToBizName, exactReplace);
|
||||
parseVisitorHelper.replaceColumn((Column) expression, fieldNameMap, exactReplace);
|
||||
}
|
||||
if (expression instanceof Function) {
|
||||
Function function = (Function) expression;
|
||||
List<Expression> expressions = function.getParameters().getExpressions();
|
||||
for (Expression column : expressions) {
|
||||
if (column instanceof Column) {
|
||||
parseVisitorHelper.replaceColumn((Column) column, fieldToBizName, exactReplace);
|
||||
parseVisitorHelper.replaceColumn((Column) column, fieldNameMap, exactReplace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,23 +11,23 @@ import org.apache.commons.lang3.StringUtils;
|
||||
@Slf4j
|
||||
public class ParseVisitorHelper {
|
||||
|
||||
public void replaceColumn(Column column, Map<String, String> fieldToBizName, boolean exactReplace) {
|
||||
public void replaceColumn(Column column, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
String columnName = column.getColumnName();
|
||||
String replaceColumn = getReplaceColumn(columnName, fieldToBizName, exactReplace);
|
||||
String replaceColumn = getReplaceColumn(columnName, fieldNameMap, exactReplace);
|
||||
if (StringUtils.isNotBlank(replaceColumn)) {
|
||||
column.setColumnName(replaceColumn);
|
||||
}
|
||||
}
|
||||
|
||||
public String getReplaceColumn(String columnName, Map<String, String> fieldToBizName, boolean exactReplace) {
|
||||
String fieldBizName = fieldToBizName.get(columnName);
|
||||
if (StringUtils.isNotBlank(fieldBizName)) {
|
||||
return fieldBizName;
|
||||
public String getReplaceColumn(String columnName, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
String fieldName = fieldNameMap.get(columnName);
|
||||
if (StringUtils.isNotBlank(fieldName)) {
|
||||
return fieldName;
|
||||
}
|
||||
if (exactReplace) {
|
||||
return null;
|
||||
}
|
||||
Optional<Entry<String, String>> first = fieldToBizName.entrySet().stream().sorted((k1, k2) -> {
|
||||
Optional<Entry<String, String>> first = fieldNameMap.entrySet().stream().sorted((k1, k2) -> {
|
||||
String k1FieldNameDb = k1.getKey();
|
||||
String k2FieldNameDb = k2.getKey();
|
||||
Double k1Similarity = getSimilarity(columnName, k1FieldNameDb);
|
||||
|
||||
@@ -65,11 +65,11 @@ public class SqlParserUpdateHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceFields(String sql, Map<String, String> fieldToBizName) {
|
||||
return replaceFields(sql, fieldToBizName, false);
|
||||
public static String replaceFields(String sql, Map<String, String> fieldNameMap) {
|
||||
return replaceFields(sql, fieldNameMap, false);
|
||||
}
|
||||
|
||||
public static String replaceFields(String sql, Map<String, String> fieldToBizName, boolean exactReplace) {
|
||||
public static String replaceFields(String sql, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
@@ -78,7 +78,7 @@ public class SqlParserUpdateHelper {
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
//1. replace where fields
|
||||
Expression where = plainSelect.getWhere();
|
||||
FieldReplaceVisitor visitor = new FieldReplaceVisitor(fieldToBizName, exactReplace);
|
||||
FieldReplaceVisitor visitor = new FieldReplaceVisitor(fieldNameMap, exactReplace);
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(visitor);
|
||||
}
|
||||
@@ -92,14 +92,14 @@ public class SqlParserUpdateHelper {
|
||||
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
|
||||
if (!CollectionUtils.isEmpty(orderByElements)) {
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
orderByElement.accept(new OrderByReplaceVisitor(fieldToBizName, exactReplace));
|
||||
orderByElement.accept(new OrderByReplaceVisitor(fieldNameMap, exactReplace));
|
||||
}
|
||||
}
|
||||
|
||||
//4. replace group by fields
|
||||
GroupByElement groupByElement = plainSelect.getGroupBy();
|
||||
if (Objects.nonNull(groupByElement)) {
|
||||
groupByElement.accept(new GroupByReplaceVisitor(fieldToBizName, exactReplace));
|
||||
groupByElement.accept(new GroupByReplaceVisitor(fieldNameMap, exactReplace));
|
||||
}
|
||||
//5. replace having fields
|
||||
Expression having = plainSelect.getHaving();
|
||||
|
||||
Reference in New Issue
Block a user