KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > exp > Join


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.exp;
13
14 import java.util.Map JavaDoc;
15
16 /**
17  * A join to a table.
18  * @see SelectExp
19  */

20 public class Join {
21
22     /**
23      * The table being joined to.
24      */

25     public SelectExp selectExp;
26     /**
27      * The expression that joins the tables.
28      */

29     public SqlExp exp;
30     /**
31      * The next join in the list.
32      */

33     public Join next;
34     private Join mergedWith;
35
36     public Join() {
37     }
38
39     public Join getClone(Map JavaDoc cloneMap) {
40         Join clone = new Join();
41         if (selectExp != null) clone.selectExp = (SelectExp) SqlExp.createClone(selectExp, cloneMap);
42         if (exp != null) clone.exp = SqlExp.createClone(exp, cloneMap);
43         if (next != null) clone.next = next.getClone(cloneMap);
44         return clone;
45     }
46
47     public String JavaDoc toString() {
48         return "Join@" + System.identityHashCode(this) + " " + selectExp;
49     }
50
51     /**
52      * Dump debugging info to System.out.
53      */

54     public void dump(String JavaDoc indent) {
55         System.out.println(indent + this);
56         indent = indent + " ";
57         if (exp == null) {
58             System.out.println(indent + " exp is NULL");
59         } else {
60             exp.dump(indent);
61         }
62         selectExp.dump(indent);
63     }
64
65     /**
66      * Dump us and our list to System.out.
67      */

68     public void dumpList(String JavaDoc indent) {
69         dump(indent);
70         for (Join j = next; j != null; j = j.next) j.dump(indent);
71     }
72
73     /**
74      * Create an aliases for any tables we may have.
75      */

76     public int createAlias(int index) {
77         if (mergedWith != null) {
78             index = mergedWith.createAlias(index);
79             selectExp.alias = mergedWith.selectExp.alias;
80         }
81
82         index = selectExp.createAlias(index);
83         if (exp != null) {
84             return exp.createAlias(index);
85         } else {
86             return index;
87         }
88     }
89
90     /**
91      * Add an extra expression to our exp. This will be joined with any
92      * existing exp using and.
93      */

94     public void appendJoinExp(SqlExp e) {
95         if (exp == null) {
96             exp = e;
97         } else if (exp instanceof AndExp) {
98             exp.append(e);
99         } else {
100             AndExp ae = new AndExp(exp);
101             exp.next = e;
102             exp = ae;
103         }
104     }
105
106     /**
107      * Find the deepest Join from us down. This will return this if there
108      * are no joins from our selectExp.
109      */

110     public Join findDeepestJoin() {
111         if (selectExp.joinList == null) return this;
112         Join j;
113         for (j = selectExp.joinList; j.next != null; j = j.next);
114         return j.findDeepestJoin();
115     }
116
117     /**
118      * If the join tree's are equivalent.
119      */

120     public static boolean isEqaul(Join j1, Join j2) {
121         if (j1 == j2) return true;
122         if (j1 == null) return false;
123         if (j2 == null) return false;
124
125         if (j1.selectExp != null && j2.selectExp != null) {
126             if (j1.selectExp.jdbcField == j2.selectExp.jdbcField) {
127                 if (j1.exp instanceof JoinExp && j2.exp instanceof JoinExp) {
128                     if (JoinExp.isEqual((JoinExp)j1.exp, (JoinExp)j2.exp)) {
129                         if (isEqaul(j1.selectExp.joinList, j2.selectExp.joinList)) {
130                             return Join.isEqaul(j1.next, j2.next);
131                         }
132                     }
133                 }
134             }
135         }
136         return false;
137     }
138
139     /**
140      * Only check if the actual two joins are equal and ignore next joins.
141      */

142     public static boolean isCurrentEqaul(Join j1, Join j2) {
143         if (j1 == j2) return true;
144         if (j1 == null) return false;
145         if (j2 == null) return false;
146         
147         if (j1.selectExp != null && j2.selectExp != null) {
148             if (j1.selectExp.jdbcField == j2.selectExp.jdbcField) {
149                 if (j1.exp instanceof JoinExp && j2.exp instanceof JoinExp) {
150                     return JoinExp.isEqual((JoinExp)j1.exp, (JoinExp)j2.exp);
151                 }
152             }
153         }
154         return false;
155     }
156
157     /**
158      * Return true if this join is only acting as a place holder for the selectList
159      */

160     public boolean isMerged() {
161         return mergedWith != null;
162     }
163
164     public Join getMergedWith() {
165         return mergedWith;
166     }
167
168     public void setMergedWith(Join mergedWith) {
169         this.mergedWith = mergedWith;
170     }
171 }
172
Popular Tags