refactor: update primarky logic

This commit is contained in:
vran
2022-02-01 14:07:15 +08:00
parent 5a9a42eaf7
commit 1e6846c9b0
17 changed files with 141 additions and 131 deletions

View File

@@ -26,4 +26,5 @@ public class ColumnMeta {
private String autoIncrement;
private Boolean isPrimaryKey;
}

View File

@@ -15,7 +15,5 @@ public class IndexMeta {
@Builder.Default
private List<String> columnNames = Collections.emptyList();
private Boolean isPrimaryKey;
private Boolean isUniqueKey;
}

View File

@@ -7,6 +7,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -29,6 +30,7 @@ public class JdbcColumnMetaRepository implements ColumnMetaRepository {
List<ColumnMeta> columnDocs = new ArrayList<>();
String databaseName = tableCondition.getDatabaseName();
String tableName = tableCondition.getTableName();
List<String> primaryKeyColumns = selectPrimaryKeyColumns(connection.getMetaData(), databaseName, tableName);
ResultSet columnsResult;
try {
columnsResult = connection.getMetaData().getColumns(databaseName, null, tableName, null);
@@ -73,6 +75,7 @@ public class JdbcColumnMetaRepository implements ColumnMetaRepository {
.autoIncrement(isAutoIncrement)
.comment(columnComment)
.defaultValue(defaultValue)
.isPrimaryKey(primaryKeyColumns.contains(columnName))
.build();
columnDocs.add(columnMeta);
}
@@ -80,4 +83,14 @@ public class JdbcColumnMetaRepository implements ColumnMetaRepository {
}
return columnDocs;
}
private List<String> selectPrimaryKeyColumns(DatabaseMetaData meta, String catalog, String tableName) throws SQLException {
ResultSet result = meta.getPrimaryKeys(catalog, null, tableName);
List<String> columns = new ArrayList<>();
while (result.next()) {
String columnName = result.getString("COLUMN_NAME");
columns.add(columnName);
}
return columns;
}
}

View File

@@ -47,7 +47,6 @@ public class JdbcIndexMetaRepository implements IndexMetaRepository {
IndexMeta indexMeta = IndexMeta.builder()
.name(indexName)
.columnNames(columns)
.isPrimaryKey(Objects.equals("PRIMARY", indexName))
.isUniqueKey(Objects.equals(nonUnique, false))
.build();
pojoGroupByName.put(indexName, indexMeta);

View File

@@ -57,7 +57,6 @@ public class RenderConfig {
protected LinkedHashMap<String, Function<IndexMeta, String>> indexTitleAndValueMapping() {
LinkedHashMap<String, Function<IndexMeta, String>> mapping = new LinkedHashMap<>();
mapping.put("Name", IndexMeta::getName);
mapping.put("IsPrimary", index -> index.getIsPrimaryKey() ? "YES" : "");
mapping.put("IsUnique", index -> index.getIsUniqueKey() ? "YES" : "");
mapping.put("Columns", index -> String.join(", ", index.getColumnNames()));
return mapping;

View File

@@ -33,7 +33,7 @@ public class App {
// this config is used by mysql
info.put("useInformationSchema", "true");
String url = "jdbc:mysql://localhost:3306/patient?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true";
String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true";
return DriverManager.getConnection(url, info);
}
}

0
plugin/user.md Normal file
View File