org.jooq.impl.AbstractContext Maven / Gradle / Ivy
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.impl;
import static org.jooq.conf.ParamType.INDEXED;
import static org.jooq.impl.Utils.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;
/**
* @author Lukas Eder
*/
@SuppressWarnings("unchecked")
abstract class AbstractContext> extends AbstractScope implements Context {
final PreparedStatement stmt;
boolean declareFields;
boolean declareTables;
boolean declareWindows;
boolean declareCTE;
boolean subquery;
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
ParamType paramType = ParamType.INDEXED;
boolean qualify = true;
CastMode castMode = CastMode.DEFAULT;
AbstractContext(Configuration configuration, PreparedStatement stmt) {
super(configuration);
this.stmt = stmt;
this.visitClauses = new ArrayDeque();
VisitListenerProvider[] providers = configuration.visitListenerProviders();
this.visitListeners = new VisitListener[providers.length + 1];
this.visitContext = new DefaultVisitContext();
this.visitParts = new ArrayDeque();
for (int i = 0; i < providers.length; i++) {
this.visitListeners[i] = providers[i].provide();
}
this.visitListeners[providers.length] = new InternalVisitListener();
}
// ------------------------------------------------------------------------
// 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.addLast(clause);
for (VisitListener listener : visitListeners) {
listener.clauseStart(visitContext);
}
}
return (C) this;
}
@Override
public final C end(Clause clause) {
if (clause != 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) {
visitParts.addLast(part);
for (VisitListener listener : visitListeners) {
listener.visitStart(visitContext);
}
return visitParts.peekLast();
}
private final void end(QueryPart part) {
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