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 bea2950ef1..1ea83f57a8 100644 --- a/backend/src/main/java/io/dataease/provider/oracle/OracleQueryProvider.java +++ b/backend/src/main/java/io/dataease/provider/oracle/OracleQueryProvider.java @@ -83,16 +83,32 @@ public class OracleQueryProvider extends QueryProvider { @Override public String createQueryCountSQLAsTmp(String sql) { - return createQueryCountSQL(" (" + sqlFix(sql) + ") AS tmp "); + return createQueryCountSQL(" (" + sqlFix(sql) + ") DE_TMP "); } @Override public String createSQLPreview(String sql, String orderBy) { - return "SELECT * FROM (" + sqlFix(sql) + ") tmp " + " WHERE rownum <= 1000"; + return "SELECT * FROM (" + sqlFix(sql) + ") DE_TMP " + " WHERE rownum <= 1000"; } @Override public String createQuerySQL(String table, List fields) { + SQLObj tableObj = SQLObj.builder() + .tableName((table.startsWith("(") && table.endsWith(")")) ? table : String.format(OracleConstants.KEYWORD_TABLE, table)) + .tableAlias(String.format(OracleConstants.ALIAS_FIX, String.format(TABLE_ALIAS_PREFIX, 0))) + .build(); + List xFields = xFields(table, fields); + + STGroup stg = new STGroupFile(SQLConstants.SQL_TEMPLATE); + ST st_sql = stg.getInstanceOf("querySql"); + if (CollectionUtils.isNotEmpty(xFields)) st_sql.add("groups", xFields); + if (ObjectUtils.isNotEmpty(tableObj)) st_sql.add("table", tableObj); + return st_sql.render(); + } + + + + private List xFields(String table, List fields){ SQLObj tableObj = SQLObj.builder() .tableName((table.startsWith("(") && table.endsWith(")")) ? table : String.format(OracleConstants.KEYWORD_TABLE, table)) .tableAlias(String.format(OracleConstants.ALIAS_FIX, String.format(TABLE_ALIAS_PREFIX, 0))) @@ -137,17 +153,11 @@ public class OracleQueryProvider extends QueryProvider { .build()); } } - - STGroup stg = new STGroupFile(SQLConstants.SQL_TEMPLATE); - ST st_sql = stg.getInstanceOf("querySql"); - if (CollectionUtils.isNotEmpty(xFields)) st_sql.add("groups", xFields); - if (ObjectUtils.isNotEmpty(tableObj)) st_sql.add("table", tableObj); - return st_sql.render(); + return xFields; } - - private String sqlColumn(List fields) { - String[] array = fields.stream().map(f -> { - return f.getDataeaseName(); + private String sqlColumn(List xFields) { + String[] array = xFields.stream().map(f -> { + return f.getFieldAlias(); }).toArray(String[]::new); return StringUtils.join(array, ","); } @@ -159,24 +169,27 @@ public class OracleQueryProvider extends QueryProvider { @Override public String createQuerySQLWithPage(String table, List fields, Integer page, Integer pageSize, Integer realSize) { - return MessageFormat.format("SELECT {0} FROM ( SELECT tmp.*, rownum r FROM ( {1} ) tmp WHERE rownum <= {2} ) tmp2 WHERE r > {3} ", - sqlColumn(fields), createQuerySQL(table, fields), Integer.valueOf(page * realSize).toString(), Integer.valueOf((page - 1) * pageSize).toString()); + List xFields = xFields(table, fields); + + return MessageFormat.format("SELECT {0} FROM ( SELECT DE_TMP.*, rownum r FROM ( {1} ) DE_TMP WHERE rownum <= {2} ) WHERE r > {3} ", + sqlColumn(xFields), createQuerySQL(table, fields), Integer.valueOf(page * realSize).toString(), Integer.valueOf((page - 1) * pageSize).toString()); } @Override public String createQueryTableWithLimit(String table, List fields, Integer limit) { - return createQuerySQL(table, fields) + " WHERE rownum <= " + limit; + return String.format("SELECT %s.* from %s WHERE rownum <= %s ", table, table, limit.toString()); } @Override public String createQuerySqlWithLimit(String sql, List fields, Integer limit) { - return createQuerySQLAsTmp(sql, fields) + " WHERE rownum <= " + limit; + return String.format("SELECT * from %s WHERE rownum <= %s ", "(" + sqlFix(sql) + ")", limit.toString()); } @Override public String createQuerySQLAsTmpWithPage(String sql, List fields, Integer page, Integer pageSize, Integer realSize) { - return MessageFormat.format("SELECT {0} FROM ( SELECT tmp.*, rownum r FROM ( {1} ) tmp WHERE rownum <= {2} ) tmp2 WHERE r > {3} ", - sqlColumn(fields), createQuerySQLAsTmp(sql, fields), Integer.valueOf(page * realSize).toString(), Integer.valueOf((page - 1) * pageSize).toString()); + List xFields = xFields("(" + sqlFix(sql) + ")", fields); + return MessageFormat.format("SELECT {0} FROM ( SELECT DE_TMP.*, rownum r FROM ( {1} ) DE_TMP WHERE rownum <= {2} ) WHERE r > {3} ", + sqlColumn(xFields), createQuerySQLAsTmp(sql, fields), Integer.valueOf(page * realSize).toString(), Integer.valueOf((page - 1) * pageSize).toString()); } @Override @@ -353,7 +366,7 @@ public class OracleQueryProvider extends QueryProvider { if (sql.lastIndexOf(";") == (sql.length() - 1)) { sql = sql.substring(0, sql.length() - 1); } - String tmpSql = "SELECT * FROM (" + sql + ") tmp " + " where rownum <= 0"; + String tmpSql = "SELECT * FROM (" + sql + ") DE_TMP " + " where rownum <= 0"; return tmpSql; } @@ -361,7 +374,7 @@ public class OracleQueryProvider extends QueryProvider { public String createRawQuerySQL(String table, List fields) { String[] array = fields.stream().map(f -> { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(" ").append(f.getOriginName()).append(" AS ").append(f.getDataeaseName()); + stringBuilder.append(" ").append(f.getOriginName()); return stringBuilder.toString(); }).toArray(String[]::new); return MessageFormat.format("SELECT {0} FROM {1} ORDER BY null", StringUtils.join(array, ","), table); @@ -369,7 +382,7 @@ public class OracleQueryProvider extends QueryProvider { @Override public String createRawQuerySQLAsTmp(String sql, List fields) { - return createRawQuerySQL(" (" + sqlFix(sql) + ") tmp ", fields); + return createRawQuerySQL(" (" + sqlFix(sql) + ") DE_TMP ", fields); } public String transMysqlFilterTerm(String term) { diff --git a/backend/src/main/java/io/dataease/service/dataset/ExtractDataService.java b/backend/src/main/java/io/dataease/service/dataset/ExtractDataService.java index 99ce8a58ce..4e0a1cdc4e 100644 --- a/backend/src/main/java/io/dataease/service/dataset/ExtractDataService.java +++ b/backend/src/main/java/io/dataease/service/dataset/ExtractDataService.java @@ -720,7 +720,7 @@ public class ExtractDataService { transMeta.addDatabase(dataMeta); selectSQL = getSelectSQL(extractType, datasetTable, datasource, datasetTableFields, selectSQL); inputStep = inputStep(transMeta, selectSQL); - udjcStep = udjc(datasetTableFields, false); + udjcStep = udjc(datasetTableFields, DatasourceTypes.mysql); break; case sqlServer: SqlServerConfigration sqlServerConfigration = new Gson().fromJson(datasource.getConfiguration(), SqlServerConfigration.class); @@ -728,7 +728,7 @@ public class ExtractDataService { transMeta.addDatabase(dataMeta); selectSQL = getSelectSQL(extractType, datasetTable, datasource, datasetTableFields, selectSQL); inputStep = inputStep(transMeta, selectSQL); - udjcStep = udjc(datasetTableFields, false); + udjcStep = udjc(datasetTableFields, DatasourceTypes.sqlServer); break; case oracle: OracleConfigration oracleConfigration = new Gson().fromJson(datasource.getConfiguration(), OracleConfigration.class); @@ -742,12 +742,12 @@ public class ExtractDataService { selectSQL = getSelectSQL(extractType, datasetTable, datasource, datasetTableFields, selectSQL); inputStep = inputStep(transMeta, selectSQL); - udjcStep = udjc(datasetTableFields, false); + udjcStep = udjc(datasetTableFields, DatasourceTypes.oracle); break; case excel: String filePath = new Gson().fromJson(datasetTable.getInfo(), DataTableInfoDTO.class).getData(); inputStep = excelInputStep(filePath, datasetTableFields); - udjcStep = udjc(datasetTableFields, true); + udjcStep = udjc(datasetTableFields, DatasourceTypes.excel); default: break; } @@ -878,13 +878,9 @@ public class ExtractDataService { return outputStep; } - private StepMeta udjc(List datasetTableFields, boolean isExcel) { + private StepMeta udjc(List datasetTableFields, DatasourceTypes datasourceType) { String needToChangeColumnType = ""; -// for (DatasetTableField datasetTableField : datasetTableFields) { -// if (datasetTableField.getDeExtractType() != null && datasetTableField.getDeExtractType() == 4) { -// needToChangeColumnType = needToChangeColumnType + alterColumnTypeCode.replace("FILED", datasetTableField.getDataeaseName()); -// } -// } + UserDefinedJavaClassMeta userDefinedJavaClassMeta = new UserDefinedJavaClassMeta(); List fields = new ArrayList<>(); @@ -895,10 +891,16 @@ public class ExtractDataService { String tmp_code = code.replace("alterColumnTypeCode", needToChangeColumnType); tmp_code = tmp_code.replace("handleWraps", handleWraps); - if(isExcel){ - tmp_code = tmp_code.replace("handleExcelIntColumn", handleExcelIntColumn).replace("Column_Fields", String.join(",", datasetTableFields.stream().map(DatasetTableField::getOriginName).collect(Collectors.toList())));; + String Column_Fields = ""; + if(datasourceType.equals(DatasourceTypes.excel) || datasourceType.equals(DatasourceTypes.oracle)){ + Column_Fields = String.join(",", datasetTableFields.stream().map(DatasetTableField::getOriginName).collect(Collectors.toList())); }else { - tmp_code = tmp_code.replace("handleExcelIntColumn", "").replace("Column_Fields", String.join(",", datasetTableFields.stream().map(DatasetTableField::getDataeaseName).collect(Collectors.toList())));; + Column_Fields = String.join(",", datasetTableFields.stream().map(DatasetTableField::getDataeaseName).collect(Collectors.toList())); + } + if(datasourceType.equals(DatasourceTypes.excel)){ + tmp_code = tmp_code.replace("handleExcelIntColumn", handleExcelIntColumn).replace("Column_Fields", Column_Fields); + }else { + tmp_code = tmp_code.replace("handleExcelIntColumn", "").replace("Column_Fields", Column_Fields); } UserDefinedJavaClassDef userDefinedJavaClassDef = new UserDefinedJavaClassDef(UserDefinedJavaClassDef.ClassType.TRANSFORM_CLASS, "Processor", tmp_code); diff --git a/backend/src/main/resources/sql/sqlTemplate.stg b/backend/src/main/resources/sql/sqlTemplate.stg index 71eb2c3b70..5b2bd4c82e 100644 --- a/backend/src/main/resources/sql/sqlTemplate.stg +++ b/backend/src/main/resources/sql/sqlTemplate.stg @@ -12,7 +12,7 @@ SELECT AS }; separator=",\n"> FROM - AS + WHERE }; separator="\nAND ">