KickJava   Java API By Example, From Geeks To Geeks.

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


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: TableExprAsJoins.java,v 1.1 2002/11/24 06:02:47 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.util.HashMap JavaDoc;
14
15
16 /**
17  * A SQL table expression that joins superclass tables by joining them directly
18  * to the surrounding QueryStatement.
19  *
20  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
21  * @version $Revision: 1.1 $
22  *
23  * @see QueryStatement
24  */

25
26 class TableExprAsJoins extends TableExpression
27 {
28     protected final HashMap JavaDoc rangeVarsByTable = new HashMap JavaDoc();
29
30     public TableExprAsJoins(QueryStatement qs, Table mainTable, SQLIdentifier mainRangeVar)
31     {
32         super(qs, mainTable, mainRangeVar);
33
34         rangeVarsByTable.put(mainTable, mainRangeVar);
35     }
36
37     public String JavaDoc referenceColumn(Column col)
38     {
39         assertNotFrozen();
40
41         Table table = col.getTable();
42         SQLIdentifier rangeVar = (SQLIdentifier)rangeVarsByTable.get(table);
43
44         if (rangeVar == null)
45         {
46             if (!(mainTable instanceof ClassBaseTable) || !(table instanceof ClassBaseTable))
47                 throw new TableMismatchException(col, mainTable);
48
49             /*
50              * Since both tables are ClassBaseTables we assume that the column
51              * is a superclass field, meaning 'table' is a supertable of
52              * 'mainTable'. We join the supertable to the query statement using
53              * range variable derived from the main range variable (an
54              * underscore and a counter are appended).
55              *
56              * So even though the caller may have been looking for column FOO
57              * relative to rangevar THIS he may actually get back something like
58              * THIS_1.FOO.
59              */

60
61             rangeVar = new SQLIdentifier(storeMgr.getDatabaseAdapter(), mainRangeVar.toString() + '_' + rangeVarsByTable.size());
62             rangeVarsByTable.put(table, rangeVar);
63
64             /* mt... = "main table" */
65             /* st... = "supertable" */
66             ClassBaseTable mt = (ClassBaseTable)mainTable;
67             ClassBaseTable st = (ClassBaseTable)table;
68             TableExpression stExpr = qs.newTableExpression(st, rangeVar);
69
70             QueryStatement.QueryColumn mtCol = qs.getColumn(this, mt.getIDMapping().getColumn());
71             QueryStatement.QueryColumn stCol = qs.getColumn(stExpr, st.getIDMapping().getColumn());
72
73             qs.innerJoin(mtCol, stCol);
74         }
75
76         return rangeVar + "." + col.getName();
77     }
78
79     public String JavaDoc toString()
80     {
81         if (sqlText == null)
82         {
83             SQLIdentifier mainTableName = mainTable.getName();
84
85             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(mainTableName.toString());
86
87             if (!mainRangeVar.equals(mainTableName))
88                 sb.append(' ').append(mainRangeVar);
89
90             sqlText = sb.toString();
91         }
92
93         return sqlText;
94     }
95 }
96
Popular Tags