完成表信息储存服务

This commit is contained in:
makejava 2021-08-16 10:21:23 +08:00
parent 566e4830d8
commit 5859fc256f
7 changed files with 206 additions and 26 deletions

View File

@ -1,8 +1,15 @@
package com.sjhy.plugin.dto;
import com.intellij.database.model.DasColumn;
import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.tool.CurrGroupUtils;
import com.sjhy.plugin.tool.NameUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 列信息传输对象
@ -12,7 +19,35 @@ import java.util.Map;
* @date 2021/08/14 17:29
*/
@Data
@NoArgsConstructor
public class ColumnInfoDTO {
public ColumnInfoDTO(DasColumn column) {
this.name = NameUtils.getInstance().getJavaName(column.getName());
this.comment = column.getComment();
this.type = getJavaType(column.getDataType().toString());
this.custom = false;
this.ext = new HashMap<>();
}
private String getJavaType(String dbType) {
for (TypeMapper typeMapper : CurrGroupUtils.getCurrTypeMapperGroup().getElementList()) {
switch (typeMapper.getMatchType()) {
case REGEX:
if (Pattern.compile(typeMapper.getColumnType()).matcher(dbType).matches()) {
return typeMapper.getJavaType();
}
break;
case ORDINARY:
default:
if (dbType.equalsIgnoreCase(typeMapper.getColumnType())) {
return typeMapper.getJavaType();
}
break;
}
}
return "java.lang.Object";
}
/**
* 名称
*/

View File

@ -1,8 +1,22 @@
package com.sjhy.plugin.dto;
import com.intellij.database.model.DasColumn;
import com.intellij.database.psi.DbTable;
import com.intellij.database.util.DasUtil;
import com.intellij.util.containers.JBIterable;
import com.sjhy.plugin.entity.ColumnInfo;
import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.NameUtils;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 表格信息传输对象
@ -12,7 +26,76 @@ import java.util.List;
* @date 2021/08/14 17:28
*/
@Data
@NoArgsConstructor
public class TableInfoDTO {
public TableInfoDTO(TableInfoDTO dto, DbTable dbTable) {
this(dbTable);
merge(dto, this);
}
private TableInfoDTO(DbTable dbTable) {
this.name = NameUtils.getInstance().getClassName(dbTable.getName());
this.preName = "";
this.comment = dbTable.getComment();
this.templateGroupName = "";
this.savePackageName = "";
this.savePath = "";
this.saveModelName = "";
this.fullColumn = new ArrayList<>();
// 处理所有列
JBIterable<? extends DasColumn> columns = DasUtil.getColumns(dbTable);
for (DasColumn column : columns) {
this.fullColumn.add(new ColumnInfoDTO(column));
}
}
private static void merge(TableInfoDTO oldData, TableInfoDTO newData) {
if (oldData == null || CollectionUtil.isEmpty(oldData.getFullColumn())) {
return;
}
if (!StringUtils.isEmpty(oldData.getPreName())) {
newData.preName = oldData.getPreName();
}
if (!StringUtils.isEmpty(oldData.getTemplateGroupName())) {
newData.templateGroupName = oldData.getTemplateGroupName();
}
if (!StringUtils.isEmpty(oldData.getSavePackageName())) {
newData.savePackageName = oldData.getSavePackageName();
}
if (!StringUtils.isEmpty(oldData.getSavePath())) {
newData.savePath = oldData.getSavePath();
}
if (!StringUtils.isEmpty(oldData.getSaveModelName())) {
newData.saveModelName = oldData.getSaveModelName();
}
// 补充自定义列
for (ColumnInfoDTO oldColumn : oldData.getFullColumn()) {
if (!oldColumn.getCustom()) {
continue;
}
newData.getFullColumn().add(oldColumn);
}
// 保持旧列的顺序
Map<String, ColumnInfoDTO> map = newData.getFullColumn().stream().collect(Collectors.toMap(ColumnInfoDTO::getName, val -> val));
List<ColumnInfoDTO> tmpList = new ArrayList<>();
for (ColumnInfoDTO columnInfo : oldData.getFullColumn()) {
ColumnInfoDTO newColumn = map.get(columnInfo.getName());
if (newColumn != null) {
tmpList.add(newColumn);
}
}
// 补充剩余列
for (ColumnInfoDTO columnInfoDTO : newData.getFullColumn()) {
if (!tmpList.contains(columnInfoDTO)) {
tmpList.add(columnInfoDTO);
}
}
// list数据替换
newData.getFullColumn().clear();
newData.getFullColumn().addAll(tmpList);
}
/**
* 表名首字母大写
*/
@ -45,4 +128,67 @@ public class TableInfoDTO {
* 保存的model名称
*/
private String saveModelName;
public TableInfo toTableInfo(DbTable dbTable) {
TableInfo tableInfo = new TableInfo();
tableInfo.setObj(dbTable);
tableInfo.setName(this.getName());
tableInfo.setPreName(this.getPreName());
tableInfo.setTemplateGroupName(this.getTemplateGroupName());
tableInfo.setSavePackageName(this.getSavePackageName());
tableInfo.setSavePath(this.getSavePath());
tableInfo.setComment(this.getComment());
tableInfo.setSaveModelName(this.getSaveModelName());
tableInfo.setFullColumn(new ArrayList<>());
tableInfo.setPkColumn(new ArrayList<>());
tableInfo.setOtherColumn(new ArrayList<>());
//
JBIterable<? extends DasColumn> columns = DasUtil.getColumns(dbTable);
Map<String, DasColumn> nameToObj = new HashMap<>(columns.size());
for (DasColumn column : columns) {
nameToObj.put(NameUtils.getInstance().getJavaName(column.getName()), column);
}
for (ColumnInfoDTO dto : this.getFullColumn()) {
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setObj(nameToObj.get(dto.getName()));
columnInfo.setName(dto.getName());
columnInfo.setType(dto.getType());
// 最后一节为短类型
String[] split = dto.getType().split("\\.");
columnInfo.setShortType(split[split.length - 1]);
columnInfo.setComment(dto.getComment());
columnInfo.setCustom(dto.getCustom());
columnInfo.setExt(dto.getExt());
tableInfo.getFullColumn().add(columnInfo);
if (columnInfo.getObj() != null && DasUtil.isPrimary(columnInfo.getObj())) {
tableInfo.getPkColumn().add(columnInfo);
} else {
tableInfo.getOtherColumn().add(columnInfo);
}
}
return tableInfo;
}
public static TableInfoDTO parse(TableInfo tableInfo) {
TableInfoDTO dto = new TableInfoDTO();
dto.setName(tableInfo.getName());
dto.setTemplateGroupName(tableInfo.getTemplateGroupName());
dto.setSavePath(tableInfo.getSavePath());
dto.setPreName(tableInfo.getPreName());
dto.setComment(tableInfo.getComment());
dto.setSavePackageName(tableInfo.getSavePackageName());
dto.setSaveModelName(tableInfo.getSaveModelName());
dto.setFullColumn(new ArrayList<>());
// 处理列
for (ColumnInfo columnInfo : tableInfo.getFullColumn()) {
ColumnInfoDTO columnInfoDTO = new ColumnInfoDTO();
columnInfoDTO.setName(columnInfo.getName());
columnInfoDTO.setType(columnInfo.getType());
columnInfoDTO.setExt(columnInfo.getExt());
columnInfoDTO.setCustom(columnInfo.getCustom());
columnInfoDTO.setComment(columnInfo.getComment());
dto.getFullColumn().add(columnInfoDTO);
}
return dto;
}
}

View File

@ -59,17 +59,9 @@ public class TableInfoSettingsDTO {
}
String key = generateKey(dbTable);
TableInfoDTO dto = this.tableInfoMap.get(key);
if (dto == null) {
dto = new TableInfoDTO();
dto.setName(dbTable.getName());
dto.setComment(dbTable.getComment());
dto.setPreName("");
dto.setSaveModelName("");
dto.setSavePath("");
dto.setTemplateGroupName("");
this.tableInfoMap.put(key, dto);
}
return null;
dto = new TableInfoDTO(dto, dbTable);
this.tableInfoMap.put(key, dto);
return dto.toTableInfo(dbTable);
}
/**
@ -85,5 +77,17 @@ public class TableInfoSettingsDTO {
if (dbTable == null) {
return;
}
String key = generateKey(dbTable);
this.tableInfoMap.put(key, TableInfoDTO.parse(tableInfo));
}
/**
* 重置表信息
*
* @param dbTable 数据库表
*/
public void resetTableInfo(DbTable dbTable) {
String key = generateKey(dbTable);
this.tableInfoMap.put(key, new TableInfoDTO(null, dbTable));
}
}

