FileDocCategorySizeDatePackage
Constraint.javaAPI DocGlassfish v2 API12013Fri May 04 22:35:14 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.sql.constraint

Constraint

public class Constraint extends Object

Fields Summary
public List
stack
The stack that contains all constraints beside join constraints. Join constraints are handled by a separate stack.
private List
outerJoinStack
The stack that contains outer join constraints. All the elements of this stack are instance of {@link ConstraintJoin}. Outer join constraints can be appended to the query w/o changing the semantic.
Constructors Summary
public Constraint()

        stack = new ArrayList();
        outerJoinStack = new ArrayList();
    
Methods Summary
private booleanaddAnd(com.sun.jdo.spi.persistence.support.sqlstore.sql.constraint.Constraint foreignConstraint, int joinOp)
Decides, if we need to add an additional "AND" constraint to the stack after merging the foreign stack.

param
foreignConstraint Constraint to be joined.
param
joinOp Join operator.
return
True, if we need to add an additional "AND" constraint.

        // Add "AND" constraints for equi-joins only.
        // * Don't add an "AND" constraint for outer joins. Outer joins
        // don't contribute to the "where"-clause in ANSI-case. In the
        // non-ANSI case, outer joins can be appended to the query w/o
        // changing the semantic.
        // * Don't add an "AND" constaint for non-relationship joins,
        // as no additional join constraint is added in this case.
        return (joinOp == ActionDesc.OP_EQUIJOIN)

        // Never add an "AND" constraint, if the foreign stack is
        // empty or contains "ORDER_BY" constraints only, as "ORDER_BY"
        // constraints don't contribute to the "where"-clause.
        && !foreignConstraint.isEmptyOrOrderBy();
    
public voidaddConstraintFieldSubQuery(java.lang.String field, com.sun.jdo.spi.persistence.support.sqlstore.ActionDesc rd)
Adds a subquery constraint on the stack.

param
field The field on which subquery constraint is added.
param
rd Retrieve descriptor corresponding to the subquery.

        stack.add(new ConstraintFieldNameSubQuery(field, rd));
    
public voidaddField(java.lang.String name, com.sun.jdo.spi.persistence.support.sqlstore.ActionDesc desc)
Adds a field to the constraint stack. AddField creates a ConstraintField node for the indicated named field, optionally including an operation descriptor and adds it to the constraint stack.

param
name The name parameter specifies the name of the field to be added to the constrant stack.
param
desc The desc parameter specifies an operation descriptor describing what is to be done with the field named by the name parameter.

        stack.add(new ConstraintFieldName(name, desc));
    
public voidaddField(com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc desc)
Adds a field to the constraint stack. AddField creates a ConstraintFieldDesc node for the indicated field descriptor and adds it to the constraint stack.

param
desc The Desc parameter is the field descriptor to be added to the constrant stack.

        stack.add(new ConstraintFieldDesc(desc));
    
public voidaddField(com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc desc, com.sun.jdo.spi.persistence.support.sqlstore.sql.generator.QueryPlan plan)
Adds a field to the constraint stack. AddField creates a ConstraintFieldDesc node for the indicated field descriptor and adds it to the constraint stack.

param
desc The Desc parameter is the field descriptor to be added to the constrant stack.
param
plan The query plan to which this desc belongs

        stack.add(new ConstraintFieldDesc(desc, plan));
    
public voidaddField(ConstraintFieldDesc constraintDesc)

        stack.add(constraintDesc);
    
public voidaddForeignField(java.lang.String name, com.sun.jdo.spi.persistence.support.sqlstore.ActionDesc desc)

        stack.add(new ConstraintForeignFieldName(name, desc));
    
public voidaddJoinConstraint(ConstraintJoin join)
Adds specified join constraint. Equi joins are added to the constraint stack, outer joins are added to a separate stack, the outerJoinStack.

param
join The join constraint to be added.

        if (join.operation == ActionDesc.OP_EQUIJOIN) {
            stack.add(join);
        } else {
            // The current logic is written with the assumption that a join
            // that is not an equi join is always a left join
            assert join.operation == ActionDesc.OP_LEFTJOIN;
            outerJoinStack.add(join);
        }
    
public voidaddOperation(int operation)
Adds an operation to the constraint stack. AddOperation creates a ConstraintOperation node whose operation is operation and adds it to the constraint stack.

param
operation The operation parameter specifies the operation to be added to the constrant stack.

        stack.add(new ConstraintOperation(operation));
    
public voidaddParamIndex(int index, int enumType, com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc localField)
Adds the index of a parameter to the stack.

param
index the parameter index.
param
enumType the type for this parameter.
param
localField the localField to which this parameter is bound.

        stack.add(new ConstraintParamIndex(index,enumType, localField));
    
public voidaddValue(java.lang.Object value, com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc localField)
Adds a data value to the constraint stack. Creates a ConstraintValue node whose value is value and adds it to the constraint stack.

param
value The value to be added to the constrant stack.
param
localField The localField to which this value is bound. Please note that localField can be null for values involved in complex expressions in a query.

        stack.add(new ConstraintValue(value, localField));
    
public java.util.ListgetConstraints()
Gets the where clause constraints for this Constraint

return
The where clause constraints for this Constraint

         return stack;
     
public java.util.ListgetOuterJoinConstraints()
Gets the outer join constraints for this Constraint

return
The outer join constraints for this Constraint

         return outerJoinStack;
     
private booleanisEmptyOrOrderBy()
Checks, if the constraint stack is empty or contains "ORDER_BY" constraints only. "ORDER_BY" constraints are recognized as an ORDER_BY operation followed by a field name constraint, and an optional Value constraint giving the position of the Order By constraint. NOTE: The value constraints giving the position for the order by constraints are currently not generated by the query compiler. Order by constraints stay in the correct order because of two reasons
  • the way, constraints are processed by the query compiler
  • the way constraint stacks are joined in {@link com.sun.jdo.spi.persistence.support.sqlstore.sql.generator.SelectQueryPlan#processForeignConstraints}.

        boolean rc = true;
        // Abort the loop at first possible opportunity.
        for (int i = stack.size() - 1; i >= 0 && rc; ) {
            ConstraintNode node = (ConstraintNode) stack.get(i);

            if ((node instanceof ConstraintOperation)
                    && ((((ConstraintOperation) node).operation == ActionDesc.OP_ORDERBY) ||
                    (((ConstraintOperation) node).operation == ActionDesc.OP_ORDERBY_DESC))) {
                if ((i > 0) && (
                        stack.get(i - 1) instanceof ConstraintFieldName ||
                        stack.get(i - 1) instanceof ConstraintFieldDesc)) {
                    // Order By constraint.
                    i--;
                    if ((i > 0) && (stack.get(i - 1) instanceof ConstraintValue)) {
                        // Optional Value constraint.
                        i--;
                    }
                } else {
                    rc = false;
                }
            } else {
                rc = false;
            }

            // Check the next constraint if any.
            i--;
        }
        return rc;
    
public booleanmergeConstraint(com.sun.jdo.spi.persistence.support.sqlstore.sql.constraint.Constraint foreignConstraint, int joinOp)
Merges the stack with the specified foreign constraint stack.

param
foreignConstraint The constraint to be merged.
param
joinOp Join operation as defined in {@link ActionDesc}.
return
True, if we need to add an additional "AND" constraint.


        stack.addAll(foreignConstraint.stack);
        outerJoinStack.addAll(foreignConstraint.outerJoinStack);

        return addAnd(foreignConstraint, joinOp);