Skip to content

Commit

Permalink
[#7164] Add Meta.getXYZ(String), getXYZ(Name) accessors for all objec…
Browse files Browse the repository at this point in the history
…t types
  • Loading branch information
lukaseder committed Feb 12, 2018
1 parent cb64b2b commit af01732
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 20 deletions.
86 changes: 79 additions & 7 deletions jOOQ/src/main/java/org/jooq/Meta.java
Expand Up @@ -80,13 +80,11 @@
public interface Meta {

/**
* Get all catalog objects from the underlying {@link DatabaseMetaData}.
* Get all catalog objects from the underlying meta data source.
* <p>
* For those databases that don't really support JDBC meta data catalogs, a
* single empty catalog (named <code>""</code>) will be returned. In other
* words, there is always at least one catalog in a database.
* <p>
* NOTE: Catalogs are experimental in jOOQ 3.0
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
Expand All @@ -95,7 +93,27 @@ public interface Meta {
List<Catalog> getCatalogs() throws DataAccessException;

/**
* Get all schema objects from the underlying {@link DatabaseMetaData}.
* Get a catalog object by name from the underlying meta data source, or
* <code>null</code> if no such object exists.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
Catalog getCatalog(String name) throws DataAccessException;

/**
* Get a catalog object by name from the underlying meta data source, or
* <code>null</code> if no such object exists.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
Catalog getCatalog(Name name) throws DataAccessException;

/**
* Get all schema objects from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
Expand All @@ -104,7 +122,25 @@ public interface Meta {
List<Schema> getSchemas() throws DataAccessException;

/**
* Get all table objects from the underlying {@link DatabaseMetaData}.
* Get all schema objects by name from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
List<Schema> getSchemas(String name) throws DataAccessException;

/**
* Get all schema objects by name from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
List<Schema> getSchemas(Name name) throws DataAccessException;

/**
* Get all table objects from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
Expand All @@ -113,7 +149,25 @@ public interface Meta {
List<Table<?>> getTables() throws DataAccessException;

/**
* Get all sequence objects from the underlying {@link DatabaseMetaData}.
* Get all table objects by name from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
List<Table<?>> getTables(String name) throws DataAccessException;

/**
* Get all table objects by name from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
List<Table<?>> getTables(Name name) throws DataAccessException;

/**
* Get all sequence objects from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
Expand All @@ -122,7 +176,25 @@ public interface Meta {
List<Sequence<?>> getSequences() throws DataAccessException;

/**
* Get all primary keys from the underlying {@link DatabaseMetaData}.
* Get all sequence objects by name from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES })
List<Sequence<?>> getSequences(String name) throws DataAccessException;

/**
* Get all sequence objects by name from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, POSTGRES })
List<Sequence<?>> getSequences(Name name) throws DataAccessException;

/**
* Get all primary keys from the underlying meta data source.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
Expand Down
172 changes: 172 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/AbstractMeta.java
@@ -0,0 +1,172 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;

import static org.jooq.impl.DSL.name;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jooq.Catalog;
import org.jooq.Meta;
import org.jooq.Name;
import org.jooq.Named;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;

/**
* @author Lukas Eder
*/
abstract class AbstractMeta implements Meta, Serializable {

private static final long serialVersionUID = 910484713008245977L;

private final Map<Name, Catalog> cachedCatalogs;
private final Map<Name, Schema> cachedQualifiedSchemas;
private final Map<Name, Table<?>> cachedQualifiedTables;
private final Map<Name, Sequence<?>> cachedQualifiedSequences;
private final Map<Name, List<Schema>> cachedUnqualifiedSchemas;
private final Map<Name, List<Table<?>>> cachedUnqualifiedTables;
private final Map<Name, List<Sequence<?>>> cachedUnqualifiedSequences;

AbstractMeta() {

// [#7165] TODO: Allow for opting out of this cache
this.cachedCatalogs = new LinkedHashMap<Name, Catalog>();
this.cachedQualifiedSchemas = new LinkedHashMap<Name, Schema>();
this.cachedQualifiedTables = new LinkedHashMap<Name, Table<?>>();
this.cachedQualifiedSequences = new LinkedHashMap<Name, Sequence<?>>();
this.cachedUnqualifiedSchemas = new LinkedHashMap<Name, List<Schema>>();
this.cachedUnqualifiedTables = new LinkedHashMap<Name, List<Table<?>>>();
this.cachedUnqualifiedSequences = new LinkedHashMap<Name, List<Sequence<?>>>();
}

@Override
public final Catalog getCatalog(String name) {
return getCatalog(name(name));
}

@Override
public final Catalog getCatalog(Name name) {
if (cachedCatalogs.isEmpty())
for (Catalog catalog : getCatalogs())
cachedCatalogs.put(catalog.getQualifiedName(), catalog);

return cachedCatalogs.get(name);
}

@Override
public final List<Schema> getSchemas(String name) {
return getSchemas(name(name));
}

@Override
public final List<Schema> getSchemas(Name name) {
return get(name, new Iterable<Schema>() {
@Override
public Iterator<Schema> iterator() {
return getSchemas().iterator();
}
}, cachedQualifiedSchemas, cachedUnqualifiedSchemas);
}

@Override
public final List<Table<?>> getTables(String name) {
return getTables(name(name));
}

@Override
public final List<Table<?>> getTables(Name name) {
return get(name, new Iterable<Table<?>>() {
@Override
public Iterator<Table<?>> iterator() {
return getTables().iterator();
}
}, cachedQualifiedTables, cachedUnqualifiedTables);
}

@Override
public final List<Sequence<?>> getSequences(String name) {
return getSequences(name(name));
}

@Override
public final List<Sequence<?>> getSequences(Name name) {
return get(name, new Iterable<Sequence<?>>() {
@Override
public Iterator<Sequence<?>> iterator() {
return getSequences().iterator();
}
}, cachedQualifiedSequences, cachedUnqualifiedSequences);
}

private final <T extends Named> List<T> get(Name name, Iterable<T> i, Map<Name, T> qualified, Map<Name, List<T>> unqualified) {
if (qualified.isEmpty()) {
for (T object : i) {
Name q = object.getQualifiedName();
Name u = object.getUnqualifiedName();

qualified.put(q, object);

List<T> list = unqualified.get(u);
if (list == null) {
list = new ArrayList<T>();
unqualified.get(u);
}

list.add(object);
}
}

T object = qualified.get(name);
if (object != null)
return Collections.singletonList(object);

List<T> list = unqualified.get(name);
if (list == null)
return Collections.emptyList();
else
return Collections.unmodifiableList(list);
}
}
5 changes: 3 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/CatalogMetaImpl.java
Expand Up @@ -44,7 +44,6 @@

import org.jooq.Catalog;
import org.jooq.Configuration;
import org.jooq.Meta;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
Expand All @@ -53,7 +52,9 @@
/**
* @author Lukas Eder
*/
final class CatalogMetaImpl implements Meta {
final class CatalogMetaImpl extends AbstractMeta {

private static final long serialVersionUID = 7582210274970452691L;

@SuppressWarnings("unused")
private final Configuration configuration;
Expand Down
Expand Up @@ -57,7 +57,6 @@
import org.jooq.DataType;
import org.jooq.ForeignKey;
import org.jooq.Index;
import org.jooq.Meta;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Schema;
Expand All @@ -77,7 +76,9 @@
/**
* @author Lukas Eder
*/
final class InformationSchemaMetaImpl implements Meta {
final class InformationSchemaMetaImpl extends AbstractMeta {

private static final long serialVersionUID = -1623783405104005307L;

private final Configuration configuration;
private final InformationSchema source;
Expand Down
6 changes: 1 addition & 5 deletions jOOQ/src/main/java/org/jooq/impl/MetaImpl.java
Expand Up @@ -131,12 +131,8 @@
*
* @author Lukas Eder
*/
final class MetaImpl implements Meta, Serializable {
final class MetaImpl extends AbstractMeta implements Serializable {


/**
* Generated UID
*/
private static final long serialVersionUID = 3582980783173033809L;
private static final EnumSet<SQLDialect> INVERSE_SCHEMA_CATALOG = EnumSet.of(MYSQL, MARIADB);
private static final EnumSet<SQLDialect> CURRENT_TIMESTAMP_COLUMN_DEFAULT = EnumSet.of(MYSQL, MARIADB);
Expand Down
5 changes: 3 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/SchemaMetaImpl.java
Expand Up @@ -46,7 +46,6 @@

import org.jooq.Catalog;
import org.jooq.Configuration;
import org.jooq.Meta;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
Expand All @@ -55,7 +54,9 @@
/**
* @author Lukas Eder
*/
final class SchemaMetaImpl implements Meta {
final class SchemaMetaImpl extends AbstractMeta {

private static final long serialVersionUID = -505810795492145873L;

@SuppressWarnings("unused")
private final Configuration configuration;
Expand Down
5 changes: 3 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/TableMetaImpl.java
Expand Up @@ -46,7 +46,6 @@

import org.jooq.Catalog;
import org.jooq.Configuration;
import org.jooq.Meta;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
Expand All @@ -55,7 +54,9 @@
/**
* @author Lukas Eder
*/
final class TableMetaImpl implements Meta {
final class TableMetaImpl extends AbstractMeta {

private static final long serialVersionUID = 2910000827304539796L;

@SuppressWarnings("unused")
private final Configuration configuration;
Expand Down

0 comments on commit af01732

Please sign in to comment.