Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Nov 2, 2024
1 parent b315b88 commit 6013e61
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.babyfish.jimmer
version=0.9.8
version=0.9.9
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.sql.exception.ExecutionException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.babyfish.jimmer.lang.Ref;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.meta.LogicalDeletedInfo;
import org.babyfish.jimmer.meta.TargetLevel;
import org.babyfish.jimmer.sql.DatabaseValidationIgnore;
import org.babyfish.jimmer.sql.association.meta.AssociationType;
Expand Down Expand Up @@ -213,6 +214,7 @@ private void validateForeignKey(ImmutableType type) throws SQLException {
targetForeignKey.assertReferencedColumns(ctx, prop.getTargetType());
}
}
assertMiddleTablePrimaryKey(prop, middleTableMeta, middleTable);
}
} else if (storage != null && prop.isReference(TargetLevel.PERSISTENT)) {
ColumnDefinition columnDefinition = prop.getStorage(strategy);
Expand Down Expand Up @@ -250,7 +252,8 @@ private Table tableOf(ImmutableType type) throws SQLException {
new DatabaseValidationException.Item(
type,
null,
"Too many matched tables: " + tables
"Too many matched tables: " + tables +
conflictReason()
)
);
tableRef = Ref.empty();
Expand All @@ -264,6 +267,23 @@ private Table tableOf(ImmutableType type) throws SQLException {
return tableRef.getValue();
}

private String conflictReason() {
String reason;
if (catalog == null && schema == null) {
reason = ", please specify the configuration " +
"`databaseValidationCatalog` or `databaseValidationSchema`";
} else if (catalog == null) {
reason = ", please specify the configuration " +
"`databaseValidationCatalog`";
} else if (schema == null) {
reason = ", please specify the configuration " +
"`databaseValidationSchema`";
} else {
reason = "";
}
return reason;
}

private Table middleTableOf(ImmutableProp prop) throws SQLException {
Ref<Table> tableRef = middleTableRefMap.get(prop);
if (tableRef == null) {
Expand Down Expand Up @@ -455,6 +475,54 @@ private Map<Set<String>, ForeignKey> foreignKeys(Table table) throws SQLExceptio
return foreignKeyMap;
}

private void assertMiddleTablePrimaryKey(ImmutableProp prop, MiddleTable meta, Table table) {

Set<String> unmanagedColumnNames = new LinkedHashSet<>();

ColumnDefinition cd = meta.getColumnDefinition();
for (int i = cd.size() - 1; i >= 0; --i) {
if (!table.primaryKeyColumns.contains(DatabaseIdentifiers.comparableIdentifier(cd.name(i)))) {
unmanagedColumnNames.add(cd.name(i));
}
}

ColumnDefinition tcd = meta.getTargetColumnDefinition();
for (int i = tcd.size() - 1; i >= 0; --i) {
if (!table.primaryKeyColumns.contains(DatabaseIdentifiers.comparableIdentifier(tcd.name(i)))) {
unmanagedColumnNames.add(tcd.name(i));
}
}

LogicalDeletedInfo ldi = meta.getLogicalDeletedInfo();
if (ldi != null) {
if (!table.primaryKeyColumns.contains(DatabaseIdentifiers.comparableIdentifier(ldi.getColumnName()))) {
unmanagedColumnNames.add(ldi.getColumnName());
}
}

JoinTableFilterInfo fi = meta.getFilterInfo();
if (fi != null) {
if (!table.primaryKeyColumns.contains(DatabaseIdentifiers.comparableIdentifier(fi.getColumnName()))) {
unmanagedColumnNames.add(fi.getColumnName());
}
}

for (String unmanagedColumnName : unmanagedColumnNames) {
items.add(
new DatabaseValidationException.Item(
prop.getDeclaringType(),
prop,
"The primary key of middle table must contain all columns, " +
"but column \"" +
unmanagedColumnName +
"\" of table \"" +
meta.getTableName() +
"\" is not part of the primary key"
)
);
}
}

private static String upper(String text) {
return text == null ? null : text.toUpperCase();
}
Expand Down

0 comments on commit 6013e61

Please sign in to comment.