From 15cfbaaea8d9a83feb55e9411606747417cac9fc Mon Sep 17 00:00:00 2001 From: junjie Date: Mon, 2 Aug 2021 15:46:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AD=97=E6=AE=B5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=85=AC=E5=BC=8F=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dataease/dto/chart/ChartViewFieldDTO.java | 2 + .../provider/doris/DorisQueryProvider.java | 122 +++++++++++++++-- .../provider/mysql/MysqlQueryProvider.java | 127 ++++++++++++++++-- .../provider/oracle/OracleQueryProvider.java | 122 +++++++++++++++-- .../service/dataset/DataSetTableService.java | 16 ++- frontend/src/lang/en.js | 3 +- frontend/src/lang/tw.js | 3 +- frontend/src/lang/zh.js | 3 +- frontend/src/views/chart/group/Group.vue | 3 +- frontend/src/views/dataset/group/Group.vue | 3 +- 10 files changed, 358 insertions(+), 46 deletions(-) diff --git a/backend/src/main/java/io/dataease/dto/chart/ChartViewFieldDTO.java b/backend/src/main/java/io/dataease/dto/chart/ChartViewFieldDTO.java index b21b9adc8c..2d90dfbcbf 100644 --- a/backend/src/main/java/io/dataease/dto/chart/ChartViewFieldDTO.java +++ b/backend/src/main/java/io/dataease/dto/chart/ChartViewFieldDTO.java @@ -42,4 +42,6 @@ public class ChartViewFieldDTO implements Serializable { private String dateStyle; private String datePattern; + + private Integer extField; } diff --git a/backend/src/main/java/io/dataease/provider/doris/DorisQueryProvider.java b/backend/src/main/java/io/dataease/provider/doris/DorisQueryProvider.java index 5132840ea6..f55c2d442b 100644 --- a/backend/src/main/java/io/dataease/provider/doris/DorisQueryProvider.java +++ b/backend/src/main/java/io/dataease/provider/doris/DorisQueryProvider.java @@ -1,13 +1,14 @@ package io.dataease.provider.doris; import io.dataease.base.domain.DatasetTableField; +import io.dataease.base.domain.DatasetTableFieldExample; +import io.dataease.base.mapper.DatasetTableFieldMapper; import io.dataease.controller.request.chart.ChartExtFilterRequest; import io.dataease.dto.chart.ChartCustomFilterDTO; import io.dataease.dto.chart.ChartViewFieldDTO; import io.dataease.dto.sqlObj.SQLObj; import io.dataease.provider.QueryProvider; import io.dataease.provider.SQLConstants; -import io.dataease.provider.mysql.MySQLConstants; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -16,11 +17,12 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupFile; +import javax.annotation.Resource; import java.text.MessageFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static io.dataease.provider.SQLConstants.TABLE_ALIAS_PREFIX; @@ -30,6 +32,9 @@ import static io.dataease.provider.SQLConstants.TABLE_ALIAS_PREFIX; */ @Service("dorisQuery") public class DorisQueryProvider extends QueryProvider { + @Resource + private DatasetTableFieldMapper datasetTableFieldMapper; + @Override public Integer transFieldType(String field) { switch (field) { @@ -91,7 +96,15 @@ public class DorisQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(fields)) { for (int i = 0; i < fields.size(); i++) { DatasetTableField f = fields.get(i); - String originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getDataeaseName()); + String originField; + if (ObjectUtils.isNotEmpty(f.getExtField()) && f.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(f.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(f.getExtField()) && f.getExtField() == 1) { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getDataeaseName()); + } else { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getDataeaseName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); String fieldName = ""; // 处理横轴字段 @@ -181,7 +194,15 @@ public class DorisQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(xAxis)) { for (int i = 0; i < xAxis.size(); i++) { ChartViewFieldDTO x = xAxis.get(i); - String originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getDataeaseName()); + String originField; + if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(x.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 1) { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getDataeaseName()); + } else { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getDataeaseName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); // 处理横轴字段 xFields.add(getXFields(x, originField, fieldAlias)); @@ -203,7 +224,15 @@ public class DorisQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + } else { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -279,7 +308,15 @@ public class DorisQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(xList)) { for (int i = 0; i < xList.size(); i++) { ChartViewFieldDTO x = xList.get(i); - String originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getDataeaseName()); + String originField; + if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(x.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 1) { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getDataeaseName()); + } else { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getDataeaseName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); // 处理横轴字段 xFields.add(getXFields(x, originField, fieldAlias)); @@ -301,7 +338,15 @@ public class DorisQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + } else { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -380,7 +425,15 @@ public class DorisQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + } else { + originField = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getDataeaseName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -515,7 +568,17 @@ public class DorisQueryProvider extends QueryProvider { String whereName = ""; String whereTerm = transMysqlFilterTerm(request.getTerm()); String whereValue = ""; - String originName = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getDataeaseName()); + + String originName; + if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originName = calcFieldRegex(field.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 1) { + originName = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getDataeaseName()); + } else { + originName = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getDataeaseName()); + } + if (field.getDeType() == 1 && field.getDeExtractType() != 1) { String cast = String.format(DorisConstants.CAST, originName, DorisConstants.DEFAULT_INT_FORMAT) + "/1000"; whereName = String.format(DorisConstants.FROM_UNIXTIME, cast, DorisConstants.DEFAULT_DATE_FORMAT); @@ -557,7 +620,16 @@ public class DorisQueryProvider extends QueryProvider { String whereName = ""; String whereTerm = transMysqlFilterTerm(request.getOperator()); String whereValue = ""; - String originName = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getDataeaseName()); + + String originName; + if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originName = calcFieldRegex(field.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 1) { + originName = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getDataeaseName()); + } else { + originName = String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getDataeaseName()); + } if (field.getDeType() == 1 && field.getDeExtractType() != 1) { String cast = String.format(DorisConstants.CAST, originName, DorisConstants.DEFAULT_INT_FORMAT) + "/1000"; @@ -580,7 +652,7 @@ public class DorisQueryProvider extends QueryProvider { String endTime = simpleDateFormat.format(new Date(Long.parseLong(value.get(1)))); whereValue = String.format(DorisConstants.WHERE_BETWEEN, startTime, endTime); } else { - whereValue = String.format(MySQLConstants.WHERE_BETWEEN, value.get(0), value.get(1)); + whereValue = String.format(DorisConstants.WHERE_BETWEEN, value.get(0), value.get(1)); } } else { whereValue = String.format(DorisConstants.WHERE_VALUE_VALUE, value.get(0)); @@ -736,4 +808,28 @@ public class DorisQueryProvider extends QueryProvider { } return list; } + + private String calcFieldRegex(String originField, SQLObj tableObj) { + originField = originField.replaceAll("[\\t\\n\\r]]", ""); + // 正则提取[xxx] + String regex = "\\[(.*?)]"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(originField); + Set ids = new HashSet<>(); + while (matcher.find()) { + String id = matcher.group(1); + ids.add(id); + } + if (CollectionUtils.isEmpty(ids)) { + return originField; + } + DatasetTableFieldExample datasetTableFieldExample = new DatasetTableFieldExample(); + datasetTableFieldExample.createCriteria().andIdIn(new ArrayList<>(ids)); + List calcFields = datasetTableFieldMapper.selectByExample(datasetTableFieldExample); + for (DatasetTableField ele : calcFields) { + originField = originField.replaceAll("\\[" + ele.getId() + "]", + String.format(DorisConstants.KEYWORD_FIX, tableObj.getTableAlias(), ele.getOriginName())); + } + return originField; + } } diff --git a/backend/src/main/java/io/dataease/provider/mysql/MysqlQueryProvider.java b/backend/src/main/java/io/dataease/provider/mysql/MysqlQueryProvider.java index 108d0a9fd0..13ba47b27f 100644 --- a/backend/src/main/java/io/dataease/provider/mysql/MysqlQueryProvider.java +++ b/backend/src/main/java/io/dataease/provider/mysql/MysqlQueryProvider.java @@ -1,6 +1,8 @@ package io.dataease.provider.mysql; import io.dataease.base.domain.DatasetTableField; +import io.dataease.base.domain.DatasetTableFieldExample; +import io.dataease.base.mapper.DatasetTableFieldMapper; import io.dataease.controller.request.chart.ChartExtFilterRequest; import io.dataease.dto.chart.ChartCustomFilterDTO; import io.dataease.dto.chart.ChartViewFieldDTO; @@ -15,11 +17,12 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupFile; +import javax.annotation.Resource; import java.text.MessageFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static io.dataease.provider.SQLConstants.TABLE_ALIAS_PREFIX; @@ -29,6 +32,9 @@ import static io.dataease.provider.SQLConstants.TABLE_ALIAS_PREFIX; */ @Service("mysqlQuery") public class MysqlQueryProvider extends QueryProvider { + @Resource + private DatasetTableFieldMapper datasetTableFieldMapper; + @Override public Integer transFieldType(String field) { switch (field) { @@ -89,7 +95,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(fields)) { for (int i = 0; i < fields.size(); i++) { DatasetTableField f = fields.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(f.getExtField()) && f.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(f.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(f.getExtField()) && f.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); String fieldName = ""; // 处理横轴字段 @@ -171,7 +185,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(xAxis)) { for (int i = 0; i < xAxis.size(); i++) { ChartViewFieldDTO x = xAxis.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(x.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); // 处理横轴字段 xFields.add(getXFields(x, originField, fieldAlias)); @@ -193,7 +215,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -270,7 +300,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(xList)) { for (int i = 0; i < xList.size(); i++) { ChartViewFieldDTO x = xList.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(x.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); // 处理横轴字段 xFields.add(getXFields(x, originField, fieldAlias)); @@ -292,7 +330,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -313,7 +359,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(extStack)) { for (int i = 0; i < extStack.size(); i++) { ChartViewFieldDTO stack = extStack.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), stack.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(stack.getExtField()) && stack.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(stack.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(stack.getExtField()) && stack.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), stack.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), stack.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i); // 处理横轴字段 stackFields.add(getXFields(stack, originField, fieldAlias)); @@ -390,7 +444,15 @@ public class MysqlQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } else { + originField = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -522,7 +584,15 @@ public class MysqlQueryProvider extends QueryProvider { String whereName = ""; String whereTerm = transMysqlFilterTerm(request.getTerm()); String whereValue = ""; - String originName = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + String originName; + if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originName = calcFieldRegex(field.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 1) { + originName = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } else { + originName = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } if (field.getDeType() == 1 && field.getDeExtractType() != 1) { String cast = String.format(MySQLConstants.CAST, originName, MySQLConstants.DEFAULT_INT_FORMAT) + "/1000"; whereName = String.format(MySQLConstants.FROM_UNIXTIME, cast, MySQLConstants.DEFAULT_DATE_FORMAT); @@ -562,7 +632,16 @@ public class MysqlQueryProvider extends QueryProvider { String whereName = ""; String whereTerm = transMysqlFilterTerm(request.getOperator()); String whereValue = ""; - String originName = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + + String originName; + if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originName = calcFieldRegex(field.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 1) { + originName = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } else { + originName = String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } if (field.getDeType() == 1 && field.getDeExtractType() != 1) { String cast = String.format(MySQLConstants.CAST, originName, MySQLConstants.DEFAULT_INT_FORMAT) + "/1000"; @@ -743,4 +822,28 @@ public class MysqlQueryProvider extends QueryProvider { } return list; } + + private String calcFieldRegex(String originField, SQLObj tableObj) { + originField = originField.replaceAll("[\\t\\n\\r]]", ""); + // 正则提取[xxx] + String regex = "\\[(.*?)]"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(originField); + Set ids = new HashSet<>(); + while (matcher.find()) { + String id = matcher.group(1); + ids.add(id); + } + if (CollectionUtils.isEmpty(ids)) { + return originField; + } + DatasetTableFieldExample datasetTableFieldExample = new DatasetTableFieldExample(); + datasetTableFieldExample.createCriteria().andIdIn(new ArrayList<>(ids)); + List calcFields = datasetTableFieldMapper.selectByExample(datasetTableFieldExample); + for (DatasetTableField ele : calcFields) { + originField = originField.replaceAll("\\[" + ele.getId() + "]", + String.format(MySQLConstants.KEYWORD_FIX, tableObj.getTableAlias(), ele.getOriginName())); + } + return originField; + } } diff --git a/backend/src/main/java/io/dataease/provider/oracle/OracleQueryProvider.java b/backend/src/main/java/io/dataease/provider/oracle/OracleQueryProvider.java index 5b9204dee8..124c383736 100644 --- a/backend/src/main/java/io/dataease/provider/oracle/OracleQueryProvider.java +++ b/backend/src/main/java/io/dataease/provider/oracle/OracleQueryProvider.java @@ -1,13 +1,14 @@ package io.dataease.provider.oracle; import io.dataease.base.domain.DatasetTableField; +import io.dataease.base.domain.DatasetTableFieldExample; +import io.dataease.base.mapper.DatasetTableFieldMapper; import io.dataease.controller.request.chart.ChartExtFilterRequest; import io.dataease.dto.chart.ChartCustomFilterDTO; import io.dataease.dto.chart.ChartViewFieldDTO; import io.dataease.dto.sqlObj.SQLObj; import io.dataease.provider.QueryProvider; import io.dataease.provider.SQLConstants; -import io.dataease.provider.mysql.MySQLConstants; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -16,11 +17,12 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupFile; +import javax.annotation.Resource; import java.text.MessageFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static io.dataease.provider.SQLConstants.TABLE_ALIAS_PREFIX; @@ -37,6 +39,9 @@ public class OracleQueryProvider extends QueryProvider { private static Integer FLOAT = 3; private static Integer BOOLEAN = 4; + @Resource + private DatasetTableFieldMapper datasetTableFieldMapper; + @Override public Integer transFieldType(String field) { switch (field) { @@ -118,7 +123,15 @@ public class OracleQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(fields)) { for (int i = 0; i < fields.size(); i++) { DatasetTableField f = fields.get(i); - String originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(f.getExtField()) && f.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(f.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(f.getExtField()) && f.getExtField() == 1) { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getOriginName()); + } else { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), f.getOriginName()); + } String fieldAlias = String.format(OracleConstants.ALIAS_FIX, String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i)); String fieldName = ""; // 处理横轴字段 @@ -206,7 +219,15 @@ public class OracleQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(xAxis)) { for (int i = 0; i < xAxis.size(); i++) { ChartViewFieldDTO x = xAxis.get(i); - String originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(x.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 1) { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } else { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } String fieldAlias = String.format(OracleConstants.ALIAS_FIX, String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i)); // 处理横轴字段 xFields.add(getXFields(x, originField, fieldAlias)); @@ -228,7 +249,15 @@ public class OracleQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } else { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } String fieldAlias = String.format(OracleConstants.ALIAS_FIX, String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i)); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -304,7 +333,15 @@ public class OracleQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(xList)) { for (int i = 0; i < xList.size(); i++) { ChartViewFieldDTO x = xList.get(i); - String originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(x.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(x.getExtField()) && x.getExtField() == 1) { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } else { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), x.getOriginName()); + } String fieldAlias = String.format(OracleConstants.ALIAS_FIX, String.format(SQLConstants.FIELD_ALIAS_X_PREFIX, i)); // 处理横轴字段 xFields.add(getXFields(x, originField, fieldAlias)); @@ -326,7 +363,15 @@ public class OracleQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } else { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } String fieldAlias = String.format(OracleConstants.ALIAS_FIX, String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i)); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -405,7 +450,15 @@ public class OracleQueryProvider extends QueryProvider { if (CollectionUtils.isNotEmpty(yAxis)) { for (int i = 0; i < yAxis.size(); i++) { ChartViewFieldDTO y = yAxis.get(i); - String originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + String originField; + if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originField = calcFieldRegex(y.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(y.getExtField()) && y.getExtField() == 1) { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } else { + originField = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), y.getOriginName()); + } String fieldAlias = String.format(SQLConstants.FIELD_ALIAS_Y_PREFIX, i); // 处理纵轴字段 yFields.add(getYFields(y, originField, fieldAlias)); @@ -532,7 +585,17 @@ public class OracleQueryProvider extends QueryProvider { String whereName = ""; String whereTerm = transMysqlFilterTerm(request.getTerm()); String whereValue = ""; - String originName = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + + String originName; + if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originName = calcFieldRegex(field.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 1) { + originName = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } else { + originName = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } + if (field.getDeType() == 1 && field.getDeExtractType() != 1) { String cast = String.format(OracleConstants.CAST, originName, OracleConstants.DEFAULT_INT_FORMAT) + "/1000"; whereName = String.format(OracleConstants.FROM_UNIXTIME, cast, OracleConstants.DEFAULT_DATE_FORMAT); @@ -572,7 +635,16 @@ public class OracleQueryProvider extends QueryProvider { String whereName = ""; String whereTerm = transMysqlFilterTerm(request.getOperator()); String whereValue = ""; - String originName = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + + String originName; + if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 2) { + // 解析origin name中有关联的字段生成sql表达式 + originName = calcFieldRegex(field.getOriginName(), tableObj); + } else if (ObjectUtils.isNotEmpty(field.getExtField()) && field.getExtField() == 1) { + originName = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } else { + originName = String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), field.getOriginName()); + } if (field.getDeType() == 1 && field.getDeExtractType() != 1) { String cast = String.format(OracleConstants.CAST, originName, OracleConstants.DEFAULT_INT_FORMAT) + "/1000"; @@ -592,7 +664,7 @@ public class OracleQueryProvider extends QueryProvider { String endTime = simpleDateFormat.format(new Date(Long.parseLong(value.get(1)))); whereValue = String.format(OracleConstants.WHERE_BETWEEN, startTime, endTime); } else { - whereValue = String.format(MySQLConstants.WHERE_BETWEEN, value.get(0), value.get(1)); + whereValue = String.format(OracleConstants.WHERE_BETWEEN, value.get(0), value.get(1)); } } else { whereValue = String.format(OracleConstants.WHERE_VALUE_VALUE, value.get(0)); @@ -766,4 +838,28 @@ public class OracleQueryProvider extends QueryProvider { } return list; } + + private String calcFieldRegex(String originField, SQLObj tableObj) { + originField = originField.replaceAll("[\\t\\n\\r]]", ""); + // 正则提取[xxx] + String regex = "\\[(.*?)]"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(originField); + Set ids = new HashSet<>(); + while (matcher.find()) { + String id = matcher.group(1); + ids.add(id); + } + if (CollectionUtils.isEmpty(ids)) { + return originField; + } + DatasetTableFieldExample datasetTableFieldExample = new DatasetTableFieldExample(); + datasetTableFieldExample.createCriteria().andIdIn(new ArrayList<>(ids)); + List calcFields = datasetTableFieldMapper.selectByExample(datasetTableFieldExample); + for (DatasetTableField ele : calcFields) { + originField = originField.replaceAll("\\[" + ele.getId() + "]", + String.format(OracleConstants.KEYWORD_FIX, tableObj.getTableAlias(), ele.getOriginName())); + } + return originField; + } } diff --git a/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java b/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java index 2908ce5daf..620d1fc4d8 100644 --- a/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java +++ b/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java @@ -11,8 +11,8 @@ import io.dataease.base.mapper.ext.UtilMapper; import io.dataease.commons.constants.JobStatus; import io.dataease.commons.constants.ScheduleType; import io.dataease.commons.constants.TaskStatus; +import io.dataease.commons.exception.DEException; import io.dataease.commons.utils.*; -import io.dataease.controller.request.chart.ChartGroupRequest; import io.dataease.controller.request.dataset.DataSetGroupRequest; import io.dataease.controller.request.dataset.DataSetTableRequest; import io.dataease.controller.request.dataset.DataSetTaskRequest; @@ -383,12 +383,14 @@ public class DataSetTableService { data.addAll(datasourceProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { datasourceRequest.setQuery(qp.createQueryTableWithLimit(table, fields, Integer.valueOf(dataSetTableRequest.getRow()), false)); dataSetPreviewPage.setTotal(datasourceProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } else { // check doris table @@ -407,12 +409,14 @@ public class DataSetTableService { data.addAll(jdbcProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { datasourceRequest.setQuery(qp.createQueryTableWithLimit(table, fields, Integer.valueOf(dataSetTableRequest.getRow()), false)); dataSetPreviewPage.setTotal(jdbcProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } @@ -434,12 +438,14 @@ public class DataSetTableService { data.addAll(datasourceProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { datasourceRequest.setQuery(qp.createQuerySqlWithLimit(sql, fields, Integer.valueOf(dataSetTableRequest.getRow()), false)); dataSetPreviewPage.setTotal(datasourceProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } else { // check doris table @@ -458,12 +464,14 @@ public class DataSetTableService { data.addAll(jdbcProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { datasourceRequest.setQuery(qp.createQueryTableWithLimit(table, fields, Integer.valueOf(dataSetTableRequest.getRow()), false)); dataSetPreviewPage.setTotal(jdbcProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } } else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "excel")) { @@ -483,12 +491,14 @@ public class DataSetTableService { data.addAll(jdbcProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { datasourceRequest.setQuery(qp.createQueryTableWithLimit(table, fields, Integer.valueOf(dataSetTableRequest.getRow()), false)); dataSetPreviewPage.setTotal(jdbcProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "custom")) { if (datasetTable.getMode() == 0) { @@ -511,12 +521,14 @@ public class DataSetTableService { data.addAll(datasourceProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { datasourceRequest.setQuery(qp.createQuerySqlWithLimit(sql, fields, Integer.valueOf(dataSetTableRequest.getRow()), false)); dataSetPreviewPage.setTotal(datasourceProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } else { Datasource ds = (Datasource) CommonBeanFactory.getBean("DorisDatasource"); @@ -531,6 +543,7 @@ public class DataSetTableService { data.addAll(jdbcProvider.getData(datasourceRequest)); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } try { @@ -538,6 +551,7 @@ public class DataSetTableService { dataSetPreviewPage.setTotal(jdbcProvider.getData(datasourceRequest).size()); } catch (Exception e) { e.printStackTrace(); + DEException.throwException(e.getMessage()); } } } diff --git a/frontend/src/lang/en.js b/frontend/src/lang/en.js index 4483b3eb4c..6946113d10 100644 --- a/frontend/src/lang/en.js +++ b/frontend/src/lang/en.js @@ -990,7 +990,8 @@ export default { click_ref_function: 'Click Quote Function', field_manage: 'Field Manage', edit_calc_field: 'Edit calc field', - calc_field: 'Calc Field' + calc_field: 'Calc Field', + show_sql: 'Show SQL' }, datasource: { datasource: 'Data Source', diff --git a/frontend/src/lang/tw.js b/frontend/src/lang/tw.js index 962a8561d9..5e5fe23dba 100644 --- a/frontend/src/lang/tw.js +++ b/frontend/src/lang/tw.js @@ -990,7 +990,8 @@ export default { click_ref_function: '點擊引用函數', field_manage: '字段管理', edit_calc_field: '編輯計算字段', - calc_field: '計算字段' + calc_field: '計算字段', + show_sql: '顯示SQL' }, datasource: { datasource: '數據源', diff --git a/frontend/src/lang/zh.js b/frontend/src/lang/zh.js index 742fdcc74f..5ed4798003 100644 --- a/frontend/src/lang/zh.js +++ b/frontend/src/lang/zh.js @@ -990,7 +990,8 @@ export default { click_ref_function: '点击引用函数', field_manage: '字段管理', edit_calc_field: '编辑计算字段', - calc_field: '计算字段' + calc_field: '计算字段', + show_sql: '显示SQL' }, datasource: { datasource: '数据源', diff --git a/frontend/src/views/chart/group/Group.vue b/frontend/src/views/chart/group/Group.vue index 6fae053c64..3f0e5c76e7 100644 --- a/frontend/src/views/chart/group/Group.vue +++ b/frontend/src/views/chart/group/Group.vue @@ -315,7 +315,6 @@