From f78eb9f2f6c3d21bf3eb56b8eed02bdbcfa467b2 Mon Sep 17 00:00:00 2001 From: junjun Date: Wed, 28 Feb 2024 12:26:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96calcite=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=87=BD=E6=95=B0=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/provider/CalciteProvider.java | 15 --------------- .../io/dataease/engine/trans/CustomWhere2Str.java | 10 ++++++---- .../io/dataease/engine/trans/ExtWhere2Str.java | 4 ++-- .../io/dataease/engine/trans/WhereTree2Str.java | 12 ++++++------ pom.xml | 2 +- 5 files changed, 15 insertions(+), 28 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java b/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java index 7116ba8d12..786ee685e1 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java @@ -25,11 +25,9 @@ import io.dataease.utils.LogUtil; import jakarta.annotation.PostConstruct; import jakarta.annotation.Resource; import org.apache.calcite.adapter.jdbc.JdbcSchema; -import org.apache.calcite.func.scalar.ScalarFunctions; import org.apache.calcite.jdbc.CalciteConnection; import org.apache.calcite.schema.Schema; import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.schema.impl.ScalarFunctionImpl; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; @@ -37,7 +35,6 @@ import org.springframework.util.CollectionUtils; import java.io.File; import java.io.IOException; -import java.lang.reflect.Method; import java.net.URL; import java.sql.*; import java.util.*; @@ -206,20 +203,9 @@ public class CalciteProvider { DatasourceRequest datasourceRequest = new DatasourceRequest(); datasourceRequest.setDsList(dsMap); SchemaPlus rootSchema = buildSchema(datasourceRequest, calciteConnection); - addCustomFunctions(rootSchema); return connection; } - private void addCustomFunctions(SchemaPlus rootSchema) { - // scalar functions - Class clazz = ScalarFunctions.class; - Method[] methods = clazz.getMethods(); - for (Method method : methods) { - rootSchema.add(method.getName().toUpperCase(), ScalarFunctionImpl.create( - ScalarFunctions.class, method.getName())); - } - } - private void registerDriver() { for (String driverClass : getDriver()) { try { @@ -691,7 +677,6 @@ public class CalciteProvider { try { CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); SchemaPlus rootSchema = buildSchema(datasourceRequest, calciteConnection); - addCustomFunctions(rootSchema); } catch (Exception e) { DEException.throwException(e.getMessage()); } diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java index 642be9c3d6..c0f06dd061 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java @@ -6,11 +6,12 @@ import io.dataease.api.dataset.union.model.SQLMeta; import io.dataease.api.dataset.union.model.SQLObj; import io.dataease.dto.dataset.DatasetTableFieldDTO; import io.dataease.engine.constant.SQLConstants; +import io.dataease.engine.utils.DateUtils; import io.dataease.engine.utils.Utils; -import org.apache.calcite.func.scalar.ScalarFunctions; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; @@ -55,7 +56,7 @@ public class CustomWhere2Str { } if (field.getDeExtractType() == 1) { // 此处获取标准格式的日期 - String f = ScalarFunctions.get_date_format(originName); + String f = DateUtils.get_date_format(originName); whereName = String.format(SQLConstants.DATE_FORMAT, originName, f); } } else if (field.getDeType() == 2 || field.getDeType() == 3) { @@ -90,11 +91,11 @@ public class CustomWhere2Str { if (field.getDeType() == 1) { // 规定几种日期格式,一一匹配,匹配到就是该格式 if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) { - String f = ScalarFunctions.get_date_format(filterItemDTO.getValue()); + String f = DateUtils.get_date_format(filterItemDTO.getValue()); String n = String.format(SQLConstants.DE_CAST_DATE_FORMAT, whereName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT, f); whereNameReal = String.format(SQLConstants.UNIX_TIMESTAMP, n); } else { - String f = ScalarFunctions.get_date_format(filterItemDTO.getValue()); + String f = DateUtils.get_date_format(filterItemDTO.getValue()); String n = String.format(SQLConstants.DE_DATE_FORMAT, whereName, f); whereNameReal = String.format(SQLConstants.UNIX_TIMESTAMP, n); } @@ -136,4 +137,5 @@ public class CustomWhere2Str { } } + } diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java index 3c5865dc87..14cc25e598 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java @@ -5,8 +5,8 @@ import io.dataease.api.dataset.union.model.SQLMeta; import io.dataease.api.dataset.union.model.SQLObj; import io.dataease.dto.dataset.DatasetTableFieldDTO; import io.dataease.engine.constant.SQLConstants; +import io.dataease.engine.utils.DateUtils; import io.dataease.engine.utils.Utils; -import org.apache.calcite.func.scalar.ScalarFunctions; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -57,7 +57,7 @@ public class ExtWhere2Str { if (StringUtils.containsIgnoreCase(request.getOperator(), "between")) { date_format = "yyyy-MM-dd HH:mm:ss"; } else { - date_format = ScalarFunctions.get_date_format(value.get(0)); + date_format = DateUtils.get_date_format(value.get(0)); } if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) { // 此处获取标准格式的日期 diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java index 5087a44a7b..7480db3c5e 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java @@ -8,8 +8,8 @@ import io.dataease.api.permissions.dataset.dto.DatasetRowPermissionsTreeObj; import io.dataease.dto.dataset.DatasetTableFieldDTO; import io.dataease.engine.constant.ExtFieldConstant; import io.dataease.engine.constant.SQLConstants; +import io.dataease.engine.utils.DateUtils; import io.dataease.engine.utils.Utils; -import org.apache.calcite.func.scalar.ScalarFunctions; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -98,7 +98,7 @@ public class WhereTree2Str { whereName = String.format(SQLConstants.FROM_UNIXTIME, cast, SQLConstants.DEFAULT_DATE_FORMAT); } if (field.getDeExtractType() == 1) { - String f = ScalarFunctions.get_date_format(originName); + String f = DateUtils.get_date_format(originName); whereName = String.format(SQLConstants.DATE_FORMAT, originName, f); } } else if (field.getDeType() == 2 || field.getDeType() == 3) { @@ -130,12 +130,12 @@ public class WhereTree2Str { if (field.getDeType() == 1) { // 规定几种日期格式,一一匹配,匹配到就是该格式 if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) { - String f = ScalarFunctions.get_date_format(item.getValue()); - whereName = String.format(SQLConstants.DE_CAST_DATE_FORMAT, whereName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT, f); + String f = DateUtils.get_date_format(item.getValue()); + whereName = String.format(SQLConstants.DE_STR_TO_DATE, whereName, f); whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName); } else { - String f = ScalarFunctions.get_date_format(item.getValue()); - whereName = String.format(SQLConstants.DE_DATE_FORMAT, whereName, f); + String f = DateUtils.get_date_format(item.getValue()); + whereName = String.format(SQLConstants.DE_STR_TO_DATE, whereName, f); whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName); } } diff --git a/pom.xml b/pom.xml index 00ec74aab9..a8cbe273fe 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ 3.5.3.1 2.2.220 4.4.0 - 1.35.4 + 1.35.5 2.6.0 3.5.2 3.12.1