Skip to content

Commit

Permalink
Add additional 'has' methods to Table for convinience
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Jan 11, 2025
1 parent a1e3875 commit db19b0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,24 @@ public final boolean hasForeignKeys() {
return !foreignKeys.isEmpty();
}

/** {@inheritDoc} */
@Override
public final boolean hasIndexes() {
return !indexes.isEmpty();
}

/** {@inheritDoc} */
@Override
public final boolean hasPrimaryKey() {
return getPrimaryKey() != null;
}

/** {@inheritDoc} */
@Override
public final boolean hasTriggers() {
return !triggers.isEmpty();
}

/** {@inheritDoc} */
@Override
public Optional<MutablePrimaryKey> lookupAlternateKey(final String name) {
Expand Down Expand Up @@ -377,13 +389,13 @@ private <R extends TableReference> Collection<R> getTableReferences(
for (final Iterator<R> iterator = foreignKeysList.iterator(); iterator.hasNext(); ) {
final R foreignKey = iterator.next();

final boolean isExportedKey = foreignKey.getReferencedTable().equals(this);
final boolean isExportedKey = equals(foreignKey.getReferencedTable());
if (tableAssociationType == TableAssociationType.exported && !isExportedKey) {
iterator.remove();
continue;
}

final boolean isImportedKey = foreignKey.getDependentTable().equals(this);
final boolean isImportedKey = equals(foreignKey.getDependentTable());
if (tableAssociationType == TableAssociationType.imported && !isImportedKey) {
iterator.remove();
continue;
Expand All @@ -398,16 +410,16 @@ private <R extends TableReference> Collection<R> getTableReferences(
nullsLast(
((Comparator<R>)
(final R one, final R two) -> {
final boolean isOneImportedKey = one.getDependentTable().equals(this);
final boolean isTwoImportedKey = two.getDependentTable().equals(this);
final boolean isOneImportedKey = equals(one.getDependentTable());
final boolean isTwoImportedKey = equals(two.getDependentTable());

if (isOneImportedKey == isTwoImportedKey) {
return 0;
} else if (isOneImportedKey) {
}
if (isOneImportedKey) {
return -1;
} else {
return 1;
}
return 1;
})
.thenComparing(naturalOrder()));
Collections.sort(foreignKeysList, fkComparator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@

package schemacrawler.crawl;

import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
import schemacrawler.schema.Column;
import schemacrawler.schema.ForeignKey;
import schemacrawler.schema.Index;
Expand Down Expand Up @@ -154,12 +152,22 @@ public boolean hasForeignKeys() {
throw new NotLoadedException(this);
}

@Override
public boolean hasIndexes() {
return false;
}

/** {@inheritDoc} */
@Override
public boolean hasPrimaryKey() {
throw new NotLoadedException(this);
}

@Override
public boolean hasTriggers() {
return false;
}

@Override
public Optional<PrimaryKey> lookupAlternateKey(final String name) {
throw new NotLoadedException(this);
Expand All @@ -169,18 +177,16 @@ public Optional<PrimaryKey> lookupAlternateKey(final String name) {
public Optional<Column> lookupColumn(final String name) {
if (column.getName().equals(name)) {
return Optional.ofNullable(column);
} else {
return Optional.empty();
}
return Optional.empty();
}

@Override
public Optional<ForeignKey> lookupForeignKey(final String name) {
if (foreignKey.getName().equals(name)) {
return Optional.ofNullable(foreignKey);
} else {
return Optional.empty();
}
return Optional.empty();
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions schemacrawler-api/src/main/java/schemacrawler/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,27 @@ default Collection<Table> getReferencingTables() {
*/
boolean hasForeignKeys();

/**
* Checks if the table has any indexes.
*
* @return True if the table has an index.
*/
boolean hasIndexes();

/**
* Checks if the table has a primary key.
*
* @return True if the table has a primary key.
*/
boolean hasPrimaryKey();

/**
* Checks if the table has any indexes.
*
* @return True if the table has an index.
*/
boolean hasTriggers();

/**
* Gets an alternate key by unqualified name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ public int hashCode() {
return Objects.hash(name, schema);
}

@Override
public boolean hasIndexes() {
return false;
}

@Override
public boolean hasPrimaryKey() {
return false;
Expand All @@ -231,6 +236,11 @@ public boolean hasRemarks() {
return false;
}

@Override
public boolean hasTriggers() {
return !triggers.isEmpty();
}

@Override
public NamedObjectKey key() {
return new NamedObjectKey(null, null, name);
Expand Down

0 comments on commit db19b0a

Please sign in to comment.