fix: 修复字段描述信息读取不出来

This commit is contained in:
taojinlong 2024-03-20 15:18:02 +08:00
parent 412b2776a7
commit ab875b20c0
3 changed files with 140 additions and 3 deletions

View File

@ -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<TableField> fetchTableField(DatasourceRequest datasourceRequest) throws DEException {
List<TableField> 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<TableField> tableFields = (List<TableField>) 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<Long, DatasourceSchemaDTO> dsMap) {
Connection connection = getCalciteConnection();

View File

@ -748,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<TableField> tableFields = (List<TableField>) calciteProvider.fetchResultField(datasourceRequest).get("fields");
datasourceRequest.setTable(tableName);
List<TableField> tableFields = (List<TableField>) calciteProvider.fetchTableField(datasourceRequest) ;
return tableFields.stream().filter(tableField -> {
return !tableField.getOriginName().equalsIgnoreCase("dataease_uuid");
}).collect(Collectors.toList());
@ -759,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<TableField>) calciteProvider.fetchResultField(datasourceRequest).get("fields");
datasourceRequest.setTable(tableName);
return (List<TableField>) calciteProvider.fetchTableField(datasourceRequest);
}
@Override

View File

@ -1299,7 +1299,7 @@ const getMenuList = (val: boolean) => {
</template>
</el-table-column>
<el-table-column
prop="remarks"
prop="name"
show-overflow-tooltip
:label="t('datasource.field_description')"
/>