diff --git a/jOOQ/src/main/java/org/jooq/Meta.java b/jOOQ/src/main/java/org/jooq/Meta.java index c998002d3a..8a381b4f6b 100644 --- a/jOOQ/src/main/java/org/jooq/Meta.java +++ b/jOOQ/src/main/java/org/jooq/Meta.java @@ -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. *

* For those databases that don't really support JDBC meta data catalogs, a * single empty catalog (named "") will be returned. In other * words, there is always at least one catalog in a database. - *

- * NOTE: Catalogs are experimental in jOOQ 3.0 * * @throws DataAccessException If something went wrong fetching the meta * objects @@ -95,7 +93,27 @@ public interface Meta { List 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 + * null 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 + * null 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 @@ -104,7 +122,25 @@ public interface Meta { List 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 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 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 @@ -113,7 +149,25 @@ public interface Meta { List> 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> 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> 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 @@ -122,7 +176,25 @@ public interface Meta { List> 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> 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> 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 diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractMeta.java b/jOOQ/src/main/java/org/jooq/impl/AbstractMeta.java new file mode 100644 index 0000000000..5a0b64adbd --- /dev/null +++ b/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 cachedCatalogs; + private final Map cachedQualifiedSchemas; + private final Map> cachedQualifiedTables; + private final Map> cachedQualifiedSequences; + private final Map> cachedUnqualifiedSchemas; + private final Map>> cachedUnqualifiedTables; + private final Map>> cachedUnqualifiedSequences; + + AbstractMeta() { + + // [#7165] TODO: Allow for opting out of this cache + this.cachedCatalogs = new LinkedHashMap(); + this.cachedQualifiedSchemas = new LinkedHashMap(); + this.cachedQualifiedTables = new LinkedHashMap>(); + this.cachedQualifiedSequences = new LinkedHashMap>(); + this.cachedUnqualifiedSchemas = new LinkedHashMap>(); + this.cachedUnqualifiedTables = new LinkedHashMap>>(); + this.cachedUnqualifiedSequences = new LinkedHashMap>>(); + } + + @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 getSchemas(String name) { + return getSchemas(name(name)); + } + + @Override + public final List getSchemas(Name name) { + return get(name, new Iterable() { + @Override + public Iterator iterator() { + return getSchemas().iterator(); + } + }, cachedQualifiedSchemas, cachedUnqualifiedSchemas); + } + + @Override + public final List> getTables(String name) { + return getTables(name(name)); + } + + @Override + public final List> getTables(Name name) { + return get(name, new Iterable>() { + @Override + public Iterator> iterator() { + return getTables().iterator(); + } + }, cachedQualifiedTables, cachedUnqualifiedTables); + } + + @Override + public final List> getSequences(String name) { + return getSequences(name(name)); + } + + @Override + public final List> getSequences(Name name) { + return get(name, new Iterable>() { + @Override + public Iterator> iterator() { + return getSequences().iterator(); + } + }, cachedQualifiedSequences, cachedUnqualifiedSequences); + } + + private final List get(Name name, Iterable i, Map qualified, Map> unqualified) { + if (qualified.isEmpty()) { + for (T object : i) { + Name q = object.getQualifiedName(); + Name u = object.getUnqualifiedName(); + + qualified.put(q, object); + + List list = unqualified.get(u); + if (list == null) { + list = new ArrayList(); + unqualified.get(u); + } + + list.add(object); + } + } + + T object = qualified.get(name); + if (object != null) + return Collections.singletonList(object); + + List list = unqualified.get(name); + if (list == null) + return Collections.emptyList(); + else + return Collections.unmodifiableList(list); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/CatalogMetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/CatalogMetaImpl.java index 9ffbb8c828..32f06a2837 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CatalogMetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CatalogMetaImpl.java @@ -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; @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaImpl.java index a44da1c080..1e9d8992c6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaImpl.java @@ -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; @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java index a1a66628c2..4d0812c63b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java @@ -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 INVERSE_SCHEMA_CATALOG = EnumSet.of(MYSQL, MARIADB); private static final EnumSet CURRENT_TIMESTAMP_COLUMN_DEFAULT = EnumSet.of(MYSQL, MARIADB); diff --git a/jOOQ/src/main/java/org/jooq/impl/SchemaMetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/SchemaMetaImpl.java index c6765e193c..3fafbbf28d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SchemaMetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SchemaMetaImpl.java @@ -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; @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/TableMetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/TableMetaImpl.java index e9d0c071a0..094fbbc664 100644 --- a/jOOQ/src/main/java/org/jooq/impl/TableMetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/TableMetaImpl.java @@ -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; @@ -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;