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: TableExprAsSubjoins.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.ArrayList;
14: import java.util.Iterator;
15:
16: /**
17: * A SQL table expression that joins superclass tables by constructing a
18: * parenthesized set of subjoins.
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 TableExprAsSubjoins extends TableExpression {
27: protected final ArrayList super tables = new ArrayList();
28:
29: public TableExprAsSubjoins(QueryStatement qs, Table mainTable,
30: SQLIdentifier mainRangeVar) {
31: super (qs, mainTable, mainRangeVar);
32: }
33:
34: public String referenceColumn(Column col) {
35: assertNotFrozen();
36:
37: Table table = col.getTable();
38:
39: if (!table.equals(mainTable)) {
40: if (!(mainTable instanceof ClassBaseTable)
41: || !(table instanceof ClassBaseTable))
42: throw new TableMismatchException(col, mainTable);
43:
44: /*
45: * Since both tables are ClassBaseTables we assume that the column
46: * is a superclass field, meaning 'table' is a supertable of
47: * 'mainTable'. We add it to the list of supertables that will be
48: * joined in when the statement text is constructed.
49: */
50: if (!(super tables.contains(table)))
51: super tables.add(table);
52: }
53:
54: return mainRangeVar + "." + col.getName();
55: }
56:
57: public String toString() {
58: if (sqlText == null) {
59: StringBuffer sb = new StringBuffer();
60: SQLIdentifier mainTableName = mainTable.getName();
61:
62: Iterator i = super tables.iterator();
63:
64: if (i.hasNext()) {
65: SQLIdentifier mainTableIDColumnName = ((ClassBaseTable) mainTable)
66: .getIDMapping().getColumn().getName();
67: sb.append('(').append(mainTableName);
68:
69: while (i.hasNext()) {
70: ClassBaseTable super table = (ClassBaseTable) i
71: .next();
72: SQLIdentifier super tableName = super table.getName();
73:
74: sb.append(" INNER JOIN ").append(super tableName)
75: .append(" ON ").append(mainTableName)
76: .append('.').append(mainTableIDColumnName)
77: .append(" = ").append(super tableName)
78: .append('.').append(
79: super table.getIDMapping()
80: .getColumn().getName());
81: }
82:
83: sb.append(") ").append(mainRangeVar);
84: } else {
85: sb.append(mainTableName);
86:
87: if (!mainRangeVar.equals(mainTableName))
88: sb.append(' ').append(mainRangeVar);
89: }
90:
91: sqlText = sb.toString();
92: }
93:
94: return sqlText;
95: }
96: }
|