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 1c42f5dd95..b0ebbea980 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 @@ -191,6 +191,141 @@ public class CalciteProvider { return map; } + private String getTableFiledSql(DatasourceRequest datasourceRequest) { + String sql = ""; + DatasourceConfiguration configuration = null; + DatasourceType datasourceType = DatasourceType.valueOf(datasourceRequest.getDatasource().getType()); + switch (datasourceType) { + case mysql: + case mongo: + case mariadb: + case TiDB: + case StarRocks: + case doris: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Mysql.class); + sql = String.format("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", configuration.getDataBase(), datasourceRequest.getTable()); + break; + case oracle: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Oracle.class); + if (StringUtils.isEmpty(configuration.getSchema())) { + DEException.throwException(Translator.get("i18n_schema_is_empty")); + } + sql = String.format("SELECT a.COLUMN_NAME , a.DATA_TYPE , b.COMMENTS FROM all_tab_columns a LEFT JOIN all_col_comments b ON a.owner = b.owner AND a.table_name = b.table_name AND a.column_name = b.column_name WHERE a.owner = '%s' AND a.table_name = '%s' ORDER BY a.table_name, a.column_id", configuration.getSchema(), datasourceRequest.getTable()); + break; + case db2: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Db2.class); + if (StringUtils.isEmpty(configuration.getSchema())) { + DEException.throwException(Translator.get("i18n_schema_is_empty")); + } + sql = String.format("SELECT COLNAME , TYPENAME , REMARKS FROM SYSCAT.COLUMNS WHERE TABSCHEMA = 'DB2INST1' AND TABNAME = 'MJQTEST' ", configuration.getSchema(), datasourceRequest.getTable()); + break; + case sqlServer: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Sqlserver.class); + if (StringUtils.isEmpty(configuration.getSchema())) { + DEException.throwException(Translator.get("i18n_schema_is_empty")); + } + sql = String.format("SELECT \n" + + " c.name ,t.name,ep.value \n" + + "FROM \n" + + " sys.columns AS c\n" + + "LEFT JOIN sys.extended_properties AS ep ON c.object_id = ep.major_id AND c.column_id = ep.minor_id\n" + + "LEFT JOIN sys.types AS t ON c.user_type_id = t.user_type_id\n" + + "WHERE c.object_id = OBJECT_ID('%s') ", datasourceRequest.getTable()); + break; + case pg: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Pg.class); + if (StringUtils.isEmpty(configuration.getSchema())) { + DEException.throwException(Translator.get("i18n_schema_is_empty")); + } + sql = String.format("SELECT\n" + + " a.attname AS ColumnName,\n" + + " t.typname,\n" + + " b.description AS ColumnDescription\n" + + "FROM\n" + + " pg_class c\n" + + " JOIN pg_attribute a ON a.attrelid = c.oid\n" + + " LEFT JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid\n" + + " JOIN pg_type t ON a.atttypid = t.oid\n" + + "WHERE\n" + + " c.relname = '%s'\n" + + " AND a.attnum > 0\n" + + " AND NOT a.attisdropped\n" + + "ORDER BY\n" + + " a.attnum\n" + + " ", datasourceRequest.getTable()); + break; + case redshift: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), CK.class); + sql = String.format("SELECT\n" + + " a.attname AS ColumnName,\n" + + " t.typname,\n" + + " b.description AS ColumnDescription\n" + + "FROM\n" + + " pg_class c\n" + + " JOIN pg_attribute a ON a.attrelid = c.oid\n" + + " LEFT JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid\n" + + " JOIN pg_type t ON a.atttypid = t.oid\n" + + "WHERE\n" + + " c.relname = '%s'\n" + + " AND a.attnum > 0\n" + + " AND NOT a.attisdropped\n" + + "ORDER BY\n" + + " a.attnum\n" + + " ", datasourceRequest.getTable()); + break; + case ck: + configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), CK.class); + sql = String.format(" SELECT\n" + + " name,\n" + + " type,\n" + + " comment\n" + + "FROM\n" + + " system.columns\n" + + "WHERE\n" + + " database = '%s' \n" + + " AND table = '%s' ", configuration.getDataBase(), datasourceRequest.getTable()); + break; + default: + break; + } + + return sql; + } + + private TableField getTableFieldDesc(DatasourceRequest datasourceRequest, ResultSet resultSet) throws SQLException { + TableField tableField = new TableField(); + tableField.setOriginName(resultSet.getString(1)); + tableField.setType(resultSet.getString(2)); + int deType = FieldUtils.transType2DeType(tableField.getType()); + tableField.setDeExtractType(deType); + tableField.setDeType(deType); + tableField.setName(resultSet.getString(3)); + return tableField; + } + + + public List fetchTableField(DatasourceRequest datasourceRequest) throws DEException { + List datasetTableFields = new ArrayList<>(); + try (Connection con = getConnection(datasourceRequest.getDatasource()); Statement statement = getStatement(con, 30); ResultSet resultSet = statement.executeQuery(getTableFiledSql(datasourceRequest))) { + while (resultSet.next()) { + datasetTableFields.add(getTableFieldDesc(datasourceRequest, resultSet)); + } + } catch (Exception e) { + DEException.throwException(e.getMessage()); + } + + List tableFields = (List) fetchResultField(datasourceRequest).get("fields"); + for (TableField tableField : tableFields) { + for (TableField datasetTableField : datasetTableFields) { + if(tableField.getOriginName().equalsIgnoreCase(datasetTableField.getOriginName())){ + tableField.setName(datasetTableField.getName()); + } + } + } + + return tableFields; + } + public Connection initConnection(Map dsMap) { Connection connection = getCalciteConnection(); diff --git a/core/core-backend/src/main/java/io/dataease/datasource/server/DatasourceServer.java b/core/core-backend/src/main/java/io/dataease/datasource/server/DatasourceServer.java index 4b55f05a98..914622e966 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/server/DatasourceServer.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/server/DatasourceServer.java @@ -650,10 +650,14 @@ public class DatasourceServer implements DatasourceApi { } private DatasourceDTO validate(CoreDatasource coreDatasource) { + String lastStatus = coreDatasource.getStatus(); DatasourceDTO datasourceDTO = new DatasourceDTO(); BeanUtils.copyBean(datasourceDTO, coreDatasource); try { checkDatasourceStatus(coreDatasource); + if(StringUtils.isNotEmpty(lastStatus) && StringUtils.isNotEmpty(coreDatasource.getStatus()) && lastStatus.equalsIgnoreCase("Error") && coreDatasource.getStatus().equalsIgnoreCase("Success")){ + calciteProvider.update(datasourceDTO); + } } catch (Exception e) { coreDatasource.setStatus("Error"); DEException.throwException(e.getMessage()); @@ -744,7 +748,8 @@ public class DatasourceServer implements DatasourceApi { datasourceSchemaDTO.setSchemaAlias(String.format(SQLConstants.SCHEMA, datasourceSchemaDTO.getId())); datasourceRequest.setDsList(Map.of(datasourceSchemaDTO.getId(), datasourceSchemaDTO)); datasourceRequest.setQuery(TableUtils.tableName2Sql(datasourceSchemaDTO, tableName) + " LIMIT 0 OFFSET 0"); - List tableFields = (List) calciteProvider.fetchResultField(datasourceRequest).get("fields"); + datasourceRequest.setTable(tableName); + List tableFields = (List) calciteProvider.fetchTableField(datasourceRequest) ; return tableFields.stream().filter(tableField -> { return !tableField.getOriginName().equalsIgnoreCase("dataease_uuid"); }).collect(Collectors.toList()); @@ -755,7 +760,8 @@ public class DatasourceServer implements DatasourceApi { datasourceSchemaDTO.setSchemaAlias(String.format(SQLConstants.SCHEMA, datasourceSchemaDTO.getId())); datasourceRequest.setDsList(Map.of(datasourceSchemaDTO.getId(), datasourceSchemaDTO)); datasourceRequest.setQuery(TableUtils.tableName2Sql(datasourceSchemaDTO, tableName) + " LIMIT 0 OFFSET 0"); - return (List) calciteProvider.fetchResultField(datasourceRequest).get("fields"); + datasourceRequest.setTable(tableName); + return (List) calciteProvider.fetchTableField(datasourceRequest); } @Override diff --git a/core/core-backend/src/main/java/io/dataease/datasource/server/EngineServer.java b/core/core-backend/src/main/java/io/dataease/datasource/server/EngineServer.java index a4930a69f0..a2ca1de3dd 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/server/EngineServer.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/server/EngineServer.java @@ -5,6 +5,7 @@ import io.dataease.api.ds.EngineApi; import io.dataease.datasource.dao.auto.entity.CoreDeEngine; import io.dataease.datasource.dao.auto.mapper.CoreDeEngineMapper; import io.dataease.datasource.manage.EngineManage; +import io.dataease.datasource.provider.CalciteProvider; import io.dataease.utils.BeanUtils; import io.dataease.utils.IDUtils; import jakarta.annotation.Resource; @@ -24,7 +25,8 @@ public class EngineServer implements EngineApi { private CoreDeEngineMapper deEngineMapper; @Resource private EngineManage engineManage; - + @Resource + private CalciteProvider calciteProvider; @Override public DatasourceDTO getEngine() { @@ -41,15 +43,16 @@ public class EngineServer implements EngineApi { if (StringUtils.isNotEmpty(datasourceDTO.getConfiguration())) { datasourceDTO.setConfiguration(new String(Base64.getDecoder().decode(datasourceDTO.getConfiguration()))); } - CoreDeEngine coreDeEngine = new CoreDeEngine(); BeanUtils.copyBean(coreDeEngine, datasourceDTO); if(coreDeEngine.getId() == null){ coreDeEngine.setId(IDUtils.snowID()); + datasourceDTO.setId(coreDeEngine.getId()); deEngineMapper.insert(coreDeEngine); }else { deEngineMapper.updateById(coreDeEngine); } + calciteProvider.update(datasourceDTO); } @Override diff --git a/core/core-frontend/src/views/visualized/data/datasource/index.vue b/core/core-frontend/src/views/visualized/data/datasource/index.vue index a7f1867a1d..7fe3607bca 100644 --- a/core/core-frontend/src/views/visualized/data/datasource/index.vue +++ b/core/core-frontend/src/views/visualized/data/datasource/index.vue @@ -1305,7 +1305,7 @@ const getMenuList = (val: boolean) => {