From c30094199a7c57858dc2877401fa767b5475c38f Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Sat, 16 Nov 2019 15:33:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86MySQL=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E5=A2=9E=E5=92=8C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apis/database/table/TableController.java | 65 ++++++-- .../plugin/dao/database/table/ITableDao.java | 41 ++++- .../dao/database/table/impl/TableDaoImpl.java | 157 ++++++++++++++++-- .../database/table/ColumnDataTypeEnum.java | 45 +++++ .../database/table/ColumnIsNullableEnum.java | 27 +++ .../vos/database/table/TableColumnVO.java | 128 +++++++------- .../vos/database/table/TableOldColumnVO.java | 40 +++++ .../service/database/table/ITableService.java | 46 ++++- .../database/table/impl/TableServiceImpl.java | 75 +++++---- 9 files changed, 505 insertions(+), 119 deletions(-) create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnIsNullableEnum.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableOldColumnVO.java diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/table/TableController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/table/TableController.java index 55daffe..bf74de8 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/table/TableController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/table/TableController.java @@ -6,9 +6,13 @@ import com.cm.common.constants.ISystemConstant; import com.cm.common.exception.SearchException; import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; +import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO; import com.cm.common.plugin.pojo.vos.database.table.TableVO; import com.cm.common.plugin.service.database.table.ITableService; +import com.cm.common.result.ErrorResult; import com.cm.common.result.SuccessResult; +import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -32,34 +36,71 @@ public class TableController extends AbstractController { @Autowired private ITableService tableService; + @ApiOperation(value = "建表", notes = "建表接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("savetable") @CheckRequestBodyAnnotation public SuccessResult saveTable(@RequestBody TableVO tableVO) { return tableService.saveTable(tableVO); } - @GetMapping("getTableByTableName/{tableName}") + @ApiOperation(value = "删表", notes = "删表接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @DeleteMapping("removetable/{tableName}") + public SuccessResult removeTable(@PathVariable("tableName") String tableName) { + return tableService.removeTable(tableName); + } + + @ApiOperation(value = "加列", notes = "加列接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("savetablecolumn/{tableName}") + @CheckRequestBodyAnnotation + public SuccessResult saveTableColumn(@PathVariable("tableName") String tableName, @RequestBody TableColumnVO tableColumnVO) { + return tableService.saveTableColumn(tableName, tableColumnVO); + } + + @ApiOperation(value = "修改列", notes = "修改列") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PutMapping("updatetablecolumn/{tableName}") + @CheckRequestBodyAnnotation + public SuccessResult updateTableColumn(@PathVariable("tableName") String tableName, @RequestBody TableOldColumnVO tableOldColumnVO) { + return tableService.updateTableColumn(tableName, tableOldColumnVO); + } + + @ApiOperation(value = "修改列类型", notes = "修改列类型") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PutMapping("updatetablecolumntype/{tableName}") + @CheckRequestBodyAnnotation + public SuccessResult updateTableColumnType(@PathVariable("tableName") String tableName, @RequestBody TableColumnVO tableColumnVO) { + return tableService.updateTableColumnType(tableName, tableColumnVO); + } + + @ApiOperation(value = "通过表名查询表", notes = "通过表名查询表接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("gettablebytablename/{tableName}") public TableDTO getTableByTableName(@PathVariable("tableName") String tableName) { return tableService.getTableByTableName(tableName); } - /** - * 表列表 - * - * @return - */ + @ApiOperation(value = "表列表", notes = "表列表接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @GetMapping("listtables") public List listTables() throws SearchException { Map params = requestParams(); return tableService.listTables(params); } - /** - * 表列,列表 - * - * @param tableName - * @return - */ + @ApiOperation(value = "表详情", notes = "表详情接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @GetMapping("listtablecolumns/{tableName}") public List listTableColumns(@PathVariable("tableName") String tableName) throws SearchException { Map params = requestParams(); diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/ITableDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/ITableDao.java index d1c0cd2..e0f8e21 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/ITableDao.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/ITableDao.java @@ -3,7 +3,7 @@ package com.cm.common.plugin.dao.database.table; import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; -import org.springframework.stereotype.Repository; +import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO; import java.sql.Connection; import java.util.List; @@ -31,6 +31,45 @@ public interface ITableDao { */ void saveTable(Connection connection, String databaseName, String tableName, List tableColumns); + /** + * 删表 + * + * @param connection + * @param databaseName + * @param tableName + */ + void removeTable(Connection connection, String databaseName, String tableName); + + /** + * 加列 + * + * @param connection + * @param databaseName + * @param tableName + * @param tableColumnVO + */ + void saveTableColumn(Connection connection, String databaseName, String tableName, TableColumnVO tableColumnVO); + + /** + * 修改列 + * + * @param connection + * @param databaseName + * @param tableName + * @param tableOldColumnVO + */ + void updateTableColumn(Connection connection, String databaseName, String tableName, TableOldColumnVO tableOldColumnVO); + + /** + * 修改列属性 + * + * @param connection + * @param databaseName + * @param tableName + * @param tableColumnVO + */ + void updateTableColumnType(Connection connection, String databaseName, String tableName, TableColumnVO tableColumnVO); + /** * 表列列表 * diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java index 885fd22..fd105fa 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java @@ -2,27 +2,28 @@ package com.cm.common.plugin.dao.database.table.impl; import com.cm.common.exception.SaveException; import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; import com.cm.common.plugin.dao.database.table.ITableDao; +import com.cm.common.plugin.enums.database.table.ColumnDataTypeEnum; +import com.cm.common.plugin.enums.database.table.ColumnIsNullableEnum; import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; -import com.cm.common.plugin.service.database.IDatabaseService; +import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO; import com.cm.common.utils.HashMapUtil; import com.cm.common.utils.WStringUtil; import com.cm.common.utils.jdbc.JdbcUtil; -import jdk.nashorn.internal.runtime.regexp.joni.Regex; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Repository; -import org.springframework.stereotype.Service; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * When you feel like quitting. Think about why you started @@ -34,6 +35,7 @@ import java.util.regex.Pattern; * @Date: 2019/11/15 15:39 * @Version: 1.0 **/ +@Primary @Repository public class TableDaoImpl implements ITableDao { @@ -41,26 +43,88 @@ public class TableDaoImpl implements ITableDao { @Override public void saveTable(Connection connection, String databaseName, String tableName, List tableColumns) { -// try { -// String sql = ""; -// } catch (SQLException e) { -// LOG.error(e.getMessage(), e); -// throw new SaveException("建表失败"); -// } + try { + connection.setAutoCommit(false); + String sql = getSaveTableSQL(databaseName, tableName, tableColumns); + LOG.debug("sql: {}", sql); + PreparedStatement preparedStatement = connection.prepareStatement(sql); + preparedStatement.execute(); + connection.commit(); + } catch (SQLException e) { + LOG.error(e.getMessage(), e); + try { + connection.rollback(); + } catch (SQLException ex) { + LOG.error(ex.getMessage(), ex); + } + throw new SaveException("建表失败"); + } } - private String getSaveTableSQL(String databaseName, String tableName, List tableColumns) { - StringBuilder sql = new StringBuilder("CREATE TABLE `").append(databaseName).append("`.`").append(tableName).append("`"); - for (TableColumnVO tableColumnVO : tableColumns) { - + @Override + public void removeTable(Connection connection, String databaseName, String tableName) { + try { + connection.setAutoCommit(true); + String sql = String.format("DROP TABLE IF EXISTS `%s`.`%s`", databaseName, tableName); + LOG.debug("sql: {}", sql); + PreparedStatement preparedStatement = connection.prepareStatement(sql); + preparedStatement.execute(); + } catch (SQLException e) { + LOG.error(e.getMessage(), e); + throw new SaveException("删表失败"); + } + } + + @Override + public void saveTableColumn(Connection connection, String databaseName, String tableName, TableColumnVO tableColumnVO) { + try { + connection.setAutoCommit(true); + StringBuilder sqlSB = new StringBuilder(String.format("ALTER TABLE `%s`.`%s` ADD COLUMN ", databaseName, tableName)); + sqlSB.append(getSaveColumnSql(tableColumnVO)); + LOG.debug("sql: {}", sqlSB.toString()); + PreparedStatement preparedStatement = connection.prepareStatement(sqlSB.toString()); + preparedStatement.execute(); + } catch (SQLException e) { + LOG.error(e.getMessage(), e); + throw new SaveException("新增列失败"); + } + } + + @Override + public void updateTableColumn(Connection connection, String databaseName, String tableName, TableOldColumnVO tableOldColumnVO) { + try { + connection.setAutoCommit(true); + StringBuilder sqlSB = new StringBuilder(String.format("ALTER TABLE `%s`.`%s` CHANGE %s ", databaseName, tableName, WStringUtil.lowerUpper2UnderLine(tableOldColumnVO.getOldColumnName()))); + sqlSB.append(getSaveColumnSql(tableOldColumnVO)); + LOG.debug("sql: {}", sqlSB.toString()); + PreparedStatement preparedStatement = connection.prepareStatement(sqlSB.toString()); + preparedStatement.execute(); + } catch (SQLException e) { + LOG.error(e.getMessage(), e); + throw new UpdateException("修改列属性失败"); + } + } + + @Override + public void updateTableColumnType(Connection connection, String databaseName, String tableName, TableColumnVO tableColumnVO) { + try { + connection.setAutoCommit(true); + StringBuilder sqlSB = new StringBuilder(String.format("ALTER TABLE `%s`.`%s` MODIFY ", databaseName, tableName)); + sqlSB.append(getSaveColumnSql(tableColumnVO)); + LOG.debug("sql: {}", sqlSB.toString()); + PreparedStatement preparedStatement = connection.prepareStatement(sqlSB.toString()); + preparedStatement.execute(); + } catch (SQLException e) { + LOG.error(e.getMessage(), e); + throw new UpdateException("修改列属性失败"); } - return sql.toString(); } @Override public List listTableColumns(Connection connection, Map params) { try { String sql = "SELECT COLUMN_NAME columnName,COLUMN_DEFAULT columnDefault,IS_NULLABLE isNullable,DATA_TYPE dataType,CHARACTER_MAXIMUM_LENGTH characterMaximumLength,NUMERIC_PRECISION numericPrecision,NUMERIC_SCALE numericScale,DATETIME_PRECISION datetimePrecision,COLUMN_TYPE columnType,COLUMN_KEY columnKey,COLUMN_COMMENT columnComment,EXTRA extra FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA`= ? AND `TABLE_NAME`= ?"; + LOG.debug("sql: {}", sql); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, params.get("databaseName").toString()); preparedStatement.setString(2, params.get("tableName").toString()); @@ -76,6 +140,7 @@ public class TableDaoImpl implements ITableDao { public List listTables(Connection connection, Map params) { try { String sql = "SELECT TABLE_SCHEMA tableSchema, TABLE_NAME tableName FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = ?"; + LOG.debug("sql: {}", sql); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, params.get("databaseName").toString()); preparedStatement.execute(); @@ -90,6 +155,7 @@ public class TableDaoImpl implements ITableDao { public TableDTO getTableByTableName(Connection connection, String databaseName, String tableName) { try { String sql = "SELECT TABLE_SCHEMA tableSchema, TABLE_NAME tableName FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"; + LOG.debug("sql: {}", sql); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, databaseName); preparedStatement.setString(2, tableName); @@ -105,4 +171,63 @@ public class TableDaoImpl implements ITableDao { } } + /** + * 获取建表SQL语句 + * + * @param databaseName + * @param tableName + * @param tableColumns + * @return + */ + private String getSaveTableSQL(String databaseName, String tableName, List tableColumns) { + String underLineTableName = WStringUtil.lowerUpper2UnderLine(tableName); + StringBuilder sql = new StringBuilder("CREATE TABLE `").append(databaseName).append("`.`").append(tableName).append("`("); + sql.append("`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',"); + sql.append(String.format("`%s_id` CHAR(36) NOT NULL COMMENT '主键',", underLineTableName)); + for (TableColumnVO tableColumn : tableColumns) { + sql.append(getSaveColumnSql(tableColumn)).append(","); + } + sql.append("`creator` CHAR(36) DEFAULT NULL,"); + sql.append("`gmt_create` datetime DEFAULT NULL,"); + sql.append("`modifier` CHAR(36) DEFAULT NULL,"); + sql.append("`gmt_modified` datetime DEFAULT NULL,"); + sql.append("`is_delete` int(1) DEFAULT '0',"); + sql.append(String.format("PRIMARY KEY (`id`, `%s_id`)", underLineTableName)); + sql.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + return sql.toString(); + } + + /** + * 新增列的SQL + * + * @param tableColumn + * @return + */ + private String getSaveColumnSql(TableColumnVO tableColumn) { + boolean notNull = true; + StringBuilder columnSql = new StringBuilder(); + if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.VARCHAR.getDataType())) { + columnSql.append(String.format("`%s` VARCHAR(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.INT.getDataType())) { + columnSql.append(String.format("`%s` INT(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.DOUBLE.getDataType())) { + columnSql.append(String.format("`%s` DOUBLE(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.BIGINT.getDataType())) { + columnSql.append(String.format("`%s` BIGINT(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.RICH_TEXT.getDataType())) { + columnSql.append(String.format("`%s` LONGTEXT DEFAULT NULL", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + notNull = false; + } + if (notNull && StringUtils.equals(tableColumn.getIsNullable(), ColumnIsNullableEnum.YES.getIsNullable())) { + columnSql.append(" NOT NULL"); + } + if (!StringUtils.isEmpty(tableColumn.getColumnDefault())) { + columnSql.append(String.format(" DEFAULT '%s'", tableColumn.getColumnDefault())); + } + if (!StringUtils.isEmpty(tableColumn.getColumnComment())) { + columnSql.append(String.format(" COMMENT '%s'", tableColumn.getColumnComment())); + } + return columnSql.toString(); + } + } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java new file mode 100644 index 0000000..aa23db7 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java @@ -0,0 +1,45 @@ +package com.cm.common.plugin.enums.database.table; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DataTypeEnum + * @Description: 数据类型 + * @Author: WangGeng + * @Date: 2019/11/15 8:20 下午 + * @Version: 1.0 + **/ +public enum ColumnDataTypeEnum { + + /** + * 整形 + */ + INT("int"), + /** + * 长整形 + */ + BIGINT("bigint"), + /** + * 双精度 + */ + DOUBLE("double"), + /** + * 字符串 + */ + VARCHAR("varchar"), + /** + * 富文本 + */ + RICH_TEXT("richText"); + + private String dataType; + + ColumnDataTypeEnum(String dataType) { + this.dataType = dataType; + } + + public String getDataType() { + return dataType == null ? "" : dataType.trim(); + } +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnIsNullableEnum.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnIsNullableEnum.java new file mode 100644 index 0000000..c29dbce --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnIsNullableEnum.java @@ -0,0 +1,27 @@ +package com.cm.common.plugin.enums.database.table; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ColumnIsNullable + * @Description: 可为空 + * @Author: WangGeng + * @Date: 2019/11/15 9:28 下午 + * @Version: 1.0 + **/ +public enum ColumnIsNullableEnum { + + YES("YES"), + NO("NO"); + + String isNullable; + + ColumnIsNullableEnum(String isNullable) { + this.isNullable = isNullable; + } + + public String getIsNullable() { + return isNullable == null ? "" : isNullable.trim(); + } +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java index 0e43232..8531588 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java @@ -1,16 +1,20 @@ package com.cm.common.plugin.pojo.vos.database.table; import com.cm.common.annotation.CheckBooleanAnnotation; +import com.cm.common.annotation.CheckEmptyAnnotation; import com.cm.common.annotation.CheckNullAnnotation; import com.cm.common.annotation.CheckNumberAnnotation; +import com.cm.common.plugin.enums.database.table.ColumnDataTypeEnum; +import com.cm.common.plugin.enums.database.table.ColumnIsNullableEnum; import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 * * @ClassName: TableColumnVO - * @Description: //TODO + * @Description: 表列表 * @Author: WangGeng * @Date: 2019/11/15 14:47 * @Version: 1.0 @@ -18,94 +22,104 @@ import io.swagger.annotations.ApiModel; @ApiModel public class TableColumnVO { - @CheckNullAnnotation(name = "字段名称") - private String fieldName; - @CheckNullAnnotation(name = "字段说明") - private String fieldComment; - @CheckNullAnnotation(name = "字段类型") - private String fieldType; - @CheckNumberAnnotation(name = "字段长度") - private Integer fieldLength; - @CheckNumberAnnotation(name = "字段小数点长度") - private Integer fieldFloatLength; - @CheckBooleanAnnotation(name = "是否为空") - private Boolean isNull; - @CheckNullAnnotation(name = "默认值") - private String defaultValue; + @ApiModelProperty(name = "columnName", value = "字段名称") + @CheckEmptyAnnotation(name = "字段名称") + private String columnName; - public String getFieldName() { - return fieldName; + @ApiModelProperty(name = "columnDefault", value = "默认值") + private String columnDefault; + + @ApiModelProperty(name = "isNullable", value = "可空") + @CheckEmptyAnnotation(name = "可空", types = {"YES", "NO"}) + private String isNullable; + + @ApiModelProperty(name = "dataType", value = "类型") + @CheckEmptyAnnotation(name = "类型", types = {"int", "double", "bigint", "varchar", "richText"}) + private String dataType; + + @ApiModelProperty(name = "columnLength", value = "长度") + @CheckNumberAnnotation(name = "长度") + private Integer columnLength; + + @ApiModelProperty(name = "columnFloatLength", value = "小数点") + private Integer columnFloatLength; + + @ApiModelProperty(name = "columnComment", value = "字段注释") + private String columnComment; + + public String getColumnName() { + return columnName == null ? "" : columnName.trim(); } - public void setFieldName(String fieldName) { - this.fieldName = fieldName; + public void setColumnName(String columnName) { + this.columnName = columnName; } - public String getFieldComment() { - return fieldComment; + public String getColumnDefault() { + return columnDefault == null ? "" : columnDefault.trim(); } - public void setFieldComment(String fieldComment) { - this.fieldComment = fieldComment; + public void setColumnDefault(String columnDefault) { + this.columnDefault = columnDefault; } - public String getFieldType() { - return fieldType; + public String getIsNullable() { + return isNullable == null ? "" : isNullable.trim(); } - public void setFieldType(String fieldType) { - this.fieldType = fieldType; + public void setIsNullable(String isNullable) { + this.isNullable = isNullable; } - public Integer getFieldLength() { - return fieldLength; + public String getDataType() { + return dataType; } - public void setFieldLength(Integer fieldLength) { - this.fieldLength = fieldLength; + public void setDataType(String dataType) { + this.dataType = dataType; } - public Integer getFieldFloatLength() { - return fieldFloatLength; + public Integer getColumnLength() { + return columnLength; } - public void setFieldFloatLength(Integer fieldFloatLength) { - this.fieldFloatLength = fieldFloatLength; + public void setColumnLength(Integer columnLength) { + this.columnLength = columnLength; } - public Boolean getNull() { - return isNull; + public Integer getColumnFloatLength() { + return columnFloatLength; } - public void setNull(Boolean aNull) { - isNull = aNull; + public void setColumnFloatLength(Integer columnFloatLength) { + this.columnFloatLength = columnFloatLength; } - public String getDefaultValue() { - return defaultValue; + public String getColumnComment() { + return columnComment == null ? "" : columnComment.trim(); } - public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; + public void setColumnComment(String columnComment) { + this.columnComment = columnComment; } @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); - sb.append("\"fieldName\":\"") - .append(fieldName).append('\"'); - sb.append(",\"fieldComment\":\"") - .append(fieldComment).append('\"'); - sb.append(",\"fieldType\":\"") - .append(fieldType).append('\"'); - sb.append(",\"fieldLength\":") - .append(fieldLength); - sb.append(",\"fieldFloatLength\":") - .append(fieldFloatLength); - sb.append(",\"isNull\":") - .append(isNull); - sb.append(",\"defaultValue\":\"") - .append(defaultValue).append('\"'); + sb.append("\"columnName\":") + .append("\"").append(columnName).append("\""); + sb.append(",\"columnDefault\":") + .append("\"").append(columnDefault).append("\""); + sb.append(",\"isNullable\":") + .append("\"").append(isNullable).append("\""); + sb.append(",\"dataType\":") + .append(dataType); + sb.append(",\"columnLength\":") + .append(columnLength); + sb.append(",\"columnFloatLength\":") + .append(columnFloatLength); + sb.append(",\"columnComment\":") + .append("\"").append(columnComment).append("\""); sb.append('}'); return sb.toString(); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableOldColumnVO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableOldColumnVO.java new file mode 100644 index 0000000..6a523ae --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableOldColumnVO.java @@ -0,0 +1,40 @@ +package com.cm.common.plugin.pojo.vos.database.table; + +import com.cm.common.annotation.CheckEmptyAnnotation; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: TableNewColumnVO + * @Description: 新表列 + * @Author: WangGeng + * @Date: 2019/11/16 1:55 下午 + * @Version: 1.0 + **/ +@ApiModel +public class TableOldColumnVO extends TableColumnVO { + + @ApiModelProperty(name = "oldColumnName", value = "旧字段名称") + @CheckEmptyAnnotation(name = "旧字段名称") + private String oldColumnName; + + public String getOldColumnName() { + return oldColumnName == null ? "" : oldColumnName.trim(); + } + + public void setOldColumnName(String oldColumnName) { + this.oldColumnName = oldColumnName; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"oldColumnName\":") + .append("\"").append(oldColumnName).append("\""); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java index 781f24e..5656587 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java @@ -1,9 +1,13 @@ package com.cm.common.plugin.service.database.table; +import com.cm.common.exception.RemoveException; import com.cm.common.exception.SaveException; import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; +import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO; import com.cm.common.plugin.pojo.vos.database.table.TableVO; import com.cm.common.result.SuccessResult; @@ -32,6 +36,45 @@ public interface ITableService { */ SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException; + /** + * 删表 + * + * @param tableName + * @return + * @throws RemoveException + */ + SuccessResult removeTable(String tableName) throws RemoveException; + + /** + * 加列 + * + * @param tableName + * @param tableColumnVO + * @return + * @throws SaveException + */ + SuccessResult saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException; + + /** + * 修改列 + * + * @param tableName + * @param tableOldColumnVO + * @return + * @throws UpdateException + */ + SuccessResult updateTableColumn(String tableName, TableOldColumnVO tableOldColumnVO) throws UpdateException; + + /** + * 修改表列类型 + * + * @param tableName + * @param tableColumnVO + * @return + * @throws UpdateException + */ + SuccessResult updateTableColumnType(String tableName, TableColumnVO tableColumnVO) throws UpdateException; + /** * 通过表名获取表 * @@ -51,7 +94,7 @@ public interface ITableService { List listTables(Map params) throws SearchException; /** - * 表列,列表 + * 表详情 * * @param params * @return @@ -59,5 +102,4 @@ public interface ITableService { */ List listTableColumns(Map params) throws SearchException; - } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java index 135448a..ac02aa5 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java @@ -1,23 +1,23 @@ package com.cm.common.plugin.service.database.table.impl; import com.cm.common.base.AbstractService; +import com.cm.common.exception.RemoveException; import com.cm.common.exception.SaveException; import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; import com.cm.common.plugin.dao.database.table.ITableDao; import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; +import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO; import com.cm.common.plugin.pojo.vos.database.table.TableVO; import com.cm.common.plugin.service.database.IDatabaseService; import com.cm.common.plugin.service.database.table.ITableService; import com.cm.common.result.SuccessResult; -import com.cm.common.utils.HashMapUtil; -import com.cm.common.utils.jdbc.JdbcUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.List; import java.util.Map; @@ -40,36 +40,51 @@ public class TableServiceImpl extends AbstractService implements ITableService { @Override public synchronized SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException { Connection connection = getConnectionFromSession(); - if (connection == null) { - throw new SearchException("数据库连接无效"); - } String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); String tableName = tableVO.getTableName(); TableDTO tableDTO = getTableByTableName(tableName); if (tableDTO != null) { throw new SaveException("表已存在"); } - try { - connection.setAutoCommit(false); - tableDao.saveTable(connection, databaseName, tableName, tableVO.getTableColumns()); - connection.commit(); - } catch (SQLException e) { - LOG.error(e.getMessage(), e); - try { - connection.rollback(); - } catch (SQLException ex) { - LOG.error(ex.getMessage(), ex); - } - } + tableDao.saveTable(connection, databaseName, tableName, tableVO.getTableColumns()); return new SuccessResult(); } @Override - public TableDTO getTableByTableName(String tableName) { + public SuccessResult removeTable(String tableName) throws RemoveException { + Connection connection = getConnectionFromSession(); + String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); + tableDao.removeTable(connection, databaseName, tableName); + return new SuccessResult(); + } + + @Override + public SuccessResult saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException { + Connection connection = getConnectionFromSession(); + String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); + tableDao.saveTableColumn(connection, databaseName, tableName, tableColumnVO); + return new SuccessResult(); + } + + @Override + public SuccessResult updateTableColumn(String tableName, TableOldColumnVO tableOldColumnVO) throws UpdateException { + Connection connection = getConnectionFromSession(); + String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); + tableDao.updateTableColumn(connection, databaseName, tableName, tableOldColumnVO); + return new SuccessResult(); + } + + @Override + public SuccessResult updateTableColumnType(String tableName, TableColumnVO tableColumnVO) throws UpdateException { + Connection connection = getConnectionFromSession(); + String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); + tableDao.updateTableColumnType(connection, databaseName, tableName, tableColumnVO); + return new SuccessResult(); + } + + @Override + public TableDTO getTableByTableName(String tableName) throws SearchException { Connection connection = getConnectionFromSession(); - if (connection == null) { - throw new SearchException("数据库连接无效"); - } String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); return tableDao.getTableByTableName(connection, databaseName, tableName); } @@ -77,19 +92,13 @@ public class TableServiceImpl extends AbstractService implements ITableService { @Override public List listTables(Map params) throws SearchException { Connection connection = getConnectionFromSession(); - if (connection == null) { - throw new SearchException("数据库连接无效"); - } params.put("databaseName", getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString()); return tableDao.listTables(connection, params); } @Override - public List listTableColumns(Map params) { + public List listTableColumns(Map params) throws SearchException { Connection connection = getConnectionFromSession(); - if (connection == null) { - throw new SearchException("数据库连接无效"); - } params.put("databaseName", getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString()); return tableDao.listTableColumns(connection, params); } @@ -100,6 +109,10 @@ public class TableServiceImpl extends AbstractService implements ITableService { * @return */ private Connection getConnectionFromSession() { - return (Connection) getHttpSession().getAttribute(IDatabaseService.DATABASE_CONNECTION); + Object connectionObj = getHttpSession().getAttribute(IDatabaseService.DATABASE_CONNECTION); + if (connectionObj == null) { + throw new SearchException("数据库连接无效"); + } + return (Connection) connectionObj; } }