1 21 22 package oracle.toplink.essentials.internal.expressions; 24 25 import java.util.*; 26 import java.io.*; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.mappings.*; 29 import oracle.toplink.essentials.queryframework.*; 30 import oracle.toplink.essentials.internal.helper.*; 31 import oracle.toplink.essentials.expressions.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 33 import oracle.toplink.essentials.internal.sessions.AbstractSession; 34 35 40 public class FieldExpression extends DataExpression { 41 protected DatabaseField field; 42 protected transient DatabaseField aliasedField; 43 44 47 public FieldExpression() { 48 super(); 49 } 50 51 54 public FieldExpression(DatabaseField newField) { 55 super(); 56 field = newField; 57 } 58 59 62 public FieldExpression(DatabaseField newField, Expression myBase) { 63 super(); 64 field = newField; 65 baseExpression = myBase; 66 } 67 68 71 public void clearAliases() { 72 super.clearAliases(); 73 aliasedField = null; 74 } 75 76 80 public String descriptionOfNodeType() { 81 return "Field"; 82 } 83 84 88 public DatabaseField getAliasedField() { 89 if (aliasedField == null) { 90 initializeAliasedField(); 91 } 92 return aliasedField; 93 94 } 95 96 99 private DatabaseTable getAliasedTable() { 100 DataExpression base = (DataExpression)getBaseExpression(); 101 if (!getField().hasTableName()) { 102 base.getDescriptor().buildField(getField()); 103 } 104 105 DatabaseTable alias = base.aliasForTable(getField().getTable()); 106 if (alias == null) { 107 return getField().getTable(); 108 } else { 109 return alias; 110 } 111 } 112 113 117 public DatabaseField getClonedField() { 118 return (DatabaseField)getField().clone(); 119 } 120 121 125 public Vector getClonedFields() { 126 Vector result = new Vector(1); 127 result.addElement(getField().clone()); 128 return result; 129 } 130 131 134 public DatabaseField getField() { 135 return field; 136 } 137 138 142 public Vector getFields() { 143 Vector result = new Vector(1); 144 result.addElement(getField()); 145 return result; 146 } 147 148 152 private void initializeAliasedField() { 153 DatabaseField tempField = (DatabaseField)getField().clone(); 154 DatabaseTable aliasedTable = getAliasedTable(); 155 156 aliasedField = tempField; 163 aliasedField.setTable(aliasedTable); 164 } 166 167 170 public boolean isAttribute() { 171 return true; 172 } 173 174 public boolean isFieldExpression() { 175 return true; 176 } 177 178 182 public void printSQL(ExpressionSQLPrinter printer) { 183 printer.printField(getAliasedField()); 184 } 185 186 190 public void printJava(ExpressionJavaPrinter printer) { 191 getBaseExpression().printJava(printer); 192 printer.printString(".getField(\"" + getField().getQualifiedName() + "\")"); 193 } 194 195 200 public Expression rebuildOn(Expression newBase) { 201 FieldExpression expression = new FieldExpression(getField(), getBaseExpression().rebuildOn(newBase)); 202 expression.setSelectIfOrderedBy(selectIfOrderedBy()); 203 return expression; 204 } 205 206 210 public void setField(DatabaseField newField) { 211 field = newField; 212 } 213 214 221 public Expression twistedForBaseAndContext(Expression newBase, Expression context) { 222 Expression twistedBase = getBaseExpression().twistedForBaseAndContext(newBase, context); 223 return twistedBase.getField(getField()); 224 } 225 226 229 public void validateNode() { 230 DataExpression base = (DataExpression)getBaseExpression(); 231 if (getField().getTable().hasName()) { 232 Vector tables = base.getOwnedTables(); 233 if ((tables != null) && (!tables.contains((getField().getTable())))) { 234 throw QueryException.invalidTableForFieldInExpression(getField()); 235 } 236 } 237 } 238 239 244 public Object valueFromObject(Object object, AbstractSession session, AbstractRecord translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered) { 245 if (getBuilder() != getBaseExpression()) { 247 throw QueryException.cannotConformExpression(); 248 } 249 250 DatabaseMapping mapping = session.getDescriptor(object.getClass()).getObjectBuilder().getMappingForField(getField()); 254 if (mapping == null) { 255 throw QueryException.cannotConformExpression(); 256 } 257 258 return mapping.valueFromObject(object, getField(), session); 259 } 260 261 265 public void writeDescriptionOn(BufferedWriter writer) throws IOException { 266 writer.write(getField().toString()); 267 } 268 269 272 public void writeFields(ExpressionSQLPrinter printer, Vector newFields, SQLSelectStatement statement) { 273 DatabaseField field = getField(); 274 275 if (field != null) { 276 newFields.addElement(field); 277 writeField(printer, field, statement); 278 } 279 } 280 } | Popular Tags |