KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querymodel > GroupByNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querymodel;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 public class GroupByNode implements GroupBy {
26
27     // Fields
28

29     // A vector of Column specifications
30
// This will eventually include functions, but for now is simple columns
31

32     // ToDo: consider replacing this with a HashMap
33
private List JavaDoc _columnList;
34
35
36     // Constructor
37

38     public GroupByNode() {
39     }
40
41     public GroupByNode(ArrayList JavaDoc columnList) {
42         _columnList = columnList;
43     }
44
45
46     // Return the Select clause as a SQL string
47

48     public String JavaDoc genText() {
49         String JavaDoc res = "\nGROUP BY "; // NOI18N
50

51         if (_columnList.size() > 0) {
52             res += ((ColumnNode)_columnList.get(0)).genText();
53             for (int i=1; i<_columnList.size(); i++) {
54                 res += ", " + ((ColumnNode)_columnList.get(i)).genText(); // NOI18N
55
}
56         }
57
58         return res;
59     }
60
61
62     // Accessors/Mutators
63

64     public void setColumnList(List JavaDoc columnList) {
65         _columnList = columnList;
66     }
67
68     // adds any column in the condition to the ArrayList of columns
69
public void getReferencedColumns(Collection JavaDoc columns) {
70         if (_columnList != null)
71             columns.addAll(_columnList);
72     }
73
74     public void getQueryItems(Collection JavaDoc items) {
75         if (_columnList != null)
76             items.addAll(_columnList);
77     }
78
79     public void addColumn(Column col) {
80         _columnList.add(col);
81     }
82
83     public void addColumn(String JavaDoc tableSpec, String JavaDoc columnName) {
84         _columnList.add(new ColumnNode(tableSpec, columnName));
85     }
86
87     // Remove the specified column from the SELECT list
88
// Iterate back-to-front for stability under deletion
89
public void removeColumn(String JavaDoc tableSpec, String JavaDoc columnName) {
90         for (int i=_columnList.size()-1; i>=0; i--) {
91             ColumnNode c = (ColumnNode) _columnList.get(i);
92             if ((c.getTableSpec().equals(tableSpec)) &&
93                 (c.getColumnName().equals(columnName))) {
94                 _columnList.remove(i);
95             }
96         }
97     }
98
99     /**
100      * Remove any GroupBy targets that reference this table
101      */

102     void removeTable (String JavaDoc tableSpec) {
103         for (int i=_columnList.size()-1; i>=0; i--) {
104             ColumnNode c = (ColumnNode) _columnList.get(i);
105             if (c.getTableSpec().equals(tableSpec))
106                 _columnList.remove(i);
107         }
108     }
109
110     void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
111
112         for (int i=0; i<_columnList.size(); i++)
113             ((ColumnNode)_columnList.get(i)).renameTableSpec(oldTableSpec, corrName);
114     }
115 }
116
Popular Tags