[jOOQ/jOOQ#9429] Add DSLContext.meta(Query...) as a shortcut for meta(Source...)
This commit is contained in:
parent
86ea965ea2
commit
fdf10aea70
@ -314,6 +314,14 @@ public interface DSLContext extends Scope , AutoCloseable {
|
||||
*/
|
||||
Meta meta(Source... sources);
|
||||
|
||||
/**
|
||||
* Create meta data from a set of DDL queries.
|
||||
* <p>
|
||||
* This works the same way as {@link #meta(Source...)}, without the need of
|
||||
* parsing the DDL scripts.
|
||||
*/
|
||||
Meta meta(Query... queries);
|
||||
|
||||
/**
|
||||
* Convenience method for {@link Meta#informationSchema()}.
|
||||
*
|
||||
|
||||
@ -79,10 +79,13 @@ import org.jooq.UniqueKey;
|
||||
import org.jooq.exception.DataAccessException;
|
||||
import org.jooq.exception.DataDefinitionException;
|
||||
import org.jooq.impl.ConstraintImpl.Action;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
final class DDLInterpreter {
|
||||
|
||||
private static final JooqLogger log = JooqLogger.getLogger(DDLInterpreter.class);
|
||||
|
||||
private final Configuration configuration;
|
||||
private final Map<Name, MutableCatalog> catalogs = new LinkedHashMap<>();
|
||||
private final MutableCatalog defaultCatalog;
|
||||
@ -118,6 +121,8 @@ final class DDLInterpreter {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
final void accept(Query query) {
|
||||
log.info(query);
|
||||
|
||||
if (query instanceof CreateSchemaImpl)
|
||||
accept0((CreateSchemaImpl) query);
|
||||
else if (query instanceof AlterSchemaImpl)
|
||||
|
||||
@ -44,7 +44,6 @@ import org.jooq.Configuration;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Meta;
|
||||
import org.jooq.MetaProvider;
|
||||
import org.jooq.Queries;
|
||||
import org.jooq.Query;
|
||||
import org.jooq.Source;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
@ -65,10 +64,18 @@ final class DDLInterpreterMetaProvider implements MetaProvider {
|
||||
|
||||
private final Configuration configuration;
|
||||
private final Source[] scripts;
|
||||
private final Query[] queries;
|
||||
|
||||
public DDLInterpreterMetaProvider(Configuration configuration, Source... scripts) {
|
||||
this.configuration = configuration == null ? new DefaultConfiguration() : configuration;
|
||||
this.scripts = scripts;
|
||||
this.queries = null;
|
||||
}
|
||||
|
||||
public DDLInterpreterMetaProvider(Configuration configuration, Query... queries) {
|
||||
this.configuration = configuration == null ? new DefaultConfiguration() : configuration;
|
||||
this.scripts = null;
|
||||
this.queries = queries;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -76,8 +83,13 @@ final class DDLInterpreterMetaProvider implements MetaProvider {
|
||||
final DDLInterpreter interpreter = new DDLInterpreter(configuration);
|
||||
Configuration localConfiguration = configuration.derive();
|
||||
DSLContext ctx = DSL.using(localConfiguration);
|
||||
for (Source script : scripts)
|
||||
loadScript(ctx, script, interpreter);
|
||||
|
||||
if (scripts != null)
|
||||
for (Source script : scripts)
|
||||
loadScript(ctx, script, interpreter);
|
||||
else
|
||||
for (Query query : queries)
|
||||
interpreter.accept(query);
|
||||
|
||||
return interpreter.meta();
|
||||
}
|
||||
@ -86,12 +98,9 @@ final class DDLInterpreterMetaProvider implements MetaProvider {
|
||||
Reader reader = source.reader();
|
||||
try {
|
||||
Scanner s = new Scanner(reader).useDelimiter("\\A");
|
||||
Queries queries = ctx.parser().parse(s.hasNext() ? s.next() : "");
|
||||
|
||||
for (Query query : queries) {
|
||||
for (Query query : ctx.parser().parse(s.hasNext() ? s.next() : ""))
|
||||
interpreter.accept(query);
|
||||
log.info(query);
|
||||
}
|
||||
}
|
||||
catch (ParserException e) {
|
||||
log.error("An exception occurred while parsing a DDL script: " + e.getMessage()
|
||||
|
||||
@ -444,6 +444,11 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
return new SourceMetaProvider(configuration(), sources).provide();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Meta meta(Query... queries) {
|
||||
return new DDLInterpreterMetaProvider(configuration(), queries).provide();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InformationSchema informationSchema(Catalog catalog) {
|
||||
return InformationSchemaExport.exportCatalogs(configuration(), Arrays.asList(catalog));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user