KickJava   Java API By Example, From Geeks To Geeks.

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


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 QueryNode implements Query {
26
27     // Fields
28

29     SelectNode _select;
30     FromNode _from;
31     WhereNode _where;
32     GroupByNode _groupBy;
33     HavingNode _having;
34     OrderByNode _orderBy;
35
36
37     // Constructors
38

39     public QueryNode() {
40     }
41
42     public QueryNode(SelectNode select, FromNode from, WhereNode where,
43                  GroupByNode groupBy, HavingNode having, OrderByNode orderBy) {
44         _select = select;
45         _from = from;
46         _where = where;
47         _groupBy = groupBy;
48         _having = having;
49         _orderBy = orderBy;
50     }
51
52     public QueryNode(SelectNode select, FromNode from) {
53         this(select, from, null, null, null, null);
54     }
55
56
57     // Methods
58

59     // Generate the SQL string corresponding to this model
60

61     public String JavaDoc genText() {
62         String JavaDoc res = _select.genText() + " " + _from.genText(); // NOI18N
63

64         if (_where!=null)
65             res += _where.genText();
66
67         if (_groupBy!=null)
68             res += _groupBy.genText();
69
70         if (_having!=null)
71             res += _having.genText();
72
73         if (_orderBy!=null)
74             res += _orderBy.genText();
75
76         return res;
77     }
78
79
80     // Dump out the model, for debugging purposes
81

82     public String JavaDoc toString() {
83         return (_select.toString() +
84                 _from.toString() +
85                 _where.toString() );
86     }
87
88
89     // Accessors/Mutators
90

91     public Select getSelect() {
92         return _select;
93     }
94
95     public void setSelect(Select select) {
96         _select = (SelectNode)select;
97     }
98
99     public From getFrom() {
100         return _from;
101     }
102
103     public void setFrom(From from) {
104         _from = (FromNode)from;
105     }
106
107     public Where getWhere() {
108         return _where;
109     }
110
111     public void setWhere(Where where) {
112         _where = (WhereNode)where;
113     }
114
115     public GroupBy getGroupBy() {
116         return _groupBy;
117     }
118
119     public void setGroupBy(GroupBy groupBy) {
120         _groupBy = (GroupByNode)groupBy;
121     }
122
123     public OrderBy getOrderBy() {
124         return _orderBy;
125     }
126
127     public void setOrderBy(OrderBy orderBy) {
128         _orderBy = (OrderByNode)orderBy;
129     }
130
131     public Having getHaving() {
132         return _having;
133     }
134
135     public void setHaving(Having having) {
136         _having = (HavingNode)having;
137     }
138
139     public void removeTable (String JavaDoc tableSpec) {
140         // Find the FROM clause for this tableName, and remove it
141
_from.removeTable(tableSpec);
142
143         // ToDo: Remove any other joins that mention this table?
144

145         // Find any SELECT targets for this tableName, and remove them
146
_select.removeTable(tableSpec);
147
148         // Find any WHERE clauses that mention this table, and remove them
149
if (_where!=null) {
150             _where.removeTable(tableSpec);
151             if (_where.getExpression() == null)
152                 _where = null;
153         }
154
155         // Find any GROUPBY clauses that mention this table, and remove them
156
if (_groupBy!=null)
157         {
158             _groupBy.removeTable(tableSpec);
159             if (_from._tableList.size() == 0)
160                 _groupBy = null;
161         }
162         removeSortSpecification(tableSpec);
163     }
164
165     public void replaceStar(ColumnProvider tableReader) {
166         if (_select.hasAsteriskQualifier()) { // NOI18N
167

168             // Hack - if there's a star, just replace the whole list
169
ArrayList JavaDoc columns = new ArrayList JavaDoc();
170
171             // Get the list of table objects from FROM
172
ArrayList JavaDoc tables = _from.getTables();
173
174             // Iterate through it
175
for (int i=0; i<tables.size(); i++) {
176                 TableNode tbl = (TableNode) tables.get(i);
177                 String JavaDoc fullTableName = tbl.getFullTableName();
178                 List JavaDoc columnNames = new ArrayList JavaDoc();
179                 tableReader.getColumnNamesFull(fullTableName, columnNames);
180                 String JavaDoc corrName=tbl.getCorrName();
181                 String JavaDoc tableName=tbl.getTableName();
182                 String JavaDoc schemaName=tbl.getSchemaName();
183                 for (int j=0; j<columnNames.size(); j++) {
184                     String JavaDoc columnName = (String JavaDoc) columnNames.get(j);
185                     columns.add(new ColumnNode(tableName, columnName, corrName, schemaName));
186                 }
187             }
188             _select.setColumnList(columns);
189         }
190     }
191
192     public void addColumn(String JavaDoc tableSpec, String JavaDoc columnName) {
193         // Get the corresponding Table object from the FROM, to resolve issues
194
// of corrName/tableName
195
Table table = _from.findTable(tableSpec);
196         ColumnNode col = new ColumnNode(table, columnName);
197         
198         // Note that they will share the column. Copy if this causes problem
199
_select.addColumn(col);
200         if (_groupBy != null)
201             _groupBy.addColumn(col);
202     }
203
204     public void removeColumn(String JavaDoc tableSpec, String JavaDoc columnName) {
205         _select.removeColumn(tableSpec, columnName);
206         if (_groupBy != null)
207             _groupBy.removeColumn(tableSpec, columnName);
208         // Remove the sort spec for this column if there was one
209
removeSortSpecification(tableSpec, columnName);
210     }
211     
212     public void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
213         _from.renameTableSpec(oldTableSpec, corrName);
214         _select.renameTableSpec(oldTableSpec, corrName);
215         if (_where!=null)
216             _where.renameTableSpec(oldTableSpec, corrName);
217         if (_groupBy!=null)
218             _groupBy.renameTableSpec(oldTableSpec, corrName);
219         if (_having!=null)
220             _having.renameTableSpec(oldTableSpec, corrName);
221         if (_orderBy!=null)
222             _orderBy.renameTableSpec(oldTableSpec, corrName);
223     }
224
225     public void getReferencedColumns(Collection JavaDoc columns) {
226         _from.getReferencedColumns(columns);
227         _select.getReferencedColumns(columns);
228         if (_where!=null)
229             _where.getReferencedColumns(columns);
230         if (_groupBy!=null)
231             _groupBy.getReferencedColumns(columns);
232         if (_having!=null)
233             _having.getReferencedColumns(columns);
234         if (_orderBy!=null)
235             _orderBy.getReferencedColumns(columns);
236     }
237     
238     public void getQueryItems(Collection JavaDoc items) {
239         items.add(_from);
240         items.add(_select);
241         if (_where!=null)
242             items.add(_where);
243         if (_groupBy!=null)
244             items.add(_groupBy);
245         if (_having!=null)
246             items.add(_having);
247         if (_orderBy!=null)
248             items.add(_orderBy);
249     }
250     //
251
// private implementation
252
//
253

254     private void removeSortSpecification(String JavaDoc tableSpec) {
255         if (_orderBy!=null)
256             _orderBy.removeSortSpecification(tableSpec);
257     }
258
259     private void removeSortSpecification(String JavaDoc tableSpec, String JavaDoc columnName) {
260         if (_orderBy!=null)
261             _orderBy.removeSortSpecification(tableSpec, columnName);
262     }
263
264 }
265
266
267
Popular Tags