mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-08 17:32:14 +08:00
refactor: update primarky logic
This commit is contained in:
@@ -26,4 +26,5 @@ public class ColumnMeta {
|
||||
|
||||
private String autoIncrement;
|
||||
|
||||
private Boolean isPrimaryKey;
|
||||
}
|
@@ -15,7 +15,5 @@ public class IndexMeta {
|
||||
@Builder.Default
|
||||
private List<String> columnNames = Collections.emptyList();
|
||||
|
||||
private Boolean isPrimaryKey;
|
||||
|
||||
private Boolean isUniqueKey;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
|
@@ -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;
|
||||
|
@@ -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
0
plugin/user.md
Normal file
Reference in New Issue
Block a user