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: /**
044: * Represents a generalized table specification in a SQL FROM clause
045: * Example forms:
046: * employees e
047: * INNER JOIN employees e ON e.id = e.id
048: */
049:
050: import org.netbeans.modules.db.sql.visualeditor.querybuilder.QueryBuilderMetaData;
051:
052: import java.util.Collection;
053:
054: import org.netbeans.api.db.sql.support.SQLIdentifiers;
055:
056: public class JoinTableNode implements JoinTable {
057:
058: // Fields
059:
060: private String _joinType; // INNER, OUTER, CROSS, NATURAL, ,
061: private TableNode _table;
062: private Expression _condition; // simplified WHERE clause
063:
064: // Constructors
065:
066: public JoinTableNode() {
067: }
068:
069: public JoinTableNode(TableNode table, String joinType,
070: Expression condition) {
071: _table = table;
072: _joinType = joinType;
073: _condition = condition;
074: }
075:
076: public JoinTableNode(TableNode table) {
077: this (table, null, null);
078: }
079:
080: // Methods
081:
082: // Return the SQL string that corresponds to this From clause
083: // For now, assume no joins
084: public String genText(SQLIdentifiers.Quoter quoter) {
085: String res = (((_joinType == null) || (_joinType
086: .equals("CROSS"))) ? ", " : " " + _joinType
087: + " JOIN ") // NOI18N
088: + _table.genText(quoter, true);
089:
090: if (_condition != null) {
091: res += " ON " + _condition.genText(quoter); // NOI18N
092: }
093:
094: return res;
095: }
096:
097: // Special processing for the first table in the list
098: // Omit the join specification
099: public String genText(SQLIdentifiers.Quoter quoter, boolean first) {
100: return (first ? _table.genText(quoter, true) : this
101: .genText(quoter));
102: }
103:
104: // Methods
105:
106: // Accessors/Mutators
107:
108: public Table getTable() {
109: return _table;
110: }
111:
112: public String getTableName() {
113: return _table.getTableName();
114: }
115:
116: public String getCorrName() {
117: return _table.getCorrName();
118: }
119:
120: public String getTableSpec() {
121: return _table.getTableSpec();
122: }
123:
124: public String getFullTableName() {
125: return _table.getFullTableName();
126: }
127:
128: public String getJoinType() {
129: return _joinType;
130: }
131:
132: public void setJoinType(String joinType) {
133: _joinType = joinType;
134: }
135:
136: public Expression getExpression() {
137: return _condition;
138: }
139:
140: public void setExpression(Expression condition) {
141: _condition = condition;
142: }
143:
144: // adds any column in the condition to the ArrayList of columns
145: public void getReferencedColumns(Collection columns) {
146: if (_condition != null)
147: _condition.getReferencedColumns(columns);
148: }
149:
150: void renameTableSpec(String oldTableSpec, String corrName) {
151: ((TableNode) this .getTable()).renameTableSpec(oldTableSpec,
152: corrName);
153:
154: if (_condition instanceof Predicate)
155: ((Predicate) _condition).renameTableSpec(oldTableSpec,
156: corrName);
157: }
158:
159: void setTableSpec(String oldTableSpec, String newTableSpec) {
160: ((TableNode) this .getTable()).setTableSpec(oldTableSpec,
161: newTableSpec);
162: }
163:
164: public void addJoinCondition(String[] rel) {
165:
166: // Convert relationship into join
167: ColumnNode col1 = new ColumnNode(rel[0], rel[1]);
168: ColumnNode col2 = new ColumnNode(rel[2], rel[3]);
169: Predicate pred = new Predicate(col1, col2);
170:
171: // Update the JoinTable object with join information
172: setJoinType("INNER"); // NOI18N
173: setExpression(pred);
174: }
175: }
|