KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querymodel > JoinTableNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querymodel;
20
21 /**
22  * Represents a generalized table specification in a SQL FROM clause
23  * Example forms:
24  * employees e
25  * INNER JOIN employees e ON e.id = e.id
26  */

27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30
31 public class JoinTableNode implements JoinTable {
32
33     // Fields
34

35     private String JavaDoc _joinType; // INNER, OUTER, CROSS, NATURAL, ,
36
private TableNode _table;
37     private Expression _condition; // simplified WHERE clause
38

39     // Constructors
40

41     public JoinTableNode () {
42     }
43
44     public JoinTableNode(TableNode table, String JavaDoc joinType, Expression condition) {
45         _table = table;
46         _joinType = joinType;
47         _condition = condition;
48     }
49
50     public JoinTableNode(TableNode table) {
51         this(table, null, null);
52     }
53
54
55     // Methods
56

57     // Return the SQL string that corresponds to this From clause
58
// For now, assume no joins
59
public String JavaDoc genText() {
60         String JavaDoc res =
61             (((_joinType==null)||(_joinType.equals("CROSS")))
62              ? ", "
63              : "\n " +_joinType + " JOIN ") // NOI18N
64
+ _table.genText(true);
65
66         if (_condition != null) {
67             res += " ON " + _condition.genText(); // NOI18N
68
}
69
70         return res;
71     }
72
73
74     // Special processing for the first table in the list
75
// Omit the join specification
76
public String JavaDoc genText(boolean first) {
77         return (first ? _table.genText(true) : this.genText());
78     }
79
80
81     // Methods
82

83     // Accessors/Mutators
84

85     public Table getTable() {
86         return _table;
87     }
88
89     public String JavaDoc getTableName() {
90         return _table.getTableName();
91     }
92
93     public String JavaDoc getCorrName() {
94         return _table.getCorrName();
95     }
96
97     public String JavaDoc getTableSpec() {
98         return _table.getTableSpec();
99     }
100
101     public String JavaDoc getFullTableName() {
102         return _table.getFullTableName();
103     }
104
105     public String JavaDoc getJoinType () {
106         return _joinType;
107     }
108
109     public void setJoinType (String JavaDoc joinType) {
110         _joinType = joinType;
111     }
112
113     public Expression getExpression() {
114         return _condition;
115     }
116
117     public void setExpression(Expression condition) {
118         _condition = condition;
119     }
120
121     // adds any column in the condition to the ArrayList of columns
122
public void getReferencedColumns (Collection JavaDoc columns) {
123         if (_condition != null)
124             _condition.getReferencedColumns(columns);
125     }
126
127     public void getQueryItems(Collection JavaDoc items) {
128         if (_condition != null) {
129             items.add(_table);
130             items.add(_condition);
131         }
132     }
133
134     void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
135         ((TableNode)this.getTable()).renameTableSpec(oldTableSpec, corrName);
136
137         if (_condition instanceof Predicate)
138             ((Predicate)_condition).renameTableSpec(oldTableSpec, corrName);
139     }
140
141     void setTableSpec(String JavaDoc oldTableSpec, String JavaDoc newTableSpec)
142     {
143         ((TableNode)this.getTable()).setTableSpec(oldTableSpec, newTableSpec);
144     }
145
146     public void addJoinCondition(String JavaDoc[] rel) {
147
148         // Convert relationship into join
149
ColumnNode col1 = new ColumnNode(rel[0], rel[1]);
150         ColumnNode col2 = new ColumnNode(rel[2], rel[3]);
151         Predicate pred = new Predicate(col1, col2);
152
153         // Update the JoinTable object with join information
154
setJoinType("INNER"); // NOI18N
155
setExpression(pred);
156     }
157 }
158
Popular Tags