Package org.jooq

Examples of org.jooq.ExecuteContext


            return 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
        DataType<?>[] paramTypes = dataTypes(query.getParams().values().toArray(new Field[0]));

        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
                List<Field<?>> params = (paramTypes.length > 0)
                    ? fields(bindValues, paramTypes)
                    : fields(bindValues);

                visitAll(new DefaultBindContext(configuration, ctx.statement()), params);

                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 {
            Utils.safeClose(listener, ctx);
        }
    }
View Full Code Here


        return queries.length;
    }

    @Override
    public final int[] execute() {
        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 {
            Utils.safeClose(listener, ctx);
        }
    }
View Full Code Here

        return 0;
    }

    private final int executeCallableStatement() {
        Configuration configuration = attachable.getConfiguration();
        ExecuteContext ctx = new DefaultExecuteContext(configuration, this);
        ExecuteListener listener = new ExecuteListeners(ctx);

        try {
            Connection connection = configuration.getConnection();

            listener.renderStart(ctx);
            ctx.sql(create(configuration).render(this));
            listener.renderEnd(ctx);

            listener.prepareStart(ctx);
            ctx.statement(connection.prepareCall(ctx.sql()));
            listener.prepareEnd(ctx);

            listener.bindStart(ctx);
            create(configuration).bind(this, ctx.statement());
            registerOutParameters(configuration, (CallableStatement) ctx.statement());
            listener.bindEnd(ctx);

            // Postgres requires two separate queries running in the same
            // transaction to be executed when fetching refcursor types
            boolean autoCommit = connection.getAutoCommit();
            if (autoCommit && configuration.getDialect() == SQLDialect.POSTGRES) {
                connection.setAutoCommit(false);
            }

            listener.executeStart(ctx);
            ctx.statement().execute();
            listener.executeEnd(ctx);

            if (autoCommit && configuration.getDialect() == SQLDialect.POSTGRES) {
                connection.setAutoCommit(autoCommit);
            }

            fetchOutParameters(ctx);
            return 0;
        }
        catch (SQLException e) {
            throw translate("AbstractRoutine.executeCallableStatement", ctx.sql(), e);
        }
        finally {
            Util.safeClose(listener, ctx);
        }
    }
View Full Code Here

            }

            // [#1191] The following triggers a start event on all listeners.
            // This may be used to provide jOOQ with a JDBC connection, in case
            // this Query / Configuration was previously deserialised
            ExecuteContext ctx = new DefaultExecuteContext(configuration, this);
            ExecuteListener listener = new ExecuteListeners(ctx);

            Connection connection = configuration.getConnection();
            if (connection == null) {
                throw new DetachedException("Cannot execute query. No Connection configured");
            }

            // Ensure that all depending Attachables are attached
            attach(configuration);

            int result = 0;
            try {
                listener.renderStart(ctx);
                ctx.sql(getSQL());
                listener.renderEnd(ctx);

                listener.prepareStart(ctx);
                prepare(ctx);
                listener.prepareEnd(ctx);

                // [#1145] Bind variables only for true prepared statements
                if (executePreparedStatements(getConfiguration().getSettings())) {
                    listener.bindStart(ctx);
                    create(configuration).bind(this, ctx.statement());
                    listener.bindEnd(ctx);
                }

                result = execute(ctx, listener);
                return result;
            }
            catch (SQLException e) {
                throw Util.translate("AbstractQuery.execute", ctx.sql(), e);
            }
            finally {
                if (!keepStatementOpen()) {
                    Util.safeClose(listener, ctx);
                }
View Full Code Here

     * @param rs The JDBC ResultSet to fetch data from
     * @return The resulting jOOQ Result
     */
    @Override
    public final Result<Record> fetch(ResultSet rs) {
        ExecuteContext ctx = new DefaultExecuteContext(this);
        ExecuteListener listener = new ExecuteListeners(ctx);

        try {
            FieldProvider fields = new MetaDataFieldProvider(this, rs.getMetaData());

            ctx.resultSet(rs);
            return new CursorImpl<Record>(ctx, listener, fields).fetch();
        }
        catch (SQLException e) {
            throw Util.translate("Factory.fetch", ctx.sql(), e);
        }
    }
View Full Code Here

                    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, returning, getInto().getRecordType()).fetch();
            return result;
        }
    }
View Full Code Here

    }

    private final int[] executePrepared() {
        Connection connection = create.getConnection();

        ExecuteContext ctx = new DefaultExecuteContext(create, new Query[] { query });
        ExecuteListener listener = new ExecuteListeners(ctx);

        try {
            listener.renderStart(ctx);
            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);
                new DefaultBindContext(create, ctx.statement()).bindValues(bindValues);
                listener.bindEnd(ctx);

                ctx.statement().addBatch();
            }

            listener.executeStart(ctx);
            int[] result = ctx.statement().executeBatch();
            listener.executeEnd(ctx);

            return result;
        }
        catch (SQLException e) {
            throw Util.translate("BatchSingle.execute", ctx.sql(), e);
        }
        finally {
            Util.safeClose(listener, ctx);
        }
    }
View Full Code Here

    @Override
    public final int[] execute() {
        Connection connection = create.getConnection();

        ExecuteContext ctx = new DefaultExecuteContext(create, queries);
        ExecuteListener listener = new ExecuteListeners(ctx);

        try {
            ctx.statement(new PreparedStatementProxy(connection));

            String[] batchSQL = ctx.batchSQL();
            for (int i = 0; i < queries.length; i++) {
                listener.renderStart(ctx);
                batchSQL[i] = create.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();
            listener.executeEnd(ctx);

            return result;
        }
        catch (SQLException e) {
            throw Util.translate("BatchMultiple.execute", ctx.sql(), e);
        }
        finally {
            Util.safeClose(listener, ctx);
        }
    }
View Full Code Here

                    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 (getInto().fields().length > 0)
                returned = returned.into(getInto());
View Full Code Here

        outValues.put(returnParameter, create(configuration).select(field).fetchOne(field));
        return 0;
    }

    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(configuration, (CallableStatement) ctx.statement());
            listener.bindEnd(ctx);

            execute0(ctx, listener);

            /* [pro] xx
            xx xxxxxxx xxx xxxxx xx xxxxxxxxx xxxxxx xxxx xxx xxxx xxxxxxxxx
            xx xxx xxxxxxxxxx xx xxxxxxxx xx xxx xxxxxx
            xx [/pro] */
            Utils.consumeResultSets(ctx, listener, results, null);
            fetchOutParameters(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 {
            Utils.safeClose(listener, ctx);
        }
    }
View Full Code Here

TOP

Related Classes of org.jooq.ExecuteContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.