KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > JoiningSet


1 /**
2  * com.mckoi.database.JoiningSet 20 Sep 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * Used in TableSet to describe how we naturally join the tables together.
31  * This is used when the TableSet has evaluated the search condition and it
32  * is required for any straggling tables to be naturally joined. In SQL,
33  * these joining types are specified in the FROM clause.
34  * <p>
35  * For example,<br><pre>
36  * FROM table_a LEFT OUTER JOIN table_b ON ( table_a.id = table_b.id ), ...
37  * </pre><p>
38  * A ',' should donate an INNER_JOIN in an SQL FROM clause.
39  *
40  * @author Tobias Downer
41  */

42
43 public final class JoiningSet implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
44
45   static final long serialVersionUID = -380871062550922402L;
46
47   /**
48    * Statics for Join Types.
49    */

50   // Donates a standard inner join (in SQL, this is ',' in the FROM clause)
51
public final static int INNER_JOIN = 1;
52   // Left Outer Join,
53
public final static int LEFT_OUTER_JOIN = 2;
54   // Right Outer Join,
55
public final static int RIGHT_OUTER_JOIN = 3;
56   // Full Outer Join
57
public final static int FULL_OUTER_JOIN = 4;
58
59   /**
60    * The list of tables we are joining together a JoinPart object that
61    * represents how the tables are joined.
62    */

63   private ArrayList JavaDoc join_set;
64
65   /**
66    * Constructs the JoiningSet.
67    */

68   public JoiningSet() {
69     join_set = new ArrayList JavaDoc();
70   }
71
72   /**
73    * Resolves the schema of tables in this joining set. This runs through
74    * each table in the joining set and if the schema has not been set for the
75    * table then it attempts to resolve it against the given DatabaseConnection
76    * object. This would typically be called in the preparation of a statement.
77    */

78   public void prepare(DatabaseConnection connection) {
79   }
80
81   /**
82    * Adds a new table into the set being joined. The table name should be the
83    * unique name that distinguishes this table in the TableSet.
84    */

85   public void addTable(TableName table_name) {
86     join_set.add(table_name);
87   }
88
89   /**
90    * Hack, add a joining type to the previous entry from the end. This is
91    * an artifact of how joins are parsed.
92    */

93   public void addPreviousJoin(int type, Expression on_expression) {
94     join_set.add(join_set.size() - 1, new JoinPart(type, on_expression));
95   }
96
97   /**
98    * Adds a joining type to the set, and an 'on' expression.
99    */

100   public void addJoin(int type, Expression on_expression) {
101     join_set.add(new JoinPart(type, on_expression));
102   }
103
104   /**
105    * Adds a joining type to the set with no 'on' expression.
106    */

107   public void addJoin(int type) {
108     join_set.add(new JoinPart(type));
109   }
110
111   /**
112    * Returns the number of tables that are in this set.
113    */

114   public int getTableCount() {
115     return (join_set.size() + 1) / 2;
116   }
117
118   /**
119    * Returns the first table in the join set.
120    */

121   public TableName getFirstTable() {
122     return getTable(0);
123   }
124
125   /**
126    * Returns table 'n' in the result set where table 0 is the first table in
127    * the join set.
128    */

129   public TableName getTable(int n) {
130     return (TableName) join_set.get(n * 2);
131   }
132
133   /**
134    * Sets the table at the given position in this joining set.
135    */

136   private void setTable(int n, TableName table) {
137     join_set.set(n * 2, table);
138   }
139
140   /**
141    * Returns the type of join after table 'n' in the set. An example
142    * of using this;<p><pre>
143    *
144    * String table1 = joins.getFirstTable();
145    * for (int i = 0; i < joins.getTableCount() - 1; ++i) {
146    * int type = joins.getJoinType(i);
147    * String table2 = getTable(i + 1);
148    * // ... Join table1 and table2 ...
149    * table1 = table2;
150    * }
151    *
152    * </pre>
153    */

154   public int getJoinType(int n) {
155     return ((JoinPart) join_set.get((n * 2) + 1)).type;
156   }
157
158   /**
159    * Returns the ON Expression for the type of join after table 'n' in the
160    * set.
161    */

162   public Expression getOnExpression(int n) {
163     return ((JoinPart) join_set.get((n * 2) + 1)).on_expression;
164   }
165
166   /**
167    * Performs a deep clone on this object.
168    */

169   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
170     JoiningSet v = (JoiningSet) super.clone();
171     int size = join_set.size();
172     ArrayList JavaDoc cloned_join_set = new ArrayList JavaDoc(size);
173     v.join_set = cloned_join_set;
174
175     for (int i = 0; i < size; ++i) {
176       Object JavaDoc element = join_set.get(i);
177       if (element instanceof TableName) {
178         // immutable so leave alone
179
}
180       else if (element instanceof JoinPart) {
181         element = ((JoinPart) element).clone();
182       }
183       else {
184         throw new CloneNotSupportedException JavaDoc(element.getClass().toString());
185       }
186       cloned_join_set.add(element);
187     }
188
189     return v;
190   }
191
192
193
194   // ---------- Inner classes ----------
195

196   public static class JoinPart implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
197
198     static final long serialVersionUID = -1664565759669808084L;
199
200     /**
201      * The type of join. Either LEFT_OUTER_JOIN,
202      * RIGHT_OUTER_JOIN, FULL_OUTER_JOIN, INNER_JOIN.
203      */

204     int type;
205
206     /**
207      * The expression that we are joining on (eg. ON clause in SQL). If there
208      * is no ON expression (such as in the case of natural joins) then this is
209      * null.
210      */

211     Expression on_expression;
212
213     /**
214      * Constructs the JoinPart.
215      */

216     public JoinPart(int type, Expression on_expression) {
217       this.type = type;
218       this.on_expression = on_expression;
219     }
220
221     public JoinPart(int type) {
222       this(type, null);
223     }
224
225     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
226       JoinPart v = (JoinPart) super.clone();
227       if (on_expression != null) {
228         v.on_expression = (Expression) on_expression.clone();
229       }
230       return v;
231     }
232
233   }
234
235 }
236
Popular Tags