feat(数据集): 数据集添加SQL保存,数据预览展示等功能

This commit is contained in:
junjie 2021-03-15 15:24:10 +08:00
parent ac8f8271ae
commit c8f9ba00dd
5 changed files with 85 additions and 19 deletions

View File

@ -1,5 +1,6 @@
package io.dataease.datasource.provider;
import io.dataease.base.domain.DatasetTableField;
import io.dataease.base.domain.Datasource;
import io.dataease.datasource.dto.TableFiled;
import io.dataease.datasource.request.DatasourceRequest;
@ -32,6 +33,6 @@ public abstract class DatasourceProvider {
abstract public List<String[]> fetchResult(ResultSet rs) throws Exception;
abstract public List<String> fetchResultField(ResultSet rs) throws Exception;
abstract public List<TableFiled> fetchResultField(ResultSet rs) throws Exception;
}

View File

@ -1,6 +1,7 @@
package io.dataease.datasource.provider;
import com.google.gson.Gson;
import io.dataease.base.domain.DatasetTableField;
import io.dataease.datasource.constants.DatasourceTypes;
import io.dataease.datasource.dto.MysqlConfigrationDTO;
import io.dataease.datasource.dto.SqlServerConfigration;
@ -90,12 +91,19 @@ public class JdbcProvider extends DatasourceProvider {
}
@Override
public List<String> fetchResultField(ResultSet rs) throws Exception {
List<String> fieldList = new ArrayList<>();
public List<TableFiled> fetchResultField(ResultSet rs) throws Exception {
List<TableFiled> fieldList = new ArrayList<>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int j = 0; j < columnCount; j++) {
fieldList.add(metaData.getColumnName(j + 1));
String f = metaData.getColumnName(j + 1);
String l = StringUtils.isNotEmpty(metaData.getColumnLabel(j + 1)) ? metaData.getColumnLabel(j + 1) : f;
String t = metaData.getColumnTypeName(j + 1);
TableFiled field = new TableFiled();
field.setFieldName(f);
field.setRemarks(l);
field.setFieldType(t);
fieldList.add(field);
}
return fieldList;
}

View File

