KickJava   Java API By Example, From Geeks To Geeks.

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


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: TableExprAsSubquery.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.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16
17 /**
18  * A SQL table expression that joins superclass tables by constructing a
19  * parenthesized sub-SELECT statement.
20  *
21  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
22  * @version $Revision: 1.1 $
23  *
24  * @see QueryStatement
25  */

26
27 class TableExprAsSubquery extends TableExpression
28 {
29     protected final ArrayList JavaDoc columns = new ArrayList JavaDoc();
30     protected boolean multipleTablesReferenced = false;
31
32     public TableExprAsSubquery(QueryStatement qs, Table mainTable, SQLIdentifier mainRangeVar)
33     {
34         super(qs, mainTable, mainRangeVar);
35     }
36
37     public String JavaDoc referenceColumn(Column col)
38     {
39         assertNotFrozen();
40
41         Table table = col.getTable();
42
43         if (!table.equals(mainTable))
44         {
45             if (!(mainTable instanceof ClassBaseTable) || !(table instanceof ClassBaseTable))
46                 throw new TableMismatchException(col, mainTable);
47
48             /*
49              * Since both tables are ClassBaseTables we assume that the column
50              * is a superclass field, meaning 'table' is a supertable of
51              * 'mainTable'. We set the flag indicating that this expression
52              * must become a subquery that joins the necessary tables.
53              */

54
55             multipleTablesReferenced = true;
56         }
57
58         if (!columns.contains(col))
59             columns.add(col);
60
61         return mainRangeVar + "." + col.getName();
62     }
63
64     public String JavaDoc toString()
65     {
66         if (sqlText == null)
67         {
68             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
69             SQLIdentifier mainTableName = mainTable.getName();
70
71             if (!multipleTablesReferenced)
72             {
73                 sb.append(mainTableName);
74
75                 if (!mainRangeVar.equals(mainTableName))
76                     sb.append(' ').append(mainRangeVar);
77             }
78             else
79             {
80                 FetchStatement subQuery = new FetchStatement((ClassBaseTable)mainTable);
81
82                 Iterator JavaDoc i = columns.iterator();
83
84                 while (i.hasNext())
85                     subQuery.select((Column)i.next());
86
87                 sb.append('(').append(subQuery).append(") ").append(mainRangeVar);
88             }
89
90             sqlText = sb.toString();
91         }
92
93         return sqlText;
94     }
95 }
96
Popular Tags