Search in sources :

Example 6 with ExecuteContext

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

the class BatchSingle method executePrepared.

private final int[] executePrepared() {
    ExecuteContext ctx = new DefaultExecuteContext(configuration, new Query[] { query });
    ExecuteListener listener = new ExecuteListeners(ctx);
    Connection connection = ctx.connection();
    // [#1371] fetch bind variables to restore them again, later
    // [#3940] Don't include inlined bind variables
    // [#4062] Make sure we collect also repeated named parameters
    ParamCollector collector = new ParamCollector(configuration, false);
    collector.visit(query);
    List<Param<?>> params = new ArrayList<Param<?>>();
    for (Entry<String, Param<?>> entry : collector.resultList) params.add(entry.getValue());
    DataType<?>[] paramTypes = dataTypes(params.toArray(EMPTY_FIELD));
    try {
        listener.renderStart(ctx);
        // [#1520] TODO: Should the number of bind values be checked, here?
        ctx.sql(create.render(query));
        listener.renderEnd(ctx);
        listener.prepareStart(ctx);
        ctx.statement(connection.prepareStatement(ctx.sql()));
        listener.prepareEnd(ctx);
        for (Object[] bindValues : allBindValues) {
            listener.bindStart(ctx);
            // [#1371] [#2139] Don't bind variables directly onto statement, bind them through the collected params
            //                 list to preserve type information
            // [#3547]         The original query may have no Params specified - e.g. when it was constructed with
            //                 plain SQL. In that case, infer the bind value type directly from the bind value
            visitAll(new DefaultBindContext(configuration, ctx.statement()), (paramTypes.length > 0) ? fields(bindValues, paramTypes) : fields(bindValues));
            listener.bindEnd(ctx);
            ctx.statement().addBatch();
        }
        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) ArrayList(java.util.ArrayList) ExecuteContext(org.jooq.ExecuteContext) ExecuteListener(org.jooq.ExecuteListener) Param(org.jooq.Param) DataType(org.jooq.DataType) ControlFlowSignal(org.jooq.exception.ControlFlowSignal)

Example 7 with ExecuteContext

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

the class AbstractRoutine method executeCallableStatement.

private final int executeCallableStatement() {
    ExecuteContext ctx = new DefaultExecuteContext(configuration, this);
    ExecuteListener listener = new ExecuteListeners(ctx);
    try {
        Connection connection = ctx.connection();
        listener.renderStart(ctx);
        // [#1520] TODO: Should the number of bind values be checked, here?
        ctx.sql(create(configuration).render(this));
        listener.renderEnd(ctx);
        listener.prepareStart(ctx);
        ctx.statement(connection.prepareCall(ctx.sql()));
        // [#1856] TODO: Add Statement flags like timeout here
        listener.prepareEnd(ctx);
        listener.bindStart(ctx);
        using(configuration).bindContext(ctx.statement()).visit(this);
        registerOutParameters(ctx);
        listener.bindEnd(ctx);
        execute0(ctx, listener);
        //         http://tracker.firebirdsql.org/browse/JDBC-350
        if (ctx.family() != FIREBIRD)
            Tools.consumeResultSets(ctx, listener, results, null);
        listener.outStart(ctx);
        fetchOutParameters(ctx);
        listener.outEnd(ctx);
        return 0;
    }// [#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)

Example 8 with ExecuteContext

use of org.jooq.ExecuteContext in project spring-boot by spring-projects.

the class JooqExceptionTranslatorTests method exceptionTranslation.

@Test
public void exceptionTranslation() {
    ExecuteContext context = mock(ExecuteContext.class);
    Configuration configuration = mock(Configuration.class);
    given(context.configuration()).willReturn(configuration);
    given(configuration.dialect()).willReturn(this.dialect);
    given(context.sqlException()).willReturn(this.sqlException);
    this.exceptionTranslator.exception(context);
    ArgumentCaptor<RuntimeException> captor = ArgumentCaptor.forClass(RuntimeException.class);
    verify(context).exception(captor.capture());
    assertThat(captor.getValue()).isInstanceOf(BadSqlGrammarException.class);
}
Also used : Configuration(org.jooq.Configuration) ExecuteContext(org.jooq.ExecuteContext) Test(org.junit.Test)

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