Search in sources :

Example 1 with Queries

use of org.jooq.Queries in project jOOQ by jOOQ.

the class DiagnosticsConnection method parse.

final String parse(String sql) {
    Queries queries = null;
    String normalised;
    try {
        queries = parser.parse(sql);
        normalised = normalisingRenderer.render(queries);
    } catch (ParserException exception) {
        normalised = sql;
        listeners.exception(new DefaultDiagnosticsContext("Query could not be parsed.", sql, exception));
    }
    try {
        Set<String> duplicates = null;
        synchronized (DUPLICATE_SQL) {
            duplicates = duplicates(DUPLICATE_SQL, sql, normalised);
        }
        if (duplicates != null)
            listeners.duplicateStatements(new DefaultDiagnosticsContext("Duplicate statements encountered.", sql, normalised, duplicates, null, queries, null));
        List<String> repetitions = repetitions(repeatedSQL, sql, normalised);
        if (repetitions != null)
            listeners.repeatedStatements(new DefaultDiagnosticsContext("Repeated statements encountered.", sql, normalised, null, repetitions, queries, null));
    } catch (Error e) {
        throw e;
    } catch (Throwable exception) {
        listeners.exception(new DefaultDiagnosticsContext("An unexpected exception has occurred. See exception for details.", sql, normalised, null, null, queries, exception));
    }
    return sql;
}
Also used : Queries(org.jooq.Queries)

Example 2 with Queries

use of org.jooq.Queries in project jOOQ by jOOQ.

the class DDLDatabase method load.

private void load(DSLContext ctx, Source source) {
    Reader r = null;
    try {
        Scanner s = new Scanner(r = source.reader()).useDelimiter("\\A");
        Queries queries = ctx.parser().parse(s.hasNext() ? s.next() : "");
        for (Query query : queries) {
            repeat: for (; ; ) {
                try {
                    if (logExecutedQueries)
                        log.info(query);
                    if (logExecutedQueries && logExecutionResults)
                        if (query instanceof ResultQuery)
                            log.info("\n" + ((ResultQuery<?>) query).fetch());
                        else
                            log.info("Update count: " + query.execute());
                    else
                        // [#10008] Execute all queries. Could have FOR UPDATE or other side effects
                        query.execute();
                    break repeat;
                } catch (DataAccessException e) {
                    // [#7039] Auto create missing schemas. We're using the
                    if (Integer.toString(ErrorCode.SCHEMA_NOT_FOUND_1).equals(e.sqlState())) {
                        SQLException cause = e.getCause(SQLException.class);
                        if (cause != null) {
                            Matcher m = P_NAME.matcher(cause.getMessage());
                            if (m.find()) {
                                Query createSchema = ctx.createSchemaIfNotExists(name(m.group(1)));
                                createSchema.execute();
                                log.info(createSchema);
                                continue repeat;
                            }
                        }
                    }
                    throw e;
                }
            }
        }
    } catch (DataAccessException e) {
        // [#9138] Make users aware of the new parse ignore comment syntax
        log.error("DDLDatabase Error", "Your SQL string could not be parsed or interpreted. This may have a variety of reasons, including:\n" + "- The jOOQ parser doesn't understand your SQL\n" + "- The jOOQ DDL simulation logic (translating to H2) cannot simulate your SQL\n" + "\n" + "If you think this is a bug or a feature worth requesting, please report it here: https://github.com/jOOQ/jOOQ/issues/new/choose\n" + "\n" + "As a workaround, you can use the Settings.parseIgnoreComments syntax documented here:\n" + "https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-parser/");
        throw e;
    } finally {
        JDBCUtils.safeClose(r);
    }
}
Also used : Scanner(java.util.Scanner) Queries(org.jooq.Queries) ResultQuery(org.jooq.ResultQuery) Query(org.jooq.Query) SQLException(java.sql.SQLException) Matcher(java.util.regex.Matcher) Reader(java.io.Reader) ResultQuery(org.jooq.ResultQuery) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

Queries (org.jooq.Queries)2 Reader (java.io.Reader)1 SQLException (java.sql.SQLException)1 Scanner (java.util.Scanner)1 Matcher (java.util.regex.Matcher)1 Query (org.jooq.Query)1 ResultQuery (org.jooq.ResultQuery)1 DataAccessException (org.jooq.exception.DataAccessException)1