AbstractContext(Configuration configuration, PreparedStatement stmt) { super(configuration); this.stmt = stmt; VisitListenerProvider[] providers = configuration.visitListenerProviders(); boolean userInternalVisitListener = false; this.visitListeners = new VisitListener[providers.length + (userInternalVisitListener ? 1 : 0)]; for (int i = 0; i < providers.length; i++) this.visitListeners[i] = providers[i].provide(); if (this.visitListeners.length > 0) { this.visitContext = new DefaultVisitContext(); this.visitParts = new ArrayDeque<QueryPart>(); this.visitClauses = new ArrayDeque<Clause>(); } else { this.visitContext = null; this.visitParts = null; this.visitClauses = null; } forcedParamType = SettingsTools.getStatementType(settings()) == StatementType.STATIC_STATEMENT ? ParamType.INLINED : null; }
@Override protected Factory create(Settings settings) { settings = (settings != null) ? settings : new Settings(); RenderMapping mapping = SettingsTools.getRenderMapping(settings); List<MappedSchema> schemata = mapping.getSchemata(); if (schemata.size() == 0) { schemata.add(new MappedSchema() .withInput(TAuthor().getSchema().getName()) .withOutput(Public.PUBLIC.getName())); } else { schemata.get(0) .withInput(TAuthor().getSchema().getName()) .withOutput(Public.PUBLIC.getName()); } return new HSQLDBFactory(getConnection(), settings); }
@Test public void testExecuteListenerOnResultQuery() throws Exception { Executor create = create(); create.getExecuteListeners().add(new ResultQueryListener()); create.setData("Foo", "Bar"); create.setData("Bar", "Baz"); Result<?> result = create.select(TBook_ID(), val("Hello")) .from(TBook()) .where(TBook_ID().in(1, 2)) .fetch(); // [#1145] When inlining variables, no bind events are triggered int plus = (SettingsTools.executePreparedStatements(create.getSettings()) ? 2 : 0); // Check correct order of listener method invocation assertEquals(1, ResultQueryListener.start); assertEquals(2, ResultQueryListener.renderStart); assertEquals(3, ResultQueryListener.renderEnd); assertEquals(4, ResultQueryListener.prepareStart); assertEquals(5, ResultQueryListener.prepareEnd); assertEquals(plus > 0 ? 6 : 0, ResultQueryListener.bindStart); assertEquals(plus > 0 ? 7 : 0, ResultQueryListener.bindEnd); assertEquals(6 + plus, ResultQueryListener.executeStart); assertEquals(7 + plus, ResultQueryListener.executeEnd); assertEquals(8 + plus, ResultQueryListener.fetchStart); assertEquals(9 + plus, ResultQueryListener.resultStart); assertEquals(asList(10 + plus, 12 + plus), ResultQueryListener.recordStart); assertEquals(asList(11 + plus, 13 + plus), ResultQueryListener.recordEnd); assertEquals(14 + plus, ResultQueryListener.resultEnd); assertEquals(15 + plus, ResultQueryListener.fetchEnd); assertEquals(16 + plus, ResultQueryListener.end); assertEquals(2, result.size()); }
/** * Initialise the render mapping's default schema. * * <p>For convenience, this schema-specific factory should override any pre-existing setting */ private final void initDefaultSchema() { org.jooq.conf.SettingsTools.getRenderMapping(getSettings()) .setDefaultSchema(inf89823.tsiss.socialmedia.model.Public.PUBLIC.getName()); }
@Override public final int execute() { if (isExecutable()) { // Get the attached configuration of this query Configuration c = configuration(); // [#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 DefaultExecuteContext ctx = new DefaultExecuteContext(c, this); ExecuteListener listener = new ExecuteListeners(ctx); int result = 0; try { // [#385] If a statement was previously kept open if (keepStatement() && statement != null) { ctx.sql(rendered.sql); ctx.statement(statement); // [#3191] Pre-initialise the ExecuteContext with a previous connection, if available. ctx.connection(c.connectionProvider(), statement.getConnection()); } // [#385] First time statement preparing else { listener.renderStart(ctx); rendered = getSQL0(ctx); ctx.sql(rendered.sql); listener.renderEnd(ctx); rendered.sql = ctx.sql(); // [#3234] Defer initialising of a connection until the prepare step // This optimises unnecessary ConnectionProvider.acquire() calls when // ControlFlowSignals are thrown if (ctx.connection() == null) { throw new DetachedException("Cannot execute query. No Connection configured"); } listener.prepareStart(ctx); prepare(ctx); listener.prepareEnd(ctx); statement = ctx.statement(); } // [#1856] [#4753] Set the query timeout onto the Statement int t = SettingsTools.getQueryTimeout(timeout, ctx.settings()); if (t != 0) { ctx.statement().setQueryTimeout(t); } if ( // [#1145] Bind variables only for true prepared statements // [#2414] Even if parameters are inlined here, child // QueryParts may override this behaviour! executePreparedStatements(c.settings()) && // [#1520] Renderers may enforce static statements, too !Boolean.TRUE.equals(ctx.data(DATA_FORCE_STATIC_STATEMENT))) { listener.bindStart(ctx); if (rendered.bindValues != null) using(c).bindContext(ctx.statement()).visit(rendered.bindValues); listener.bindEnd(ctx); } result = execute(ctx, listener); 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 { // [#2385] Successful fetchLazy() needs to keep open resources if (!keepResultSet() || ctx.exception() != null) { Tools.safeClose(listener, ctx, keepStatement()); } if (!keepStatement()) { statement = null; rendered = null; } } } else { if (log.isDebugEnabled()) log.debug("Query is not executable", this); return 0; } }