1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import java.util.*; 25 26 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.expressions.*; 29 import oracle.toplink.essentials.mappings.DatabaseMapping; 30 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery; 31 import oracle.toplink.essentials.queryframework.ReportQuery; 32 33 42 public class VariableNode extends Node { 43 44 45 private String variableName; 46 47 48 private String canonicalName; 49 50 53 public VariableNode() { 54 super(); 55 } 56 57 public VariableNode(String newVariableName) { 58 setVariableName(newVariableName); 59 } 60 61 public String getVariableName() { 62 return variableName; 63 } 64 65 public void setVariableName(String newVariableName) { 66 variableName = newVariableName; 67 canonicalName = IdentificationVariableDeclNode.calculateCanonicalName(newVariableName); 68 } 69 70 71 public String getCanonicalVariableName() { 72 return canonicalName; 73 } 74 75 79 public boolean isVariableNode() { 80 return true; 81 } 82 83 87 public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext generationContext) { 88 String name = getCanonicalVariableName(); 89 ParseTreeContext context = generationContext.getParseTreeContext(); 90 if (theQuery instanceof ReportQuery) { 91 ReportQuery reportQuery = (ReportQuery)theQuery; 92 Expression expression = generationContext.expressionFor(name); 93 if (expression == null) { 94 expression = generateExpression(generationContext); 95 } 96 addAttribute(reportQuery, expression, generationContext); 97 } 98 } 99 100 101 private void addAttribute(ReportQuery reportQuery, Expression expression, 102 GenerationContext context) { 103 String name = getCanonicalVariableName(); 104 List fetchJoinNodes = context.getParseTreeContext().getFetchJoins(name); 105 if (fetchJoinNodes == null) { 106 reportQuery.addAttribute(name, expression); 107 } else { 108 List fetchJoinExprs = new ArrayList(fetchJoinNodes.size()); 109 for (Iterator i = fetchJoinNodes.iterator(); i.hasNext(); ) { 110 Node node = (Node)i.next(); 111 fetchJoinExprs.add(node.generateExpression(context)); 112 } 113 reportQuery.addItem(name, expression, fetchJoinExprs); 114 } 115 } 116 117 121 public void validate(ParseTreeContext context) { 122 TypeHelper typeHelper = context.getTypeHelper(); 123 String name = getCanonicalVariableName(); 124 if (context.isRangeVariable(name)) { 125 String schema = context.schemaForVariable(name); 126 setType(typeHelper.resolveSchema(schema)); 127 } else { 128 Node path = context.pathForVariable(name); 129 if (path == null) { 130 throw EJBQLException.aliasResolutionException(name); 131 } else { 132 setType(path.getType()); 133 } 134 } 135 context.usedVariable(name); 136 if (context.isDeclaredInOuterScope(name)) { 137 context.registerOuterScopeVariable(name); 138 } 139 } 140 141 public Expression generateBaseBuilderExpression(GenerationContext context) { 142 if (context.useParallelExpressions()) { 145 return new ExpressionBuilder(this.resolveClass(context)); 146 } else { 147 return new ExpressionBuilder(); 148 } 149 } 150 151 public Expression generateExpression(GenerationContext generationContext) { 152 Expression myExpression = null; 153 String name = getCanonicalVariableName(); 154 155 myExpression = generationContext.expressionFor(name); 157 if (myExpression != null) { 158 return myExpression; 159 } 160 161 if (generationContext.getParseTreeContext().isRangeVariable(name)) { 163 myExpression = generateBaseBuilderExpression(generationContext); 164 } else { 165 myExpression = generateExpressionForAlias(generationContext); 166 } 167 168 generationContext.addExpression(myExpression, name); 169 return myExpression; 170 } 171 172 public Expression generateExpressionForAlias(GenerationContext context) { 173 if (context.getParseTree().getQueryNode().isSelectNode() && context.shouldCheckSelectNodeBeforeResolving() && (((SelectNode)context.getParseTree().getQueryNode()).isSelected(this.getCanonicalVariableName()))) { 177 return new ExpressionBuilder(); 178 } 179 180 Node nodeForAlias = getNodeForAlias(context); 181 182 if (nodeForAlias == null) { 186 throw EJBQLException.aliasResolutionException(getVariableName()); 187 } 188 189 return nodeForAlias.generateExpression(context); 191 } 192 193 public Node getNodeForAlias(GenerationContext context) { 194 return context.getParseTreeContext().pathForVariable(getCanonicalVariableName()); 197 } 198 199 203 public boolean isAlias(GenerationContext context) { 204 return isAlias(context.getParseTreeContext()); 205 } 206 207 public boolean isAlias(ParseTreeContext context) { 208 String classNameForAlias = context.schemaForVariable(getCanonicalVariableName()); 209 return classNameForAlias != null; 210 } 211 212 216 public Class resolveClass(GenerationContext generationContext) { 217 Class clazz = null; 218 String name = getCanonicalVariableName(); 219 ParseTreeContext context = generationContext.getParseTreeContext(); 220 if (context.isRangeVariable(name)) { 221 String schema = context.schemaForVariable(name); 222 clazz = context.classForSchemaName(schema, generationContext); 223 } else { 224 DotNode path = (DotNode)context.pathForVariable(name); 225 if (path == null) { 226 throw EJBQLException.aliasResolutionException(name); 227 } else { 228 clazz = path.resolveClass(generationContext); 229 } 230 } 231 return clazz; 232 } 233 234 public String toString(int indent) { 235 StringBuffer buffer = new StringBuffer (); 236 toStringIndent(indent, buffer); 237 buffer.append(toStringDisplayName() + "[" + getVariableName() + "]"); 238 return buffer.toString(); 239 } 240 241 245 public String getAsString() { 246 return getVariableName(); 247 } 248 249 } 250 | Popular Tags |