@ -54,8 +54,8 @@ public class DataSetTableService {
DataTableInfoDTO dataTableInfoDTO = new DataTableInfoDTO();
if (StringUtils.equalsIgnoreCase("db", datasetTable.getType())) {
dataTableInfoDTO.setTable(datasetTable.getName());
datasetTable.setInfo(new Gson().toJson(dataTableInfoDTO));
}
datasetTable.setInfo(new Gson().toJson(dataTableInfoDTO));
int insert = datasetTableMapper.insert(datasetTable);
// 添加表成功后获取当前表字段和类型抽象到dataease数据库
if (insert == 1) {
@ -143,15 +143,22 @@ public class DataSetTableService {
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
String table = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getTable();
DatasetTableField datasetTableField = DatasetTableField.builder().build();
datasetTableField.setTableId(dataSetTableRequest.getId());
datasetTableField.setChecked(Boolean.TRUE);
List<DatasetTableField> fields = dataSetTableFieldsService.list(datasetTableField);
String[] fieldArray = fields.stream().map(DatasetTableField::getOriginName).toArray(String[]::new);
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray) + " LIMIT 0,10");// todo limit
DataTableInfoDTO dataTableInfoDTO = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class);
DatasetTable datasetTable = datasetTableMapper.selectByPrimaryKey(dataSetTableRequest.getId());
if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "db")) {
String table = dataTableInfoDTO.getTable();
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray) + " LIMIT 0,10");// todo limit
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "sql")) {
String sql = dataTableInfoDTO.getSql();
datasourceRequest.setQuery(sql);// todo 因为编辑可能取消某些字段展示这里sql看看怎么处理
}
List<String[]> data = new ArrayList<>();
try {
@ -170,7 +177,6 @@ public class DataSetTableService {
}).collect(Collectors.toList());
}
Map<String, Object> map = new HashMap<>();
map.put("fields", fields);
map.put("data", jsonArray);
@ -187,14 +193,15 @@ public class DataSetTableService {
datasourceRequest.setQuery(sql);
ResultSet dataResultSet = datasourceProvider.getDataResultSet(datasourceRequest);
List<String[]> data = datasourceProvider.fetchResult(dataResultSet);
List<String> fields = datasourceProvider.fetchResultField(dataResultSet);
List<TableFiled> fields = datasourceProvider.fetchResultField(dataResultSet);
String[] fieldArray = fields.stream().map(TableFiled::getFieldName).toArray(String[]::new);
List<Map<String, Object>> jsonArray = new ArrayList<>();
if (CollectionUtils.isNotEmpty(data)) {
jsonArray = data.stream().map(ele -> {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < ele.length; i++) {
map.put(fields.get(i), ele[i]);
map.put(fieldArray[i], ele[i]);
}
return map;
}).collect(Collectors.toList());
@ -258,8 +265,19 @@ public class DataSetTableService {
Datasource ds = datasourceMapper.selectByPrimaryKey(datasetTable.getDataSourceId());
DataSetTableRequest dataSetTableRequest = new DataSetTableRequest();
BeanUtils.copyBean(dataSetTableRequest, datasetTable);
List<TableFiled> fields = getFields(dataSetTableRequest);
List<TableFiled> fields = new ArrayList<>();
long syncTime = System.currentTimeMillis();
if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "db")) {
fields = getFields(dataSetTableRequest);
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "sql")) {
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
datasourceRequest.setQuery(new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getSql());
ResultSet dataResultSet = datasourceProvider.getDataResultSet(datasourceRequest);
fields = datasourceProvider.fetchResultField(dataResultSet);
}
if (CollectionUtils.isNotEmpty(fields)) {
for (int i = 0; i < fields.size(); i++) {
TableFiled filed = fields.get(i);

View File

@ -667,7 +667,8 @@ export default {
required: '必填',
input_content: '请输入内容',
add_sql_table: '添加SQL',
preview: '预览'
preview: '预览',
pls_input_name: '请输入名称'
},
datasource: {
create: '新建数据连接',

View File

@ -9,7 +9,7 @@
<el-button size="mini" @click="cancel">
{{ $t('dataset.cancel') }}
</el-button>
<el-button size="mini" type="primary">
<el-button size="mini" type="primary" @click="save">
{{ $t('dataset.confirm') }}
</el-button>
</el-row>
@ -65,10 +65,10 @@
>
<ux-table-column
v-for="field in fields"
:key="field"
:key="field.fieldName"
min-width="200px"
:field="field"
:title="field"
:field="field.fieldName"
:title="field.remarks"
:resizable="true"
/>
</ux-grid>
@ -100,6 +100,10 @@ import 'codemirror/addon/dialog/dialog.css'
import 'codemirror/addon/search/searchcursor.js'
import 'codemirror/addon/search/search.js'
import 'codemirror/keymap/emacs.js'
//
import 'codemirror/addon/hint/show-hint.css'
import 'codemirror/addon/hint/sql-hint'
import 'codemirror/addon/hint/show-hint'
export default {
name: 'AddSQL',
@ -123,8 +127,8 @@ export default {
line: true,
mode: 'text/x-sql',
theme: 'solarized',
hintOptions: {
completeSingle: true
hintOptions: { //
completeSingle: false //
}
},
data: [],
@ -140,6 +144,9 @@ export default {
watch: {},
mounted() {
this.initDataSource()
this.$refs.myCm.codemirror.on('keypress', () => {
this.$refs.myCm.codemirror.showHint()
})
},
methods: {
initDataSource() {
@ -169,6 +176,37 @@ export default {
})
},
save() {
if (!this.dataSource || this.datasource === '') {
this.$message({
showClose: true,
message: this.$t('dataset.pls_slc_data_source'),
type: 'error'
})
return
}
if (!this.name || this.name === '') {
this.$message({
showClose: true,
message: this.$t('dataset.pls_input_name'),
type: 'error'
})
return
}
const table = {
name: this.name,
sceneId: this.param.id,
dataSourceId: this.dataSource,
type: 'sql',
mode: parseInt(this.mode),
info: '{"sql":"' + this.sql + '"}'
}
post('/dataset/table/update', table).then(response => {
this.$store.dispatch('dataset/setSceneData', new Date().getTime())
this.cancel()
})
},
cancel() {
// this.dataReset()
this.$emit('switchComponent', { name: '' })