feat(数据集): 数据库表:mysql直连模式,表字段 类型 数据 读取展示

This commit is contained in:
junjie
2021-02-23 23:34:52 +08:00
parent 923d7509a2
commit 9272a0b954
9 changed files with 188 additions and 22 deletions

View File

@@ -19,5 +19,7 @@ public class DatasetTable implements Serializable {
private Long createTime;
private String info;
private static final long serialVersionUID = 1L;
}

View File

@@ -16,15 +16,21 @@ public interface DatasetTableMapper {
int insertSelective(DatasetTable record);
List<DatasetTable> selectByExampleWithBLOBs(DatasetTableExample example);
List<DatasetTable> selectByExample(DatasetTableExample example);
DatasetTable selectByPrimaryKey(String id);
int updateByExampleSelective(@Param("record") DatasetTable record, @Param("example") DatasetTableExample example);
int updateByExampleWithBLOBs(@Param("record") DatasetTable record, @Param("example") DatasetTableExample example);
int updateByExample(@Param("record") DatasetTable record, @Param("example") DatasetTableExample example);
int updateByPrimaryKeySelective(DatasetTable record);
int updateByPrimaryKeyWithBLOBs(DatasetTable record);
int updateByPrimaryKey(DatasetTable record);
}

View File

@@ -10,6 +10,9 @@
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
<result column="create_time" jdbcType="BIGINT" property="createTime" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.dataease.base.domain.DatasetTable">
<result column="info" jdbcType="LONGVARCHAR" property="info" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
@@ -71,6 +74,25 @@
<sql id="Base_Column_List">
id, `name`, scene_id, data_source_id, `type`, create_by, create_time
</sql>
<sql id="Blob_Column_List">
info
</sql>
<select id="selectByExampleWithBLOBs" parameterType="io.dataease.base.domain.DatasetTableExample" resultMap="ResultMapWithBLOBs">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from dataset_table
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByExample" parameterType="io.dataease.base.domain.DatasetTableExample" resultMap="BaseResultMap">
select
<if test="distinct">
@@ -85,9 +107,11 @@
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="ResultMapWithBLOBs">
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from dataset_table
where id = #{id,jdbcType=VARCHAR}
</select>
@@ -104,10 +128,10 @@
<insert id="insert" parameterType="io.dataease.base.domain.DatasetTable">
insert into dataset_table (id, `name`, scene_id,
data_source_id, `type`, create_by,
create_time)
create_time, info)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{sceneId,jdbcType=VARCHAR},
#{dataSourceId,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR},
#{createTime,jdbcType=BIGINT})
#{createTime,jdbcType=BIGINT}, #{info,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="io.dataease.base.domain.DatasetTable">
insert into dataset_table
@@ -133,6 +157,9 @@
<if test="createTime != null">
create_time,
</if>
<if test="info != null">
info,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
@@ -156,6 +183,9 @@
<if test="createTime != null">
#{createTime,jdbcType=BIGINT},
</if>
<if test="info != null">
#{info,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="io.dataease.base.domain.DatasetTableExample" resultType="java.lang.Long">
@@ -188,11 +218,28 @@
<if test="record.createTime != null">
create_time = #{record.createTime,jdbcType=BIGINT},
</if>
<if test="record.info != null">
info = #{record.info,jdbcType=LONGVARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExampleWithBLOBs" parameterType="map">
update dataset_table
set id = #{record.id,jdbcType=VARCHAR},
`name` = #{record.name,jdbcType=VARCHAR},
scene_id = #{record.sceneId,jdbcType=VARCHAR},
data_source_id = #{record.dataSourceId,jdbcType=VARCHAR},
`type` = #{record.type,jdbcType=VARCHAR},
create_by = #{record.createBy,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=BIGINT},
info = #{record.info,jdbcType=LONGVARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update dataset_table
set id = #{record.id,jdbcType=VARCHAR},
@@ -227,9 +274,23 @@
<if test="createTime != null">
create_time = #{createTime,jdbcType=BIGINT},
</if>
<if test="info != null">
info = #{info,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="io.dataease.base.domain.DatasetTable">
update dataset_table
set `name` = #{name,jdbcType=VARCHAR},
scene_id = #{sceneId,jdbcType=VARCHAR},
data_source_id = #{dataSourceId,jdbcType=VARCHAR},
`type` = #{type,jdbcType=VARCHAR},
create_by = #{createBy,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=BIGINT},
info = #{info,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="io.dataease.base.domain.DatasetTable">
update dataset_table
set `name` = #{name,jdbcType=VARCHAR},

