From c8f9ba00ddc05e5a2d97ad4db4755d1174e82cb6 Mon Sep 17 00:00:00 2001 From: junjie Date: Mon, 15 Mar 2021 15:24:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=95=B0=E6=8D=AE=E9=9B=86):=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E6=B7=BB=E5=8A=A0SQL=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=EF=BC=8C=E6=95=B0=E6=8D=AE=E9=A2=84=E8=A7=88=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E7=AD=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../provider/DatasourceProvider.java | 3 +- .../datasource/provider/JdbcProvider.java | 14 ++++-- .../service/dataset/DataSetTableService.java | 34 ++++++++++--- frontend/src/lang/zh.js | 3 +- frontend/src/views/dataset/add/AddSQL.vue | 50 ++++++++++++++++--- 5 files changed, 85 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java b/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java index 1339ac63ac..71b513471f 100644 --- a/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java +++ b/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java @@ -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 fetchResult(ResultSet rs) throws Exception; - abstract public List fetchResultField(ResultSet rs) throws Exception; + abstract public List fetchResultField(ResultSet rs) throws Exception; } diff --git a/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java b/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java index d015063659..f0292b6664 100644 --- a/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java +++ b/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java @@ -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 fetchResultField(ResultSet rs) throws Exception { - List fieldList = new ArrayList<>(); + public List fetchResultField(ResultSet rs) throws Exception { + List 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; } diff --git a/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java b/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java index b74124be9f..fae8ba496e 100644 --- a/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java +++ b/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java @@ -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 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 data = new ArrayList<>(); try { @@ -170,7 +177,6 @@ public class DataSetTableService { }).collect(Collectors.toList()); } - Map 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 data = datasourceProvider.fetchResult(dataResultSet); - List fields = datasourceProvider.fetchResultField(dataResultSet); + List fields = datasourceProvider.fetchResultField(dataResultSet); + String[] fieldArray = fields.stream().map(TableFiled::getFieldName).toArray(String[]::new); List> jsonArray = new ArrayList<>(); if (CollectionUtils.isNotEmpty(data)) { jsonArray = data.stream().map(ele -> { Map 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 fields = getFields(dataSetTableRequest); + + List 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); diff --git a/frontend/src/lang/zh.js b/frontend/src/lang/zh.js index 539eef1de9..ee5fc92263 100644 --- a/frontend/src/lang/zh.js +++ b/frontend/src/lang/zh.js @@ -667,7 +667,8 @@ export default { required: '必填', input_content: '请输入内容', add_sql_table: '添加SQL', - preview: '预览' + preview: '预览', + pls_input_name: '请输入名称' }, datasource: { create: '新建数据连接', diff --git a/frontend/src/views/dataset/add/AddSQL.vue b/frontend/src/views/dataset/add/AddSQL.vue index d55f8296e2..17d9b5e056 100644 --- a/frontend/src/views/dataset/add/AddSQL.vue +++ b/frontend/src/views/dataset/add/AddSQL.vue @@ -9,7 +9,7 @@ {{ $t('dataset.cancel') }} - + {{ $t('dataset.confirm') }} @@ -65,10 +65,10 @@ > @@ -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: '' })