Search in sources :

Example 1 with ExecuteContext

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

the class SakilaReportService method initDB.

private static void initDB() throws Exception {
    final Properties properties = new Properties();
    properties.load(SakilaReportService.class.getResourceAsStream("/config.properties"));
    Class.forName(properties.getProperty("db.driver"));
    HikariDataSource ds = new HikariDataSource();
    ds.setJdbcUrl(properties.getProperty("db.url"));
    ds.setUsername(properties.getProperty("db.username"));
    ds.setPassword(properties.getProperty("db.password"));
    // Some nice debug logging of formatted queries and the first 5 rows in each result set.
    dsl = DSL.using(new DefaultConfiguration().set(ds).set(DefaultExecuteListenerProvider.providers(new DefaultExecuteListener() {

        @Override
        public void executeEnd(ExecuteContext ctx) {
            Configuration config = ctx.configuration().derive();
            config.settings().setRenderFormatted(true);
            log.info("\n" + DSL.using(config).renderInlined(ctx.query()));
        }

        @Override
        public void fetchEnd(ExecuteContext ctx) {
            log.info("\n" + ctx.result().format(5));
        }
    })));
}
Also used : DefaultExecuteListener(org.jooq.impl.DefaultExecuteListener) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) ExecuteContext(org.jooq.ExecuteContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Properties(java.util.Properties)

Example 2 with ExecuteContext

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

the class AbstractDatabase method create.

@SuppressWarnings("serial")
protected final DSLContext create(boolean muteExceptions) {
    // [#3800] Make sure that faulty queries are logged in a formatted
    //         way to help users provide us with bug reports
    final Configuration configuration = create0().configuration();
    if (muteExceptions) {
        return DSL.using(configuration);
    } else {
        final Settings newSettings = SettingsTools.clone(configuration.settings()).withRenderFormatted(true);
        final ExecuteListenerProvider[] oldProviders = configuration.executeListenerProviders();
        final ExecuteListenerProvider[] newProviders = new ExecuteListenerProvider[oldProviders.length + 1];
        System.arraycopy(oldProviders, 0, newProviders, 0, oldProviders.length);
        newProviders[oldProviders.length] = new DefaultExecuteListenerProvider(new DefaultExecuteListener() {

            class SQLPerformanceWarning extends Exception {
            }

            @Override
            public void start(ExecuteContext ctx) {
                //         SQLPerformanceWarning.
                if (!initialised) {
                    DSL.using(configuration).selectOne().fetch();
                    initialised = true;
                }
            }

            @Override
            public void executeStart(ExecuteContext ctx) {
                ctx.data("org.jooq.util.AbstractDatabase.watch", new StopWatch());
            }

            @Override
            public void executeEnd(ExecuteContext ctx) {
                StopWatch watch = (StopWatch) ctx.data("org.jooq.util.AbstractDatabase.watch");
                if (watch.split() > TimeUnit.SECONDS.toNanos(5L)) {
                    watch.splitWarn("Slow SQL");
                    log.warn("Slow SQL", "jOOQ Meta executed a slow query (slower than 5 seconds)" + "\n\n" + "Please report this bug here: https://github.com/jOOQ/jOOQ/issues/new\n\n" + formatted(ctx.query()), new SQLPerformanceWarning());
                }
            }

            @Override
            public void exception(ExecuteContext ctx) {
                log.warn("SQL exception", "Exception while executing meta query: " + (ctx.sqlException() != null ? ctx.sqlException().getMessage() : ctx.exception() != null ? ctx.exception().getMessage() : "No exception available") + "\n\n" + "Please report this bug here: https://github.com/jOOQ/jOOQ/issues/new\n\n" + formatted(ctx.query()));
            }

            private String formatted(Query query) {
                return DSL.using(configuration.derive(newSettings)).renderInlined(query);
            }
        });
        return DSL.using(configuration.derive(newProviders));
    }
}
Also used : DefaultExecuteListener(org.jooq.impl.DefaultExecuteListener) Configuration(org.jooq.Configuration) Query(org.jooq.Query) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) ExecuteListenerProvider(org.jooq.ExecuteListenerProvider) ExecuteContext(org.jooq.ExecuteContext) Settings(org.jooq.conf.Settings) StopWatch(org.jooq.tools.StopWatch)

Example 3 with ExecuteContext

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

the class DefaultDSLContext method fetchLazy.

@Override
public Cursor<Record> fetchLazy(ResultSet rs, Field<?>... fields) {
    ExecuteContext ctx = new DefaultExecuteContext(configuration());
    ExecuteListener listener = new ExecuteListeners(ctx);
    ctx.resultSet(rs);
    return new CursorImpl<Record>(ctx, listener, fields, null, false, true);
}
Also used : ExecuteContext(org.jooq.ExecuteContext) ExecuteListener(org.jooq.ExecuteListener)

Example 4 with ExecuteContext

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

