Search in sources :

Example 1 with DefaultVisitListener

use of org.jooq.impl.DefaultVisitListener in project jOOQ by jOOQ.

the class DDLDatabase method export.

@Override
protected void export() throws Exception {
    Settings defaultSettings = new Settings();
    String scripts = getProperties().getProperty("scripts");
    String encoding = getProperties().getProperty("encoding", "UTF-8");
    String sort = getProperties().getProperty("sort", "semantic").toLowerCase();
    final String defaultNameCase = getProperties().getProperty("defaultNameCase", "as_is").toUpperCase();
    boolean parseIgnoreComments = !"false".equalsIgnoreCase(getProperties().getProperty("parseIgnoreComments"));
    String parseIgnoreCommentStart = getProperties().getProperty("parseIgnoreCommentStart", defaultSettings.getParseIgnoreCommentStart());
    String parseIgnoreCommentStop = getProperties().getProperty("parseIgnoreCommentStop", defaultSettings.getParseIgnoreCommentStop());
    logExecutedQueries = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutedQueries"));
    logExecutionResults = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutionResults"));
    if (isBlank(scripts)) {
        scripts = "";
        log.warn("No scripts defined", "It is recommended that you provide an explicit script directory to scan");
    }
    try {
        final DSLContext ctx = DSL.using(connection(), new Settings().withParseIgnoreComments(parseIgnoreComments).withParseIgnoreCommentStart(parseIgnoreCommentStart).withParseIgnoreCommentStop(parseIgnoreCommentStop).withParseUnknownFunctions(ParseUnknownFunctions.IGNORE));
        // [#7771] [#8011] Ignore all parsed storage clauses when executing the statements
        ctx.data("org.jooq.ddl.ignore-storage-clauses", true);
        // [#8910] Parse things a bit differently for use with the DDLDatabase
        ctx.data("org.jooq.ddl.parse-for-ddldatabase", true);
        if (!"AS_IS".equals(defaultNameCase)) {
            ctx.configuration().set(new DefaultVisitListener() {

                @Override
                public void visitStart(VisitContext vc) {
                    if (vc.queryPart() instanceof Name) {
                        Name n = (Name) vc.queryPart();
                        Name[] parts = n.parts();
                        boolean changed = false;
                        for (int i = 0; i < parts.length; i++) {
                            // flag for DSL.systemName() names
                            if (parts[i].quoted() == Quoted.UNQUOTED) {
                                parts[i] = DSL.quotedName("UPPER".equals(defaultNameCase) ? parts[i].first().toUpperCase(renderLocale(ctx.settings())) : parts[i].first().toLowerCase(renderLocale(ctx.settings())));
                                changed = true;
                            }
                        }
                        if (changed)
                            vc.queryPart(DSL.name(parts));
                    }
                }
            });
        }
        new FilePattern().encoding(encoding).basedir(new File(getBasedir())).pattern(scripts).sort(Sort.of(sort)).load(source -> DDLDatabase.this.load(ctx, source));
    } catch (ParserException e) {
        log.error("An exception occurred while parsing script source : " + scripts + ". Please report this error to https://github.com/jOOQ/jOOQ/issues/new", e);
        throw e;
    }
}
Also used : ParserException(org.jooq.impl.ParserException) VisitContext(org.jooq.VisitContext) DSLContext(org.jooq.DSLContext) FilePattern(org.jooq.FilePattern) DefaultVisitListener(org.jooq.impl.DefaultVisitListener) File(java.io.File) Settings(org.jooq.conf.Settings) Name(org.jooq.Name)

Aggregations

File (java.io.File)1 DSLContext (org.jooq.DSLContext)1 FilePattern (org.jooq.FilePattern)1 Name (org.jooq.Name)1 VisitContext (org.jooq.VisitContext)1 Settings (org.jooq.conf.Settings)1 DefaultVisitListener (org.jooq.impl.DefaultVisitListener)1 ParserException (org.jooq.impl.ParserException)1