View File

@@ -2,6 +2,7 @@ package io.dataease.controller.dataset;
import io.dataease.base.domain.DatasetTable;
import io.dataease.controller.request.dataset.DataSetTableRequest;
import io.dataease.datasource.dto.TableFiled;
import io.dataease.service.dataset.DataSetTableService;
import org.springframework.web.bind.annotation.*;
@@ -42,4 +43,14 @@ public class DataSetTableController {
public DatasetTable get(@PathVariable String id) {
return dataSetTableService.get(id);
}
@PostMapping("getFields")
public List<TableFiled> getFields(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
return dataSetTableService.getFields(dataSetTableRequest);
}
@PostMapping("getData")
public List<Object> getData(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
return dataSetTableService.getData(dataSetTableRequest);
}
}

View File

@@ -0,0 +1,15 @@
package io.dataease.dto.dataset;
import lombok.Getter;
import lombok.Setter;
/**
* @Author gin
* @Date 2021/2/23 8:47 下午
*/
@Setter
@Getter
public class DataTableInfoDTO {
private String table;
private String sql;
}

View File

@@ -1,11 +1,20 @@
package io.dataease.service.dataset;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.util.UuidUtils;
import com.google.gson.Gson;
import io.dataease.base.domain.DatasetTable;
import io.dataease.base.domain.DatasetTableExample;
import io.dataease.base.domain.Datasource;
import io.dataease.base.mapper.DatasetTableMapper;
import io.dataease.commons.utils.BeanUtils;
import io.dataease.base.mapper.DatasourceMapper;
import io.dataease.controller.request.dataset.DataSetTableRequest;
import io.dataease.datasource.dto.TableFiled;
import io.dataease.datasource.provider.DatasourceProvider;
import io.dataease.datasource.provider.ProviderFactory;
import io.dataease.datasource.request.DatasourceRequest;
import io.dataease.dto.dataset.DataTableInfoDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@@ -20,6 +29,8 @@ import java.util.List;
public class DataSetTableService {
@Resource
private DatasetTableMapper datasetTableMapper;
@Resource
private DatasourceMapper datasourceMapper;
public void batchInsert(List<DatasetTable> datasetTable) {
for (DatasetTable table : datasetTable) {
@@ -31,9 +42,14 @@ public class DataSetTableService {
if (StringUtils.isEmpty(datasetTable.getId())) {
datasetTable.setId(UuidUtils.generateUuid());
datasetTable.setCreateTime(System.currentTimeMillis());
DataTableInfoDTO dataTableInfoDTO = new DataTableInfoDTO();
if (StringUtils.equalsIgnoreCase("db", datasetTable.getType())) {
dataTableInfoDTO.setTable(datasetTable.getName());
}
datasetTable.setInfo(new Gson().toJson(dataTableInfoDTO));
datasetTableMapper.insert(datasetTable);
} else {
datasetTableMapper.updateByPrimaryKey(datasetTable);
datasetTableMapper.updateByPrimaryKeyWithBLOBs(datasetTable);
}
return datasetTable;
}
@@ -48,10 +64,42 @@ public class DataSetTableService {
if (StringUtils.isNotEmpty(dataSetTableRequest.getSort())) {
datasetTableExample.setOrderByClause(dataSetTableRequest.getSort());
}
return datasetTableMapper.selectByExample(datasetTableExample);
return datasetTableMapper.selectByExampleWithBLOBs(datasetTableExample);
}
public DatasetTable get(String id) {
return datasetTableMapper.selectByPrimaryKey(id);
}
public List<TableFiled> getFields(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
datasourceRequest.setTable(new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getTable());
return datasourceProvider.getTableFileds(datasourceRequest);
}
public List<Object> getData(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
String table = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getTable();
datasourceRequest.setQuery("SELECT * FROM " + table + " LIMIT 0,100;");
List<TableFiled> fields = getFields(dataSetTableRequest);
List<String[]> data = datasourceProvider.getData(datasourceRequest);
JSONArray jsonArray = new JSONArray();
data.forEach(ele -> {
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < ele.length; i++) {
jsonObject.put(fields.get(i).getFieldName(), ele[i]);
}
jsonArray.add(jsonObject);
});
return jsonArray;
}
}