diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 8b404f5479..185309670c 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -297,19 +297,22 @@ public interface DSLContext extends Scope , AutoCloseable { */ Meta meta(InformationSchema schema); - /*** - * Access the database meta data for a database represented by a list of DDL - * scripts. The DDL scripts will be parsed using {@link #parser()}, which in - * turn uses the relevant {@link #settings() settings} (e.g. - * {@link Settings#getParseDialect()}) of this object. + /** + * Create meta data from a set of sources. + *

+ * This method creates a {@link Meta} representation from a set of source + * content, which can be any of: + *

*

* This will not connect to your database to get live meta information, * unlike {@link #meta()} and {@link #meta(DatabaseMetaData)}. - *

- * This method is for internal only. Do not reference directly. */ - @Internal - Meta meta(Source... scripts); + Meta meta(Source... sources); /** * Convenience method for {@link Meta#informationSchema()}. diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 678b4e17c9..4e3474b936 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -440,8 +440,8 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri } @Override - public Meta meta(Source... scripts) { - return new DDLMetaProvider(configuration(), scripts).provide(); + public Meta meta(Source... sources) { + return new SourceMetaProvider(configuration(), sources).provide(); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaProvider.java b/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaProvider.java index 411d97ec38..7b75c9062a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaProvider.java +++ b/jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaProvider.java @@ -40,6 +40,8 @@ package org.jooq.impl; import org.jooq.Configuration; import org.jooq.Meta; import org.jooq.MetaProvider; +import org.jooq.Source; +import org.jooq.util.jaxb.tools.MiniJAXB; import org.jooq.util.xml.jaxb.InformationSchema; /** @@ -50,16 +52,33 @@ import org.jooq.util.xml.jaxb.InformationSchema; */ public class InformationSchemaMetaProvider implements MetaProvider { - private final Configuration configuration; + private final Configuration configuration; private final InformationSchema schema; + private final Source[] sources; + + public InformationSchemaMetaProvider(Configuration configuration, Source... sources) { + this.configuration = configuration; + this.schema = null; + this.sources = sources; + } public InformationSchemaMetaProvider(Configuration configuration, InformationSchema schema) { this.configuration = configuration; this.schema = schema; + this.sources = null; } @Override public Meta provide() { - return new InformationSchemaMetaImpl(configuration, schema); + InformationSchema s = schema; + + if (s == null) { + s = new InformationSchema(); + + for (Source source : sources) + MiniJAXB.append(s, MiniJAXB.unmarshal(source.reader(), InformationSchema.class)); + } + + return new InformationSchemaMetaImpl(configuration, s); } } diff --git a/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java b/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java new file mode 100644 index 0000000000..1f734aa301 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java @@ -0,0 +1,93 @@ +/* + * 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 java.io.IOException; +import java.io.Reader; +import java.io.StringWriter; + +import org.jooq.Configuration; +import org.jooq.Meta; +import org.jooq.MetaProvider; +import org.jooq.Source; + + +/** + * A {@link MetaProvider} implementation that can handle different types of + * {@link Source} content. + * + * @author Lukas Eder + */ +final class SourceMetaProvider implements MetaProvider { + + private final Configuration configuration; + private final Source[] sources; + + SourceMetaProvider(Configuration configuration, Source... sources) { + this.configuration = configuration; + this.sources = sources; + } + + @Override + public final Meta provide() { + if (sources.length > 0) { + StringWriter w = new StringWriter(); + + try { + Reader r = sources[0].reader(); + char[] buffer = new char[8192]; + int nRead; + while ((nRead = r.read(buffer, 0, 8192)) >= 0) { + w.write(buffer, 0, nRead); + } + } + catch (IOException e) { + throw new org.jooq.exception.IOException("Could not read source", e); + } + + String s = w.toString(); + sources[0] = Source.of(w.toString()); + + // TODO: Implement more thorough and reusable "isXML()" check in MiniJAXB + if (s.startsWith(" T unmarshal(Reader reader, Class type) { + return unmarshal0(new InputSource(reader), type); + } + public static T unmarshal(InputStream in, Class type) { return unmarshal0(new InputSource(in), type); }