1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.mappings.*; 28 import oracle.toplink.essentials.queryframework.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.expressions.*; 31 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 import oracle.toplink.essentials.descriptors.ClassDescriptor; 34 35 38 public class ParameterExpression extends Expression { 39 40 41 protected DatabaseField field; 42 43 44 protected Expression localBase; 45 46 47 protected Expression baseExpression; 48 49 53 Object type; 54 55 public ParameterExpression() { 56 super(); 57 } 58 59 public ParameterExpression(String fieldName) { 60 this(new DatabaseField(fieldName)); 61 } 62 63 public ParameterExpression(DatabaseField field) { 64 super(); 65 this.field = field; 66 } 67 68 public ParameterExpression(String fieldName, Expression baseExpression, Object type) { 71 this(new DatabaseField(fieldName), baseExpression); 72 this.type = type; 73 } 74 75 public ParameterExpression(DatabaseField field, Expression baseExpression) { 76 super(); 77 this.field = field; 78 localBase = baseExpression; 79 } 80 81 85 public String basicDescription() { 86 return String.valueOf(getField()); 87 } 88 89 93 public String descriptionOfNodeType() { 94 return "Parameter"; 95 } 96 97 101 public Expression get(String attributeOrQueryKey) { 102 ParameterExpression expression = new ParameterExpression(attributeOrQueryKey); 103 expression.setBaseExpression(this); 104 105 return expression; 106 } 107 108 112 public Expression getBaseExpression() { 113 return baseExpression; 114 } 115 116 120 public ExpressionBuilder getBuilder() { 121 if (localBase == null) { 122 if (getBaseExpression() != null) { 124 return ((ParameterExpression)getBaseExpression()).getBuilder(); 125 } else { 126 return null; 127 } 128 } 129 return localBase.getBuilder(); 130 } 131 132 public DatabaseField getField() { 133 return field; 134 } 135 136 140 public Expression getField(DatabaseField field) { 141 ParameterExpression expression = new ParameterExpression(field); 142 expression.setBaseExpression(this); 143 144 return expression; 145 } 146 147 150 public Expression getLocalBase() { 151 return localBase; 152 } 153 154 159 public Object getType() { return type; } 160 161 165 public Object getValue(AbstractRecord translationRow, AbstractSession session) { 166 if (getField() == null) { 167 return null; 168 } 169 170 Object value = null; 171 172 if (getBaseExpression() != null) { 174 value = ((ParameterExpression)getBaseExpression()).getValue(translationRow, session); 175 if (value == null) { 176 return null; 177 } 178 179 ClassDescriptor descriptor = session.getDescriptor(value); 180 if (descriptor.isAggregateDescriptor() && ((ParameterExpression)getBaseExpression()).getLocalBase().isObjectExpression()) { 182 descriptor = ((ObjectExpression)((ParameterExpression)getBaseExpression()).getLocalBase()).getDescriptor(); 183 } 184 if (descriptor != null) { 185 value = descriptor.getObjectBuilder().unwrapObject(value, session); 190 translationRow.put(((ParameterExpression)getBaseExpression()).getField(), value); 191 192 DatabaseMapping mapping = descriptor.getObjectBuilder().getMappingForField(getField()); 194 if (mapping != null) { 195 value = mapping.valueFromObject(value, getField(), session); 196 } else { 197 mapping = descriptor.getObjectBuilder().getMappingForAttributeName(getField().getName()); 198 if (mapping != null) { 199 value = mapping.getRealAttributeValueFromObject(value, session); 200 } else { 201 DatabaseField queryKeyField = descriptor.getObjectBuilder().getFieldForQueryKeyName(getField().getName()); 202 if (queryKeyField != null) { 203 mapping = descriptor.getObjectBuilder().getMappingForField(getField()); 204 if (mapping != null) { 205 value = mapping.valueFromObject(value, getField(), session); 206 } 207 } 208 } 209 } 210 } 211 } else { 212 value = translationRow.getIndicatingNoEntry(getField()); 213 if (value == oracle.toplink.essentials.internal.sessions.AbstractRecord.noEntry) { 215 throw QueryException.parameterNameMismatch(getField().getName()); 216 } 217 } 218 219 if (getLocalBase() != null) { 221 value = getLocalBase().getFieldValue(value); 222 } 223 224 return value; 225 } 226 227 public boolean isParameterExpression() { 228 return true; 229 } 230 231 234 public boolean isValueExpression() { 235 return true; 236 } 237 238 242 protected void postCopyIn(Dictionary alreadyDone) { 243 super.postCopyIn(alreadyDone); 244 if (getLocalBase() != null) { 245 setLocalBase(getLocalBase().copiedVersionFrom(alreadyDone)); 246 } 247 if (getBaseExpression() != null) { 248 setBaseExpression(getBaseExpression().copiedVersionFrom(alreadyDone)); 249 } 250 } 251 252 256 public void printSQL(ExpressionSQLPrinter printer) { 257 if (printer.shouldPrintParameterValues()) { 258 Object value = getValue(printer.getTranslationRow(), printer.getSession()); 259 if(getField() == null) { 260 printer.printPrimitive(value); 261 } else { 262 if (value instanceof Vector) { 263 printer.printValuelist((Vector)value); 264 } else { 265 printer.printParameter(this); 266 } 267 } 268 } else { 269 if (getField() != null) { 270 printer.printParameter(this); 271 } 272 } 273 } 274 275 279 public void printJava(ExpressionJavaPrinter printer) { 280 ((DataExpression)getLocalBase()).getBaseExpression().printJava(printer); 281 printer.printString(".getParameter(\"" + getField().getQualifiedName() + "\")"); 282 } 283 284 289 public Expression rebuildOn(Expression newBase) { 290 ParameterExpression result = (ParameterExpression)clone(); 291 result.setLocalBase(localBase.rebuildOn(newBase)); 292 return result; 293 } 294 295 299 protected void setBaseExpression(Expression baseExpression) { 300 this.baseExpression = baseExpression; 301 } 302 303 306 public void setLocalBase(Expression localBase) { 307 this.localBase = localBase; 308 } 309 310 317 public Expression twistedForBaseAndContext(Expression newBase, Expression context) { 318 return context.getField(getField()); 319 } 320 321 326 public Object valueFromObject(Object object, AbstractSession session, AbstractRecord translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered) { 327 if (getField() != null) { 329 return getValue(translationRow, session); 330 } 331 332 throw QueryException.cannotConformExpression(); 333 } 334 335 339 public void writeDescriptionOn(BufferedWriter writer) throws IOException { 340 writer.write(basicDescription()); 341 } 342 343 346 public void writeSubexpressionsTo(BufferedWriter writer, int indent) throws IOException { 347 if (getBaseExpression() != null) { 348 getBaseExpression().toString(writer, indent); 349 } 350 } 351 } 352 | Popular Tags |