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: TableExpression.java,v 1.4 2003/08/04 16:40:35 pierreg0 Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import javax.jdo.JDOFatalInternalException;
14: import javax.jdo.JDOUserException;
15:
16: /**
17: * Represents a SQL table expression as might be listed in the FROM clause of
18: * a SELECT statement.
19: * A table expression is a fragment of a larger containing QueryStatement.
20: * <p>
21: * A table expression has a base "main" table.
22: * If that table serves as backing for a Java class, and that class has
23: * persistence-capable superclasses, then the table expression may include
24: * joins to superclass tables, or may cause such joins to occur in its
25: * surrounding QueryStatement.
26: *
27: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
28: * @version $Revision: 1.4 $
29: *
30: * @see QueryStatement
31: */
32:
33: abstract class TableExpression {
34: protected final QueryStatement qs;
35: protected final Table mainTable;
36: protected final SQLIdentifier mainRangeVar;
37: protected final StoreManager storeMgr;
38: protected String sqlText = null;
39:
40: protected TableExpression(QueryStatement qs, Table mainTable,
41: SQLIdentifier mainRangeVar) {
42: this .qs = qs;
43: this .mainTable = mainTable;
44: this .mainRangeVar = mainRangeVar;
45:
46: storeMgr = mainTable.getStoreManager();
47: }
48:
49: protected void assertNotFrozen() {
50: if (sqlText != null)
51: throw new JDOFatalInternalException(
52: "A table expression cannot be modified after being output");
53: }
54:
55: public final Table getMainTable() {
56: return mainTable;
57: }
58:
59: public final SQLIdentifier getRangeVariable() {
60: return mainRangeVar;
61: }
62:
63: public SQLExpression newFieldExpression(String fieldName) {
64: if (!(mainTable instanceof ClassTable))
65: throw new JDOUserException(
66: "'"
67: + fieldName
68: + "' can't be referenced in "
69: + mainTable.getName()
70: + ": table does not store a persistence-capable class");
71:
72: ClassTable ct = (ClassTable) mainTable;
73: Mapping m;
74:
75: if (fieldName.equals("this") && ct instanceof ClassBaseTable)
76: m = ((ClassBaseTable) ct).getIDMapping();
77: else
78: m = ct.getFieldMapping(fieldName);
79:
80: return m.newSQLExpression(qs, this , fieldName);
81: }
82:
83: public abstract String referenceColumn(Column col);
84:
85: public abstract String toString();
86: }
|