KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.versant.core.jdbc.sql.SqlDriver;
15 import com.versant.core.jdbc.metadata.JdbcColumn;
16 import com.versant.core.util.CharBuf;
17 import com.versant.core.common.Debug;
18
19 import java.util.Map JavaDoc;
20
21 import com.versant.core.common.BindingSupportImpl;
22
23 /**
24  * This is a join between two columns.
25  */

26 public class JoinExp extends LeafExp {
27
28     private JdbcColumn left;
29     private SelectExp leftTable;
30     private JdbcColumn right;
31     private SelectExp rightTable;
32
33     public JoinExp(JdbcColumn left, SelectExp leftTable,
34             JdbcColumn right, SelectExp rightTable) {
35         if (Debug.DEBUG) {
36             if (left.table != leftTable.table) {
37                 throw BindingSupportImpl.getInstance().internal("The column's table is not " +
38                         "the same as the table being joined from");
39             }
40
41             if (right.table != rightTable.table) {
42                 throw BindingSupportImpl.getInstance().internal("The column's table is not " +
43                         "the same as the table being joined too");
44             }
45         }
46         this.left = left;
47         this.leftTable = leftTable;
48         this.right = right;
49         this.rightTable = rightTable;
50     }
51
52     public void setLeftTable(SelectExp leftSExp) {
53         leftTable = leftSExp;
54     }
55
56     public JoinExp() {
57     }
58
59     public SqlExp createInstance() {
60         return new JoinExp();
61     }
62
63     public SqlExp getClone(SqlExp clone, Map JavaDoc cloneMap) {
64         super.getClone(clone, cloneMap);
65         JoinExp cst = (JoinExp) clone;
66
67         cst.left = left;
68         if (leftTable != null) cst.leftTable = (SelectExp) createClone(leftTable, cloneMap);
69         cst.right = right;
70         if (rightTable != null) cst.rightTable = (SelectExp) createClone(rightTable, cloneMap);
71
72         return clone;
73     }
74
75     public String JavaDoc toString() {
76         return super.toString() + " " + left + " = " + right;
77     }
78
79     /**
80      * Append SQL for this node to s.
81      *
82      * @param driver The driver being used
83      * @param s Append the SQL here
84      * @param leftSibling
85      */

86     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
87         driver.appendSqlJoin(leftTable.alias, left,
88                 rightTable.alias, right, rightTable.outer, s);
89     }
90
91     /**
92      * Make us an outer join or not. This is a NOP except for JoinExp and
93      * AndJoinExp.
94      * @see JoinExp
95      * @see AndJoinExp
96      */

97     public void setOuter(boolean on) {
98         rightTable.outer = on;
99     }
100
101     public boolean isOuter() {
102         return rightTable.outer;
103     }
104
105     /**
106      * Replace any references to old with nw. This is used when redundant
107      * joins are removed.
108      *
109      * @see com.versant.core.jdo.query.OrNode#mergeRedundantExistsSelects
110      */

111     public void replaceSelectExpRef(SelectExp old, SelectExp nw) {
112         if (old == leftTable) leftTable = nw;
113         if (old == rightTable) rightTable = nw;
114     }
115
116     public static boolean isEqual(JoinExp jExp1, JoinExp jExp2) {
117         if (jExp1 == jExp2) return true;
118         if (jExp1 == null) return false;
119         if (jExp2 == null) return false;
120
121         if ((jExp1.left == jExp2.left)
122                 && (jExp1.right == jExp2.right)
123                 && (jExp1.isOuter() == jExp2.isOuter())) {
124             return true;
125         }
126         return false;
127
128     }
129 }
130
131
Popular Tags