[jOOQ/jOOQ#9426] DSLContext.meta(Source...) should be able to read InformationSchema from sources
This commit is contained in:
parent
5bd99d0f7c
commit
e6f14c3cf4
@ -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.
|
||||
* <p>
|
||||
* This method creates a {@link Meta} representation from a set of source
|
||||
* content, which can be any of:
|
||||
* <ul>
|
||||
* <li>A set of DDL scripts, which will be parsed using
|
||||
* {@link #parser()}.</li>
|
||||
* <li>A set of XML files, which will be unmarshalled into
|
||||
* {@link InformationSchema} objects.</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* This will not connect to your database to get live meta information,
|
||||
* unlike {@link #meta()} and {@link #meta(DatabaseMetaData)}.
|
||||
* <p>
|
||||
* 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()}.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
93
jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java
Normal file
93
jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java
Normal file
@ -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("<?xml") || s.startsWith("<information_schema") || s.startsWith("<!--"))
|
||||
return new InformationSchemaMetaProvider(configuration, sources).provide();
|
||||
}
|
||||
|
||||
return new DDLMetaProvider(configuration, sources).provide();
|
||||
}
|
||||
}
|
||||
@ -147,6 +147,10 @@ public final class MiniJAXB {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends XMLAppendable> T unmarshal(Reader reader, Class<T> type) {
|
||||
return unmarshal0(new InputSource(reader), type);
|
||||
}
|
||||
|
||||
public static <T extends XMLAppendable> T unmarshal(InputStream in, Class<T> type) {
|
||||
return unmarshal0(new InputSource(in), type);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user