FileDocCategorySizeDatePackage
ParameterExpression.javaAPI DocGlassfish v2 API14706Tue May 22 16:54:32 BST 2007oracle.toplink.essentials.internal.expressions

ParameterExpression

public class ParameterExpression extends Expression
Used for parameterized expressions, such as expression defined in mapping queries.

Fields Summary
protected DatabaseField
field
The parameter field or name.
protected Expression
localBase
The opposite side of the relation, this is used for conversion of the parameter using the others mapping.
protected Expression
baseExpression
The base expression is what the parameter was derived from, used for nested parameters.
Object
type
The infered type of the parameter. Please note that the type might not be always initialized to correct value. It might be null if not initialized correctly.
Constructors Summary
public ParameterExpression()

        super();
    
public ParameterExpression(String fieldName)

        this(new DatabaseField(fieldName));
    
public ParameterExpression(DatabaseField field)

        super();
        this.field = field;
    
public ParameterExpression(String fieldName, Expression baseExpression, Object type)

        this(new DatabaseField(fieldName), baseExpression);
        this.type = type;
    
public ParameterExpression(DatabaseField field, Expression baseExpression)

        super();
        this.field = field;
        localBase = baseExpression;
    
Methods Summary
public java.lang.StringbasicDescription()
Return description. Used for toString.

        return String.valueOf(getField());
    
public java.lang.StringdescriptionOfNodeType()
INTERNAL: Used for debug printing.

        return "Parameter";
    
public oracle.toplink.essentials.expressions.Expressionget(java.lang.String attributeOrQueryKey)
This allows for nesting of parametrized expression. This is used for parameterizing object comparisons.

        ParameterExpression expression = new ParameterExpression(attributeOrQueryKey);
        expression.setBaseExpression(this);

        return expression;
    
public oracle.toplink.essentials.expressions.ExpressiongetBaseExpression()
The base expression is what the parameter was derived from. This is used for nested parameters.

        return baseExpression;
    
public oracle.toplink.essentials.expressions.ExpressionBuildergetBuilder()
Return the expression builder which is the ultimate base of this expression, or null if there isn't one (shouldn't happen if we start from a root)

        if (localBase == null) {
            //Bug#5097278 Need to return the builder from the base expression if nested.
            if (getBaseExpression() != null) {
                return ((ParameterExpression)getBaseExpression()).getBuilder();
            } else {
                return null;
            }
        }
        return localBase.getBuilder();
    
public oracle.toplink.essentials.internal.helper.DatabaseFieldgetField()

        return field;
    
public oracle.toplink.essentials.expressions.ExpressiongetField(oracle.toplink.essentials.internal.helper.DatabaseField field)
This allows for nesting of parametrized expression. This is used for parameterizing object comparisons.

        ParameterExpression expression = new ParameterExpression(field);
        expression.setBaseExpression(this);

        return expression;
    
public oracle.toplink.essentials.expressions.ExpressiongetLocalBase()
The opposite side of the relation, this is used for conversion of the parameter using the others mapping.

        return localBase;
    
public java.lang.ObjectgetType()
The infered type of this parameter. Please note that the type might not be always initialized to correct value. It might be null if not initialized correctly

 return type; 
public java.lang.ObjectgetValue(oracle.toplink.essentials.internal.sessions.AbstractRecord translationRow, oracle.toplink.essentials.internal.sessions.AbstractSession session)
Extract the value from the row. This may require recusion if it is a nested parameter.

        if (getField() == null) {
            return null;
        }

        Object value = null;

        // Check for nested parameters.
        if (getBaseExpression() != null) {
            value = ((ParameterExpression)getBaseExpression()).getValue(translationRow, session);
            if (value == null) {
                return null;
            }

            ClassDescriptor descriptor = session.getDescriptor(value);
            //Bug4924639  Aggregate descriptors have to be acquired from their mapping as they are cloned and initialized by each mapping
            if (descriptor.isAggregateDescriptor() && ((ParameterExpression)getBaseExpression()).getLocalBase().isObjectExpression()) {
                descriptor = ((ObjectExpression)((ParameterExpression)getBaseExpression()).getLocalBase()).getDescriptor();
            }
            if (descriptor != null) {
                // For bug 2990493 must unwrap for EJBQL "Select Person(p) where p = ?1"
                //if we had to unwrap it make sure we replace the argument with this value
                //incase it is needed again, say in conforming.
                //bug 3750793
                value = descriptor.getObjectBuilder().unwrapObject(value, session);
                translationRow.put(((ParameterExpression)getBaseExpression()).getField(), value);

                // The local parameter is either a field or attribute of the object.
                DatabaseMapping mapping = descriptor.getObjectBuilder().getMappingForField(getField());
                if (mapping != null) {
                    value = mapping.valueFromObject(value, getField(), session);
                } else {
                    mapping = descriptor.getObjectBuilder().getMappingForAttributeName(getField().getName());
                    if (mapping != null) {
                        value = mapping.getRealAttributeValueFromObject(value, session);
                    } else {
                        DatabaseField queryKeyField = descriptor.getObjectBuilder().getFieldForQueryKeyName(getField().getName());
                        if (queryKeyField != null) {
                            mapping = descriptor.getObjectBuilder().getMappingForField(getField());
                            if (mapping != null) {
                                value = mapping.valueFromObject(value, getField(), session);
                            }
                        }
                    }
                }
            }
        } else {
            value = translationRow.getIndicatingNoEntry(getField());
            //Throw an exception if the field is not mapped
            if (value == oracle.toplink.essentials.internal.sessions.AbstractRecord.noEntry) {
                throw QueryException.parameterNameMismatch(getField().getName());
            }
        }

        // Convert the value to the correct type, i.e. object type mappings.
        if (getLocalBase() != null) {
            value = getLocalBase().getFieldValue(value);
        }

        return value;
    
