KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 /**
26  * Represents a SQL FROM clause
27  */

28 public class FromNode implements From {
29
30     // Fields
31

32     // A vector of generalized Table objects (JoinTables)
33

34     ArrayList JavaDoc _tableList;
35
36
37     // Constructors
38

39     public FromNode() {
40     }
41
42     public FromNode(ArrayList JavaDoc tableList) {
43         _tableList = tableList;
44     }
45
46
47     // Methods
48

49     // Return the SQL string that corresponds to this From clause
50
public String JavaDoc genText() {
51         String JavaDoc res = ""; // NOI18N
52

53         if (_tableList.size() > 0) {
54
55             res = "\nFROM " + ((JoinTableNode)_tableList.get(0)).genText(true); // NOI18N
56

57             for (int i=1; i<_tableList.size(); i++)
58                 res += ((JoinTableNode)_tableList.get(i)).genText();
59         }
60
61         return res;
62     }
63
64
65     public String JavaDoc toString() {
66
67         String JavaDoc res = "FROM:\n"; // NOI18N
68

69         for (int i=0; i<_tableList.size(); i++)
70             res += "\t" + ((JoinTableNode)_tableList.get(i)).toString() + "\n"; // NOI18N
71

72         return res;
73     }
74
75
76     // Methods
77

78     // Accessors/Mutators
79

80     public List JavaDoc getTableList() {
81
82         return _tableList;
83     }
84
85     // Return the Table objects in the table list
86

87     ArrayList JavaDoc getTables() {
88         ArrayList JavaDoc tableRefs = new ArrayList JavaDoc();
89         for (int i=0; i<_tableList.size(); i++)
90             tableRefs.add(((JoinTableNode)_tableList.get(i)).getTable());
91         return tableRefs;
92     }
93
94     // Return the name of the penultimate table in the current FROM list,
95
// to see if it's a join candidate
96
public String JavaDoc getPreviousTableFullName() {
97         JoinTableNode jt = (JoinTableNode)_tableList.get(_tableList.size()-2);
98         return jt.getTable().getFullTableName();
99     }
100
101
102     // Return the Table object with this tablespec
103

104     public Table findTable(String JavaDoc tableSpec) {
105         for (int i=0; i<_tableList.size(); i++) {
106             JoinTableNode jt = (JoinTableNode) _tableList.get(i);
107             if (jt.getTableSpec().equals(tableSpec))
108                 return jt.getTable();
109         }
110         return null;
111     }
112
113     public JoinTable findJoinTable(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2) {
114         ArrayList JavaDoc tableList = _tableList;
115         for (int i=0; i<tableList.size(); i++) {
116             JoinTableNode jt = (JoinTableNode) tableList.get(i);
117             Expression cond = jt.getExpression();
118             if (cond instanceof Predicate) {
119                 Predicate pred = (Predicate) cond;
120                 Value val1 = pred.getVal1();
121                 Value val2 = pred.getVal2();
122                 if ((val1 instanceof ColumnNode) && (val2 instanceof ColumnNode)) {
123                     ColumnNode col1 = (ColumnNode) val1;
124                     ColumnNode col2 = (ColumnNode) val2;
125                     if (((col1.getTableSpec().equals(table1)) &&
126                          (col1.getColumnName().equals(column1)) &&
127                          (col2.getTableSpec().equals(table2)) &&
128                          (col2.getColumnName().equals(column2))) ||
129                         ((col2.getTableSpec().equals(table1)) &&
130                          (col2.getColumnName().equals(column1)) &&
131                          (col1.getTableSpec().equals(table2)) &&
132                          (col1.getColumnName().equals(column2)))) {
133                         return jt;
134                     }
135                 }
136             }
137         }
138         return null;
139     }
140
141     public String JavaDoc getFullTableName(String JavaDoc corrName) {
142         for (int i=0; i<_tableList.size(); i++) {
143             JoinTable jt = (JoinTable) _tableList.get(i);
144             String JavaDoc cn=jt.getTableSpec();
145             if ((cn!=null) && (cn.equals(corrName)))
146                 return jt.getFullTableName();
147         }
148         return null;
149     }
150
151     public String JavaDoc getTableSpec(String JavaDoc fullTableName) {
152         for (int i=0; i<_tableList.size(); i++) {
153             JoinTable jt = (JoinTable) _tableList.get(i);
154             String JavaDoc cn=jt.getFullTableName();
155             if ((cn!=null) && (cn.equals(fullTableName)))
156                 return jt.getTableSpec();
157         }
158         return null;
159     }
160
161     
162     // Graph manipulation methods
163

164     public void addTable(JoinTable jt) {
165         _tableList.add(jt);
166     }
167
168     // Remove this table from the FROM list
169
void removeTable(String JavaDoc tableSpec) {
170         for (int i=_tableList.size()-1; i>=0; i--)
171             if (((JoinTableNode)_tableList.get(i)).getTableSpec().equals(tableSpec))
172                 _tableList.remove(i);
173     }
174
175     /**
176      * Rename a table
177      */

178     void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
179         for (int i=0; i<_tableList.size(); i++) {
180             JoinTableNode jt = (JoinTableNode) _tableList.get(i);
181             jt.renameTableSpec(oldTableSpec, corrName);
182         }
183     }
184     
185
186     /**
187      * set table name
188      */

189     public void setTableSpec(String JavaDoc oldTableSpec, String JavaDoc newTableSpec) {
190         for (int i=0; i<_tableList.size(); i++) {
191             JoinTableNode jt = (JoinTableNode) _tableList.get(i);
192             jt.setTableSpec(oldTableSpec, newTableSpec);
193         }
194     }
195
196     public void getReferencedColumns(Collection JavaDoc columns) {}
197     public void getQueryItems(Collection JavaDoc items) {}
198 }
199
Popular Tags