From 1e6846c9b076998712aefff159e8142d85683746 Mon Sep 17 00:00:00 2001 From: vran Date: Tue, 1 Feb 2022 14:07:15 +0800 Subject: [PATCH] refactor: update primarky logic --- .../converter/DocumentPojoConverter.java | 1 - .../data/DatabaseDocumentResponse.java | 4 +- .../dao/tables/TableColumnDocument.java | 13 ++- .../dao/tables/TableIndexDocument.java | 13 +-- .../tables/pojos/TableColumnDocumentPojo.java | 19 ++++ .../tables/pojos/TableIndexDocumentPojo.java | 19 ---- .../records/TableColumnDocumentRecord.java | 94 +++++++++++++------ .../records/TableIndexDocumentRecord.java | 86 +++++------------ .../java/com/databasir/dao/impl/BaseDao.java | 1 + .../main/resources/db/migration/V1__init.sql | 2 +- .../databasir/core/meta/data/ColumnMeta.java | 1 + .../databasir/core/meta/data/IndexMeta.java | 2 - .../impl/jdbc/JdbcColumnMetaRepository.java | 13 +++ .../impl/jdbc/JdbcIndexMetaRepository.java | 1 - .../databasir/core/render/RenderConfig.java | 1 - plugin/src/test/java/App.java | 2 +- plugin/user.md | 0 17 files changed, 141 insertions(+), 131 deletions(-) create mode 100644 plugin/user.md diff --git a/core/src/main/java/com/databasir/core/domain/document/converter/DocumentPojoConverter.java b/core/src/main/java/com/databasir/core/domain/document/converter/DocumentPojoConverter.java index d4c562f..54d38ff 100644 --- a/core/src/main/java/com/databasir/core/domain/document/converter/DocumentPojoConverter.java +++ b/core/src/main/java/com/databasir/core/domain/document/converter/DocumentPojoConverter.java @@ -51,7 +51,6 @@ public interface DocumentPojoConverter extends BaseConverter { .collect(Collectors.toList()); } - @Mapping(target = "isPrimary", source = "meta.isPrimaryKey") @Mapping(target = "isUnique", source = "meta.isUniqueKey") @Mapping(target = "columnNameArray", source = "meta.columnNames") TableIndexDocumentPojo toIndexPojo(Integer databaseDocumentId, diff --git a/core/src/main/java/com/databasir/core/domain/document/data/DatabaseDocumentResponse.java b/core/src/main/java/com/databasir/core/domain/document/data/DatabaseDocumentResponse.java index 0afe0f1..e7d446e 100644 --- a/core/src/main/java/com/databasir/core/domain/document/data/DatabaseDocumentResponse.java +++ b/core/src/main/java/com/databasir/core/domain/document/data/DatabaseDocumentResponse.java @@ -72,6 +72,8 @@ public class DatabaseDocumentResponse { private String comment; + private Boolean isPrimaryKey; + private String nullable; private String autoIncrement; @@ -91,8 +93,6 @@ public class DatabaseDocumentResponse { private String name; - private Boolean isPrimary; - private Boolean isUnique; @Builder.Default diff --git a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableColumnDocument.java b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableColumnDocument.java index 2672f31..ea326fe 100644 --- a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableColumnDocument.java +++ b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableColumnDocument.java @@ -19,7 +19,7 @@ import org.jooq.Identity; import org.jooq.Index; import org.jooq.Name; import org.jooq.Record; -import org.jooq.Row12; +import org.jooq.Row13; import org.jooq.Schema; import org.jooq.Table; import org.jooq.TableField; @@ -98,6 +98,11 @@ public class TableColumnDocument extends TableImpl { */ public final TableField DECIMAL_DIGITS = createField(DSL.name("decimal_digits"), SQLDataType.INTEGER, this, ""); + /** + * The column databasir.table_column_document.is_primary_key. + */ + public final TableField IS_PRIMARY_KEY = createField(DSL.name("is_primary_key"), SQLDataType.BOOLEAN.nullable(false), this, ""); + /** * The column databasir.table_column_document.nullable. YES, * NO, UNKNOWN @@ -197,11 +202,11 @@ public class TableColumnDocument extends TableImpl { } // ------------------------------------------------------------------------- - // Row12 type methods + // Row13 type methods // ------------------------------------------------------------------------- @Override - public Row12 fieldsRow() { - return (Row12) super.fieldsRow(); + public Row13 fieldsRow() { + return (Row13) super.fieldsRow(); } } diff --git a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableIndexDocument.java b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableIndexDocument.java index a97605a..1d4233d 100644 --- a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableIndexDocument.java +++ b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/TableIndexDocument.java @@ -20,7 +20,7 @@ import org.jooq.Index; import org.jooq.JSON; import org.jooq.Name; import org.jooq.Record; -import org.jooq.Row8; +import org.jooq.Row7; import org.jooq.Schema; import org.jooq.Table; import org.jooq.TableField; @@ -73,11 +73,6 @@ public class TableIndexDocument extends TableImpl { */ public final TableField NAME = createField(DSL.name("name"), SQLDataType.CLOB.nullable(false), this, ""); - /** - * The column databasir.table_index_document.is_primary. - */ - public final TableField IS_PRIMARY = createField(DSL.name("is_primary"), SQLDataType.BOOLEAN.nullable(false), this, ""); - /** * The column databasir.table_index_document.is_unique. */ @@ -175,11 +170,11 @@ public class TableIndexDocument extends TableImpl { } // ------------------------------------------------------------------------- - // Row8 type methods + // Row7 type methods // ------------------------------------------------------------------------- @Override - public Row8 fieldsRow() { - return (Row8) super.fieldsRow(); + public Row7 fieldsRow() { + return (Row7) super.fieldsRow(); } } diff --git a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableColumnDocumentPojo.java b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableColumnDocumentPojo.java index ca88836..23881c7 100644 --- a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableColumnDocumentPojo.java +++ b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableColumnDocumentPojo.java @@ -25,6 +25,7 @@ public class TableColumnDocumentPojo implements Serializable { private String defaultValue; private Integer size; private Integer decimalDigits; + private Boolean isPrimaryKey; private String nullable; private String autoIncrement; private LocalDateTime createAt; @@ -41,6 +42,7 @@ public class TableColumnDocumentPojo implements Serializable { this.defaultValue = value.defaultValue; this.size = value.size; this.decimalDigits = value.decimalDigits; + this.isPrimaryKey = value.isPrimaryKey; this.nullable = value.nullable; this.autoIncrement = value.autoIncrement; this.createAt = value.createAt; @@ -56,6 +58,7 @@ public class TableColumnDocumentPojo implements Serializable { String defaultValue, Integer size, Integer decimalDigits, + Boolean isPrimaryKey, String nullable, String autoIncrement, LocalDateTime createAt @@ -69,6 +72,7 @@ public class TableColumnDocumentPojo implements Serializable { this.defaultValue = defaultValue; this.size = size; this.decimalDigits = decimalDigits; + this.isPrimaryKey = isPrimaryKey; this.nullable = nullable; this.autoIncrement = autoIncrement; this.createAt = createAt; @@ -204,6 +208,20 @@ public class TableColumnDocumentPojo implements Serializable { this.decimalDigits = decimalDigits; } + /** + * Getter for databasir.table_column_document.is_primary_key. + */ + public Boolean getIsPrimaryKey() { + return this.isPrimaryKey; + } + + /** + * Setter for databasir.table_column_document.is_primary_key. + */ + public void setIsPrimaryKey(Boolean isPrimaryKey) { + this.isPrimaryKey = isPrimaryKey; + } + /** * Getter for databasir.table_column_document.nullable. YES, * NO, UNKNOWN @@ -263,6 +281,7 @@ public class TableColumnDocumentPojo implements Serializable { sb.append(", ").append(defaultValue); sb.append(", ").append(size); sb.append(", ").append(decimalDigits); + sb.append(", ").append(isPrimaryKey); sb.append(", ").append(nullable); sb.append(", ").append(autoIncrement); sb.append(", ").append(createAt); diff --git a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableIndexDocumentPojo.java b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableIndexDocumentPojo.java index 7fd02b8..ceb0fb4 100644 --- a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableIndexDocumentPojo.java +++ b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/pojos/TableIndexDocumentPojo.java @@ -22,7 +22,6 @@ public class TableIndexDocumentPojo implements Serializable { private Integer tableDocumentId; private Integer databaseDocumentId; private String name; - private Boolean isPrimary; private Boolean isUnique; private JSON columnNameArray; private LocalDateTime createAt; @@ -34,7 +33,6 @@ public class TableIndexDocumentPojo implements Serializable { this.tableDocumentId = value.tableDocumentId; this.databaseDocumentId = value.databaseDocumentId; this.name = value.name; - this.isPrimary = value.isPrimary; this.isUnique = value.isUnique; this.columnNameArray = value.columnNameArray; this.createAt = value.createAt; @@ -45,7 +43,6 @@ public class TableIndexDocumentPojo implements Serializable { Integer tableDocumentId, Integer databaseDocumentId, String name, - Boolean isPrimary, Boolean isUnique, JSON columnNameArray, LocalDateTime createAt @@ -54,7 +51,6 @@ public class TableIndexDocumentPojo implements Serializable { this.tableDocumentId = tableDocumentId; this.databaseDocumentId = databaseDocumentId; this.name = name; - this.isPrimary = isPrimary; this.isUnique = isUnique; this.columnNameArray = columnNameArray; this.createAt = createAt; @@ -118,20 +114,6 @@ public class TableIndexDocumentPojo implements Serializable { this.name = name; } - /** - * Getter for databasir.table_index_document.is_primary. - */ - public Boolean getIsPrimary() { - return this.isPrimary; - } - - /** - * Setter for databasir.table_index_document.is_primary. - */ - public void setIsPrimary(Boolean isPrimary) { - this.isPrimary = isPrimary; - } - /** * Getter for databasir.table_index_document.is_unique. */ @@ -182,7 +164,6 @@ public class TableIndexDocumentPojo implements Serializable { sb.append(", ").append(tableDocumentId); sb.append(", ").append(databaseDocumentId); sb.append(", ").append(name); - sb.append(", ").append(isPrimary); sb.append(", ").append(isUnique); sb.append(", ").append(columnNameArray); sb.append(", ").append(createAt); diff --git a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/records/TableColumnDocumentRecord.java b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/records/TableColumnDocumentRecord.java index 9a87151..84f8972 100644 --- a/dao/generated-src/jooq/main/java/com/databasir/dao/tables/records/TableColumnDocumentRecord.java +++ b/dao/generated-src/jooq/main/java/com/databasir/dao/tables/records/TableColumnDocumentRecord.java @@ -11,8 +11,8 @@ import java.time.LocalDateTime; import org.jooq.Field; import org.jooq.Record1; -import org.jooq.Record12; -import org.jooq.Row12; +import org.jooq.Record13; +import org.jooq.Row13; import org.jooq.impl.UpdatableRecordImpl; @@ -20,7 +20,7 @@ import org.jooq.impl.UpdatableRecordImpl; * This class is generated by jOOQ. */ @SuppressWarnings({ "all", "unchecked", "rawtypes" }) -public class TableColumnDocumentRecord extends UpdatableRecordImpl implements Record12 { +public class TableColumnDocumentRecord extends UpdatableRecordImpl implements Record13 { private static final long serialVersionUID = 1L; @@ -154,12 +154,26 @@ public class TableColumnDocumentRecord extends UpdatableRecordImpldatabasir.table_column_document.is_primary_key. + */ + public void setIsPrimaryKey(Boolean value) { + set(9, value); + } + + /** + * Getter for databasir.table_column_document.is_primary_key. + */ + public Boolean getIsPrimaryKey() { + return (Boolean) get(9); + } + /** * Setter for databasir.table_column_document.nullable. YES, * NO, UNKNOWN */ public void setNullable(String value) { - set(9, value); + set(10, value); } /** @@ -167,7 +181,7 @@ public class TableColumnDocumentRecord extends UpdatableRecordImpldatabasir.table_column_document.create_at. */ public void setCreateAt(LocalDateTime value) { - set(11, value); + set(12, value); } /** * Getter for databasir.table_column_document.create_at. */ public LocalDateTime getCreateAt() { - return (LocalDateTime) get(11); + return (LocalDateTime) get(12); } // ------------------------------------------------------------------------- @@ -210,17 +224,17 @@ public class TableColumnDocumentRecord extends UpdatableRecordImpl fieldsRow() { - return (Row12) super.fieldsRow(); + public Row13 fieldsRow() { + return (Row13) super.fieldsRow(); } @Override - public Row12 valuesRow() { - return (Row12) super.valuesRow(); + public Row13 valuesRow() { + return (Row13) super.valuesRow(); } @Override @@ -269,17 +283,22 @@ public class TableColumnDocumentRecord extends UpdatableRecordImpl field10() { - return TableColumnDocument.TABLE_COLUMN_DOCUMENT.NULLABLE; + public Field field10() { + return TableColumnDocument.TABLE_COLUMN_DOCUMENT.IS_PRIMARY_KEY; } @Override public Field field11() { + return TableColumnDocument.TABLE_COLUMN_DOCUMENT.NULLABLE; + } + + @Override + public Field field12() { return TableColumnDocument.TABLE_COLUMN_DOCUMENT.AUTO_INCREMENT; } @Override - public Field field12() { + public Field field13() { return TableColumnDocument.TABLE_COLUMN_DOCUMENT.CREATE_AT; } @@ -329,17 +348,22 @@ public class TableColumnDocumentRecord extends UpdatableRecordImpl implements Record8 { +public class TableIndexDocumentRecord extends UpdatableRecordImpl implements Record7 { private static final long serialVersionUID = 1L; @@ -83,60 +83,46 @@ public class TableIndexDocumentRecord extends UpdatableRecordImpldatabasir.table_index_document.is_primary. - */ - public void setIsPrimary(Boolean value) { - set(4, value); - } - - /** - * Getter for databasir.table_index_document.is_primary. - */ - public Boolean getIsPrimary() { - return (Boolean) get(4); - } - /** * Setter for databasir.table_index_document.is_unique. */ public void setIsUnique(Boolean value) { - set(5, value); + set(4, value); } /** * Getter for databasir.table_index_document.is_unique. */ public Boolean getIsUnique() { - return (Boolean) get(5); + return (Boolean) get(4); } /** * Setter for databasir.table_index_document.column_name_array. */ public void setColumnNameArray(JSON value) { - set(6, value); + set(5, value); } /** * Getter for databasir.table_index_document.column_name_array. */ public JSON getColumnNameArray() { - return (JSON) get(6); + return (JSON) get(5); } /** * Setter for databasir.table_index_document.create_at. */ public void setCreateAt(LocalDateTime value) { - set(7, value); + set(6, value); } /** * Getter for databasir.table_index_document.create_at. */ public LocalDateTime getCreateAt() { - return (LocalDateTime) get(7); + return (LocalDateTime) get(6); } // ------------------------------------------------------------------------- @@ -149,17 +135,17 @@ public class TableIndexDocumentRecord extends UpdatableRecordImpl fieldsRow() { - return (Row8) super.fieldsRow(); + public Row7 fieldsRow() { + return (Row7) super.fieldsRow(); } @Override - public Row8 valuesRow() { - return (Row8) super.valuesRow(); + public Row7 valuesRow() { + return (Row7) super.valuesRow(); } @Override @@ -184,21 +170,16 @@ public class TableIndexDocumentRecord extends UpdatableRecordImpl field5() { - return TableIndexDocument.TABLE_INDEX_DOCUMENT.IS_PRIMARY; - } - - @Override - public Field field6() { return TableIndexDocument.TABLE_INDEX_DOCUMENT.IS_UNIQUE; } @Override - public Field field7() { + public Field field6() { return TableIndexDocument.TABLE_INDEX_DOCUMENT.COLUMN_NAME_ARRAY; } @Override - public Field field8() { + public Field field7() { return TableIndexDocument.TABLE_INDEX_DOCUMENT.CREATE_AT; } @@ -224,21 +205,16 @@ public class TableIndexDocumentRecord extends UpdatableRecordImpl columnNames = Collections.emptyList(); - private Boolean isPrimaryKey; - private Boolean isUniqueKey; } diff --git a/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java b/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java index fde8ad6..054307a 100644 --- a/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java +++ b/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java @@ -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 columnDocs = new ArrayList<>(); String databaseName = tableCondition.getDatabaseName(); String tableName = tableCondition.getTableName(); + List 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 selectPrimaryKeyColumns(DatabaseMetaData meta, String catalog, String tableName) throws SQLException { + ResultSet result = meta.getPrimaryKeys(catalog, null, tableName); + List columns = new ArrayList<>(); + while (result.next()) { + String columnName = result.getString("COLUMN_NAME"); + columns.add(columnName); + } + return columns; + } } diff --git a/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcIndexMetaRepository.java b/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcIndexMetaRepository.java index b2ebc38..5d0ded5 100644 --- a/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcIndexMetaRepository.java +++ b/plugin/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcIndexMetaRepository.java @@ -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); diff --git a/plugin/src/main/java/com/databasir/core/render/RenderConfig.java b/plugin/src/main/java/com/databasir/core/render/RenderConfig.java index 4b614f3..237c8fa 100644 --- a/plugin/src/main/java/com/databasir/core/render/RenderConfig.java +++ b/plugin/src/main/java/com/databasir/core/render/RenderConfig.java @@ -57,7 +57,6 @@ public class RenderConfig { protected LinkedHashMap> indexTitleAndValueMapping() { LinkedHashMap> 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; diff --git a/plugin/src/test/java/App.java b/plugin/src/test/java/App.java index b81d09f..219b45b 100644 --- a/plugin/src/test/java/App.java +++ b/plugin/src/test/java/App.java @@ -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); } } diff --git a/plugin/user.md b/plugin/user.md new file mode 100644 index 0000000..e69de29