01: /*
02: * Copyright 2002 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: ObjectFieldExpression.java,v 1.4 2003/08/04 16:40:35 pierreg0 Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: class ObjectFieldExpression extends ObjectExpression {
14: private final QueryStatement.QueryColumn qsc;
15: private final String fieldName;
16: private final Class fieldType;
17: private final Class castType;
18:
19: public ObjectFieldExpression(QueryStatement qs,
20: QueryStatement.QueryColumn qsc, String fieldName,
21: Class fieldType) {
22: this (qs, qsc, fieldName, fieldType, fieldType);
23: }
24:
25: protected ObjectFieldExpression(QueryStatement qs,
26: QueryStatement.QueryColumn qsc, String fieldName,
27: Class fieldType, Class castType) {
28: super (qs, qsc);
29:
30: this .qsc = qsc;
31: this .fieldName = fieldName;
32: this .fieldType = fieldType;
33: this .castType = castType;
34: }
35:
36: public SQLExpression cast(Class castType) {
37: return new ObjectFieldExpression(qs, qsc, fieldName, fieldType,
38: castType);
39: }
40:
41: public SQLExpression accessField(String subfieldName) {
42: StoreManager storeMgr = qs.getStoreManager();
43: DatabaseAdapter dba = storeMgr.getDatabaseAdapter();
44:
45: /* jt... = "joined table" */
46: String jtJavaName = qsc.te.getRangeVariable().getJavaName();
47:
48: if (!fieldName.equals("this"))
49: jtJavaName += '.' + fieldName;
50:
51: if (!castType.equals(fieldType)) {
52: String castTypeName = castType.getName();
53: jtJavaName += '.' + castTypeName.substring(castTypeName
54: .lastIndexOf('.') + 1);
55: }
56:
57: TableIdentifier jtRangeVar = new TableIdentifier(dba,
58: jtJavaName);
59: TableExpression jtExpr = qs.getTableExpression(jtRangeVar);
60:
61: if (jtExpr == null) {
62: ClassBaseTable jt = storeMgr.getClassBaseTable(castType);
63: jtExpr = qs.newTableExpression(jt, jtRangeVar);
64:
65: QueryStatement.QueryColumn jtCol = qs.getColumn(jtExpr, jt
66: .getIDMapping().getColumn());
67:
68: if (qsc.column.isNullable())
69: qs.leftOuterJoin(qsc, jtCol);
70: else
71: qs.innerJoin(qsc, jtCol);
72: }
73:
74: return jtExpr.newFieldExpression(subfieldName);
75: }
76: }
|