新增: 加载自段
parent
02c9281c98
commit
17011d5d0b
@ -0,0 +1,58 @@
|
|||||||
|
package space.caoshd.otone.builder;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import space.caoshd.otone.entity.ColumnInfo;
|
||||||
|
import space.caoshd.otone.tool.PropertyTools;
|
||||||
|
import space.caoshd.otone.util.ConfigConsts;
|
||||||
|
import space.caoshd.otone.util.DataTypeUtils;
|
||||||
|
import space.caoshd.otone.util.DatabaseUtils;
|
||||||
|
import space.caoshd.otone.util.PropertiesConsts;
|
||||||
|
import space.caoshd.otone.util.SqlConsts;
|
||||||
|
import space.caoshd.otone.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class FieldBuilder {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
|
||||||
|
|
||||||
|
private static final PropertyTools sqlProperties =
|
||||||
|
new PropertyTools(PropertiesConsts.MYSQL_PROPERTIES_PATH);
|
||||||
|
|
||||||
|
private static final PropertyTools configProperties =
|
||||||
|
new PropertyTools(PropertiesConsts.CONFIG_PROPERTIES_PATH);
|
||||||
|
|
||||||
|
public static List<ColumnInfo> loadFieldInfo(String schemaName, String tableName) {
|
||||||
|
|
||||||
|
String sql = sqlProperties.getString(SqlConsts.COLUMN_SQL_SCHEMA_TABLE);
|
||||||
|
List<String> params = Arrays.asList(schemaName, tableName);
|
||||||
|
List<Map<String, String>> columns = DatabaseUtils.list(sql, params);
|
||||||
|
|
||||||
|
List<ColumnInfo> result = new ArrayList<>();
|
||||||
|
for (Map<String, String> field : columns) {
|
||||||
|
result.add(createFiledInfo(field));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ColumnInfo createFiledInfo(Map<String, String> column) {
|
||||||
|
ColumnInfo result = new ColumnInfo();
|
||||||
|
String columnComment = sqlProperties.getString(SqlConsts.LABEL_COLUMN_COMMENT);
|
||||||
|
result.setComment(column.get(columnComment));
|
||||||
|
String columnName = sqlProperties.getString(SqlConsts.LABEL_COLUMN_NAME);
|
||||||
|
result.setColumnName(column.get(columnName));
|
||||||
|
String propertyName = StringUtils.toCamelCase(result.getColumnName(), false);
|
||||||
|
result.setPropertyName(propertyName);
|
||||||
|
String sqlDataType = sqlProperties.getString(SqlConsts.LABEL_DATA_TYPE);
|
||||||
|
result.setSqlDataType(column.get(sqlDataType));
|
||||||
|
String javaDataType = DataTypeUtils.toJavaDataType(result.getSqlDataType());
|
||||||
|
result.setJavaDataType(javaDataType);
|
||||||
|
String extra = sqlProperties.getString(SqlConsts.LABEL_EXTRA);
|
||||||
|
result.setAutoIncrement(column.get(extra).contains(SqlConsts.VALUE_AUTO_INCREMENT));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
package space.caoshd.otone.entity;
|
||||||
|
|
||||||
|
public class ColumnInfo {
|
||||||
|
|
||||||
|
private String columnName;
|
||||||
|
|
||||||
|
private String propertyName;
|
||||||
|
|
||||||
|
private String sqlDataType;
|
||||||
|
|
||||||
|
private String javaDataType;
|
||||||
|
|
||||||
|
private String comment;
|
||||||
|
|
||||||
|
private Boolean autoIncrement;
|
||||||
|
|
||||||
|
public String getColumnName() {
|
||||||
|
return columnName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnName(String columnName) {
|
||||||
|
this.columnName = columnName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPropertyName() {
|
||||||
|
return propertyName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPropertyName(String propertyName) {
|
||||||
|
this.propertyName = propertyName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSqlDataType() {
|
||||||
|
return sqlDataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSqlDataType(String sqlDataType) {
|
||||||
|
this.sqlDataType = sqlDataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getJavaDataType() {
|
||||||
|
return javaDataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJavaDataType(String javaDataType) {
|
||||||
|
this.javaDataType = javaDataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getComment() {
|
||||||
|
return comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComment(String comment) {
|
||||||
|
this.comment = comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getAutoIncrement() {
|
||||||
|
return autoIncrement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAutoIncrement(Boolean autoIncrement) {
|
||||||
|
this.autoIncrement = autoIncrement;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuffer sb = new StringBuffer("ColumnInfo{");
|
||||||
|
sb.append("columnName='").append(columnName).append('\'');
|
||||||
|
sb.append(", propertyName='").append(propertyName).append('\'');
|
||||||
|
sb.append(", sqlType='").append(sqlDataType).append('\'');
|
||||||
|
sb.append(", javaType='").append(javaDataType).append('\'');
|
||||||
|
sb.append(", comment='").append(comment).append('\'');
|
||||||
|
sb.append(", autoIncrement=").append(autoIncrement);
|
||||||
|
sb.append('}');
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -1,64 +0,0 @@
|
|||||||
package space.caoshd.otone.entity;
|
|
||||||
|
|
||||||
public class FieldInfo {
|
|
||||||
|
|
||||||
private String fieldName;
|
|
||||||
|
|
||||||
private String propertyName;
|
|
||||||
|
|
||||||
private String sqlType;
|
|
||||||
|
|
||||||
private String javaType;
|
|
||||||
|
|
||||||
private String comment;
|
|
||||||
|
|
||||||
private Boolean autoIncrement;
|
|
||||||
|
|
||||||
public String getFieldName() {
|
|
||||||
return fieldName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFieldName(String fieldName) {
|
|
||||||
this.fieldName = fieldName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPropertyName() {
|
|
||||||
return propertyName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPropertyName(String propertyName) {
|
|
||||||
this.propertyName = propertyName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSqlType() {
|
|
||||||
return sqlType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSqlType(String sqlType) {
|
|
||||||
this.sqlType = sqlType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getJavaType() {
|
|
||||||
return javaType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setJavaType(String javaType) {
|
|
||||||
this.javaType = javaType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getComment() {
|
|
||||||
return comment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setComment(String comment) {
|
|
||||||
this.comment = comment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean getAutoIncrement() {
|
|
||||||
return autoIncrement;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAutoIncrement(Boolean autoIncrement) {
|
|
||||||
this.autoIncrement = autoIncrement;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,59 @@
|
|||||||
|
package space.caoshd.otone.util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DataTypeUtils {
|
||||||
|
|
||||||
|
public static final List<String> SQL_TYPE_DATE_TIME = Arrays.asList("datetime", "timestamp");
|
||||||
|
|
||||||
|
public static final List<String> SQL_TYPE_DATE = Collections.singletonList("date");
|
||||||
|
|
||||||
|
public static final List<String> SQL_TYPE_DECIMAL = Arrays.asList("decimal", "double", "float");
|
||||||
|
|
||||||
|
public static final List<String> SQL_TYPE_STRING = Arrays.asList(
|
||||||
|
"char",
|
||||||
|
"varchar",
|
||||||
|
"text",
|
||||||
|
"mediumtext",
|
||||||
|
"longtext"
|
||||||
|
);
|
||||||
|
|
||||||
|
public static final List<String> SQL_TYPE_INT = Arrays.asList("int", "tinyint");
|
||||||
|
|
||||||
|
public static final List<String> SQL_TYPE_LONG = Collections.singletonList("bigint");
|
||||||
|
|
||||||
|
public static final String JAVA_TYPE_STRING = "String";
|
||||||
|
|
||||||
|
public static final String JAVA_TYPE_INTEGER = "Integer";
|
||||||
|
|
||||||
|
public static final String JAVA_TYPE_LONG= "Long";
|
||||||
|
|
||||||
|
public static final String JAVA_TYPE_DECIMAL= "BigDecimal";
|
||||||
|
|
||||||
|
public static final String JAVA_TYPE_DATE= "Date";
|
||||||
|
|
||||||
|
public static String toJavaDataType(String sqlDataType) {
|
||||||
|
if (SQL_TYPE_STRING.contains(sqlDataType)) {
|
||||||
|
return JAVA_TYPE_STRING;
|
||||||
|
}
|
||||||
|
if (SQL_TYPE_INT.contains(sqlDataType)) {
|
||||||
|
return JAVA_TYPE_INTEGER;
|
||||||
|
}
|
||||||
|
if (SQL_TYPE_LONG.contains(sqlDataType)) {
|
||||||
|
return JAVA_TYPE_LONG;
|
||||||
|
}
|
||||||
|
if (SQL_TYPE_DATE.contains(sqlDataType)) {
|
||||||
|
return JAVA_TYPE_DATE;
|
||||||
|
}
|
||||||
|
if (SQL_TYPE_DATE_TIME.contains(sqlDataType)) {
|
||||||
|
return JAVA_TYPE_DATE;
|
||||||
|
}
|
||||||
|
if (SQL_TYPE_DECIMAL.contains(sqlDataType)) {
|
||||||
|
return JAVA_TYPE_DECIMAL;
|
||||||
|
}
|
||||||
|
throw new RuntimeException("unknown sql type:" + sqlDataType);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package space.caoshd.otone.util;
|
package space.caoshd.otone.util;
|
||||||
|
|
||||||
public class PropertiesConstants {
|
public class PropertiesConsts {
|
||||||
|
|
||||||
public PropertiesConstants() {}
|
public PropertiesConsts() {}
|
||||||
|
|
||||||
public static final String DATASOURCE_PROPERTIES_PATH = "datasource.properties";
|
public static final String DATASOURCE_PROPERTIES_PATH = "datasource.properties";
|
||||||
|
|
@ -1,18 +0,0 @@
|
|||||||
package space.caoshd.otone.util;
|
|
||||||
|
|
||||||
public class SqlConstants {
|
|
||||||
|
|
||||||
private SqlConstants() {}
|
|
||||||
|
|
||||||
public static final String TABLE_DETAIL_SQL = "table.detail.sql";
|
|
||||||
|
|
||||||
public static final String TABLE_DETAIL_SQL_SCHEMA = "table.detail.sql.schema";
|
|
||||||
|
|
||||||
public static final String TABLE_DETAIL_SQL_SCHEMA_TABLE = "table.detail.sql.schema.table";
|
|
||||||
|
|
||||||
public static final String TABLE_DETAIL_LABEL_TABLE_NAME = "table.detail.label.table-name";
|
|
||||||
|
|
||||||
public static final String TABLE_DETAIL_LABEL_TABLE_COMMENT =
|
|
||||||
"table.detail.label.table-comment";
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,31 @@
|
|||||||
|
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 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_EXTRA = "label.extra";
|
||||||
|
|
||||||
|
public static final String VALUE_AUTO_INCREMENT = "auto_increment";
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package space.caoshd.otone.util;
|
||||||
|
|
||||||
|
public class StringUtils {
|
||||||
|
|
||||||
|
private StringUtils() {}
|
||||||
|
|
||||||
|
public static String last(String str, String regex) {
|
||||||
|
String[] split = str.split(regex);
|
||||||
|
return split.length >= 1 ? split[split.length - 1] : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String first(String str, String regex) {
|
||||||
|
String[] split = str.split(regex);
|
||||||
|
return split.length >= 1 ? split[0] : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String removePrefix(String str, String prefix) {
|
||||||
|
if (str.startsWith(prefix)) {
|
||||||
|
return str.substring(prefix.length());
|
||||||
|
} else {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String defaultIfBlank(String str, String defaultStr) {
|
||||||
|
if (str == null || str.trim().isEmpty()) {
|
||||||
|
return defaultStr;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toCamelCase(String str, boolean capitalizeFirstLetter) {
|
||||||
|
String lowerCaseStr = str.toLowerCase();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean upperCase = capitalizeFirstLetter;
|
||||||
|
for (int i = 0; i < lowerCaseStr.length(); i++) {
|
||||||
|
char c = lowerCaseStr.charAt(i);
|
||||||
|
if (c == '_') {
|
||||||
|
upperCase = true;
|
||||||
|
} else {
|
||||||
|
if (upperCase) {
|
||||||
|
sb.append(Character.toUpperCase(c));
|
||||||
|
upperCase = false;
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
database.schemas=otone
|
#database.schemas=otone
|
||||||
table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
|
#table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
|
||||||
table.prefixes=t_,m_
|
table.prefixes=t_,m_
|
||||||
|
bean.suffix=Query
|
@ -1,5 +1,13 @@
|
|||||||
table.detail.sql=select * from information_schema.tables
|
table.sql=select * from information_schema.tables where TABLE_SCHEMA not in ('mysql', 'information_schema', 'performance_schema', 'sys')
|
||||||
table.detail.sql.schema=select * from information_schema.tables where table_schema = ?
|
table.sql.schema=select * from information_schema.tables where table_schema = ?
|
||||||
table.detail.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
|
table.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
|
||||||
table.detail.label.table-name=TABLE_NAME
|
|
||||||
table.detail.label.table-comment=TABLE_COMMENT
|
column.sql.schema.table=select * from information_schema.columns where TABLE_SCHEMA = ? and table_name = ?
|
||||||
|
|
||||||
|
|
||||||
|
label.table_schema=TABLE_SCHEMA
|
||||||
|
label.table_name=TABLE_NAME
|
||||||
|
label.table_comment=TABLE_COMMENT
|
||||||
|
label.column_name=COLUMN_NAME
|
||||||
|
label.column_comment=COLUMN_COMMENT
|
||||||
|
label.data_type=DATA_TYPE
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package space.caoshd.otone.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class StringUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void last() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void first() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void removePrefix(){
|
||||||
|
String username = StringUtils.removePrefix("t_user_name", "t_");
|
||||||
|
System.out.println(username);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void toCamelCase() {
|
||||||
|
String userName = StringUtils.toCamelCase("user_name", false);
|
||||||
|
String className = StringUtils.toCamelCase("class_name", true);
|
||||||
|
System.out.println(userName);
|
||||||
|
System.out.println(className);
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
database.schemas=otone
|
#database.schemas=otone
|
||||||
table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
|
#table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
|
||||||
|
#bean.suffix=Query
|
||||||
table.prefixes=t_,m_
|
table.prefixes=t_,m_
|
@ -1,5 +1,13 @@
|
|||||||
table.detail.sql=select * from information_schema.tables
|
table.sql=select * from information_schema.tables where TABLE_SCHEMA not in ('mysql', 'information_schema', 'performance_schema', 'sys')
|
||||||
table.detail.sql.schema=select * from information_schema.tables where table_schema = ?
|
table.sql.schema=select * from information_schema.tables where table_schema = ?
|
||||||
table.detail.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
|
table.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
|
||||||
table.detail.label.table-name=TABLE_NAME
|
|
||||||
table.detail.label.table-comment=TABLE_COMMENT
|
column.sql.schema.table=select * from information_schema.columns where TABLE_SCHEMA = ? and table_name = ?
|
||||||
|
|
||||||
|
label.table_schema=TABLE_SCHEMA
|
||||||
|
label.table_name=TABLE_NAME
|
||||||
|
label.table_comment=TABLE_COMMENT
|
||||||
|
label.column_name=COLUMN_NAME
|
||||||
|
label.column_comment=COLUMN_COMMENT
|
||||||
|
label.data_type=DATA_TYPE
|
||||||
|
label.extra=EXTRA
|
||||||
|
Loading…
Reference in New Issue