View File

@ -39,7 +39,7 @@ public class ColumnInfo {
/**
* 标记是否为自定义附加列
*/
private boolean custom;
private Boolean custom;
/**
* 扩展数据
*/

View File

@ -216,7 +216,7 @@ public class TableInfoServiceImpl implements TableInfoService {
}
// 添加附加列
for (ColumnInfo configColumn : tableInfoConfig.getFullColumn()) {
if (configColumn.isCustom()) {
if (configColumn.getCustom()) {
fullColumn.add(configColumn);
}
}
@ -341,7 +341,7 @@ public class TableInfoServiceImpl implements TableInfoService {
columnInfo.setExt(new LinkedHashMap<>());
}
// 已经不存在的非自定义列直接删除
if (!exists && !columnInfo.isCustom()) {
if (!exists && !columnInfo.getCustom()) {
columnIterable.remove();
}
}

View File

@ -7,7 +7,7 @@ import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.entity.ColumnConfig;
import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.factory.CellEditorFactory;
import com.sjhy.plugin.service.TableInfoService;
import com.sjhy.plugin.service.TableInfoSettingsService;
import com.sjhy.plugin.tool.CacheDataUtils;
import com.sjhy.plugin.tool.CurrGroupUtils;
import com.sjhy.plugin.tool.ProjectUtils;
@ -31,10 +31,6 @@ public class ConfigTableDialog extends DialogWrapper {
* 主面板
*/
private JPanel mainPanel;
/**
* 表信息服务
*/
private TableInfoService tableInfoService;
/**
* 表信息对象
*/
@ -42,16 +38,15 @@ public class ConfigTableDialog extends DialogWrapper {
public ConfigTableDialog() {
super(ProjectUtils.getCurrProject());
this.tableInfoService = TableInfoService.getInstance(ProjectUtils.getCurrProject());
this.mainPanel = new JPanel(new BorderLayout());
this.initPanel();
}
private void initPanel() {
init();
this.tableInfo = this.tableInfoService.getTableInfoAndConfig(CacheDataUtils.getInstance().getSelectDbTable());
setTitle("Config Table " + tableInfo.getObj().getName());
ConfigTableModel model = new ConfigTableModel(tableInfo);
this.tableInfo = TableInfoSettingsService.getInstance().getTableInfo(CacheDataUtils.getInstance().getSelectDbTable());
setTitle("Config Table " + this.tableInfo.getObj().getName());
ConfigTableModel model = new ConfigTableModel(this.tableInfo);
JBTable table = new JBTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
@ -96,7 +91,7 @@ public class ConfigTableDialog extends DialogWrapper {
protected void processDoNotAskOnOk(int exitCode) {
super.processDoNotAskOnOk(exitCode);
// 保存信息
tableInfoService.save(tableInfo);
TableInfoSettingsService.getInstance().saveTableInfo(tableInfo);
}
}

View File

@ -94,7 +94,7 @@ public class ConfigTableModel extends DefaultTableModel implements EditableModel
return;
}
// 非自定义数据不允许修改列名
if (!columnInfo.isCustom() && column == 0) {
if (!columnInfo.getCustom() && column == 0) {
return;
}
switch (column) {
@ -138,7 +138,7 @@ public class ConfigTableModel extends DefaultTableModel implements EditableModel
return;
}
// 非自定义列不允许删除
if (!columnInfo.isCustom()) {
if (!columnInfo.getCustom()) {
return;
}
this.tableInfo.getFullColumn().remove(row);