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: public class GroupByNode implements GroupBy {
050:
051: // Fields
052:
053: // A vector of Column specifications
054: // This will eventually include functions, but for now is simple columns
055:
056: // ToDo: consider replacing this with a HashMap
057: private List _columnList;
058:
059: // Constructor
060:
061: public GroupByNode() {
062: }
063:
064: public GroupByNode(ArrayList columnList) {
065: _columnList = columnList;
066: }
067:
068: // Return the Select clause as a SQL string
069:
070: public String genText(SQLIdentifiers.Quoter quoter) {
071: String res = " GROUP BY "; // NOI18N
072:
073: if (_columnList.size() > 0) {
074: res += ((ColumnNode) _columnList.get(0)).genText(quoter);
075: for (int i = 1; i < _columnList.size(); i++) {
076: res += ", "
077: + ((ColumnNode) _columnList.get(i))
078: .genText(quoter); // NOI18N
079: }
080: }
081:
082: return res;
083: }
084:
085: // Accessors/Mutators
086:
087: public void setColumnList(List columnList) {
088: _columnList = columnList;
089: }
090:
091: // adds any column in the condition to the ArrayList of columns
092: public void getReferencedColumns(Collection columns) {
093: if (_columnList != null)
094: columns.addAll(_columnList);
095: }
096:
097: public void addColumn(Column col) {
098: _columnList.add(col);
099: }
100:
101: public void addColumn(String tableSpec, String columnName) {
102: _columnList.add(new ColumnNode(tableSpec, columnName));
103: }
104:
105: // Remove the specified column from the SELECT list
106: // Iterate back-to-front for stability under deletion
107: public void removeColumn(String tableSpec, String columnName) {
108: for (int i = _columnList.size() - 1; i >= 0; i--) {
109: ColumnNode c = (ColumnNode) _columnList.get(i);
110: if ((c.getTableSpec().equals(tableSpec))
111: && (c.getColumnName().equals(columnName))) {
112: _columnList.remove(i);
113: }
114: }
115: }
116:
117: /**
118: * Remove any GroupBy targets that reference this table
119: */
120: void removeTable(String tableSpec) {
121: for (int i = _columnList.size() - 1; i >= 0; i--) {
122: ColumnNode c = (ColumnNode) _columnList.get(i);
123: if (c.getTableSpec().equals(tableSpec))
124: _columnList.remove(i);
125: }
126: }
127:
128: void renameTableSpec(String oldTableSpec, String corrName) {
129:
130: for (int i = 0; i < _columnList.size(); i++)
131: ((ColumnNode) _columnList.get(i)).renameTableSpec(
132: oldTableSpec, corrName);
133: }
134: }
|