From 9432db6ad1d630645e33f2fef3698249769de476 Mon Sep 17 00:00:00 2001 From: caoshd Date: Fri, 5 Apr 2024 19:04:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=20H2=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../caoshd/otone/builder/ColumnBuilder.java | 23 ++++--- .../caoshd/otone/builder/TableBuilder.java | 14 ++-- .../space/caoshd/otone/helper/SqlHelper.java | 64 +++++++++++++++++++ .../space/caoshd/otone/helper/SqlUtils.java | 63 ------------------ .../space/caoshd/otone/util/CfgUtils.java | 14 ++++ .../space/caoshd/otone/util/TypeUtils.java | 20 +++--- .../space/caoshd/otone/util/DBUtilsTest.java | 4 +- src/test/resources/config/config.properties | 1 + .../resources/config/datasource.properties | 3 +- 10 files changed, 121 insertions(+), 91 deletions(-) create mode 100644 src/main/java/space/caoshd/otone/helper/SqlHelper.java delete mode 100644 src/main/java/space/caoshd/otone/helper/SqlUtils.java diff --git a/pom.xml b/pom.xml index a24a722..4d9ca07 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,12 @@ mysql-connector-j 8.2.0 + + com.h2database + h2 + 2.2.224 + test + ch.qos.logback logback-classic diff --git a/src/main/java/space/caoshd/otone/builder/ColumnBuilder.java b/src/main/java/space/caoshd/otone/builder/ColumnBuilder.java index ce7ca10..8a6f5e2 100644 --- a/src/main/java/space/caoshd/otone/builder/ColumnBuilder.java +++ b/src/main/java/space/caoshd/otone/builder/ColumnBuilder.java @@ -3,7 +3,8 @@ package space.caoshd.otone.builder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import space.caoshd.otone.entity.ColumnInfo; -import space.caoshd.otone.helper.SqlUtils; +import space.caoshd.otone.helper.SqlHelper; +import space.caoshd.otone.util.CfgUtils; import space.caoshd.otone.util.DBUtils; import space.caoshd.otone.util.JsonUtils; import space.caoshd.otone.util.StrUtils; @@ -18,6 +19,8 @@ public class ColumnBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(ColumnBuilder.class); + private static final SqlHelper SQL_HELPER = new SqlHelper(CfgUtils.getSqlConfigFile()); + private final String schemaName; private final String tableName; @@ -27,7 +30,7 @@ public class ColumnBuilder { } public List build() { - String sql = SqlUtils.getColumnSql(); + String sql = SQL_HELPER.getColumnSql(); List params = Arrays.asList(schemaName, tableName); List> columns = DBUtils.list(sql, params); @@ -40,25 +43,25 @@ public class ColumnBuilder { private ColumnInfo createColumnInfo(Map column) { ColumnInfo result = new ColumnInfo(); - String columnName = SqlUtils.getLabelColumnName(); + String columnName = SQL_HELPER.getLabelColumnName(); result.setColumnName(column.get(columnName)); String fieldName = StrUtils.toCamelCase(result.getColumnName(), false); result.setFieldName(fieldName); String fieldNameCap = StrUtils.toCamelCase(result.getColumnName(), true); result.setFieldNameCap(fieldNameCap); - String columnComment = SqlUtils.getLabelColumnComment(); + String columnComment = SQL_HELPER.getLabelColumnComment(); result.setComment(column.get(columnComment)); - String dbType = SqlUtils.getLabelDataType(); + String dbType = SQL_HELPER.getLabelDataType(); result.setDbType(column.get(dbType)); - String javaType = TypeUtils.toJavaDataType(result.getDbType()); + String javaType = TypeUtils.toJavaType(result.getDbType()); result.setJavaType(javaType); - String autoIncrement = SqlUtils.getLabelAutoIncrement(); - result.setAutoIncrement(SqlUtils.VALUE_AUTO_INCREMENT_YES.equals(column.get(autoIncrement))); + String autoIncrement = SQL_HELPER.getLabelAutoIncrement(); + result.setAutoIncrement(SqlHelper.VALUE_AUTO_INCREMENT_YES.equals(column.get(autoIncrement))); - String primary = SqlUtils.getLabelPrimary(); - result.setPrimary(SqlUtils.VALUE_PRIMARY_YES.equals(column.get(primary))); + String primary = SQL_HELPER.getLabelPrimary(); + result.setPrimary(SqlHelper.VALUE_PRIMARY_YES.equals(column.get(primary))); LOGGER.debug(JsonUtils.toJson(result)); return result; diff --git a/src/main/java/space/caoshd/otone/builder/TableBuilder.java b/src/main/java/space/caoshd/otone/builder/TableBuilder.java index 8cf447e..4805056 100644 --- a/src/main/java/space/caoshd/otone/builder/TableBuilder.java +++ b/src/main/java/space/caoshd/otone/builder/TableBuilder.java @@ -4,7 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import space.caoshd.otone.entity.ColumnInfo; import space.caoshd.otone.entity.TableInfo; -import space.caoshd.otone.helper.SqlUtils; +import space.caoshd.otone.helper.SqlHelper; import space.caoshd.otone.util.CfgUtils; import space.caoshd.otone.util.DBUtils; import space.caoshd.otone.util.JsonUtils; @@ -18,16 +18,18 @@ public class TableBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class); + private static final SqlHelper SQL_HELPER = new SqlHelper(CfgUtils.getSqlConfigFile()); + public List build() { List result = new ArrayList<>(); - String sql = SqlUtils.getTableSql(); + String sql = SQL_HELPER.getTableSql(); List> tables = DBUtils.list(sql); for (Map table : tables) { List tableNames = CfgUtils.getDatabaseTableIncludes(); if (tableNames.isEmpty()) { result.add(createTableInfo(table)); } else { - if (tableNames.contains(SqlUtils.getLabelTableName())) { + if (tableNames.contains(SQL_HELPER.getLabelTableName())) { result.add(createTableInfo(table)); } } @@ -38,15 +40,15 @@ public class TableBuilder { private TableInfo createTableInfo(Map table) { TableInfo result = new TableInfo(); - String tableSchemaLabel = SqlUtils.getLabelTableSchema(); + String tableSchemaLabel = SQL_HELPER.getLabelTableSchema(); String tableSchema = table.get(tableSchemaLabel); result.setTableSchema(tableSchema); - String tableCommentLabel = SqlUtils.getLabelTableComment(); + String tableCommentLabel = SQL_HELPER.getLabelTableComment(); String comment = table.get(tableCommentLabel); result.setComment(comment); - String tableNameLabel = SqlUtils.getLabelTableName(); + String tableNameLabel = SQL_HELPER.getLabelTableName(); String tableName = table.get(tableNameLabel); result.setTableName(tableName); diff --git a/src/main/java/space/caoshd/otone/helper/SqlHelper.java b/src/main/java/space/caoshd/otone/helper/SqlHelper.java new file mode 100644 index 0000000..a174b09 --- /dev/null +++ b/src/main/java/space/caoshd/otone/helper/SqlHelper.java @@ -0,0 +1,64 @@ +package space.caoshd.otone.helper; + +public class SqlHelper { + + public static final String TABLE_SQL = "table.sql"; + public static final String COLUMN_SQL = "column.sql"; + 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_DATA_TYPE = "label.data_type"; + public static final String LABEL_AUTO_INCREMENT = "label.auto_increment"; + public static final String LABEL_PRIMARY = "label.primary"; + public static final String VALUE_AUTO_INCREMENT_YES = "1"; + public static final String VALUE_PRIMARY_YES = "1"; + + private final PropHelper config; + + public SqlHelper(String configFilePath) { + this.config = new PropHelper(configFilePath); + } + + public String getTableSql() { + return config.getString(TABLE_SQL); + } + + public String getColumnSql() { + return config.getString(COLUMN_SQL); + } + + public String getLabelTableName() { + return config.getString(LABEL_TABLE_NAME); + } + + public String getLabelTableSchema() { + return config.getString(LABEL_TABLE_SCHEMA); + } + + public String getLabelTableComment() { + return config.getString(LABEL_TABLE_COMMENT); + } + + public String getLabelColumnComment() { + return config.getString(LABEL_COLUMN_COMMENT); + } + + public String getLabelColumnName() { + return config.getString(LABEL_COLUMN_NAME); + } + + public String getLabelDataType() { + return config.getString(LABEL_DATA_TYPE); + } + + public String getLabelAutoIncrement() { + return config.getString(LABEL_AUTO_INCREMENT); + } + + public String getLabelPrimary() { + return config.getString(LABEL_PRIMARY); + } + +} diff --git a/src/main/java/space/caoshd/otone/helper/SqlUtils.java b/src/main/java/space/caoshd/otone/helper/SqlUtils.java deleted file mode 100644 index b4ba670..0000000 --- a/src/main/java/space/caoshd/otone/helper/SqlUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -package space.caoshd.otone.helper; - -import space.caoshd.otone.util.PathConsts; - -public class SqlUtils { - - public static final String TABLE_SQL = "table.sql"; - public static final String COLUMN_SQL = "column.sql"; - 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_DATA_TYPE = "label.data_type"; - public static final String LABEL_AUTO_INCREMENT = "label.auto_increment"; - public static final String LABEL_PRIMARY = "label.primary"; - public static final String VALUE_AUTO_INCREMENT_YES = "1"; - public static final String VALUE_PRIMARY_YES = "1"; - private static final PropHelper CONFIG = new PropHelper(PathConsts.MYSQL_PROPERTIES_PATH); - - private SqlUtils() {} - - public static String getTableSql() { - return CONFIG.getString(TABLE_SQL); - } - - public static String getColumnSql() { - return CONFIG.getString(COLUMN_SQL); - } - - public static String getLabelTableName() { - return CONFIG.getString(LABEL_TABLE_NAME); - } - - public static String getLabelTableSchema() { - return CONFIG.getString(LABEL_TABLE_SCHEMA); - } - - public static String getLabelTableComment() { - return CONFIG.getString(LABEL_TABLE_COMMENT); - } - - public static String getLabelColumnComment() { - return CONFIG.getString(LABEL_COLUMN_COMMENT); - } - - public static String getLabelColumnName() { - return CONFIG.getString(LABEL_COLUMN_NAME); - } - - public static String getLabelDataType() { - return CONFIG.getString(LABEL_DATA_TYPE); - } - - public static String getLabelAutoIncrement() { - return CONFIG.getString(LABEL_AUTO_INCREMENT); - } - - public static String getLabelPrimary() { - return CONFIG.getString(LABEL_PRIMARY); - } - -} diff --git a/src/main/java/space/caoshd/otone/util/CfgUtils.java b/src/main/java/space/caoshd/otone/util/CfgUtils.java index 2096623..ade6604 100644 --- a/src/main/java/space/caoshd/otone/util/CfgUtils.java +++ b/src/main/java/space/caoshd/otone/util/CfgUtils.java @@ -12,6 +12,7 @@ public class CfgUtils { public static final String DATABASE_TABLE_PREFIXES = "database.table.prefixes"; public static final String DATABASE_TABLE_INVALID_COLUMNS = "database.table.invalid_columns"; public static final String DATABASE_TABLE_INVALID_VALUES = "database.table.invalid_values"; + public static final String DATABASE_TYPE = "database.type"; // 输出配置 public static final String OUTPUT_COVER_IF_EXIST = "output.cover_if_exists"; @@ -104,12 +105,25 @@ public class CfgUtils { public static final String DEFAULT_TEMPLATE_PATH_SERVICE_INTF = "template/service.intf.vm"; public static final String DEFAULT_TEMPLATE_PATH_MAPPER = "template/mapper.vm"; public static final String DEFAULT_TEMPLATE_PATH_MAPPER_XML = "template/mapper.xml.vm"; + + public static final String DEFAULT_SQL_PATH_TEMPLATE = "config/%s.properties"; + private static final PropHelper CONFIG = new PropHelper(PathConsts.CONFIG_PROPERTIES_PATH); + private static final String JAVA_FILE_SUFFIX = ".java"; // 私有化构造器 private CfgUtils() {} + public static String getDatabaseType() { + return CONFIG.getString(DATABASE_TYPE); + } + + public static String getSqlConfigFile() { + String databaseType = getDatabaseType(); + return String.format(DEFAULT_SQL_PATH_TEMPLATE, databaseType); + } + public static List getDatabaseTableIncludes() { return CONFIG.getStringList(DATABASE_TABLE_INCLUDES); } diff --git a/src/main/java/space/caoshd/otone/util/TypeUtils.java b/src/main/java/space/caoshd/otone/util/TypeUtils.java index fa3276c..d8a040b 100644 --- a/src/main/java/space/caoshd/otone/util/TypeUtils.java +++ b/src/main/java/space/caoshd/otone/util/TypeUtils.java @@ -15,12 +15,13 @@ public class TypeUtils { public static final List SQL_TYPE_STRING = Arrays.asList( "char", "varchar", + "character varying", "text", "mediumtext", "longtext" ); - public static final List SQL_TYPE_INT = Arrays.asList("int", "tinyint"); + public static final List SQL_TYPE_INT = Arrays.asList("integer", "int", "tinyint"); public static final List SQL_TYPE_LONG = Collections.singletonList("bigint"); @@ -34,26 +35,27 @@ public class TypeUtils { public static final String JAVA_TYPE_DATE = "Date"; - public static String toJavaDataType(String sqlDataType) { - if (SQL_TYPE_STRING.contains(sqlDataType)) { + public static String toJavaType(String sqlType) { + String sqlTypeLowerCase = sqlType.toLowerCase(); + if (SQL_TYPE_STRING.contains(sqlTypeLowerCase)) { return JAVA_TYPE_STRING; } - if (SQL_TYPE_INT.contains(sqlDataType)) { + if (SQL_TYPE_INT.contains(sqlTypeLowerCase)) { return JAVA_TYPE_INTEGER; } - if (SQL_TYPE_LONG.contains(sqlDataType)) { + if (SQL_TYPE_LONG.contains(sqlTypeLowerCase)) { return JAVA_TYPE_LONG; } - if (SQL_TYPE_DATE.contains(sqlDataType)) { + if (SQL_TYPE_DATE.contains(sqlTypeLowerCase)) { return JAVA_TYPE_DATE; } - if (SQL_TYPE_DATE_TIME.contains(sqlDataType)) { + if (SQL_TYPE_DATE_TIME.contains(sqlTypeLowerCase)) { return JAVA_TYPE_DATE; } - if (SQL_TYPE_DECIMAL.contains(sqlDataType)) { + if (SQL_TYPE_DECIMAL.contains(sqlTypeLowerCase)) { return JAVA_TYPE_DECIMAL; } - throw new RuntimeException("unknown sql type:" + sqlDataType); + throw new RuntimeException("unknown sql type:" + sqlType); } } diff --git a/src/test/java/space/caoshd/otone/util/DBUtilsTest.java b/src/test/java/space/caoshd/otone/util/DBUtilsTest.java index 2ee308b..77f274e 100644 --- a/src/test/java/space/caoshd/otone/util/DBUtilsTest.java +++ b/src/test/java/space/caoshd/otone/util/DBUtilsTest.java @@ -2,7 +2,7 @@ package space.caoshd.otone.util; import org.junit.Assert; import org.junit.Test; -import space.caoshd.otone.helper.SqlUtils; +import space.caoshd.otone.helper.SqlHelper; import java.util.List; import java.util.Map; @@ -11,7 +11,7 @@ public class DBUtilsTest { @Test public void executeQuery() { - String tableSql = SqlUtils.getTableSql(); + String tableSql = new SqlHelper(CfgUtils.getSqlConfigFile()).getTableSql(); List> maps = DBUtils.list(tableSql); Assert.assertFalse(maps.isEmpty()); } diff --git a/src/test/resources/config/config.properties b/src/test/resources/config/config.properties index 7785756..bff7cf5 100644 --- a/src/test/resources/config/config.properties +++ b/src/test/resources/config/config.properties @@ -1,5 +1,6 @@ project.path.base=D:/workspace/otono-debug/ package.name.base=space.caoshd.navigator +database.type=h2 database.table.includes= database.table.prefixes=t_,m_ output.cover_if_exists=true diff --git a/src/test/resources/config/datasource.properties b/src/test/resources/config/datasource.properties index d40a2d0..03defdf 100644 --- a/src/test/resources/config/datasource.properties +++ b/src/test/resources/config/datasource.properties @@ -1,4 +1,5 @@ username=root password=otone@132! -url=jdbc:mysql://121.43.48.195:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai +#url=jdbc:mysql://121.43.48.195:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai +url=jdbc:h2:~/h2db/otone driver-class-name=com.mysql.cj.jdbc.Driver \ No newline at end of file