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 generalized table
049: */
050: public class TableNode implements Table {
051:
052: // Fields
053:
054: private Identifier _tableName;
055:
056: private Identifier _corrName;
057:
058: private Identifier _schemaName;
059:
060: // Constructors
061:
062: public TableNode(String tableName, String corrName,
063: String schemaName) {
064: _tableName = new Identifier(tableName);
065: _corrName = corrName == null ? null : new Identifier(corrName);
066: _schemaName = schemaName == null ? null : new Identifier(
067: schemaName);
068: }
069:
070: public TableNode(String tableName, String corrName) {
071: this (tableName, corrName, null);
072: }
073:
074: public TableNode(String tableName) {
075: this (tableName, null, null);
076: }
077:
078: public TableNode() {
079: }
080:
081: // PseudoConstructor
082: // Use static methods because we can't overload Identier, String
083: public static TableNode make(Identifier tableName,
084: Identifier corrName, Identifier schemaName) {
085: TableNode t = new TableNode();
086: t._tableName = tableName;
087: t._corrName = corrName;
088: t._schemaName = schemaName;
089: return t;
090: }
091:
092: // Methods
093: public String genText(SQLIdentifiers.Quoter quoter) {
094: return genText(quoter, false);
095: }
096:
097: // Return the SQL string that corresponds to this Table
098: // This was originally called only in FROM clauses, but is now used as
099: // part of column specifications.
100: // For now, assume no joins
101: public String genText(SQLIdentifiers.Quoter quoter, boolean from) {
102: if (from) // Calling from within a FROM clause
103: return ((_schemaName == null) ? "" : _schemaName
104: .genText(quoter)
105: + ".")
106: + // NOI18N
107: _tableName.genText(quoter) +
108: // remove AS to fix CR5097412
109: ((_corrName == null) ? "" : " "
110: + _corrName.genText(quoter)); // NOI18N
111: else
112: // Calling from within a column
113: return ((_corrName != null) ? _corrName.genText(quoter)
114: : ((_schemaName == null) ? "" : _schemaName
115: .genText(quoter)
116: + ".")
117: + // NOI18N
118: _tableName.genText(quoter));
119: }
120:
121: // Accessors/Mutators
122:
123: public String getTableName() {
124: return _tableName.getName();
125: }
126:
127: public String getFullTableName() {
128: return ((_schemaName == null) ? "" : _schemaName.getName()
129: + ".")
130: + // NOI18N
131: _tableName.getName();
132: }
133:
134: public String getCorrName() {
135: return (_corrName == null) ? null : _corrName.getName();
136: }
137:
138: public String getSchemaName() {
139: return (_schemaName == null) ? null : _schemaName.getName();
140: }
141:
142: public String getTableSpec() {
143: return (_corrName != null) ? _corrName.getName()
144: : getFullTableName();
145: }
146:
147: // if oldTableSpec is null, just replace the table schema and name.
148: public void renameTableSpec(String oldTableSpec, String corrName) {
149: // If we really have a correlation name, there cannot be any schema specified to split
150: if (oldTableSpec == null) {
151: String[] table = corrName.split("\\."); // NOI18N
152: if (table.length > 1) {
153: _schemaName = new Identifier(table[0]);
154: _tableName = new Identifier(table[1]);
155: } else
156: _tableName = new Identifier(table[0]);
157: } else if (getTableSpec().equals(oldTableSpec))
158: _corrName = (corrName == null) ? null : new Identifier(
159: corrName);
160: }
161:
162: void setTableName(String tableName) {
163: String[] table = tableName.split("\\."); // NOI18N
164: if (table.length > 1) {
165: _schemaName = new Identifier(table[0]);
166: _tableName = new Identifier(table[1]);
167: } else
168: _tableName = new Identifier(table[0]);
169: }
170:
171: void setCorrName(String corrName) {
172: _corrName = new Identifier(corrName);
173: }
174:
175: void setTableSpec(String oldTableSpec, String newTableSpec) {
176: if (getTableSpec().equals(oldTableSpec)) {
177: String[] table = newTableSpec.split("\\."); // NOI18N
178: if (table.length > 1) {
179: _schemaName = new Identifier(table[0]);
180: _tableName = new Identifier(table[1]);
181: } else
182: _tableName = new Identifier(table[0]);
183: }
184:
185: }
186:
187: public void getReferencedColumns(Collection columns) {
188: }
189: }
|