KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
23 import java.util.List JavaDoc;
24
25 public class SelectNode implements Select {
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 ArrayList JavaDoc _selectItemList;
34     private String JavaDoc _quantifier;
35
36
37     // Constructor
38

39     public SelectNode() {
40     }
41
42     public SelectNode(ArrayList JavaDoc columnList, String JavaDoc quantifier) {
43         _selectItemList = columnList;
44         _quantifier = quantifier;
45     }
46
47     public SelectNode(ArrayList JavaDoc columnList) {
48         this(columnList, "ALL"); // NOI18N
49
}
50
51
52     // Return the Select clause as a SQL string
53

54     public String JavaDoc genText() {
55         String JavaDoc res = ""; // NOI18N
56
String JavaDoc res_select_quantifier = ""; // NOI18N
57

58         if (_selectItemList.size() > 0) {
59
60             res_select_quantifier = "SELECT " + _quantifier + " " ;
61             res = res_select_quantifier
62                         + ((ColumnItem)_selectItemList.get(0)).genText(true); // NOI18N
63

64             for (int i=1; i<_selectItemList.size(); i++) {
65                 ColumnItem col = (ColumnItem)_selectItemList.get(i);
66                 if (col != null)
67                 {
68                     res += ", " + "\n " + col.genText(true); // NOI18N
69
}
70             }
71         }
72         return res;
73     }
74
75
76     // Accessors/Mutators
77

78     public void setColumnList(ArrayList JavaDoc columnList) {
79         _selectItemList = columnList;
80     }
81
82     public void getReferencedColumns(Collection JavaDoc columns) {
83         for (int i = 0; i < _selectItemList.size(); i++)
84             columns.add(((ColumnItem)_selectItemList.get(i)).getReferencedColumn());
85     }
86
87     public void getQueryItems(Collection JavaDoc items) {
88         items.add(_selectItemList);
89     }
90
91     public void addColumn(Column col) {
92         _selectItemList.add(col);
93     }
94
95     public void addColumn(String JavaDoc tableSpec, String JavaDoc columnName) {
96         _selectItemList.add(new ColumnNode(tableSpec, columnName));
97     }
98
99     // Remove the specified column from the SELECT list
100
// Iterate back-to-front for stability under deletion
101
public void removeColumn(String JavaDoc tableSpec, String JavaDoc columnName) {
102         for (int i=_selectItemList.size()-1; i>=0; i--) {
103             ColumnItem item = (ColumnItem) _selectItemList.get(i);
104             ColumnNode c = (ColumnNode) item.getReferencedColumn();
105             if ((c != null) && (c.getTableSpec().equals(tableSpec)) && (c.getColumnName().equals(columnName)))
106             {
107                 _selectItemList.remove(i);
108             }
109         }
110     }
111
112     /**
113      * set column name
114      */

115     public void setColumnName (String JavaDoc oldColumnName, String JavaDoc newColumnName) {
116         for (int i=0; i<_selectItemList.size(); i++) {
117             ColumnNode c = (ColumnNode) _selectItemList.get(i);
118             if ( c != null) {
119                 c.setColumnName(oldColumnName, newColumnName);
120             }
121         }
122     }
123
124     public boolean hasAsteriskQualifier() {
125         for (int i=0; i<_selectItemList.size(); i++) {
126             ColumnItem item = (ColumnItem) _selectItemList.get(i);
127             if (item instanceof ColumnNode) {
128                 ColumnNode c = (ColumnNode) item;
129                 if (c.getColumnName().equals("*")) {
130                     return true;
131                 }
132             }
133         }
134         return false;
135     }
136
137     /**
138      * Remove any SELECT targets that reference this table
139      */

140     void removeTable (String JavaDoc tableSpec) {
141         for (int i=_selectItemList.size()-1; i>=0; i--) {
142             ColumnItem item = (ColumnItem) _selectItemList.get(i);
143             ColumnNode c = (ColumnNode) item.getReferencedColumn();
144             if (c != null) {
145                 String JavaDoc tabSpec = c.getTableSpec();
146                 if (tabSpec != null && tabSpec.equals(tableSpec))
147                     _selectItemList.remove(i);
148             }
149         }
150     }
151
152     /**
153      * Rename a table
154      */

155     void renameTableSpec (String JavaDoc oldTableSpec, String JavaDoc corrName) {
156         for (int i=0; i<_selectItemList.size(); i++) {
157             ColumnItem item = (ColumnItem) _selectItemList.get(i);
158             ColumnNode c = (ColumnNode) item.getReferencedColumn();
159             if ( c != null)
160             {
161                 c.renameTableSpec(oldTableSpec, corrName);
162             }
163         }
164     }
165
166 }
167
Popular Tags