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.Collection;
044:
045: import org.netbeans.api.db.sql.support.SQLIdentifiers;
046:
047: /**
048: * Represents a column in a SELECT clause
049: */
050: public class ColumnNode extends ColumnItem implements Column {
051:
052: // Fields
053:
054: private TableNode _table;
055:
056: private Identifier _columnName;
057:
058: private Identifier _derivedColName; // Column alias
059:
060: // Constructors
061:
062: // Called from a number of places in the editor
063: // First arg is a tableSpec, which may need to be split
064: public ColumnNode(String tableSpec, String columnName) {
065:
066: String tableName = null, schemaName = null;
067:
068: // See if we've got a schema specified with the table
069: String[] table = tableSpec.split("\\.");
070: if (table.length > 1) {
071: schemaName = table[0];
072: tableName = table[1];
073: } else
074: tableName = tableSpec;
075:
076: // Note that this will take care of delimiters if necessary
077: _table = new TableNode(tableName, null, schemaName);
078: _columnName = new Identifier(columnName);
079: }
080:
081: // Called only from QueryModel.replaceStar
082: public ColumnNode(String tableName, String columnName,
083: String corrName, String schemaName) {
084: _table = new TableNode(tableName, corrName, schemaName);
085: _columnName = new Identifier(columnName);
086:
087: }
088:
089: // public ColumnNode(String tableName, String columnName, String corrName) {
090: // this(tableName, columnName, corrName, null);
091: // }
092:
093: // // Used mainly for "*", "?"
094: // public ColumnNode(String columnName) {
095: // this(null, columnName, null, null);
096: // }
097:
098: // Special case where we already have the table object
099: public ColumnNode(Table table, String columnName) {
100: _table = (TableNode) table;
101: _columnName = new Identifier(columnName);
102: }
103:
104: // Ctor used by the make method
105: private ColumnNode() {
106: }
107:
108: // Pseudo-constructor
109: // These constructors take Strings, but can't be overloaded with 'Identifier'
110: // because of compiler ambiguity
111: public static ColumnNode make(Identifier tableName,
112: Identifier columnName, Identifier schemaName,
113: Identifier derivedColName) {
114: ColumnNode c = new ColumnNode();
115: c._columnName = columnName;
116: c._derivedColName = derivedColName;
117: c._table = (tableName != null) ? TableNode.make(tableName,
118: null, schemaName) : null;
119: return c;
120: }
121:
122: // Methods
123:
124: Column getReferencedColumn() {
125: return this ;
126: }
127:
128: public void getReferencedColumns(Collection columns) {
129: }
130:
131: public boolean matches(String table, String column) {
132: return (table.equals(getTableSpec()) && column
133: .equals(getColumnName()));
134: }
135:
136: public boolean matches(String table) {
137: return table.equals(getTableSpec());
138: }
139:
140: public boolean equals(Column column) {
141: return column.matches(getTableSpec(), getColumnName());
142: }
143:
144: public String genText(SQLIdentifiers.Quoter quoter, boolean select) {
145: return
146: // Table Spec, if any
147: (((_table != null) && (_table.getTableSpec() != null)) ? _table
148: .genText(quoter, false)
149: + "." : // NOI18N
150: "")
151: + // NOI18N
152:
153: // Column Name
154: _columnName.genText(quoter) +
155:
156: // Derived Column Name, if there is one and we're in a SELECT
157: (((select) && (_derivedColName != null)) ? " AS "
158: + _derivedColName.genText(quoter) : // NOI18N
159: ""); // NOI18N
160: }
161:
162: public String genText(SQLIdentifiers.Quoter quoter) {
163: return genText(quoter, false);
164: }
165:
166: /**
167: * Rename the table part of the column spec
168: */
169: public void renameTableSpec(String oldTableSpec, String corrName) {
170: _table.renameTableSpec(oldTableSpec, corrName);
171: }
172:
173: /**
174: * set table name
175: */
176: public void setTableSpec(String oldTableSpec, String newTableSpec) {
177: if (_table == null) {
178: String tableName = null, schemaName = null;
179:
180: // See if we've got a schema specified with the table
181: String[] table = newTableSpec.split("\\.");
182: if (table.length > 1) {
183: schemaName = table[0];
184: tableName = table[1];
185: } else
186: tableName = newTableSpec;
187:
188: // Note that this will take care of delimiters if necessary
189: _table = new TableNode(tableName, null, schemaName);
190: }
191:
192: _table.setTableSpec(oldTableSpec, newTableSpec);
193: }
194:
195: // Accessors/Mutators
196:
197: public String getColumnName() {
198: return _columnName.getName();
199: }
200:
201: public String getTableSpec() {
202: return (_table == null) ? null : _table.getTableSpec();
203: }
204:
205: public String getFullTableName() {
206: return (_table == null) ? null : _table.getFullTableName();
207: }
208:
209: public String getDerivedColName() {
210: return (_derivedColName == null) ? null : _derivedColName
211: .getName();
212: }
213:
214: public void setDerivedColName(String derivedColName) {
215: _derivedColName = (derivedColName == null) ? null
216: : new Identifier(derivedColName);
217: }
218:
219: /**
220: * set column name
221: */
222: public void setColumnName(String oldColumnName, String newColumnName) {
223: if (_columnName.getName().equals(oldColumnName)) {
224: _columnName = new Identifier(newColumnName);
225: }
226: }
227:
228: public void setColumnTableName(String tableName) {
229: if (_table == null) {
230: // this should never happen.
231: _table = new TableNode();
232: }
233: _table.setTableName(tableName);
234: }
235:
236: public void setColumnCorrName(String corrName) {
237: if (_table == null) {
238: // this should never happen.
239: _table = new TableNode();
240: }
241: _table.setCorrName(corrName);
242: }
243:
244: public boolean isParameterized() {
245: return false;
246: }
247:
248: public Expression findExpression(String table1, String column1,
249: String table2, String column2) {
250: return null;
251: }
252: }
|