org.jooq.impl.AbstractContext Maven / Gradle / Ivy
Show all versions of payment-retries-plugin Show documentation
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static java.util.Arrays.asList;
// ...
// ...
// ...
// ...
import static org.jooq.conf.ParamType.INDEXED;
import static org.jooq.impl.Tools.EMPTY_CLAUSE;
import static org.jooq.impl.Tools.EMPTY_QUERYPART;
import static org.jooq.impl.Tools.DataKey.DATA_OMIT_CLAUSE_EVENT_EMISSION;
import java.sql.PreparedStatement;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import org.jooq.BindContext;
import org.jooq.Clause;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.QueryPart;
import org.jooq.QueryPartInternal;
import org.jooq.RenderContext;
import org.jooq.RenderContext.CastMode;
import org.jooq.SQLDialect;
import org.jooq.Table;
import org.jooq.VisitContext;
import org.jooq.VisitListener;
import org.jooq.VisitListenerProvider;
import org.jooq.conf.ParamType;
import org.jooq.conf.Settings;
import org.jooq.conf.SettingsTools;
import org.jooq.conf.StatementType;
/**
* @author Lukas Eder
*/
@SuppressWarnings("unchecked")
abstract class AbstractContext> extends AbstractScope implements Context {
final PreparedStatement stmt;
boolean declareFields;
boolean declareTables;
boolean declareAliases;
boolean declareWindows;
boolean declareCTE;
boolean subquery;
int stringLiteral;
String stringLiteralEscapedApos = "'";
int index;
// [#2665] VisitListener API
final VisitListener[] visitListeners;
private final Deque visitClauses;
private final DefaultVisitContext visitContext;
private final Deque visitParts;
// [#2694] Unified RenderContext and BindContext traversal
final ParamType forcedParamType;
ParamType paramType = ParamType.INDEXED;
boolean qualifySchema = true;
boolean qualifyCatalog = true;
CastMode castMode = CastMode.DEFAULT;
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();
this.visitClauses = new ArrayDeque();
}
else {
this.visitContext = null;
this.visitParts = null;
this.visitClauses = null;
}
forcedParamType = SettingsTools.getStatementType(settings()) == StatementType.STATIC_STATEMENT
? ParamType.INLINED
: null;
}
// ------------------------------------------------------------------------
// VisitListener API
// ------------------------------------------------------------------------
@Override
public final C visit(QueryPart part) {
if (part != null) {
// Issue start clause events
// -----------------------------------------------------------------
Clause[] clauses = visitListeners.length > 0 ? clause(part) : null;
if (clauses != null)
for (int i = 0; i < clauses.length; i++)
start(clauses[i]);
// Perform the actual visiting, or recurse into the replacement
// -----------------------------------------------------------------
QueryPart original = part;
QueryPart replacement = start(part);
if (original == replacement)
visit0(original);
else
visit0(replacement);
end(replacement);
// Issue end clause events
// -----------------------------------------------------------------
if (clauses != null)
for (int i = clauses.length - 1; i >= 0; i--)
end(clauses[i]);
}
return (C) this;
}
/**
* Emit a clause from a query part being visited.
*
* This method returns a clause to emit as a surrounding event before /
* after visiting a query part. This is needed for all reusable query parts,
* whose clause type is ambiguous at the container site. An example:
*
* SELECT * FROM [A CROSS JOIN B]
*
* The type of the above JoinTable
modelling
* A CROSS JOIN B
is not known to the surrounding
* SELECT
statement, which only knows {@link Table} types. The
* {@link Clause#TABLE_JOIN} event that is required to be emitted around the
* {@link Context#visit(QueryPart)} event has to be issued here in
* AbstractContext
.
*/
private final Clause[] clause(QueryPart part) {
if (part instanceof QueryPartInternal && data(DATA_OMIT_CLAUSE_EVENT_EMISSION) == null)
return ((QueryPartInternal) part).clauses(this);
return null;
}
@Override
public final C start(Clause clause) {
if (clause != null && visitClauses != null) {
visitClauses.addLast(clause);
for (VisitListener listener : visitListeners)
listener.clauseStart(visitContext);
}
return (C) this;
}
@Override
public final C end(Clause clause) {
if (clause != null && visitClauses != null) {
for (VisitListener listener : visitListeners)
listener.clauseEnd(visitContext);
if (visitClauses.removeLast() != clause)
throw new IllegalStateException("Mismatch between visited clauses!");
}
return (C) this;
}
private final QueryPart start(QueryPart part) {
if (visitParts != null) {
visitParts.addLast(part);
for (VisitListener listener : visitListeners)
listener.visitStart(visitContext);
return visitParts.peekLast();
}
else {
return part;
}
}
private final void end(QueryPart part) {
if (visitParts != null) {
for (VisitListener listener : visitListeners)
listener.visitEnd(visitContext);
if (visitParts.removeLast() != part)
throw new RuntimeException("Mismatch between visited query parts");
}
}
/**
* A {@link VisitContext} is always in the scope of the current
* {@link RenderContext}.
*/
private class DefaultVisitContext implements VisitContext {
@Override
public final Map