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: TableExprAsJoins.java,v 1.1 2002/11/24 06:02:47 jackknifebarber Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import java.util.HashMap;
14:
15: /**
16: * A SQL table expression that joins superclass tables by joining them directly
17: * to the surrounding QueryStatement.
18: *
19: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
20: * @version $Revision: 1.1 $
21: *
22: * @see QueryStatement
23: */
24:
25: class TableExprAsJoins extends TableExpression {
26: protected final HashMap rangeVarsByTable = new HashMap();
27:
28: public TableExprAsJoins(QueryStatement qs, Table mainTable,
29: SQLIdentifier mainRangeVar) {
30: super (qs, mainTable, mainRangeVar);
31:
32: rangeVarsByTable.put(mainTable, mainRangeVar);
33: }
34:
35: public String referenceColumn(Column col) {
36: assertNotFrozen();
37:
38: Table table = col.getTable();
39: SQLIdentifier rangeVar = (SQLIdentifier) rangeVarsByTable
40: .get(table);
41:
42: if (rangeVar == null) {
43: if (!(mainTable instanceof ClassBaseTable)
44: || !(table instanceof ClassBaseTable))
45: throw new TableMismatchException(col, mainTable);
46:
47: /*
48: * Since both tables are ClassBaseTables we assume that the column
49: * is a superclass field, meaning 'table' is a supertable of
50: * 'mainTable'. We join the supertable to the query statement using
51: * range variable derived from the main range variable (an
52: * underscore and a counter are appended).
53: *
54: * So even though the caller may have been looking for column FOO
55: * relative to rangevar THIS he may actually get back something like
56: * THIS_1.FOO.
57: */
58:
59: rangeVar = new SQLIdentifier(storeMgr.getDatabaseAdapter(),
60: mainRangeVar.toString() + '_'
61: + 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
69: .newTableExpression(st, rangeVar);
70:
71: QueryStatement.QueryColumn mtCol = qs.getColumn(this , mt
72: .getIDMapping().getColumn());
73: QueryStatement.QueryColumn stCol = qs.getColumn(stExpr, st
74: .getIDMapping().getColumn());
75:
76: qs.innerJoin(mtCol, stCol);
77: }
78:
79: return rangeVar + "." + col.getName();
80: }
81:
82: public String toString() {
83: if (sqlText == null) {
84: SQLIdentifier mainTableName = mainTable.getName();
85:
86: StringBuffer sb = new StringBuffer(mainTableName.toString());
87:
88: if (!mainRangeVar.equals(mainTableName))
89: sb.append(' ').append(mainRangeVar);
90:
91: sqlText = sb.toString();
92: }
93:
94: return sqlText;
95: }
96: }
|