新增: 目标路径文件已存在时 默认不覆盖

master
曹世达 6 months ago
parent 7553b636cb
commit c8ca3d2007

@ -5,5 +5,5 @@ import space.caoshd.otone.entity.ColumnInfo;
import java.util.Map;
public interface ColumnCreateAware {
void addon(ColumnInfo tableInfo, Map<String,Object> addInfo);
void addon(ColumnInfo tableInfo, Map<String, Object> addInfo);
}

@ -2,15 +2,15 @@ package space.caoshd.otone.builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.ColumnInfo;
import space.caoshd.otone.aware.ColumnCreateAware;
import space.caoshd.otone.entity.ColumnInfo;
import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.TypeUtils;
import space.caoshd.otone.util.DBUtils;
import space.caoshd.otone.util.JsonUtils;
import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.SqlConsts;
import space.caoshd.otone.util.StrUtils;
import space.caoshd.otone.util.TypeUtils;
import java.util.ArrayList;
import java.util.Arrays;
@ -21,25 +21,25 @@ public class ColumnBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(ColumnBuilder.class);
private static final PropTools sqlProperties =
private static final PropTools CONFIG =
new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
private final String schemaName;
private final String tableName;
private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
public ColumnBuilder(String schemaName, String tableName) {
this.schemaName = schemaName;
this.tableName = tableName;
}
private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
public ColumnBuilder addColumnAddonAble(ColumnCreateAware columnCreateAware) {
this.columnCreateAwares.add(columnCreateAware);
return this;
}
public List<ColumnInfo> build() {
String sql = sqlProperties.getString(SqlConsts.COLUMN_SQL_SCHEMA_TABLE);
String sql = CONFIG.getString(SqlConsts.COLUMN_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName);
List<Map<String, String>> columns = DBUtils.list(sql, params);
@ -52,17 +52,17 @@ public class ColumnBuilder {
private ColumnInfo createColumnInfo(Map<String, String> column) {
ColumnInfo result = new ColumnInfo();
String columnComment = sqlProperties.getString(SqlConsts.LABEL_COLUMN_COMMENT);
String columnComment = CONFIG.getString(SqlConsts.LABEL_COLUMN_COMMENT);
result.setComment(column.get(columnComment));
String columnName = sqlProperties.getString(SqlConsts.LABEL_COLUMN_NAME);
String columnName = CONFIG.getString(SqlConsts.LABEL_COLUMN_NAME);
result.setColumnName(column.get(columnName));
String propertyName = StrUtils.toCamelCase(result.getColumnName(), false);
result.setPropertyName(propertyName);
String sqlDataType = sqlProperties.getString(SqlConsts.LABEL_DATA_TYPE);
String sqlDataType = CONFIG.getString(SqlConsts.LABEL_DATA_TYPE);
result.setSqlDataType(column.get(sqlDataType));
String javaDataType = TypeUtils.toJavaDataType(result.getSqlDataType());
result.setJavaDataType(javaDataType);
String extra = sqlProperties.getString(SqlConsts.LABEL_EXTRA);
String extra = CONFIG.getString(SqlConsts.LABEL_EXTRA);
result.setAutoIncrement(column.get(extra).contains(SqlConsts.VALUE_AUTO_INCREMENT));
for (ColumnCreateAware columnCreateAware : columnCreateAwares) {

@ -18,12 +18,12 @@ public class IndexBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(IndexBuilder.class);
private static final PropTools sqlProperties =
private static final PropTools CONFIG =
new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
public static List<IndexInfo> loadIndexInfo(String schemaName, String tableName) {
String sql = sqlProperties.getString(SqlConsts.INDEX_SQL_SCHEMA_TABLE);
String sql = CONFIG.getString(SqlConsts.INDEX_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName);
List<Map<String, String>> indexes = DBUtils.list(sql, params);
@ -37,13 +37,13 @@ public class IndexBuilder {
private static IndexInfo createIndexInfo(Map<String, String> index) {
IndexInfo result = new IndexInfo();
String columnName = sqlProperties.getString(SqlConsts.LABEL_COLUMN_NAME);
String columnName = CONFIG.getString(SqlConsts.LABEL_COLUMN_NAME);
result.setColumnName(index.get(columnName));
String indexName = sqlProperties.getString(SqlConsts.LABEL_INDEX_NAME);
String indexName = CONFIG.getString(SqlConsts.LABEL_INDEX_NAME);
result.setIndexName(index.get(indexName));
String nonUnique = sqlProperties.getString(SqlConsts.LABEL_NON_UNIQUE);
String nonUnique = CONFIG.getString(SqlConsts.LABEL_NON_UNIQUE);
result.setNonUnique(index.get(nonUnique));
LOGGER.debug(JsonUtils.toJson(result));

@ -5,23 +5,22 @@ import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.CfgConsts;
import space.caoshd.otone.util.FileUtils;
import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.PathUtils;
import space.caoshd.otone.util.VelocityUtils;
import java.nio.file.Paths;
import java.util.List;
public class PoBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PoBuilder.class);
private static final PropTools propTools = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private final List<TableInfo> tableInfoList;
private String templatePath = PathConsts.PO_TEMPLATE_PATH;
private String outputBase = propTools.getString(CfgConsts.OUTPUT_BASE);
private String outputPo = propTools.getString(CfgConsts.OUTPUT_PO);
public PoBuilder(List<TableInfo> tableInfoList) {
this.tableInfoList = tableInfoList;
@ -32,21 +31,32 @@ public class PoBuilder {
return this;
}
public PoBuilder setOutputBase(String outputBase) {
this.outputBase = outputBase;
return this;
}
public PoBuilder setOutputPo(String outputPo) {
this.outputPo = outputPo;
return this;
}
public void build() {
for (TableInfo tableInfo : tableInfoList) {
String beanName = tableInfo.getBeanName();
String outputPath = Paths.get(outputBase, outputPo, beanName + ".java").toString();
VelocityUtils.render(CfgConsts.VALUE_TABLE, tableInfo, templatePath, outputPath);
String packagePath = PathUtils.getPoPath();
FileUtils.mkdir(packagePath);
String outputPath = PathUtils.getPoJavaPath(beanName);
if (FileUtils.exists(outputPath)) {
if (CONFIG.getBoolean(CfgConsts.OUTPUT_COVER_IF_EXIST)) {
VelocityUtils.render(
CfgConsts.VALUE_TABLE,
tableInfo,
templatePath,
outputPath
);
} else {
LOGGER.warn(
"po file already exists: {}, "
+ "please remove it or enable auto cover exist file,"
+ " config.properties should like [output.cover_if_exists=true]",
outputPath
);
}
} else {
VelocityUtils.render(CfgConsts.VALUE_TABLE, tableInfo, templatePath, outputPath);
}
}
}
}

@ -2,19 +2,19 @@ package space.caoshd.otone.builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import space.caoshd.otone.aware.ColumnCreateAware;
import space.caoshd.otone.aware.TableCreateAware;
import space.caoshd.otone.entity.ColumnInfo;
import space.caoshd.otone.entity.IndexInfo;
import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.aware.ColumnCreateAware;
import space.caoshd.otone.aware.TableCreateAware;
import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.CfgConsts;
import space.caoshd.otone.util.TypeUtils;
import space.caoshd.otone.util.DBUtils;
import space.caoshd.otone.util.JsonUtils;
import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.SqlConsts;
import space.caoshd.otone.util.StrUtils;
import space.caoshd.otone.util.TypeUtils;
import java.util.ArrayList;
import java.util.Arrays;
@ -29,20 +29,18 @@ public class TableBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
private static final PropTools sqlProperties = new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
private static final PropTools MYSQL_CONFIG = new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
private static final PropTools configProperties =
new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private final List<TableCreateAware> tableCreateAwares = new ArrayList<>();
private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
public TableBuilder addTableAddonAble(TableCreateAware tableCreateAware) {
this.tableCreateAwares.add(tableCreateAware);
return this;
}
private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
public TableBuilder addColumnAddonAble(ColumnCreateAware columnCreateAware) {
this.columnCreateAwares.add(columnCreateAware);
return this;
@ -50,9 +48,9 @@ public class TableBuilder {
public List<TableInfo> build() {
List<TableInfo> result = new ArrayList<>();
List<String> schemaNames = configProperties.getStrings(CfgConsts.SCHEMA_NAMES);
List<String> schemaNames = CONFIG.getStrings(CfgConsts.SCHEMA_NAMES);
if (schemaNames.isEmpty()) {
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL);
String sql = MYSQL_CONFIG.getString(SqlConsts.TABLE_SQL);
List<Map<String, String>> tables = DBUtils.list(sql);
for (Map<String, String> table : tables) {
result.add(createTableInfo(table));
@ -68,9 +66,9 @@ public class TableBuilder {
public List<TableInfo> build(String schemaName) {
List<TableInfo> result = new ArrayList<>();
List<String> tableNames = configProperties.getStrings(CfgConsts.TABLE_NAMES);
List<String> tableNames = CONFIG.getStrings(CfgConsts.TABLE_NAMES);
if (tableNames.isEmpty()) {
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA);
String sql = MYSQL_CONFIG.getString(SqlConsts.TABLE_SQL_SCHEMA);
List<String> params = Collections.singletonList(schemaName);
List<Map<String, String>> tables = DBUtils.list(sql, params);
for (Map<String, String> table : tables) {
@ -88,7 +86,7 @@ public class TableBuilder {
}
public TableInfo build(String schemaName, String tableName) {
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
String sql = MYSQL_CONFIG.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName);
Map<String, String> table = DBUtils.one(sql, params);
if (Objects.isNull(table)) {
@ -101,15 +99,15 @@ public class TableBuilder {
private TableInfo createTableInfo(Map<String, String> table) {
TableInfo result = new TableInfo();
String tableSchemaLabel = sqlProperties.getString(SqlConsts.LABEL_TABLE_SCHEMA);
String tableSchemaLabel = MYSQL_CONFIG.getString(SqlConsts.LABEL_TABLE_SCHEMA);
String tableSchema = table.get(tableSchemaLabel);
result.setTableSchema(tableSchema);
String tableCommentLabel = sqlProperties.getString(SqlConsts.LABEL_TABLE_COMMENT);
String tableCommentLabel = MYSQL_CONFIG.getString(SqlConsts.LABEL_TABLE_COMMENT);
String comment = table.get(tableCommentLabel);
result.setComment(comment);
String tableNameLabel = sqlProperties.getString(SqlConsts.LABEL_TABLE_NAME);
String tableNameLabel = MYSQL_CONFIG.getString(SqlConsts.LABEL_TABLE_NAME);
String tableName = table.get(tableNameLabel);
result.setTableName(tableName);
@ -125,16 +123,19 @@ public class TableBuilder {
result.setDecimalExists(columnInfo.stream()
.anyMatch(column -> TypeUtils.JAVA_TYPE_DECIMAL.equals(column.getJavaDataType())));
List<IndexInfo> indexInfoList = IndexBuilder.loadIndexInfo(result.getTableSchema(),
List<IndexInfo> indexInfoList = IndexBuilder.loadIndexInfo(
result.getTableSchema(),
result.getTableName()
);
Map<String, List<ColumnInfo>> indexInfo = new HashMap<>();
indexInfoList.forEach(index -> {
List<ColumnInfo> indexColumnInfoList = indexInfo.computeIfAbsent(index.getIndexName(),
List<ColumnInfo> indexColumnInfoList = indexInfo.computeIfAbsent(
index.getIndexName(),
k -> new ArrayList<>()
);
ColumnInfo columnInfoByColumnName = getColumnInfoByColumnName(columnInfo,
ColumnInfo columnInfoByColumnName = getColumnInfoByColumnName(
columnInfo,
index.getColumnName()
);
indexColumnInfoList.add(columnInfoByColumnName);
@ -168,7 +169,7 @@ public class TableBuilder {
}
private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) {
List<String> tablePrefixes = configProperties.getStrings(CfgConsts.TABLE_PREFIXIES);
List<String> tablePrefixes = CONFIG.getStrings(CfgConsts.TABLE_PREFIXIES);
for (String tablePrefix : tablePrefixes) {
tableNameWithoutPrefix = StrUtils.removePrefix(tableNameWithoutPrefix, tablePrefix);

@ -5,20 +5,14 @@ import java.util.Map;
public class ColumnInfo {
private final Map<String, Object> addonInfo = new HashMap<>();
private String columnName;
private String propertyName;
private String sqlDataType;
private String javaDataType;
private String comment;
private Boolean autoIncrement;
private final Map<String, Object> addonInfo = new HashMap<>();
public String getColumnName() {
return columnName;
}

@ -8,24 +8,16 @@ import java.util.Map;
public class TableInfo {
private final Map<String, Object> addonInfo = new HashMap<>();
private String comment;
private String tableSchema;
private String tableName;
private String beanName;
private Boolean dateExists;
private Boolean decimalExists;
private List<ColumnInfo> columnInfo = new ArrayList<>();
private Map<String, List<ColumnInfo>> indexInfo = new LinkedHashMap<>();
private final Map<String, Object> addonInfo = new HashMap<>();
public String getComment() {
return comment;
}

@ -55,7 +55,11 @@ public class PropTools {
}
public Boolean getBoolean(String key) {
return Boolean.valueOf(propertyMap.get(key));
String value = propertyMap.get(key);
if (value == null) {
return Boolean.FALSE;
}
return Boolean.valueOf(value);
}
public Long getLong(String key) {

@ -2,88 +2,47 @@ package space.caoshd.otone.util;
public class CfgConsts {
private CfgConsts() {}
public static final String SCHEMA_NAMES = "database.schemas";
public static final String TABLE_NAMES = "table.names";
public static final String TABLE_PREFIXIES = "table.prefixes";
public static final String BEAN_SUFFIX = "bean.suffix";
public static final String OUTPUT_BASE = "output.base";
public static final String OUTPUT_PO = "output.po";
public static final String OUTPUT_COVER_IF_EXIST = "output.cover_if_exists";
public static final String PROJECT_PATH_BASE = "project.path.base";
public static final String PROJECT_PATH_JAVA = "project.path.java";
public static final String PROJECT_PATH_RESOURCES = "project.path.resources";
public static final String PACKAGE_BASE = "package.base";
public static final String PACKAGE_PO = "package.po";
public static final String PACKAGE_MAPPER = "package.mapper";
public static final String PACKAGE_SERVICE = "package.service";
public static final String PACKAGE_CONTROLLER = "package.controller";
public static final String PACKAGE_QUERY = "package.query";
public static final String PACKAGE_FORM = "package.form";
public static final String PACKAGE_VIEW = "package.view";
public static final String CLASS_NAME_SUFFIX_PO = "classname.suffix.po";
public static final String CLASS_NAME_SUFFIX_MAPPER = "classname.suffix.mapper";
public static final String CLASS_NAME_SUFFIX_SERVICE = "classname.suffix.service";
public static final String CLASS_NAME_SUFFIX_QUERY = "classname.suffix.query";
public static final String CLASS_NAME_SUFFIX_FORM = "classname.suffix.form";
public static final String CLASS_NAME_SUFFIX_VIEW = "classname.suffix.view";
public static final String CLASS_NAME_SUFFIX_CONTROLLER = "classname.suffix.controller";
public static final String VALUE_TABLE = "table";
public static final String DEFAULT_PROJECT_PATH_JAVA = "src/main/java";
public static final String DEFAULT_PROJECT_PATH_RESOURCES = "src/main/resources";
public static final String DEFAULT_PACKAGE_PO = "repository.po";
public static final String DEFAULT_PACKAGE_MAPPER = "repository.mapper";
public static final String DEFAULT_PACKAGE_SERVICE = "service";
public static final String DEFAULT_PACKAGE_QUERY = "controller.query";
public static final String DEFAULT_PACKAGE_FORM = "controller.form";
public static final String DEFAULT_PACKAGE_VIEW = "controller.view";
public static final String DEFAULT_PACKAGE_CONTROLLER = "controller";
public static final String DEFAULT_CLASS_NAME_SUFFIX_PO = "";
public static final String DEFAULT_CLASS_NAME_SUFFIX_MAPPER = "Mapper";
public static final String DEFAULT_CLASS_NAME_SUFFIX_SERVICE = "Service";
public static final String DEFAULT_CLASS_NAME_SUFFIX_QUERY = "Query";
public static final String DEFAULT_CLASS_NAME_SUFFIX_FORM = "Form";
public static final String DEFAULT_CLASS_NAME_SUFFIX_VIEW = "View";
public static final String DEFAULT_CLASS_NAME_SUFFIX_CONTROLLER = "Controller";
private CfgConsts() {}
}

@ -0,0 +1,35 @@
package space.caoshd.otone.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
public class FileUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
private FileUtils() {}
public static void mkdir(String path) {
File file = new File(path);
if (file.exists() && file.isDirectory()) {
return;
}
if (file.mkdirs()) {
try {
LOGGER.info("mkdir: {}", file.getCanonicalPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
LOGGER.info("mkdir error: {}", path);
}
}
public static boolean exists(String path) {
File file = new File(path);
return file.exists();
}
}

@ -2,19 +2,14 @@ package space.caoshd.otone.util;
public class PathConsts {
public PathConsts() {}
public static final String DATASOURCE_PROPERTIES_PATH = "config/datasource.properties";
public static final String CONFIG_PROPERTIES_PATH = "config/config.properties";
public static final String MYSQL_PROPERTIES_PATH = "config/mysql.properties";
public static final String VELOCITY_PROPERTIES_PATH = "config/velocity.properties";
public static final String RESOURCES_PATH = "config/velocity.properties";
public static final String PO_TEMPLATE_PATH = "template/po.vm";
public PathConsts() {}
}

@ -3,37 +3,22 @@ package space.caoshd.otone.util;
public class SqlConsts {
private SqlConsts() {}
public static final String TABLE_SQL = "table.sql";
public static final String TABLE_SQL_SCHEMA = "table.sql.schema";
public static final String TABLE_SQL_SCHEMA_TABLE = "table.sql.schema.table";
public static final String COLUMN_SQL_SCHEMA_TABLE = "column.sql.schema.table";
public static final String INDEX_SQL_SCHEMA_TABLE = "index.sql.schema.table";
public static final String LABEL_TABLE_NAME = "label.table_name";
public static final String LABEL_TABLE_SCHEMA = "label.table_schema";
public static final String LABEL_TABLE_COMMENT = "label.table_comment";
public static final String LABEL_COLUMN_NAME = "label.column_name";
public static final String LABEL_COLUMN_COMMENT = "label.column_comment";
public static final String LABEL_INDEX_NAME = "label.index_name";
public static final String LABEL_NON_UNIQUE = "label.non_unique";
public static final String LABEL_NON_UNIQUE = "label.non_unique";
public static final String LABEL_DATA_TYPE = "label.data_type";
public static final String LABEL_EXTRA = "label.extra";
public static final String VALUE_AUTO_INCREMENT = "auto_increment";
private SqlConsts() {}
}

@ -28,11 +28,11 @@ public class TypeUtils {
public static final String JAVA_TYPE_INTEGER = "Integer";
public static final String JAVA_TYPE_LONG= "Long";
public static final String JAVA_TYPE_LONG = "Long";
public static final String JAVA_TYPE_DECIMAL= "BigDecimal";
public static final String JAVA_TYPE_DECIMAL = "BigDecimal";
public static final String JAVA_TYPE_DATE= "Date";
public static final String JAVA_TYPE_DATE = "Date";
public static String toJavaDataType(String sqlDataType) {
if (SQL_TYPE_STRING.contains(sqlDataType)) {

@ -3,22 +3,16 @@ package space.caoshd.otone.util;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import space.caoshd.otone.tool.PropTools;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
public class VelocityUtils {
public static void render(
String beanName, Object bean, String templatePath, String outputPath
) {
PropTools propTools = new PropTools(PathConsts.VELOCITY_PROPERTIES_PATH);
Properties properties = propTools.getProperties();
Velocity.init(properties);
VelocityContext context = new VelocityContext();
context.put(beanName, bean);

@ -8,10 +8,11 @@ import java.util.List;
class PoBuilderTest {
@Test
void build() {
void build(){
TableBuilder tableBuilder = new TableBuilder();
List<TableInfo> tableInfoList = tableBuilder.build();
PoBuilder poBuilder = new PoBuilder(tableInfoList);
poBuilder.build();
}
}

@ -1,9 +1,9 @@
project.path.base=D:/workspace/otono-debug/
package.base=space.caoshd.navigator
output.cover_if_exists=false
#database.schemas=otone
#table.names=
#table.prefixes=
table.prefixes=t_,m_
#project.path.java=src/main/java
#project.path.resources=src/main/resources
#project.path.mapper=

Loading…
Cancel
Save