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 QueryNode implements Query {
050:
051: // Fields
052:
053: SelectNode _select;
054: FromNode _from;
055: WhereNode _where;
056: GroupByNode _groupBy;
057: HavingNode _having;
058: OrderByNode _orderBy;
059:
060: // Constructors
061:
062: public QueryNode() {
063: }
064:
065: public QueryNode(SelectNode select, FromNode from, WhereNode where,
066: GroupByNode groupBy, HavingNode having, OrderByNode orderBy) {
067: _select = select;
068: _from = from;
069: _where = where;
070: _groupBy = groupBy;
071: _having = having;
072: _orderBy = orderBy;
073: }
074:
075: public QueryNode(SelectNode select, FromNode from) {
076: this (select, from, null, null, null, null);
077: }
078:
079: // Methods
080:
081: // Generate the SQL string corresponding to this model
082:
083: public String genText(SQLIdentifiers.Quoter quoter) {
084: String res = _select.genText(quoter) + " "
085: + _from.genText(quoter); // NOI18N
086:
087: if (_where != null)
088: res += _where.genText(quoter);
089:
090: if (_groupBy != null)
091: res += _groupBy.genText(quoter);
092:
093: if (_having != null)
094: res += _having.genText(quoter);
095:
096: if (_orderBy != null)
097: res += _orderBy.genText(quoter);
098:
099: return res;
100: }
101:
102: // Dump out the model, for debugging purposes
103:
104: public String toString() {
105: return (_select.toString() + _from.toString() + _where
106: .toString());
107: }
108:
109: // Accessors/Mutators
110:
111: public Select getSelect() {
112: return _select;
113: }
114:
115: public void setSelect(Select select) {
116: _select = (SelectNode) select;
117: }
118:
119: public From getFrom() {
120: return _from;
121: }
122:
123: public void setFrom(From from) {
124: _from = (FromNode) from;
125: }
126:
127: public Where getWhere() {
128: return _where;
129: }
130:
131: public void setWhere(Where where) {
132: _where = (WhereNode) where;
133: }
134:
135: public GroupBy getGroupBy() {
136: return _groupBy;
137: }
138:
139: public void setGroupBy(GroupBy groupBy) {
140: _groupBy = (GroupByNode) groupBy;
141: }
142:
143: public OrderBy getOrderBy() {
144: return _orderBy;
145: }
146:
147: public void setOrderBy(OrderBy orderBy) {
148: _orderBy = (OrderByNode) orderBy;
149: }
150:
151: public Having getHaving() {
152: return _having;
153: }
154:
155: public void setHaving(Having having) {
156: _having = (HavingNode) having;
157: }
158:
159: public void removeTable(String tableSpec) {
160: // Find the FROM clause for this tableName, and remove it
161: _from.removeTable(tableSpec);
162:
163: // ToDo: Remove any other joins that mention this table?
164:
165: // Find any SELECT targets for this tableName, and remove them
166: _select.removeTable(tableSpec);
167:
168: // Find any WHERE clauses that mention this table, and remove them
169: if (_where != null) {
170: _where.removeTable(tableSpec);
171: if (_where.getExpression() == null)
172: _where = null;
173: }
174:
175: // Find any GROUPBY clauses that mention this table, and remove them
176: if (_groupBy != null) {
177: _groupBy.removeTable(tableSpec);
178: if (_from._tableList.size() == 0)
179: _groupBy = null;
180: }
181: removeSortSpecification(tableSpec);
182: }
183:
184: public void replaceStar(ColumnProvider tableReader) {
185: if (_select.hasAsteriskQualifier()) { // NOI18N
186:
187: // Hack - if there's a star, just replace the whole list
188: ArrayList columns = new ArrayList();
189:
190: // Get the list of table objects from FROM
191: ArrayList tables = _from.getTables();
192:
193: // Iterate through it
194: for (int i = 0; i < tables.size(); i++) {
195: TableNode tbl = (TableNode) tables.get(i);
196: String fullTableName = tbl.getFullTableName();
197: List columnNames = new ArrayList();
198: tableReader.getColumnNames(fullTableName, columnNames);
199: String corrName = tbl.getCorrName();
200: String tableName = tbl.getTableName();
201: String schemaName = tbl.getSchemaName();
202: for (int j = 0; j < columnNames.size(); j++) {
203: String columnName = (String) columnNames.get(j);
204: columns.add(new ColumnNode(tableName, columnName,
205: corrName, schemaName));
206: }
207: }
208: _select.setColumnList(columns);
209: }
210: }
211:
212: public void addColumn(String tableSpec, String columnName) {
213: // Get the corresponding Table object from the FROM, to resolve issues
214: // of corrName/tableName
215: Table table = _from.findTable(tableSpec);
216: ColumnNode col = new ColumnNode(table, columnName);
217:
218: // Note that they will share the column. Copy if this causes problem
219: _select.addColumn(col);
220: if (_groupBy != null)
221: _groupBy.addColumn(col);
222: }
223:
224: public void removeColumn(String tableSpec, String columnName) {
225: _select.removeColumn(tableSpec, columnName);
226: if (_groupBy != null)
227: _groupBy.removeColumn(tableSpec, columnName);
228: // Remove the sort spec for this column if there was one
229: removeSortSpecification(tableSpec, columnName);
230: }
231:
232: public void renameTableSpec(String oldTableSpec, String corrName) {
233: _from.renameTableSpec(oldTableSpec, corrName);
234: _select.renameTableSpec(oldTableSpec, corrName);
235: if (_where != null)
236: _where.renameTableSpec(oldTableSpec, corrName);
237: if (_groupBy != null)
238: _groupBy.renameTableSpec(oldTableSpec, corrName);
239: if (_having != null)
240: _having.renameTableSpec(oldTableSpec, corrName);
241: if (_orderBy != null)
242: _orderBy.renameTableSpec(oldTableSpec, corrName);
243: }
244:
245: public void getReferencedColumns(Collection columns) {
246: _from.getReferencedColumns(columns);
247: _select.getReferencedColumns(columns);
248: if (_where != null)
249: _where.getReferencedColumns(columns);
250: if (_groupBy != null)
251: _groupBy.getReferencedColumns(columns);
252: if (_having != null)
253: _having.getReferencedColumns(columns);
254: if (_orderBy != null)
255: _orderBy.getReferencedColumns(columns);
256: }
257:
258: //
259: // private implementation
260: //
261:
262: private void removeSortSpecification(String tableSpec) {
263: if (_orderBy != null)
264: _orderBy.removeSortSpecification(tableSpec);
265: }
266:
267: private void removeSortSpecification(String tableSpec,
268: String columnName) {
269: if (_orderBy != null)
270: _orderBy.removeSortSpecification(tableSpec, columnName);
271: }
272:
273: }
|