提交 c885a88b authored 作者: 黄承天's avatar 黄承天

初始提交

上级 be225009
package com.tykj.datahouse.controller; package com.tykj.datahouse.controller;
import com.google.common.collect.ImmutableMap;
import com.tykj.datahouse.base.result.ResultObj; import com.tykj.datahouse.base.result.ResultObj;
import com.tykj.datahouse.base.result.ResultUtil; import com.tykj.datahouse.base.result.ResultUtil;
import com.tykj.datahouse.entity.TableInfo; import com.tykj.datahouse.entity.TableInfo;
...@@ -13,7 +14,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -13,7 +14,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@Api(tags = "表信息API") @Api(tags = "表操作API")
@CrossOrigin @CrossOrigin
@RequestMapping("/table") @RequestMapping("/table")
@RestController @RestController
...@@ -25,9 +26,9 @@ public class TableInfoController { ...@@ -25,9 +26,9 @@ public class TableInfoController {
@PostMapping @PostMapping
@ApiOperation("创建一个新表") @ApiOperation("创建一个新表")
public ResponseEntity<ResultObj<Object>> createTable(@RequestBody TableInfo tableInfo) { public ResponseEntity createTable(@RequestBody TableInfo tableInfo) {
tableInfoService.createTable(tableInfo); tableInfoService.createTable(tableInfo);
return ResultUtil.success("操作成功"); return ResponseEntity.ok(ImmutableMap.of("message","操作成功"));
} }
@PutMapping @PutMapping
......
...@@ -20,7 +20,6 @@ public class ColumnInfo { ...@@ -20,7 +20,6 @@ public class ColumnInfo {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty("主键") @ApiModelProperty("主键")
@JsonIgnore
private Integer id; private Integer id;
//-----------------------------// //-----------------------------//
......
...@@ -14,12 +14,14 @@ import java.util.*; ...@@ -14,12 +14,14 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.lang.String.format; import static java.lang.String.format;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull; import static java.util.Objects.nonNull;
/** /**
* 表信息相关操作的Service * 表信息相关操作的Service
* 涉及到对表的操作 均先进行对实际表的操作(执行sql语句) 再更新表信息 * 涉及到对表的操作 均先进行对实际表的操作(执行sql语句) 再更新表信息
*/ */
@SuppressWarnings("Duplicates")
@Service @Service
public class TableInfoService { public class TableInfoService {
...@@ -78,6 +80,9 @@ public class TableInfoService { ...@@ -78,6 +80,9 @@ public class TableInfoService {
public void alterTable(TableInfo tableInfo) { public void alterTable(TableInfo tableInfo) {
//根据id查到更新之前的原表信息 //根据id查到更新之前的原表信息
dataSourceManager.clear(); dataSourceManager.clear();
if (isNull(tableInfo.getId())){
throw new RuntimeException("修改数据必须附带id");
}
TableInfo oTableInfo = tableInfoRepository.findById(tableInfo.getId()) TableInfo oTableInfo = tableInfoRepository.findById(tableInfo.getId())
.map(this::getColumns) .map(this::getColumns)
.orElseThrow(() -> new RuntimeException(format("id为%s的表不存在", tableInfo.getId()))); .orElseThrow(() -> new RuntimeException(format("id为%s的表不存在", tableInfo.getId())));
...@@ -90,7 +95,8 @@ public class TableInfoService { ...@@ -90,7 +95,8 @@ public class TableInfoService {
Set<Integer> columnInfoIds = columnInfos.stream() Set<Integer> columnInfoIds = columnInfos.stream()
.map(ColumnInfo::getId) .map(ColumnInfo::getId)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
Map<Integer, ColumnInfo> columnInfoMap = columnInfos.stream() Map<Integer, ColumnInfo> columnInfoMapForAlter = columnInfos.stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(ColumnInfo::getId, columnInfo -> columnInfo)); .collect(Collectors.toMap(ColumnInfo::getId, columnInfo -> columnInfo));
List<ColumnInfo> columnsForAdd = columnInfos.stream() List<ColumnInfo> columnsForAdd = columnInfos.stream()
.filter(columnInfo -> isForAdd(columnInfo.getId(), oColumnInfoIds)) .filter(columnInfo -> isForAdd(columnInfo.getId(), oColumnInfoIds))
...@@ -111,10 +117,10 @@ public class TableInfoService { ...@@ -111,10 +117,10 @@ public class TableInfoService {
} }
for (ColumnInfo oColumnInfo : columnsForAlter) { for (ColumnInfo oColumnInfo : columnsForAlter) {
dataSourceManager.switchToDataSource("target"); dataSourceManager.switchToDataSource("target");
String alterColumnSql = sqlCreator.alterColumnSql(tableInfo, oColumnInfo, columnInfoMap.get(oColumnInfo.getId())); String alterColumnSql = sqlCreator.alterColumnSql(tableInfo, oColumnInfo, columnInfoMapForAlter.get(oColumnInfo.getId()));
jdbcTemplate.execute(alterColumnSql); jdbcTemplate.execute(alterColumnSql);
dataSourceManager.clear(); dataSourceManager.clear();
columnInfoRepository.save(columnInfoMap.get(oColumnInfo.getId())); columnInfoRepository.save(columnInfoMapForAlter.get(oColumnInfo.getId()));
} }
for (ColumnInfo oColumnInfo : columnsForDelete) { for (ColumnInfo oColumnInfo : columnsForDelete) {
dataSourceManager.switchToDataSource("target"); dataSourceManager.switchToDataSource("target");
......
...@@ -21,10 +21,10 @@ public class MysqlSqlCreator implements SqlCreator { ...@@ -21,10 +21,10 @@ public class MysqlSqlCreator implements SqlCreator {
.orElseThrow(() -> new RuntimeException("没有主键")); .orElseThrow(() -> new RuntimeException("没有主键"));
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
//起始 //起始
result.append(format("CREATE TABLE %s(\n", tableInfo.getName())); result.append(format("CREATE TABLE `%s`(\n", tableInfo.getName()));
//其他字段 //其他字段
for (ColumnInfo columnInfo : columnInfos) { for (ColumnInfo columnInfo : columnInfos) {
result.append(format("%s %s", columnInfo.getName(), columnInfo.getType())); result.append(format("`%s` %s", columnInfo.getName(), columnInfo.getType()));
if (nonNull(columnInfo.getLength())) { if (nonNull(columnInfo.getLength())) {
result.append(format("(%s)", columnInfo.getLength())); result.append(format("(%s)", columnInfo.getLength()));
} }
...@@ -56,7 +56,7 @@ public class MysqlSqlCreator implements SqlCreator { ...@@ -56,7 +56,7 @@ public class MysqlSqlCreator implements SqlCreator {
@Override @Override
public String alterColumnSql(TableInfo tableInfo, ColumnInfo oColumnInfo, ColumnInfo columnInfo) { public String alterColumnSql(TableInfo tableInfo, ColumnInfo oColumnInfo, ColumnInfo columnInfo) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
result.append(format("ALTER TABLE %s CHANGE COLUMN %s %s %s", tableInfo.getName(), oColumnInfo.getName(), columnInfo.getName(), columnInfo.getType())); result.append(format("ALTER TABLE `%s` CHANGE COLUMN `%s` `%s` %s", tableInfo.getName(), oColumnInfo.getName(), columnInfo.getName(), columnInfo.getType()));
if (nonNull(columnInfo.getLength())) { if (nonNull(columnInfo.getLength())) {
result.append(format("(%s)", columnInfo.getLength())); result.append(format("(%s)", columnInfo.getLength()));
} }
...@@ -68,12 +68,12 @@ public class MysqlSqlCreator implements SqlCreator { ...@@ -68,12 +68,12 @@ public class MysqlSqlCreator implements SqlCreator {
@Override @Override
public String removeColumnSql(TableInfo tableInfo, ColumnInfo columnInfo) { public String removeColumnSql(TableInfo tableInfo, ColumnInfo columnInfo) {
return format("ALTER TABLE %s DROP COLUMN %s", tableInfo.getName(), columnInfo.getName()); return format("ALTER TABLE `%s` DROP COLUMN `%s`", tableInfo.getName(), columnInfo.getName());
} }
@Override @Override
public String dropTableSql(TableInfo tableInfo) { public String dropTableSql(TableInfo tableInfo) {
return format("DROP TABLE %s", tableInfo.getName()); return format("DROP TABLE `%s`", tableInfo.getName());
} }
@Override @Override
...@@ -83,9 +83,9 @@ public class MysqlSqlCreator implements SqlCreator { ...@@ -83,9 +83,9 @@ public class MysqlSqlCreator implements SqlCreator {
StringBuilder columns = new StringBuilder(); StringBuilder columns = new StringBuilder();
StringBuilder values = new StringBuilder(); StringBuilder values = new StringBuilder();
//起始 //起始
result.append(format("INSERT INTO %s ", tableInfo.getName())); result.append(format("INSERT INTO `%s` ", tableInfo.getName()));
for (ColumnInfo columnInfo : columnInfos) { for (ColumnInfo columnInfo : columnInfos) {
columns.append(columnInfo.getName()).append(","); columns.append(format("`%s`", columnInfo.getName())).append(",");
values.append(format("\'%s\',", row.get(columnInfo.getName()))); values.append(format("\'%s\',", row.get(columnInfo.getName())));
} }
int columnLastIndex = columns.lastIndexOf(","); int columnLastIndex = columns.lastIndexOf(",");
...@@ -109,15 +109,15 @@ public class MysqlSqlCreator implements SqlCreator { ...@@ -109,15 +109,15 @@ public class MysqlSqlCreator implements SqlCreator {
.orElseThrow(() -> new RuntimeException("没有主键")); .orElseThrow(() -> new RuntimeException("没有主键"));
columnInfos.remove(primary); columnInfos.remove(primary);
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
result.append(format("UPDATE %s SET ", tableInfo.getName())); result.append(format("UPDATE `%s` SET ", tableInfo.getName()));
for (ColumnInfo columnInfo : columnInfos) { for (ColumnInfo columnInfo : columnInfos) {
result.append(format("%s = \'%s\',", columnInfo.getName(), row.get(columnInfo.getName()))); result.append(format("`%s` = \'%s\',", columnInfo.getName(), row.get(columnInfo.getName())));
} }
int lastIndex = result.lastIndexOf(","); int lastIndex = result.lastIndexOf(",");
if (lastIndex > 0) { if (lastIndex > 0) {
result.delete(lastIndex, result.length()); result.delete(lastIndex, result.length());
} }
result.append(format(" WHERE (%s = \'%s\')", primary.getName(), row.get(primary.getName()))); result.append(format(" WHERE (`%s` = \'%s\')", primary.getName(), row.get(primary.getName())));
return result.toString(); return result.toString();
} }
...@@ -128,7 +128,7 @@ public class MysqlSqlCreator implements SqlCreator { ...@@ -128,7 +128,7 @@ public class MysqlSqlCreator implements SqlCreator {
.filter(ColumnInfo::getIsPrimary) .filter(ColumnInfo::getIsPrimary)
.findAny() .findAny()
.orElseThrow(() -> new RuntimeException("没有主键")); .orElseThrow(() -> new RuntimeException("没有主键"));
result.append(format("DELETE FROM %s WHERE (%s = \'%s\')", tableInfo.getName(), primary.getName(), row.get(primary.getName()))); result.append(format("DELETE FROM `%s` WHERE (`%s` = \'%s\')", tableInfo.getName(), primary.getName(), row.get(primary.getName())));
return result.toString(); return result.toString();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论