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.Collection;
045:
046: import org.netbeans.api.db.sql.support.SQLIdentifiers;
047:
048: public class SelectNode implements Select {
049:
050: // Fields
051:
052: // A vector of Column specifications
053: // This will eventually include functions, but for now is simple columns
054:
055: // ToDo: consider replacing this with a HashMap
056: private ArrayList _selectItemList;
057: private String _quantifier;
058:
059: // Constructor
060:
061: public SelectNode() {
062: }
063:
064: public SelectNode(ArrayList columnList, String quantifier) {
065: _selectItemList = columnList;
066: _quantifier = quantifier;
067: }
068:
069: public SelectNode(ArrayList columnList) {
070: this (columnList, ""); // NOI18N
071: }
072:
073: // Return the Select clause as a SQL string
074:
075: public String genText(SQLIdentifiers.Quoter quoter) {
076: String res = ""; // NOI18N
077: String res_select_quantifier = ""; // NOI18N
078:
079: if (_selectItemList.size() > 0) {
080: res_select_quantifier = (_quantifier.length() == 0) ? "SELECT "
081: : "SELECT " + _quantifier + " "; // NOI18N
082: res = res_select_quantifier
083: + ((ColumnItem) _selectItemList.get(0)).genText(
084: quoter, true); // NOI18N
085:
086: for (int i = 1; i < _selectItemList.size(); i++) {
087: ColumnItem col = (ColumnItem) _selectItemList.get(i);
088: if (col != null) {
089: res += ", " + col.genText(quoter, true); // NOI18N
090: }
091: }
092: }
093: return res;
094: }
095:
096: // Accessors/Mutators
097:
098: public void setColumnList(ArrayList columnList) {
099: _selectItemList = columnList;
100: }
101:
102: public void getReferencedColumns(Collection columns) {
103: for (int i = 0; i < _selectItemList.size(); i++)
104: columns.add(((ColumnItem) _selectItemList.get(i))
105: .getReferencedColumn());
106: }
107:
108: public int getSize() {
109: return _selectItemList.size();
110: }
111:
112: public void addColumn(Column col) {
113: _selectItemList.add(col);
114: }
115:
116: public void addColumn(String tableSpec, String columnName) {
117: _selectItemList.add(new ColumnNode(tableSpec, columnName));
118: }
119:
120: // Remove the specified column from the SELECT list
121: // Iterate back-to-front for stability under deletion
122: public void removeColumn(String tableSpec, String columnName) {
123: for (int i = _selectItemList.size() - 1; i >= 0; i--) {
124: ColumnItem item = (ColumnItem) _selectItemList.get(i);
125: ColumnNode c = (ColumnNode) item.getReferencedColumn();
126: if ((c != null) && (c.getTableSpec().equals(tableSpec))
127: && (c.getColumnName().equals(columnName))) {
128: _selectItemList.remove(i);
129: }
130: }
131: }
132:
133: /**
134: * set column name
135: */
136: public void setColumnName(String oldColumnName, String newColumnName) {
137: for (int i = 0; i < _selectItemList.size(); i++) {
138: ColumnNode c = (ColumnNode) _selectItemList.get(i);
139: if (c != null) {
140: c.setColumnName(oldColumnName, newColumnName);
141: }
142: }
143: }
144:
145: public boolean hasAsteriskQualifier() {
146: for (int i = 0; i < _selectItemList.size(); i++) {
147: ColumnItem item = (ColumnItem) _selectItemList.get(i);
148: if (item instanceof ColumnNode) {
149: ColumnNode c = (ColumnNode) item;
150: if (c.getColumnName().equals("*")) {
151: return true;
152: }
153: }
154: }
155: return false;
156: }
157:
158: /**
159: * Remove any SELECT targets that reference this table
160: */
161: void removeTable(String tableSpec) {
162: for (int i = _selectItemList.size() - 1; i >= 0; i--) {
163: ColumnItem item = (ColumnItem) _selectItemList.get(i);
164: ColumnNode c = (ColumnNode) item.getReferencedColumn();
165: if (c != null) {
166: String tabSpec = c.getTableSpec();
167: if (tabSpec != null && tabSpec.equals(tableSpec))
168: _selectItemList.remove(i);
169: }
170: }
171: }
172:
173: /**
174: * Rename a table
175: */
176: void renameTableSpec(String oldTableSpec, String corrName) {
177: for (int i = 0; i < _selectItemList.size(); i++) {
178: ColumnItem item = (ColumnItem) _selectItemList.get(i);
179: ColumnNode c = (ColumnNode) item.getReferencedColumn();
180: if (c != null) {
181: c.renameTableSpec(oldTableSpec, corrName);
182: }
183: }
184: }
185:
186: }
|