001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.db.sql.visualeditor.querymodel;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.Collection;
046:
047: import org.netbeans.api.db.sql.support.SQLIdentifiers;
048:
049: /**
050: * Represents a SQL FROM clause
051: */
052: public class FromNode implements From {
053:
054: // Fields
055:
056: // A vector of generalized Table objects (JoinTables)
057:
058: ArrayList _tableList;
059:
060: // Constructors
061:
062: public FromNode() {
063: }
064:
065: public FromNode(ArrayList tableList) {
066: _tableList = tableList;
067: }
068:
069: // Methods
070:
071: // Return the SQL string that corresponds to this From clause
072: public String genText(SQLIdentifiers.Quoter quoter) {
073: String res = ""; // NOI18N
074:
075: if (_tableList.size() > 0) {
076:
077: res = " FROM "
078: + ((JoinTableNode) _tableList.get(0)).genText(
079: quoter, true); // NOI18N
080:
081: for (int i = 1; i < _tableList.size(); i++)
082: res += ((JoinTableNode) _tableList.get(i))
083: .genText(quoter);
084: }
085:
086: return res;
087: }
088:
089: public String toString() {
090:
091: String res = "FROM:"; // NOI18N
092:
093: for (int i = 0; i < _tableList.size(); i++)
094: res += ((JoinTableNode) _tableList.get(i)).toString() + ""; // NOI18N
095:
096: return res;
097: }
098:
099: // Methods
100:
101: // Accessors/Mutators
102:
103: public List getTableList() {
104:
105: return _tableList;
106: }
107:
108: // Return the Table objects in the table list
109:
110: ArrayList getTables() {
111: ArrayList tableRefs = new ArrayList();
112: for (int i = 0; i < _tableList.size(); i++)
113: tableRefs.add(((JoinTableNode) _tableList.get(i))
114: .getTable());
115: return tableRefs;
116: }
117:
118: // Return the name of the penultimate table in the current FROM list,
119: // to see if it's a join candidate
120: public String getPreviousTableFullName() {
121: JoinTableNode jt = (JoinTableNode) _tableList.get(_tableList
122: .size() - 2);
123: return jt.getTable().getFullTableName();
124: }
125:
126: // Return the Table object with this tablespec
127:
128: public Table findTable(String tableSpec) {
129: for (int i = 0; i < _tableList.size(); i++) {
130: JoinTableNode jt = (JoinTableNode) _tableList.get(i);
131: if (jt.getTableSpec().equals(tableSpec))
132: return jt.getTable();
133: }
134: return null;
135: }
136:
137: public JoinTable findJoinTable(String table1, String column1,
138: String table2, String column2) {
139: ArrayList tableList = _tableList;
140: for (int i = 0; i < tableList.size(); i++) {
141: JoinTableNode jt = (JoinTableNode) tableList.get(i);
142: Expression cond = jt.getExpression();
143: if (cond instanceof Predicate) {
144: Predicate pred = (Predicate) cond;
145: Value val1 = pred.getVal1();
146: Value val2 = pred.getVal2();
147: if ((val1 instanceof ColumnNode)
148: && (val2 instanceof ColumnNode)) {
149: ColumnNode col1 = (ColumnNode) val1;
150: ColumnNode col2 = (ColumnNode) val2;
151: if (((col1.getTableSpec().equals(table1))
152: && (col1.getColumnName().equals(column1))
153: && (col2.getTableSpec().equals(table2)) && (col2
154: .getColumnName().equals(column2)))
155: || ((col2.getTableSpec().equals(table1))
156: && (col2.getColumnName()
157: .equals(column1))
158: && (col1.getTableSpec()
159: .equals(table2)) && (col1
160: .getColumnName().equals(column2)))) {
161: return jt;
162: }
163: }
164: }
165: }
166: return null;
167: }
168:
169: public String getFullTableName(String corrName) {
170: for (int i = 0; i < _tableList.size(); i++) {
171: JoinTable jt = (JoinTable) _tableList.get(i);
172: String cn = jt.getTableSpec();
173: if ((cn != null) && (cn.equals(corrName)))
174: return jt.getFullTableName();
175: }
176: return null;
177: }
178:
179: public String getTableSpec(String fullTableName) {
180: for (int i = 0; i < _tableList.size(); i++) {
181: JoinTable jt = (JoinTable) _tableList.get(i);
182: String cn = jt.getFullTableName();
183: if ((cn != null) && (cn.equals(fullTableName)))
184: return jt.getTableSpec();
185: }
186: return null;
187: }
188:
189: // Graph manipulation methods
190:
191: public void addTable(JoinTable jt) {
192: _tableList.add(jt);
193: }
194:
195: // Remove this table from the FROM list
196: void removeTable(String tableSpec) {
197: for (int i = _tableList.size() - 1; i >= 0; i--)
198: if (((JoinTableNode) _tableList.get(i)).getTableSpec()
199: .equals(tableSpec))
200: _tableList.remove(i);
201: }
202:
203: /**
204: * Rename a table
205: */
206: void renameTableSpec(String oldTableSpec, String corrName) {
207: for (int i = 0; i < _tableList.size(); i++) {
208: JoinTableNode jt = (JoinTableNode) _tableList.get(i);
209: jt.renameTableSpec(oldTableSpec, corrName);
210: }
211: }
212:
213: /**
214: * set table name
215: */
216: public void setTableSpec(String oldTableSpec, String newTableSpec) {
217: for (int i = 0; i < _tableList.size(); i++) {
218: JoinTableNode jt = (JoinTableNode) _tableList.get(i);
219: jt.setTableSpec(oldTableSpec, newTableSpec);
220: }
221: }
222:
223: public void getReferencedColumns(Collection columns) {
224: }
225: }
|