新增: 加载表方法

master
曹世达 6 months ago
parent ad79acc598
commit 02c9281c98

@ -0,0 +1,73 @@
package space.caoshd.otone.builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.tool.PropertyTools;
import space.caoshd.otone.util.ConfigConstants;
import space.caoshd.otone.util.DatabaseUtils;
import space.caoshd.otone.util.PropertiesConstants;
import space.caoshd.otone.util.SqlConstants;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class TableBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
public static List<TableInfo> loadTableInfo() {
List<TableInfo> result = new ArrayList<>();
PropertyTools configProperties =
new PropertyTools(PropertiesConstants.CONFIG_PROPERTIES_PATH);
List<String> schemaNames = configProperties.getStrings(ConfigConstants.SCHEMA_NAMES);
if (schemaNames.isEmpty()) {
// TODO LOAD CURRENT SCHEMA
} else {
for (String schemaName : schemaNames) {
result.addAll(loadTableInfo(schemaName));
}
}
return result;
}
public static List<TableInfo> loadTableInfo(String schema) {
List<TableInfo> result = new ArrayList<>();
PropertyTools configProperties =
new PropertyTools(PropertiesConstants.CONFIG_PROPERTIES_PATH);
List<String> tables = configProperties.getStrings(ConfigConstants.TABLE_NAMES);
if (tables.isEmpty()) {
// TODO LOAD ALL TABLE
} else {
for (String table : tables) {
TableInfo tableInfo = loadTableInfo(schema, table);
if (Objects.nonNull(tableInfo)) {
result.add(tableInfo);
}
}
}
return result;
}
public static TableInfo loadTableInfo(String schemaName, String tableName) {
PropertyTools sqlProperties = new PropertyTools(PropertiesConstants.MYSQL_PROPERTIES_PATH);
String sql = sqlProperties.getString(SqlConstants.TABLE_DETAIL_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName);
Map<String, String> table = DatabaseUtils.one(sql, params);
if (Objects.isNull(table)) {
LOGGER.warn("table: [{}] not exists", tableName);
return null;
}
TableInfo result = new TableInfo();
result.setTableName(tableName);
String tableCommentLabel =
sqlProperties.getString(SqlConstants.TABLE_DETAIL_LABEL_TABLE_COMMENT);
String comment = table.get(tableCommentLabel);
result.setComment(comment);
return result;
}
}

@ -0,0 +1,64 @@
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,88 @@
package space.caoshd.otone.util;
import space.caoshd.otone.tool.PropertyTools;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DatabaseUtils {
private DatabaseUtils() {}
public static Connection getConnection() {
try {
InputStream datasourcePropertiesStream = ResourceUtils.getClassPathFileStream(
PropertiesConstants.DATASOURCE_PROPERTIES_PATH);
PropertyTools propertyTools = new PropertyTools(datasourcePropertiesStream);
String username = propertyTools.getString("username");
String password = propertyTools.getString("password");
String driverClassName = propertyTools.getString("driver-class-name");
String url = propertyTools.getString("url");
Class.forName(driverClassName);
return DriverManager.getConnection(url, username, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static List<Map<String, String>> list(String sql, List<?> params) {
try (Connection connection = getConnection()) {
List<Map<String, String>> result = new ArrayList<>();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(i + 1, params.get(i));
}
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) {
Map<String, String> row = new HashMap<>();
String columnLabel = metaData.getColumnLabel(i);
String columnValue = resultSet.getString(i);
row.put(columnLabel.toUpperCase(), columnValue);
result.add(row);
}
}
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Map<String, String> one(String sql, List<?> params) {
try (Connection connection = getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(i + 1, params.get(i));
}
ResultSet resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) {
return null;
}
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
Map<String, String> result = new HashMap<>();
for (int i = 1; i <= columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i);
String columnValue = resultSet.getString(i);
result.put(columnLabel.toUpperCase(), columnValue);
}
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,18 @@
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,18 @@
package space.caoshd.otone.builder;
import org.junit.jupiter.api.Test;
import space.caoshd.otone.entity.TableInfo;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class TableBuilderTest {
@Test
void loadTableInfo() {
List<TableInfo> tableInfos =
TableBuilder.loadTableInfo();
System.out.println(tableInfos);
}
}
Loading…
Cancel
Save