public booleanisParameterExpression()

        return true;
    
public booleanisValueExpression()
INTERNAL:

        return true;
    
protected voidpostCopyIn(java.util.Dictionary alreadyDone)
INTERNAL: Used for cloning.

        super.postCopyIn(alreadyDone);
        if (getLocalBase() != null) {
            setLocalBase(getLocalBase().copiedVersionFrom(alreadyDone));
        }
        if (getBaseExpression() != null) {
            setBaseExpression(getBaseExpression().copiedVersionFrom(alreadyDone));
        }
    
public voidprintJava(oracle.toplink.essentials.internal.expressions.ExpressionJavaPrinter printer)
INTERNAL: Print java for project class generation

        ((DataExpression)getLocalBase()).getBaseExpression().printJava(printer);
        printer.printString(".getParameter(\"" + getField().getQualifiedName() + "\")");        
    
public voidprintSQL(oracle.toplink.essentials.internal.expressions.ExpressionSQLPrinter printer)
INTERNAL: Print SQL onto the stream, using the ExpressionPrinter for context

        if (printer.shouldPrintParameterValues()) {
            Object value = getValue(printer.getTranslationRow(), printer.getSession());
            if(getField() == null) {
                printer.printPrimitive(value);
            } else {
                if (value instanceof Vector) {
                    printer.printValuelist((Vector)value);
                } else {
                    printer.printParameter(this);
                }
            }
        } else {
            if (getField() != null) {
                printer.printParameter(this);
            }
        }
    
public oracle.toplink.essentials.expressions.ExpressionrebuildOn(oracle.toplink.essentials.expressions.Expression newBase)
INTERNAL: This expression is built on a different base than the one we want. Rebuild it and return the root of the new tree

        ParameterExpression result = (ParameterExpression)clone();
        result.setLocalBase(localBase.rebuildOn(newBase));
        return result;
    
protected voidsetBaseExpression(oracle.toplink.essentials.expressions.Expression baseExpression)
The base expression is what the parameter was derived from. This is used for nested parameters.

        this.baseExpression = baseExpression;
    
public voidsetLocalBase(oracle.toplink.essentials.expressions.Expression localBase)
The opposite side of the relation, this is used for conversion of the parameter using the others mapping.

        this.localBase = localBase;
    
public oracle.toplink.essentials.expressions.ExpressiontwistedForBaseAndContext(oracle.toplink.essentials.expressions.Expression newBase, oracle.toplink.essentials.expressions.Expression context)
INTERNAL: Rebuild against the base, with the values of parameters supplied by the context expression. This is used for transforming a standalone expression (e.g. the join criteria of a mapping) into part of some larger expression. You normally would not call this directly, instead calling twist, (see the comment there for more details).

        return context.getField(getField());
    
public java.lang.ObjectvalueFromObject(java.lang.Object object, oracle.toplink.essentials.internal.sessions.AbstractSession session, oracle.toplink.essentials.internal.sessions.AbstractRecord translationRow, oracle.toplink.essentials.queryframework.InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered)
INTERNAL: Return the value for in memory comparison. This is only valid for valueable expressions.

        // Run ourselves through the translation row to find the desired value
        if (getField() != null) {
            return getValue(translationRow, session);
        }

        throw QueryException.cannotConformExpression();
    
public voidwriteDescriptionOn(java.io.BufferedWriter writer)
INTERNAL: Used to print a debug form of the expression tree.

        writer.write(basicDescription());
    
public voidwriteFields(oracle.toplink.essentials.internal.expressions.ExpressionSQLPrinter printer, java.util.Vector newFields, oracle.toplink.essentials.internal.expressions.SQLSelectStatement statement)
INTERNAL: Append the parameter into the printer. "Normal" ReadQuery never has ParameterExpression in it's select clause hence for a "normal" ReadQuery this method is never called. The reason this method was added is that UpdateAllQuery (in case temporary storage is required) creates a "helper" ReportQuery with ReportItem corresponding to each update expression - and update expression may be a ParameterExpression. The call created by "helper" ReportQuery is never executed - it's used during construction of insert call into temporary storage.

        //print ", " before each selected field except the first one
        if (printer.isFirstElementPrinted()) {
            printer.printString(", ");
        } else {
            printer.setIsFirstElementPrinted(true);
        }

        // This field is a parameter value, so any name can be used.
        newFields.addElement(new DatabaseField("*"));
        printSQL(printer);
    
public voidwriteSubexpressionsTo(java.io.BufferedWriter writer, int indent)
Print the base for debuggin purposes.

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