KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > ObjectFieldExpression


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ObjectFieldExpression.java,v 1.4 2003/08/04 16:40:35 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13
14 class ObjectFieldExpression extends ObjectExpression
15 {
16     private final QueryStatement.QueryColumn qsc;
17     private final String JavaDoc fieldName;
18     private final Class JavaDoc fieldType;
19     private final Class JavaDoc castType;
20
21     public ObjectFieldExpression(QueryStatement qs,
22                                  QueryStatement.QueryColumn qsc,
23                                  String JavaDoc fieldName,
24                                  Class JavaDoc fieldType)
25     {
26         this(qs, qsc, fieldName, fieldType, fieldType);
27     }
28
29     protected ObjectFieldExpression(QueryStatement qs,
30                                     QueryStatement.QueryColumn qsc,
31                                     String JavaDoc fieldName,
32                                     Class JavaDoc fieldType,
33                                     Class JavaDoc castType)
34     {
35     super(qs, qsc);
36
37         this.qsc = qsc;
38         this.fieldName = fieldName;
39         this.fieldType = fieldType;
40         this.castType = castType;
41     }
42
43     public SQLExpression cast(Class JavaDoc castType)
44     {
45         return new ObjectFieldExpression(qs, qsc, fieldName, fieldType, castType);
46     }
47
48     public SQLExpression accessField(String JavaDoc subfieldName)
49     {
50         StoreManager storeMgr = qs.getStoreManager();
51         DatabaseAdapter dba = storeMgr.getDatabaseAdapter();
52
53         /* jt... = "joined table" */
54         String JavaDoc jtJavaName = qsc.te.getRangeVariable().getJavaName();
55
56         if (!fieldName.equals("this"))
57             jtJavaName += '.' + fieldName;
58
59         if (!castType.equals(fieldType))
60         {
61             String JavaDoc castTypeName = castType.getName();
62             jtJavaName += '.' + castTypeName.substring(castTypeName.lastIndexOf('.') + 1);
63         }
64
65         TableIdentifier jtRangeVar = new TableIdentifier(dba, jtJavaName);
66         TableExpression jtExpr = qs.getTableExpression(jtRangeVar);
67
68         if (jtExpr == null)
69         {
70             ClassBaseTable jt = storeMgr.getClassBaseTable(castType);
71             jtExpr = qs.newTableExpression(jt, jtRangeVar);
72
73             QueryStatement.QueryColumn jtCol = qs.getColumn(jtExpr, jt.getIDMapping().getColumn());
74
75             if (qsc.column.isNullable())
76                 qs.leftOuterJoin(qsc, jtCol);
77             else
78                 qs.innerJoin(qsc, jtCol);
79         }
80
81         return jtExpr.newFieldExpression(subfieldName);
82     }
83 }
84
Popular Tags