[jOOQ/jOOQ#10546] Added Meta.getUniqueKeys(), getForeignKeys()

This commit is contained in:
Lukas Eder 2020-11-06 17:05:47 +01:00
parent c4c727264a
commit a68a20cb0f
3 changed files with 262 additions and 19 deletions

View File

@ -251,6 +251,86 @@ public interface Meta extends Scope {
@Support
List<UniqueKey<?>> getPrimaryKeys() throws DataAccessException;
/**
* Get all primary keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<UniqueKey<?>> getPrimaryKeys(String name) throws DataAccessException;
/**
* Get all primary keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<UniqueKey<?>> getPrimaryKeys(Name name) throws DataAccessException;
/**
* Get all unique keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<UniqueKey<?>> getUniqueKeys() throws DataAccessException;
/**
* Get all unique keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<UniqueKey<?>> getUniqueKeys(String name) throws DataAccessException;
/**
* Get all unique keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<UniqueKey<?>> getUniqueKeys(Name name) throws DataAccessException;
/**
* Get all foreign keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<ForeignKey<?, ?>> getForeignKeys() throws DataAccessException;
/**
* Get all foreign keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<ForeignKey<?, ?>> getForeignKeys(String name) throws DataAccessException;
/**
* Get all foreign keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<ForeignKey<?, ?>> getForeignKeys(Name name) throws DataAccessException;
/**
* Get all indexes from the underlying meta data sources.
*
@ -261,6 +341,26 @@ public interface Meta extends Scope {
@Support
List<Index> getIndexes() throws DataAccessException;
/**
* Get all indexes from the underlying meta data sources.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<Index> getIndexes(String name) throws DataAccessException;
/**
* Get all indexes from the underlying meta data sources.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@NotNull
@Support
List<Index> getIndexes(Name name) throws DataAccessException;
/**
* A predicate to filter out query parts of a given type from meta data.
*/

View File

@ -71,20 +71,26 @@ import org.jooq.util.xml.jaxb.InformationSchema;
*/
abstract class AbstractMeta extends AbstractScope implements Meta, Serializable {
private static final long serialVersionUID = 910484713008245977L;
private static final long serialVersionUID = 910484713008245977L;
// [#9010] TODO: Allow for opting out of this cache
private Map<Name, Catalog> cachedCatalogs;
private Map<Name, Schema> cachedQualifiedSchemas;
private Map<Name, Table<?>> cachedQualifiedTables;
private Map<Name, Domain<?>> cachedQualifiedDomains;
private Map<Name, Sequence<?>> cachedQualifiedSequences;
private Map<Name, List<Schema>> cachedUnqualifiedSchemas;
private Map<Name, List<Table<?>>> cachedUnqualifiedTables;
private Map<Name, List<Domain<?>>> cachedUnqualifiedDomains;
private Map<Name, List<Sequence<?>>> cachedUnqualifiedSequences;
private List<UniqueKey<?>> cachedPrimaryKeys;
private List<Index> cachedIndexes;
private Map<Name, Catalog> cachedCatalogs;
private Map<Name, Schema> cachedQualifiedSchemas;
private Map<Name, Table<?>> cachedQualifiedTables;
private Map<Name, Domain<?>> cachedQualifiedDomains;
private Map<Name, Sequence<?>> cachedQualifiedSequences;
private Map<Name, UniqueKey<?>> cachedQualifiedPrimaryKeys;
private Map<Name, UniqueKey<?>> cachedQualifiedUniqueKeys;
private Map<Name, ForeignKey<?, ?>> cachedQualifiedForeignKeys;
private Map<Name, Index> cachedQualifiedIndexes;
private Map<Name, List<Schema>> cachedUnqualifiedSchemas;
private Map<Name, List<Table<?>>> cachedUnqualifiedTables;
private Map<Name, List<Domain<?>>> cachedUnqualifiedDomains;
private Map<Name, List<Sequence<?>>> cachedUnqualifiedSequences;
private Map<Name, List<UniqueKey<?>>> cachedUnqualifiedPrimaryKeys;
private Map<Name, List<UniqueKey<?>>> cachedUnqualifiedUniqueKeys;
private Map<Name, List<ForeignKey<?, ?>>> cachedUnqualifiedForeignKeys;
private Map<Name, List<Index>> cachedUnqualifiedIndexes;
AbstractMeta(Configuration configuration) {
super(configuration);
@ -294,15 +300,39 @@ abstract class AbstractMeta extends AbstractScope implements Meta, Serializable
return result;
}
@Override
public final List<UniqueKey<?>> getPrimaryKeys(String name) {
return getPrimaryKeys(name(name));
}
@Override
public final List<UniqueKey<?>> getPrimaryKeys(Name name) {
initPrimaryKeys();
return get(name, new Iterable<UniqueKey<?>>() {
@Override
public Iterator<UniqueKey<?>> iterator() {
return getPrimaryKeys().iterator();
}
}, cachedQualifiedPrimaryKeys, cachedUnqualifiedPrimaryKeys);
}
@Override
public final List<UniqueKey<?>> getPrimaryKeys() {
initPrimaryKeys();
return Collections.unmodifiableList(cachedPrimaryKeys);
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedPrimaryKeys.values()));
}
private final void initPrimaryKeys() {
if (cachedPrimaryKeys == null)
cachedPrimaryKeys = new ArrayList<>(getPrimaryKeys0());
if (cachedQualifiedPrimaryKeys == null) {
cachedQualifiedPrimaryKeys = new LinkedHashMap<>();
cachedUnqualifiedPrimaryKeys = new LinkedHashMap<>();
get(name(""), new Iterable<UniqueKey<?>>() {
@Override
public Iterator<UniqueKey<?>> iterator() {
return getPrimaryKeys0().iterator();
}
}, cachedQualifiedPrimaryKeys, cachedUnqualifiedPrimaryKeys);
}
}
List<UniqueKey<?>> getPrimaryKeys0() {
@ -315,15 +345,127 @@ abstract class AbstractMeta extends AbstractScope implements Meta, Serializable
return result;
}
@Override
public final List<UniqueKey<?>> getUniqueKeys(String name) {
return getUniqueKeys(name(name));
}
@Override
public final List<UniqueKey<?>> getUniqueKeys(Name name) {
initUniqueKeys();
return get(name, new Iterable<UniqueKey<?>>() {
@Override
public Iterator<UniqueKey<?>> iterator() {
return getUniqueKeys().iterator();
}
}, cachedQualifiedUniqueKeys, cachedUnqualifiedUniqueKeys);
}
@Override
public final List<UniqueKey<?>> getUniqueKeys() {
initUniqueKeys();
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedUniqueKeys.values()));
}
private final void initUniqueKeys() {
if (cachedQualifiedUniqueKeys == null) {
cachedQualifiedUniqueKeys = new LinkedHashMap<>();
cachedUnqualifiedUniqueKeys = new LinkedHashMap<>();
get(name(""), new Iterable<UniqueKey<?>>() {
@Override
public Iterator<UniqueKey<?>> iterator() {
return getUniqueKeys0().iterator();
}
}, cachedQualifiedUniqueKeys, cachedUnqualifiedUniqueKeys);
}
}
List<UniqueKey<?>> getUniqueKeys0() {
List<UniqueKey<?>> result = new ArrayList<>();
for (Table<?> table : getTables())
result.addAll(table.getUniqueKeys());
return result;
}
@Override
public final List<ForeignKey<?, ?>> getForeignKeys(String name) {
return getForeignKeys(name(name));
}
@Override
public final List<ForeignKey<?, ?>> getForeignKeys(Name name) {
initForeignKeys();
return get(name, new Iterable<ForeignKey<?, ?>>() {
@Override
public Iterator<ForeignKey<?, ?>> iterator() {
return getForeignKeys().iterator();
}
}, cachedQualifiedForeignKeys, cachedUnqualifiedForeignKeys);
}
@Override
public final List<ForeignKey<?, ?>> getForeignKeys() {
initForeignKeys();
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedForeignKeys.values()));
}
private final void initForeignKeys() {
if (cachedQualifiedForeignKeys == null) {
cachedQualifiedForeignKeys = new LinkedHashMap<>();
cachedUnqualifiedForeignKeys = new LinkedHashMap<>();
get(name(""), new Iterable<ForeignKey<?, ?>>() {
@Override
public Iterator<ForeignKey<?, ?>> iterator() {
return getForeignKeys0().iterator();
}
}, cachedQualifiedForeignKeys, cachedUnqualifiedForeignKeys);
}
}
List<ForeignKey<?, ?>> getForeignKeys0() {
List<ForeignKey<?, ?>> result = new ArrayList<>();
for (Table<?> table : getTables())
result.addAll(table.getReferences());
return result;
}
@Override
public final List<Index> getIndexes(String name) {
return getIndexes(name(name));
}
@Override
public final List<Index> getIndexes(Name name) {
initIndexes();
return get(name, new Iterable<Index>() {
@Override
public Iterator<Index> iterator() {
return getIndexes().iterator();
}
}, cachedQualifiedIndexes, cachedUnqualifiedIndexes);
}
@Override
public final List<Index> getIndexes() {
initIndexes();
return Collections.unmodifiableList(cachedIndexes);
return Collections.unmodifiableList(new ArrayList<>(cachedQualifiedIndexes.values()));
}
private final void initIndexes() {
if (cachedIndexes == null)
cachedIndexes = new ArrayList<>(getIndexes0());
if (cachedQualifiedIndexes == null) {
cachedQualifiedIndexes = new LinkedHashMap<>();
cachedUnqualifiedIndexes = new LinkedHashMap<>();
get(name(""), new Iterable<Index>() {
@Override
public Iterator<Index> iterator() {
return getIndexes0().iterator();
}
}, cachedQualifiedIndexes, cachedUnqualifiedIndexes);
}
}
List<Index> getIndexes0() {

View File

@ -513,12 +513,13 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
*/
@Override
public List<UniqueKey<R>> getKeys() {
List<UniqueKey<R>> result = new ArrayList<>(getUniqueKeys());
List<UniqueKey<R>> result = new ArrayList<>();
UniqueKey<R> pk = getPrimaryKey();
if (pk != null)
result.add(pk);
result.addAll(getUniqueKeys());
return result;
}