FileDocCategorySizeDatePackage
SubSelectExpression.javaAPI DocGlassfish v2 API10014Tue May 22 16:54:34 BST 2007oracle.toplink.essentials.internal.expressions

SubSelectExpression

public class SubSelectExpression extends Expression
This is used to support subselects. The subselect represents a mostly independent (has own expression builder) query using a report query. Subselects can be used for, in (single column), exists (empty or non-empty), comparisons (single value).

Fields Summary
protected Expression
baseExpression
protected ReportQuery
subQuery
Constructors Summary
public SubSelectExpression()

        super();
    
public SubSelectExpression(ReportQuery query, Expression baseExpression)

        this();
        this.subQuery = query;
        this.baseExpression = baseExpression;
    
Methods Summary
public java.lang.StringdescriptionOfNodeType()
INTERNAL: Used in debug printing of this node.

        return "SubSelect";
    
public oracle.toplink.essentials.expressions.ExpressiongetBaseExpression()

        return baseExpression;
    
public oracle.toplink.essentials.expressions.ExpressionBuildergetBuilder()
Return the builder from the defining expression.

        return getBaseExpression().getBuilder();
    
public oracle.toplink.essentials.queryframework.ReportQuerygetSubQuery()

        return subQuery;
    
public voiditerateOn(oracle.toplink.essentials.internal.expressions.ExpressionIterator iterator)
INTERNAL: For iterating using an inner class

        super.iterateOn(iterator);
        if (baseExpression != null) {
            baseExpression.iterateOn(iterator);
        }

        // For Flashback: It is now possible to create iterators that will span
        // the entire expression, even the where clause embedded in a subQuery.
        if (iterator.shouldIterateOverSubSelects()) {
            if (getSubQuery().getSelectionCriteria() != null) {
                getSubQuery().getSelectionCriteria().iterateOn(iterator);
            } else {
                getSubQuery().getExpressionBuilder().iterateOn(iterator);
            }
        }
    
public oracle.toplink.essentials.expressions.Expressionnormalize(oracle.toplink.essentials.internal.expressions.ExpressionNormalizer normalizer)
INTERNAL: The subquery must be normalized with the knowledge of the outer statement for outer references and correct aliasing. For CR#4223 it will now be normalized after the outer statement is, rather than somewhere in the middle of the outer statement's normalize.

        //has no effect but validateNode is here for consistency
        validateNode();
        // Defer normalization of this expression until later.
        normalizer.addSubSelectExpression(this);
        normalizer.getStatement().setRequiresAliases(true);
        return this;
    
public oracle.toplink.essentials.expressions.ExpressionnormalizeSubSelect(oracle.toplink.essentials.internal.expressions.ExpressionNormalizer normalizer, java.util.Dictionary clonedExpressions)
INTERNAL: Normalize this expression now that the parent statment has been normalized. For CR#4223

        // Anonymous subqueries: The following is to support sub-queries created
        // on the fly by OSQL Expressions isEmpty(), isNotEmpty(), size().
        if (!getSubQuery().isCallQuery() && (getSubQuery().getReferenceClass() == null)) {
            ReportQuery subQuery = getSubQuery();
            Expression criteria = subQuery.getSelectionCriteria();

            // The criteria should be of form builder.equal(exp), where exp belongs
            // to the parent statement and has already been normalized, hence it
            // knows its reference class.
            if (criteria instanceof LogicalExpression) {
                criteria = ((LogicalExpression)criteria).getFirstChild();
            }
            if (criteria instanceof RelationExpression) {
                Expression rightChild = ((RelationExpression)criteria).getSecondChild();
                if (rightChild instanceof QueryKeyExpression) {
                    subQuery.setReferenceClass(((QueryKeyExpression)rightChild).getDescriptor().getJavaClass());
                }
            }
        }

        //has no effect but validateNode is here for consistency
        validateNode();
        getSubQuery().prepareSubSelect(normalizer.getSession(), null, clonedExpressions);
        if (!getSubQuery().isCallQuery()) {
            SQLSelectStatement statement = (SQLSelectStatement)((StatementQueryMechanism)getSubQuery().getQueryMechanism()).getSQLStatement();

            // setRequiresAliases was already set for parent statement.
            statement.setRequiresAliases(true);
            statement.setParentStatement(normalizer.getStatement());
            statement.normalize(normalizer.getSession(), getSubQuery().getDescriptor(), clonedExpressions);
        }
        return this;
    
protected voidpostCopyIn(java.util.Dictionary alreadyDone)
The query must be cloned, and the sub-expression must be cloned using the same outer expression identity.

        super.postCopyIn(alreadyDone);
        setBaseExpression(getBaseExpression().copiedVersionFrom(alreadyDone));
        ReportQuery clonedQuery = (ReportQuery)getSubQuery().clone();
        if ((!clonedQuery.isCallQuery()) && (clonedQuery.getSelectionCriteria() != null)) {
            clonedQuery.setSelectionCriteria(getSubQuery().getSelectionCriteria().copiedVersionFrom(alreadyDone));

            // If we are building/cloning a selection criteria for a batch query, a little extra work
            // needs to be done (see bug 2812185).
            if (alreadyDone.get(alreadyDone) != null) {
                clonedQuery.copyReportItems(alreadyDone);
            }
        }
        setSubQuery(clonedQuery);
    
protected voidprintCustomSQL(oracle.toplink.essentials.internal.expressions.ExpressionSQLPrinter printer)
Print the sub query to the printer.


        /*
         * modified for bug#2658466.  This fix ensures that Custom SQL sub-queries are translated
         * and have variables substituted with values correctly.
         */
        SQLCall call = (SQLCall)getSubQuery().getCall();
        call.translateCustomQuery();
        printer.getCall().getParameters().addAll(call.getParameters());
        printer.getCall().getParameterTypes().addAll(call.getParameterTypes());
        printer.printString(call.getCallString());
    
public voidprintSQL(oracle.toplink.essentials.internal.expressions.ExpressionSQLPrinter printer)
Print the sub query to the printer.

        ReportQuery query = getSubQuery();
        printer.printString("(");
        if (query.isCallQuery()) {
            printCustomSQL(printer);
        } else {
            SQLSelectStatement statement = (SQLSelectStatement)((ExpressionQueryMechanism)query.getQueryMechanism()).getSQLStatement();
            boolean isFirstElementPrinted = printer.isFirstElementPrinted();
            printer.setIsFirstElementPrinted(false);
            boolean requiresDistinct = printer.requiresDistinct();
            statement.printSQL(printer);
            printer.setIsFirstElementPrinted(isFirstElementPrinted);
            printer.setRequiresDistinct(requiresDistinct);
        }
        printer.printString(")");
    
public oracle.toplink.essentials.expressions.ExpressionrebuildOn(oracle.toplink.essentials.expressions.Expression newBase)
Should not rebuild as has its on expression builder.

        return this;
    
protected voidsetBaseExpression(oracle.toplink.essentials.expressions.Expression baseExpression)

        this.baseExpression = baseExpression;
    
public voidsetSubQuery(oracle.toplink.essentials.queryframework.ReportQuery subQuery)

        this.subQuery = subQuery;
    
public voidwriteDescriptionOn(java.io.BufferedWriter writer)
INTERNAL: Used to print a debug form of the expression tree.

        writer.write(String.valueOf(getSubQuery()));
    
public voidwriteSubexpressionsTo(java.io.BufferedWriter writer, int indent)
INTERNAL: Used in SQL printing.

        if (getSubQuery().getSelectionCriteria() != null) {
            getSubQuery().getSelectionCriteria().toString(writer, indent);
        }