org.jooq.VisitListener Maven / Gradle / Ivy
/**
* 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;
import java.sql.PreparedStatement;
import java.util.EventListener;
/**
* A listener for {@link QueryPart} traversal events.
*
* Users may want to centrally inject custom behaviour when rendering their
* {@link QueryPart} objects or when binding values to {@link PreparedStatement}
* s. This service provider allows to hook in callback method implementations
* before or after these events:
*
* - The visit of a {@link Clause}
* - The visit of a {@link QueryPart}
*
*
* The following rules apply to visiting clauses and query parts:
*
* - Clauses may "surround" a query part. See an example below.
* - Not every query part is "surrounded" by a clause
*
*
* An example is given here:
* SELECT 1 FROM [A CROSS JOIN B]
*
* The above example will create the following set of events:
*
*
* {@link Clause#SELECT}
* +-{@link Clause#SELECT_SELECT}
* | +-{@link Clause#FIELD}
* | +-val(1)
* +-{@link Clause#SELECT_FROM}
* +-{@link Clause#TABLE_JOIN}
* +-{@link Clause#TABLE}
* | +-table("A")
* +-{@link Clause#TABLE_JOIN_CROSS}
* +-{@link Clause#TABLE}
* +-table("B")
*
*
* Whatever is not a {@link Clause} in the above example is a {@link QueryPart}.
*
*
A remark about performance
*
* Implementors of this SPI should be wary of performance implications of their
* implementations. The below methods are called for every AST element of every
* query, which produces a lot of calls throughout an application. What would
* otherwise be premature optimisations may have great effect inside the
* VisitListener
. For more details, please refer to this article:
*
* http://blog.jooq.org/2015/02/05/top-10-easy-performance-optimisations-in-
* java/.
*
* @author Lukas Eder
*/
public interface VisitListener extends EventListener {
/**
* Called before entering a {@link Clause}.
*
* @see Context#start(Clause)
*/
void clauseStart(VisitContext context);
/**
* Called after leaving a {@link Clause}.
*
* @see Context#end(Clause)
*/
void clauseEnd(VisitContext context);
/**
* Called before visiting a {@link QueryPart}.
*
* Certain VisitListener
implementations may chose to replace
* the {@link QueryPart} contained in the argument {@link VisitContext}
* through {@link VisitContext#queryPart(QueryPart)}. This can be used for
* many use-cases, for example to add a CHECK OPTION
to an
* Oracle INSERT
statement:
* -- Original query
* INSERT INTO book (id, author_id, title)
* VALUES (10, 15, '1984')
*
* -- Transformed query
* INSERT INTO (
* SELECT * FROM book
* WHERE author_id IN (1, 2, 3)
* WITH CHECK OPTION
* ) (id, author_id, title)
* VALUES (10, 15, '1984')
*
The above SQL transformation allows to prevent inserting
* new books for authors other than those with
* author_id IN (1, 2, 3)
*
* @see Context#visit(QueryPart)
*/
void visitStart(VisitContext context);
/**
* Called after visiting a {@link QueryPart}.
*
* @see Context#visit(QueryPart)
*/
void visitEnd(VisitContext context);
}