From a1b1b786295bf1fdc13f41c81d9ca185fb16ec10 Mon Sep 17 00:00:00 2001 From: taojinlong Date: Thu, 22 Aug 2024 16:53:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20excel=20=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/SqlparserUtils.java | 39 +++++-------------- .../datasource/provider/ExcelUtils.java | 28 ++++++++----- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/commons/utils/SqlparserUtils.java b/core/core-backend/src/main/java/io/dataease/commons/utils/SqlparserUtils.java index bb6f9c2c38..7ed21f52f7 100644 --- a/core/core-backend/src/main/java/io/dataease/commons/utils/SqlparserUtils.java +++ b/core/core-backend/src/main/java/io/dataease/commons/utils/SqlparserUtils.java @@ -59,6 +59,14 @@ public class SqlparserUtils { Statement statement = CCJSqlParserUtil.parse(tmpSql); Select select = (Select) statement; + if(CollectionUtils.isNotEmpty(select.getWithItemsList())){ + for (Iterator iter = select.getWithItemsList().iterator(); iter.hasNext(); ) { + WithItem withItem = iter.next(); + ParenthesedSelect parenthesedSelect = (ParenthesedSelect) withItem.getSelect(); + parenthesedSelect.setSelect((Select) CCJSqlParserUtil.parse(removeVariables(parenthesedSelect.getSelect().toString(), dsType))); + } + } + if (select.getSelectBody() instanceof PlainSelect) { return handlePlainSelect((PlainSelect) select.getSelectBody(), select, dsType); } else { @@ -70,7 +78,7 @@ public class SqlparserUtils { result.append(" ").append(setOperationList.getOperations().get(i).toString()).append(" "); } } - return result.toString(); + return select.toString(); } } @@ -412,35 +420,6 @@ public class SqlparserUtils { } - public static String removeVariables(final String sql) { - String tmpSql = sql; - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(sql); - boolean hasVariables = false; - while (matcher.find()) { - hasVariables = true; - tmpSql = tmpSql.replace(matcher.group(), SubstitutedParams); - } - if (!hasVariables && !tmpSql.contains(SubstitutedParams)) { - return tmpSql; - } - - SqlParser.Config config = - SqlParser.config() - .withLex(Lex.JAVA) - .withIdentifierMaxLength(256); - SqlParser sqlParser = SqlParser.create(tmpSql, config); - SqlNode sqlNode; - try { - sqlNode = sqlParser.parseStmt(); - } catch (SqlParseException e) { - throw new RuntimeException("使用 Calcite 进行语法分析发生了异常", e); - } - // 递归遍历语法树 - getDependencies(sqlNode, false); - return sqlNode.toString(); - } - private static void getDependencies(SqlNode sqlNode, Boolean fromOrJoin) { if (sqlNode == null) { return; diff --git a/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java b/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java index a87effa721..ca691ee612 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java @@ -37,8 +37,9 @@ public class ExcelUtils { private static String path = "/opt/dataease2.0/data/excel/"; private static ObjectMapper objectMapper = new ObjectMapper(); - private static TypeReference> TableFieldListTypeReference = new TypeReference>() { + private static TypeReference> TableFieldListTypeReference = new TypeReference>() { }; + public static List getTables(DatasourceRequest datasourceRequest) throws DEException { List tableDescs = new ArrayList<>(); try { @@ -48,7 +49,7 @@ public class ExcelUtils { datasetTableDTO.setTableName(rootNode.get(i).get("deTableName").asText()); datasetTableDTO.setName(rootNode.get(i).get("deTableName").asText()); datasetTableDTO.setDatasourceId(datasourceRequest.getDatasource().getId()); - datasetTableDTO.setLastUpdateTime(rootNode.get(i).get("lastUpdateTime") == null? datasourceRequest.getDatasource().getCreateTime(): rootNode.get(i).get("lastUpdateTime").asLong(0L)); + datasetTableDTO.setLastUpdateTime(rootNode.get(i).get("lastUpdateTime") == null ? datasourceRequest.getDatasource().getCreateTime() : rootNode.get(i).get("lastUpdateTime").asLong(0L)); tableDescs.add(datasetTableDTO); } } catch (Exception e) { @@ -58,12 +59,12 @@ public class ExcelUtils { return tableDescs; } - public static Map getTableNamesMap(String configration) throws DEException { - Map result = new HashMap<>(); + public static Map getTableNamesMap(String configration) throws DEException { + Map result = new HashMap<>(); try { JsonNode rootNode = objectMapper.readTree(configration); for (int i = 0; i < rootNode.size(); i++) { - result.put(rootNode.get(i).get("tableName").asText(),rootNode.get(i).get("deTableName").asText()); + result.put(rootNode.get(i).get("tableName").asText(), rootNode.get(i).get("deTableName").asText()); } } catch (Exception e) { DEException.throwException(e); @@ -293,7 +294,7 @@ public class ExcelUtils { cells.add(str); } if (!isEmpty(cells)) { - if(cells.size() > size){ + if (cells.size() > size) { cells = cells.subList(0, size); } data.add(cells.toArray(new String[]{})); @@ -307,7 +308,14 @@ public class ExcelUtils { } private String cellType(String value) { - if(value.length()> 19){ + if (StringUtils.isEmpty(value) || value.length() > 19) { + return "TEXT"; + } + if (value.length() > 1 && value.startsWith("0")) { + return "TEXT"; + } + String regex = "^\\d+(\\.\\d+)?$"; + if (!value.matches(regex)) { return "TEXT"; } try { @@ -331,9 +339,9 @@ public class ExcelUtils { tableFiled.setFieldType(cellType(value)); } else { String type = cellType(value); - if(tableFiled.getFieldType() == null){ + if (tableFiled.getFieldType() == null) { tableFiled.setFieldType(type); - }else { + } else { if (type.equalsIgnoreCase("TEXT")) { tableFiled.setFieldType(type); } @@ -454,7 +462,7 @@ public class ExcelUtils { String[] split = s.split(","); for (int i = 0; i < split.length; i++) { String filedName = split[i]; - if(StringUtils.isEmpty(filedName)){ + if (StringUtils.isEmpty(filedName)) { DEException.throwException("首行行中不允许有空单元格!"); } if (filedName.startsWith(UFEFF)) {