KickJava   Java API By Example, From Geeks To Geeks.

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


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: TableExprAsSubjoins.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 set of subjoins.
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 TableExprAsSubjoins extends TableExpression
28 {
29     protected final ArrayList JavaDoc supertables = new ArrayList JavaDoc();
30
31     public TableExprAsSubjoins(QueryStatement qs, Table mainTable, SQLIdentifier mainRangeVar)
32     {
33         super(qs, mainTable, mainRangeVar);
34     }
35
36     public String JavaDoc referenceColumn(Column col)
37     {
38         assertNotFrozen();
39
40         Table table = col.getTable();
41
42         if (!table.equals(mainTable))
43         {
44             if (!(mainTable instanceof ClassBaseTable) || !(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 add it to the list of supertables that will be
51              * joined in when the statement text is constructed.
52              */

53             if (!(supertables.contains(table)))
54                 supertables.add(table);
55         }
56
57         return mainRangeVar + "." + col.getName();
58     }
59
60     public String JavaDoc toString()
61     {
62         if (sqlText == null)
63         {
64             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
65             SQLIdentifier mainTableName = mainTable.getName();
66
67             Iterator JavaDoc i = supertables.iterator();
68
69             if (i.hasNext())
70             {
71                 SQLIdentifier mainTableIDColumnName = ((ClassBaseTable)mainTable).getIDMapping().getColumn().getName();
72                 sb.append('(').append(mainTableName);
73
74                 while (i.hasNext())
75                 {
76                     ClassBaseTable supertable = (ClassBaseTable)i.next();
77                     SQLIdentifier supertableName = supertable.getName();
78
79                     sb.append(" INNER JOIN ").append(supertableName)
80                       .append(" ON ")
81                       .append(mainTableName).append('.').append(mainTableIDColumnName)
82                       .append(" = ")
83                       .append(supertableName).append('.').append(supertable.getIDMapping().getColumn().getName());
84                 }
85
86                 sb.append(") ").append(mainRangeVar);
87             }
88             else
89             {
90                 sb.append(mainTableName);
91
92                 if (!mainRangeVar.equals(mainTableName))
93                     sb.append(' ').append(mainRangeVar);
94             }
95
96             sqlText = sb.toString();
97         }
98
99         return sqlText;
100     }
101 }
102
Popular Tags