001: /**********************************************************************
002: Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: 2003 Andy Jefferson - coding standards
017: ...
018: **********************************************************************/package org.jpox.store.expression;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022:
023: import org.jpox.store.DatastoreClass;
024: import org.jpox.store.DatastoreContainerObject;
025: import org.jpox.store.DatastoreField;
026: import org.jpox.store.DatastoreIdentifier;
027: import org.jpox.store.DatastoreObject;
028: import org.jpox.store.exceptions.TableMismatchException;
029:
030: /**
031: * A SQL table expression that joins superclass tables by constructing a
032: * parenthesized set of subjoins.
033: *
034: * @see QueryExpression
035: * @version $Revision: 1.14 $
036: */
037: public class TableExprAsSubjoins extends LogicSetExpression {
038: protected final ArrayList super tables = new ArrayList();
039:
040: /**
041: *
042: * @param qs the QueryExpression
043: * @param mainTable the main table in the query
044: * @param mainRangeVar The alias/identifier of the table in the query
045: */
046: public TableExprAsSubjoins(QueryExpression qs,
047: DatastoreContainerObject mainTable,
048: DatastoreIdentifier mainRangeVar) {
049: super (qs, mainTable, mainRangeVar);
050: }
051:
052: public String referenceColumn(DatastoreField col) {
053: assertNotFrozen();
054:
055: DatastoreObject table = col.getDatastoreContainerObject();
056:
057: if (!table.equals(mainTable)) {
058: if (!(mainTable instanceof DatastoreClass)
059: || !(table instanceof DatastoreClass)) {
060: throw new TableMismatchException(col, mainTable);
061: }
062:
063: /*
064: * Since both tables are ClassTables we assume that the column
065: * is a superclass field, meaning 'table' is a supertable of
066: * 'mainTable'. We add it to the list of supertables that will be
067: * joined in when the statement text is constructed.
068: */
069: if (!(super tables.contains(table))) {
070: super tables.add(table);
071: }
072: }
073:
074: if (mainAlias.toString().length() > 0) {
075: return col.applySelectFunction(mainAlias + "."
076: + col.getIdentifier());
077: } else {
078: return col.applySelectFunction(col.getIdentifier()
079: .toString());
080: }
081: }
082:
083: public String toString() {
084: if (sqlText == null) {
085: StringBuffer sb = new StringBuffer();
086: DatastoreIdentifier mainTableName = mainTable
087: .getIdentifier();
088:
089: Iterator i = super tables.iterator();
090: if (i.hasNext()) {
091: sb.append('(').append(mainTable.toString());
092:
093: while (i.hasNext()) {
094: DatastoreClass super table = (DatastoreClass) i
095: .next();
096:
097: sb.append(" INNER JOIN ").append(
098: super table.toString());
099: sb.append(" ON ");
100: for (int j = 0; j < ((DatastoreClass) mainTable)
101: .getIDMapping()
102: .getNumberOfDatastoreFields(); j++) {
103: DatastoreIdentifier mainTableIDColumnName = ((DatastoreClass) mainTable)
104: .getIDMapping().getDataStoreMapping(j)
105: .getDatastoreField().getIdentifier();
106: if (j > 0) {
107: sb.append(" AND ");
108: }
109: sb.append(mainTable.toString()).append('.')
110: .append(mainTableIDColumnName);
111: sb.append(" = ");
112: sb.append(super table.toString()).append('.')
113: .append(
114: super table.getIDMapping()
115: .getDataStoreMapping(j)
116: .getDatastoreField()
117: .getIdentifier());
118: }
119: }
120:
121: sb.append(") ").append(mainAlias);
122: } else {
123: sb.append(mainTable.toString());
124: if (!mainAlias.equals(mainTableName)) {
125: sb.append(' ').append(mainAlias);
126: }
127: }
128:
129: sqlText = sb.toString();
130: }
131:
132: return sqlText;
133: }
134: }
|