the class AbstractDMLQuery method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected final int execute(ExecuteContext ctx, ExecuteListener listener) throws SQLException {
    if (returning.isEmpty()) {
        return super.execute(ctx, listener);
    } else {
        int result = 1;
        ResultSet rs;
        switch(ctx.family()) {
            // SQLite can select _rowid_ after the insert
            case SQLITE:
                {
                    listener.executeStart(ctx);
                    result = ctx.statement().executeUpdate();
                    ctx.rows(result);
                    listener.executeEnd(ctx);
                    DSLContext create = DSL.using(ctx.configuration());
                    returned = create.select(returning).from(table).where(rowid().equal(rowid().getDataType().convert(create.lastID()))).fetchInto(table);
                    return result;
                }
            case CUBRID:
                {
                    listener.executeStart(ctx);
                    result = ctx.statement().executeUpdate();
                    ctx.rows(result);
                    listener.executeEnd(ctx);
                    selectReturning(ctx.configuration(), create(ctx.configuration()).lastID());
                    return result;
                }
            case DERBY:
            case H2:
            case MARIADB:
            case MYSQL:
                {
                    listener.executeStart(ctx);
                    result = ctx.statement().executeUpdate();
                    ctx.rows(result);
                    listener.executeEnd(ctx);
                    rs = ctx.statement().getGeneratedKeys();
                    try {
                        List<Object> list = new ArrayList<Object>();
                        // from getGeneratedKeys() sometimes
                        if (rs != null)
                            while (rs.next()) list.add(rs.getObject(1));
                        selectReturning(ctx.configuration(), list.toArray());
                        return result;
                    } finally {
                        JDBCUtils.safeClose(rs);
                    }
                }
            // in the Postgres JDBC driver
            case FIREBIRD:
            case POSTGRES:
                {
                    listener.executeStart(ctx);
                    rs = ctx.statement().executeQuery();
                    listener.executeEnd(ctx);
                    break;
                }
            case HSQLDB:
            default:
                {
                    listener.executeStart(ctx);
                    result = ctx.statement().executeUpdate();
                    ctx.rows(result);
                    listener.executeEnd(ctx);
                    rs = ctx.statement().getGeneratedKeys();
                    break;
                }
        }
        ExecuteContext ctx2 = new DefaultExecuteContext(ctx.configuration());
        ExecuteListener listener2 = new ExecuteListeners(ctx2);
        ctx2.resultSet(rs);
        returned = new CursorImpl<R>(ctx2, listener2, fieldArray(returning), null, false, true).fetch();
        // [#3682] Plain SQL tables do not have any fields
        if (table.fields().length > 0)
            returned = returned.into(table);
        //         execute this logic
        if (returned.size() > 0 || ctx.family() != HSQLDB) {
            result = returned.size();
            ctx.rows(result);
        }
        return result;
    }
}
Also used : ResultSet(java.sql.ResultSet) DSLContext(org.jooq.DSLContext) ExecuteContext(org.jooq.ExecuteContext) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ExecuteListener(org.jooq.ExecuteListener)

Example 5 with ExecuteContext

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

the class BatchMultiple method execute.

static int[] execute(final Configuration configuration, final Query[] queries) {
    ExecuteContext ctx = new DefaultExecuteContext(configuration, queries);
    ExecuteListener listener = new ExecuteListeners(ctx);
    Connection connection = ctx.connection();
    try {
        ctx.statement(new SettingsEnabledPreparedStatement(connection));
        String[] batchSQL = ctx.batchSQL();
        for (int i = 0; i < queries.length; i++) {
            listener.renderStart(ctx);
            batchSQL[i] = DSL.using(configuration).renderInlined(queries[i]);
            listener.renderEnd(ctx);
        }
        for (String sql : batchSQL) {
            ctx.sql(sql);
            listener.prepareStart(ctx);
            ctx.statement().addBatch(sql);
            listener.prepareEnd(ctx);
            ctx.sql(null);
        }
        listener.executeStart(ctx);
        int[] result = ctx.statement().executeBatch();
        int[] batchRows = ctx.batchRows();
        for (int i = 0; i < batchRows.length && i < result.length; i++) batchRows[i] = result[i];
        listener.executeEnd(ctx);
        return result;
    }// [#3427] ControlFlowSignals must not be passed on to ExecuteListners
     catch (ControlFlowSignal e) {
        throw e;
    } catch (RuntimeException e) {
        ctx.exception(e);
        listener.exception(ctx);
        throw ctx.exception();
    } catch (SQLException e) {
        ctx.sqlException(e);
        listener.exception(ctx);
        throw ctx.exception();
    } finally {
        Tools.safeClose(listener, ctx);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecuteContext(org.jooq.ExecuteContext) ExecuteListener(org.jooq.ExecuteListener) ControlFlowSignal(org.jooq.exception.ControlFlowSignal)

Aggregations

ExecuteContext (org.jooq.ExecuteContext)8 ExecuteListener (org.jooq.ExecuteListener)5 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Configuration (org.jooq.Configuration)3 ControlFlowSignal (org.jooq.exception.ControlFlowSignal)3 ArrayList (java.util.ArrayList)2 DefaultExecuteListener (org.jooq.impl.DefaultExecuteListener)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 ResultSet (java.sql.ResultSet)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Properties (java.util.Properties)1 DSLContext (org.jooq.DSLContext)1 DataType (org.jooq.DataType)1 ExecuteListenerProvider (org.jooq.ExecuteListenerProvider)1 Param (org.jooq.Param)1 Query (org.jooq.Query)1 Settings (org.jooq.conf.Settings)1 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)1